forked from wundergraph/astjson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_test.go
161 lines (149 loc) · 2.88 KB
/
validate_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
package astjson
import (
"encoding/json"
"strings"
"testing"
)
func TestValidateSimple(t *testing.T) {
if err := Validate(`123`); err != nil {
t.Fatalf("cannot validate number: %s", err)
}
if err := Validate(`"foobar"`); err != nil {
t.Fatalf("cannot validate string: %s", err)
}
if err := Validate(`null`); err != nil {
t.Fatalf("cannot validate null: %s", err)
}
if err := Validate(`true`); err != nil {
t.Fatalf("cannot validate true: %s", err)
}
if err := Validate(`false`); err != nil {
t.Fatalf("cannot validate false: %s", err)
}
if err := Validate(`foobar`); err == nil {
t.Fatalf("validation unexpectedly passed")
}
if err := Validate(`XDF`); err == nil {
t.Fatalf("validation unexpectedly passed")
}
if err := ValidateBytes([]byte(`{"foo":["bar", 123]}`)); err != nil {
t.Fatalf("cannot validate valid JSON: %s", err)
}
if err := ValidateBytes([]byte(`{"foo": bar`)); err == nil {
t.Fatalf("validation unexpectedly passed")
}
}
func TestValidateNumberZeroLen(t *testing.T) {
tail, err := validateNumber("")
if err == nil {
t.Fatalf("expecting non-nil error")
}
if tail != "" {
t.Fatalf("unexpected non-empty tail: %q", tail)
}
}
func TestValidate(t *testing.T) {
var tests = []string{
"",
" ",
" z",
" 1 1",
" 1 {}",
" 1 []",
" 1 true",
" 1 null",
" 1 \"n\"",
// string
`"foo"`,
"\"\xe2\x80\xa8\xe2\x80\xa9\"", // line-sep and paragraph-sep
` "\uaaaa" `,
`"\uz"`,
` "\`,
` "\z`,
" \"f\x00o\"", // control char
"\"foo\nbar\"", // control char
`"foo\qw"`, // unknown escape sequence
` "foo`,
` "\uazaa" `,
`"\"\\\/\b\f\n\r\t"`,
// number
"1",
" 0 ",
" 0e1 ",
" 0e+0 ",
" -0e+0 ",
"-0",
"1e6",
"1e+6",
"-1e+6",
"-0e+6",
" -103e+1 ",
"-0.01e+006",
"-z",
"-",
"1e",
"1e+",
" 03e+1 ",
" 1e.1 ",
" 00 ",
"1.e3",
"01e+6",
"-0.01e+0.6",
"123.",
"123.345",
"001 ",
"001",
// object
"{}",
`{"foo": 3}`,
"{\"f\x00oo\": 3}",
`{"foo\WW": 4}`, // unknown escape sequence
`{"foo": 3 "bar"}`,
` {} `,
strings.Repeat(`{"f":`, 1000) + "{}" + strings.Repeat("}", 1000),
`{"foo": [{"":3, "4": "3"}, 4, {}], "t_wo": 1}`,
` {"foo": 2,"fudge}`,
`{{"foo": }}`,
`{{"foo": [{"":3, 4: "3"}, 4, "5": {4}]}, "t_wo": 1}`,
"{",
`{"foo"`,
`{"foo",f}`,
`{"foo",`,
`{"foo"f`,
"{}}",
`{"foo": 234`,
`{"foo\"bar": 123}`,
"{\n\t\"foo\" \n\b\f: \t123}",
// array
`[]`,
`[ 1, {}]`,
strings.Repeat("[", 1000) + strings.Repeat("]", 1000),
`[1, 2, 3, 4, {}]`,
`[`,
`[1,`,
`[1a`,
`[]]`,
`[1 `,
// boolean
"true",
" true ",
"tree",
"false",
" true f",
"fals",
"falsee",
// null
"null ",
" null ",
" nulll ",
"no",
}
for i, test := range tests {
in := []byte(test)
got := ValidateBytes(in) == nil
exp := json.Valid(in)
if got != exp {
t.Errorf("#%d: %q got valid? %v, exp? %v", i, in, got, exp)
}
}
}