Skip to content

Commit 70ab2eb

Browse files
committed
test(engine): update tests for deterministic stream IDs
1 parent e642e1e commit 70ab2eb

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

engine/builtin_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4452,12 +4452,14 @@ func TestPutByte(t *testing.T) {
44524452
}
44534453

44544454
func TestPutChar(t *testing.T) {
4455+
ResetStreamIDCounter()
4456+
44554457
tests := []struct {
44564458
title string
44574459
streamOrAlias func() (Term, func(*testing.T))
44584460
char Term
44594461
ok bool
4460-
err error
4462+
err func(Term) error
44614463
}{
44624464
// 8.12.3.4 Examples
44634465
{title: "put_char(t)", streamOrAlias: func() (Term, func(*testing.T)) {
@@ -4478,34 +4480,34 @@ func TestPutChar(t *testing.T) {
44784480
// 8.12.3.3 Errors
44794481
{title: "a", streamOrAlias: func() (Term, func(*testing.T)) {
44804482
return NewVariable(), nil
4481-
}, char: NewAtom("a"), err: InstantiationError(nil)},
4483+
}, char: NewAtom("a"), err: func(Term) error { return InstantiationError(nil) }},
44824484
{title: "b", streamOrAlias: func() (Term, func(*testing.T)) {
44834485
return NewOutputTextStream(nil), nil
4484-
}, char: NewVariable(), err: InstantiationError(nil)},
4486+
}, char: NewVariable(), err: func(Term) error { return InstantiationError(nil) }},
44854487
{title: "b: atom but not one-char", streamOrAlias: func() (Term, func(*testing.T)) {
44864488
return NewOutputTextStream(nil), nil
4487-
}, char: NewAtom("foo"), err: typeError(validTypeCharacter, NewAtom("foo"), nil)},
4489+
}, char: NewAtom("foo"), err: func(Term) error { return typeError(validTypeCharacter, NewAtom("foo"), nil) }},
44884490
{title: "b: not even atom", streamOrAlias: func() (Term, func(*testing.T)) {
44894491
return NewOutputTextStream(nil), nil
4490-
}, char: Integer(1), err: typeError(validTypeCharacter, Integer(1), nil)},
4492+
}, char: Integer(1), err: func(Term) error { return typeError(validTypeCharacter, Integer(1), nil) }},
44914493
{title: "f", streamOrAlias: func() (Term, func(*testing.T)) {
44924494
return Integer(1), nil
4493-
}, char: NewAtom("a"), err: domainError(validDomainStreamOrAlias, Integer(1), nil)},
4495+
}, char: NewAtom("a"), err: func(Term) error { return domainError(validDomainStreamOrAlias, Integer(1), nil) }},
44944496
{title: "g", streamOrAlias: func() (Term, func(*testing.T)) {
44954497
return NewAtom("foo"), nil
4496-
}, char: NewAtom("a"), err: existenceError(objectTypeStream, NewAtom("foo"), nil)},
4498+
}, char: NewAtom("a"), err: func(Term) error { return existenceError(objectTypeStream, NewAtom("foo"), nil) }},
44974499
{title: "h", streamOrAlias: func() (Term, func(*testing.T)) {
44984500
return NewInputTextStream(nil), nil
4499-
}, char: NewAtom("a"), err: permissionError(operationOutput, permissionTypeStream, NewInputTextStream(nil), nil)},
4501+
}, char: NewAtom("a"), err: func(s Term) error { return permissionError(operationOutput, permissionTypeStream, s, nil) }},
45004502
{title: "i", streamOrAlias: func() (Term, func(*testing.T)) {
45014503
return NewOutputBinaryStream(nil), nil
4502-
}, char: NewAtom("a"), err: permissionError(operationOutput, permissionTypeBinaryStream, NewOutputBinaryStream(nil), nil)},
4504+
}, char: NewAtom("a"), err: func(s Term) error { return permissionError(operationOutput, permissionTypeBinaryStream, s, nil) }},
45034505

45044506
{title: "error on write", streamOrAlias: func() (Term, func(*testing.T)) {
45054507
var m mockWriter
45064508
m.On("Write", mock.Anything).Return(0, errors.New("failed"))
45074509
return NewOutputTextStream(&m), nil
4508-
}, char: NewAtom("a"), err: errors.New("failed")},
4510+
}, char: NewAtom("a"), err: func(Term) error { return errors.New("failed") }},
45094511
}
45104512

45114513
for _, tt := range tests {
@@ -4518,7 +4520,11 @@ func TestPutChar(t *testing.T) {
45184520
var vm VM
45194521
ok, err := PutChar(&vm, sOrA, tt.char, Success, nil).Force(context.Background())
45204522
assert.Equal(t, tt.ok, ok)
4521-
assert.Equal(t, tt.err, err)
4523+
if tt.err != nil {
4524+
assert.Equal(t, tt.err(sOrA), err)
4525+
} else {
4526+
assert.NoError(t, err)
4527+
}
45224528
})
45234529
}
45244530
}

engine/stream_test.go

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ package engine
33
import (
44
"bytes"
55
"errors"
6-
"github.com/stretchr/testify/assert"
7-
"github.com/stretchr/testify/mock"
86
"io"
97
"io/fs"
108
"os"
119
"testing"
1210
"time"
11+
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/mock"
1314
)
1415

1516
func TestNewInputTextStream(t *testing.T) {
17+
ResetStreamIDCounter()
18+
1619
assert.Equal(t, &Stream{
20+
id: 1,
1721
source: os.Stdin,
1822
mode: ioModeRead,
1923
eofAction: eofActionReset,
@@ -22,7 +26,10 @@ func TestNewInputTextStream(t *testing.T) {
2226
}
2327

2428
func TestNewInputBinaryStream(t *testing.T) {
29+
ResetStreamIDCounter()
30+
2531
assert.Equal(t, &Stream{
32+
id: 1,
2633
source: os.Stdin,
2734
mode: ioModeRead,
2835
eofAction: eofActionReset,
@@ -31,7 +38,10 @@ func TestNewInputBinaryStream(t *testing.T) {
3138
}
3239

3340
func TestNewOutputTextStream(t *testing.T) {
41+
ResetStreamIDCounter()
42+
3443
assert.Equal(t, &Stream{
44+
id: 1,
3545
sink: os.Stdout,
3646
mode: ioModeAppend,
3747
eofAction: eofActionReset,
@@ -40,7 +50,10 @@ func TestNewOutputTextStream(t *testing.T) {
4050
}
4151

4252
func TestNewOutputBinaryStream(t *testing.T) {
53+
ResetStreamIDCounter()
54+
4355
assert.Equal(t, &Stream{
56+
id: 1,
4457
sink: os.Stdout,
4558
mode: ioModeAppend,
4659
eofAction: eofActionReset,
@@ -49,28 +62,47 @@ func TestNewOutputBinaryStream(t *testing.T) {
4962
}
5063

5164
func TestStream_WriteTerm(t *testing.T) {
65+
ResetStreamIDCounter()
66+
5267
tests := []struct {
53-
title string
54-
s *Stream
55-
opts WriteOptions
56-
output string
68+
title string
69+
s *Stream
70+
prepare func(*Stream)
71+
output string
5772
}{
58-
{title: "stream", s: &Stream{}, output: `<stream>\(0x[[:xdigit:]]+\)`},
73+
{title: "no alias", s: NewInputTextStream(nil), output: `<stream>\(0x1\)`},
74+
{title: "registered", s: NewInputTextStream(nil), prepare: func(s *Stream) {
75+
var vm VM
76+
vm.streams.add(s)
77+
}, output: `<stream>\(0x2\)`},
78+
{title: "alias", s: &Stream{id: nextStreamID(), alias: NewAtom("foo")}, prepare: func(s *Stream) {
79+
var vm VM
80+
s.vm = &vm
81+
vm.streams.add(s)
82+
}, output: `<stream>\(foo\)`},
5983
}
6084

6185
var buf bytes.Buffer
62-
for _, tt := range tests {
63-
t.Run(tt.title, func(t *testing.T) {
86+
for _, testCase := range tests {
87+
tc := testCase
88+
t.Run(tc.title, func(t *testing.T) {
6489
buf.Reset()
65-
assert.NoError(t, tt.s.WriteTerm(&buf, &tt.opts, nil))
66-
assert.Regexp(t, tt.output, buf.String())
90+
if tc.prepare != nil {
91+
tc.prepare(tc.s)
92+
}
93+
assert.NoError(t, tc.s.WriteTerm(&buf, nil, nil))
94+
assert.Regexp(t, tc.output, buf.String())
6795
})
6896
}
6997
}
7098

7199
func TestStream_Compare(t *testing.T) {
72100
x := NewVariable()
73-
var ss [3]Stream
101+
ss := [3]Stream{
102+
{id: 1},
103+
{id: 2},
104+
{id: 3},
105+
}
74106

75107
tests := []struct {
76108
title string

0 commit comments

Comments
 (0)