-
Notifications
You must be signed in to change notification settings - Fork 2
/
decoder_test.go
184 lines (155 loc) · 3.82 KB
/
decoder_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package jzon
import (
"encoding/json"
"io"
"io/ioutil"
"strings"
"testing"
"testing/iotest"
"github.com/stretchr/testify/require"
)
type decFace interface {
UseNumber()
DisallowUnknownFields()
Decode(interface{}) error
Buffered() io.Reader
// This is incompatible
// Token() (json.Token, error)
More() bool
}
type inputOffset interface {
// Not available for go<1.13
InputOffset() int64
}
var _ decFace = &json.Decoder{}
var _ decFace = &Decoder{}
func TestDecoder(t *testing.T) {
t.Run("consecutive", func(t *testing.T) {
t.Run("success", func(t *testing.T) {
must := require.New(t)
newReader := func() io.Reader {
s := ` {} {} `
return iotest.OneByteReader(strings.NewReader(s))
}
check := func(dec decFace, length int, expMore bool, leftOffset, rightOffset int64) {
buffered := dec.Buffered()
if buffered == nil {
return
}
b, err := ioutil.ReadAll(buffered)
must.NoError(err)
if length > 0 {
t.Logf("%T: %q", dec, b)
}
must.True(length >= len(b))
ofI, ofIok := dec.(inputOffset)
if ofIok {
offset := ofI.InputOffset()
// t.Logf("%T %d", dec, offset)
must.True(leftOffset <= offset, "%T %d > %d", dec, leftOffset, offset)
must.True(rightOffset >= offset, "%T %d < %d", dec, rightOffset, offset)
}
more := dec.More()
must.Equal(expMore, more, "%T", dec)
if ofIok {
offset := ofI.InputOffset()
// t.Logf("%T %d", dec, offset)
must.True(leftOffset <= offset, "%T %d > %d", dec, leftOffset, offset)
must.True(rightOffset >= offset, "%T %d < %d", dec, rightOffset, offset)
}
}
f := func(dec decFace) {
var i, i2, i3 interface{}
check(dec, 0, true, 0, 1)
err := dec.Decode(&i)
must.NoError(err, "%T", dec)
check(dec, 0, true, 3, 4)
err2 := dec.Decode(&i2)
must.NoError(err2, "%T", dec)
check(dec, 0, false, 6, 7)
err3 := dec.Decode(&i3)
must.Equal(io.EOF, err3, "%T", dec)
check(dec, 1, false, 6, 7)
}
f(json.NewDecoder(newReader()))
dec := NewDecoder(newReader())
defer dec.Release()
f(dec)
})
t.Run("failure at start", func(t *testing.T) {
must := require.New(t)
newReader := func() io.Reader {
s := ` } {} `
return iotest.OneByteReader(strings.NewReader(s))
}
f := func(dec decFace) {
var i, i2 interface{}
err := dec.Decode(&i)
t.Logf("%T %v", dec, err)
must.Error(err)
err2 := dec.Decode(&i2)
t.Logf("%T %v", dec, err2)
must.Equal(err2, err)
}
f(json.NewDecoder(newReader()))
dec := NewDecoder(newReader())
defer dec.Release()
f(dec)
})
t.Run("failure at middle", func(t *testing.T) {
must := require.New(t)
newReader := func() io.Reader {
s := ` {} } `
return iotest.OneByteReader(strings.NewReader(s))
}
f := func(dec decFace) {
var i, i2 interface{}
err := dec.Decode(&i)
must.NoError(err)
err2 := dec.Decode(&i2)
t.Logf("%T %v", dec, err2)
must.Error(err2)
}
f(json.NewDecoder(newReader()))
dec := NewDecoder(newReader())
defer dec.Release()
f(dec)
})
})
}
func TestDecoder_UseNumber(t *testing.T) {
must := require.New(t)
s := "123.456"
newReader := func() io.Reader {
return strings.NewReader(s)
}
f := func(dec decFace) {
dec.UseNumber()
var i interface{}
err := dec.Decode(&i)
must.NoError(err)
must.Equal(json.Number(s), i)
}
f(json.NewDecoder(newReader()))
dec := NewDecoder(newReader())
defer dec.Release()
f(dec)
}
func TestDecoder_DisallowUnknownFields(t *testing.T) {
must := require.New(t)
s := `{"k":"v"}`
newReader := func() io.Reader {
return strings.NewReader(s)
}
f := func(dec decFace) {
dec.DisallowUnknownFields()
var i struct{}
err := dec.Decode(&i)
t.Log(err)
must.Error(err)
}
f(json.NewDecoder(newReader()))
dec := NewDecoder(newReader())
defer dec.Release()
f(dec)
}