-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiterator_test.go
120 lines (105 loc) · 2.32 KB
/
iterator_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
package jzon
import (
"bytes"
"errors"
"io"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestIterator_New(t *testing.T) {
must := require.New(t)
it := NewIterator()
must.Nil(it.reader)
must.Nil(it.buffer)
must.Equal(0, it.offset)
must.Equal(0, it.head)
must.Equal(0, it.tail)
// must.Nil(it.Error)
}
func TestIterator_Reset_Nil(t *testing.T) {
must := require.New(t)
it := NewIterator()
must.Nil(it.reader)
must.Nil(it.buffer)
must.Equal(0, it.offset)
must.Equal(0, it.head)
must.Equal(0, it.tail)
it.Reset(nil)
must.Nil(it.reader)
must.Nil(it.buffer)
must.Equal(0, it.offset)
must.Equal(0, it.head)
must.Equal(0, it.tail)
}
func TestIterator_Reset(t *testing.T) {
must := require.New(t)
// nil -> reader
it := NewIterator()
r := bytes.NewReader(nil)
it.Reset(r)
must.Equal(r, it.reader)
must.NotEmpty(it.buffer)
must.Equal(0, it.head)
must.Equal(0, it.tail)
// reader -> reader
addr := &it.buffer[0]
r2 := bytes.NewReader(nil)
it.Reset(r2)
must.Equal(r2, it.reader)
must.True(addr == &it.buffer[0])
must.Equal(0, it.head)
must.Equal(0, it.tail)
// reader -> byte
b := []byte("abc")
it.ResetBytes(b)
must.Nil(it.reader)
must.True(&b[0] == &it.buffer[0])
must.Equal(0, it.head)
must.Equal(len(b), it.tail)
// nil -> byte
it = NewIterator()
b2 := []byte("abc")
it.ResetBytes(b2)
must.Nil(it.reader)
must.True(&b2[0] == &it.buffer[0])
must.Equal(0, it.head)
must.Equal(len(b2), it.tail)
// byte -> byte
b3 := []byte("defg")
it.ResetBytes(b3)
must.Nil(it.reader)
must.True(&b3[0] == &it.buffer[0])
must.Equal(0, it.head)
must.Equal(len(b3), it.tail)
// byte -> reader
r3 := bytes.NewReader(nil)
it.Reset(r3)
must.Equal(r3, it.reader)
must.Equal(0, it.head)
must.Equal(0, it.tail)
}
func TestIterator_NextValueType(t *testing.T) {
must := require.New(t)
it := NewIterator()
for c, typ := range valueTypeMap {
it.ResetBytes([]byte{byte(c)})
next, err := it.NextValueType()
if typ == WhiteSpaceValue {
require.Equal(t, io.EOF, err)
} else {
require.NoError(t, err)
must.Equal(typ, next)
}
}
}
func TestIterator_WrapError(t *testing.T) {
must := require.New(t)
it := NewIterator()
s := strings.Repeat(" ", errWidth+1)
it.ResetBytes([]byte(s))
ex := errors.New("test")
err := it.WrapError(ex)
err2 := it.WrapError(err)
must.Equal(err, err2)
}