-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvisitor_test.go
130 lines (122 loc) · 2.87 KB
/
visitor_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
package jsonx
import "testing"
func TestWalkerParseErrors(t *testing.T) {
const noParseErrorCode ParseErrorCode = -1
t.Run("comments and trailing commas enabled", func(t *testing.T) {
for _, tc := range []struct {
input string
want ParseErrorCode
}{
{
input: `/* unexpected end of comment`,
want: ParseErrorUnexpectedEndOfComment,
},
{
input: `"unexpected end of string`,
want: ParseErrorUnexpectedEndOfString,
},
{
input: `2.`,
want: ParseErrorUnexpectedEndOfNumber,
},
{
input: `"\u123"`,
want: ParseErrorInvalidUnicode,
},
{
input: `"\."`,
want: ParseErrorInvalidEscapeCharacter,
},
{
input: "\"\x01\"",
want: ParseErrorInvalidCharacter,
},
{
input: `{"foo": "bar", /* this is a comment */}`,
want: noParseErrorCode,
},
} {
t.Run(tc.want.String(), func(t *testing.T) {
var have ParseErrorCode = noParseErrorCode
v := Visitor{OnError: func(errorCode ParseErrorCode, offset, length int) {
have = errorCode
}}
if !Walk(tc.input, ParseOptions{
Comments: true,
TrailingCommas: true,
}, v) {
t.Error("Walk returned false unexpectedly")
}
if have != tc.want {
t.Errorf("unexpected error code: have %v; want %v", have, tc.want)
}
})
}
})
t.Run("comments disabled", func(t *testing.T) {
for _, tc := range []struct {
input string
want ParseErrorCode
}{
{
input: `// line comment`,
want: InvalidCommentToken,
},
{
input: `/* block comment */`,
want: InvalidCommentToken,
},
{
input: `{"foo": "bar",}`,
want: noParseErrorCode,
},
} {
t.Run(tc.want.String(), func(t *testing.T) {
var have ParseErrorCode = noParseErrorCode
v := Visitor{OnError: func(errorCode ParseErrorCode, offset, length int) {
have = errorCode
}}
if !Walk(tc.input, ParseOptions{
Comments: false,
TrailingCommas: true,
}, v) {
t.Error("Walk returned false unexpectedly")
}
if have != tc.want {
t.Errorf("unexpected error code: have %v; want %v", have, tc.want)
}
})
}
})
t.Run("trailing commas disabled", func(t *testing.T) {
for _, tc := range []struct {
input string
want ParseErrorCode
}{
{
input: `{"foo": "bar"} // line comment`,
want: noParseErrorCode,
},
{
input: `{"foo": "bar",}`,
want: ValueExpected,
},
} {
t.Run(tc.want.String(), func(t *testing.T) {
var have ParseErrorCode = noParseErrorCode
v := Visitor{OnError: func(errorCode ParseErrorCode, offset, length int) {
have = errorCode
}}
if !Walk(tc.input, ParseOptions{
Comments: true,
TrailingCommas: false,
}, v) {
t.Error("Walk returned false unexpectedly")
}
if have != tc.want {
t.Errorf("unexpected error code: have %v; want %v", have, tc.want)
}
})
}
})
}