-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathempty_test.go
86 lines (74 loc) · 2.08 KB
/
empty_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package lexy_test
import (
"testing"
"github.com/phiryll/lexy"
"github.com/stretchr/testify/assert"
)
// There are good reasons to test emptyCodec in combination with other Codecs.
// In particular, it demonstrates why RequiresTerminator() must return true
// if the Codec might encode zero bytes.
// These tests should also catch if any of the aggregate Codecs don't handle termination correctly.
type emptyStruct struct{}
type mValue map[uint8]emptyStruct
var (
empty = emptyStruct{}
emptyCodec = lexy.Empty[emptyStruct]()
valueEmptyCodec = lexy.CastMapOf[mValue](lexy.Uint8(), emptyCodec)
)
func TestEmpty(t *testing.T) {
t.Parallel()
assert.True(t, emptyCodec.RequiresTerminator())
testCodec(t, emptyCodec, []testCase[emptyStruct]{
{"empty", emptyStruct{}, []byte{}},
})
}
func TestPointerEmpty(t *testing.T) {
t.Parallel()
testCodec(t, lexy.PointerTo(emptyCodec), []testCase[*emptyStruct]{
{"nil", nil, []byte{pNilFirst}},
{"*empty", ptr(empty), []byte{pNonNil}},
})
}
func TestSliceEmpty(t *testing.T) {
t.Parallel()
testCodec(t, lexy.SliceOf(emptyCodec), []testCase[[]emptyStruct]{
{"nil", nil, []byte{pNilFirst}},
{"[]", []emptyStruct{}, []byte{pNonNil}},
{"[empty]", []emptyStruct{empty}, []byte{pNonNil, term}},
{"[3x empty]", []emptyStruct{empty, empty, empty}, []byte{
pNonNil, term, term, term,
}},
})
}
func TestMapValueEmpty(t *testing.T) {
t.Parallel()
testCodec(t, valueEmptyCodec, []testCase[mValue]{
{"nil", nil, []byte{pNilFirst}},
{"{}", mValue{}, []byte{pNonNil}},
{"{2:empty}", mValue{2: empty}, []byte{
pNonNil,
0x02,
term,
}},
})
testVaryingCodec(t, valueEmptyCodec, []testCase[mValue]{
{"non-trivial", mValue{
1: empty,
167: empty,
4: empty,
17: empty,
}, nil},
})
}
func TestNegateEmpty(t *testing.T) {
t.Parallel()
testCodec(t, lexy.Negate(emptyCodec), []testCase[emptyStruct]{
{"neg(empty)", empty, []byte{0xFF}},
})
}
func TestTerminateEmpty(t *testing.T) {
t.Parallel()
testCodec(t, lexy.Terminate(emptyCodec), []testCase[emptyStruct]{
{"terminate(empty)", empty, []byte{0x00}},
})
}