-
Notifications
You must be signed in to change notification settings - Fork 3
/
cursor_test.go
74 lines (66 loc) · 1.45 KB
/
cursor_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
// MIT license · Daniel T. Gorski · dtg [at] lengo [dot] org · 10/2020
package jsonlex
import (
"bytes"
"io"
"testing"
)
func TestCursor_1(t *testing.T) {
s := `{ "foo": -1 }`
r := bytes.NewReader([]byte(s))
c := NewCursor(r, nil)
if n := c.Curr(); !n.Is(TokenLCB) {
t.Errorf("unexpected")
}
if n := c.Peek(); !n.Is(TokenSTR) {
t.Errorf("unexpected")
}
if n := c.Next(); !n.Is(TokenSTR) {
t.Errorf("unexpected")
}
if n := c.Next(); !n.Is(TokenCOL) {
t.Errorf("unexpected")
}
if n := c.Next(); !n.Is(TokenNUM) {
t.Errorf("unexpected")
}
if n := c.Next(); !n.Is(TokenRCB) {
t.Errorf("unexpected")
}
if n := c.Last(); !n.Is(TokenNUM) {
t.Errorf("unexpected")
}
if n := c.Next(); !n.Is(TokenEOF) {
t.Errorf("unexpected")
}
if n := c.Next(); !n.Is(TokenEOF) {
t.Errorf("unexpected")
}
}
func TestCursor_2(t *testing.T) {
s := `{ "foo": -1 }`
r := bytes.NewReader([]byte(s))
f := func(k TokenKind, l []byte) bool {
return !k.Is(TokenLCB) && !k.Is(TokenRCB) && !k.Is(TokenCOL)
}
c := NewCursor(r, f)
if n := c.Curr(); !n.Is(TokenSTR) {
t.Errorf("unexpected")
}
if n := c.Next(); !n.Is(TokenNUM) {
t.Errorf("unexpected")
}
if n := c.Next(); !n.Is(TokenEOF) {
t.Errorf("unexpected")
}
}
func TestCursor_3(t *testing.T) {
r := &FaultyReader{}
c := NewCursor(r, nil)
if n := c.Next(); !n.Is(TokenERR) {
t.Errorf("unexpected")
}
if c.Next().String() != io.ErrUnexpectedEOF.Error() {
t.Errorf("unexpected")
}
}