forked from muhammadmuzzammil1998/jsonc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsonc_test.go
More file actions
293 lines (277 loc) · 7.88 KB
/
jsonc_test.go
File metadata and controls
293 lines (277 loc) · 7.88 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package jsonc
import (
"encoding/json"
"os"
"reflect"
"testing"
)
func b(s string) []byte { return []byte(s) }
func s(b []byte) string { return string(b) }
type testsStruct struct {
validBlock []byte
validSingle []byte
invalidBlock []byte
invalidSingle []byte
validHash []byte
invalidHash []byte
}
var jsonTest, jsoncTest testsStruct
func init() {
jsonTest = testsStruct{
validBlock: b(`{"foo":"bar foo","true":false,"number":42,"object":{"test":"done"},"array":[1,2,3],"url":"https://github.com","escape":"\"wo//rking"}`),
invalidBlock: b(`{"foo":`),
}
jsoncTest = testsStruct{
validBlock: b(`{"foo": /** this is a bloc/k comm\"ent */ "bar foo", "true": /* true */ false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com", "escape":"\"wo//rking" }`),
invalidBlock: b(`{"foo": /* this is a block comment "bar foo", "true": false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com", "escape":"\"wo//rking }`),
validSingle: b("{\"foo\": // this is a \"single **/line comm\\\"ent\n\"bar foo\", \"true\": false, \"number\": 42, \"object\": { \"test\": \"done\" }, \"array\" : [1, 2, 3], \"url\" : \"https://github.com\", \"escape\":\"\\\"wo//rking\" }"),
invalidSingle: b(`{"foo": // this is a single line comment "bar foo", "true": false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com", "escape":"\"wo//rking" }`),
validHash: b("{\"foo\": # this is a single line comm\\\"ent\n\"bar foo\", \"true\": false, \"number\": 42, \"object\": { \"test\": \"done\" }, \"array\" : [1, 2, 3], \"url\" : \"https://github.com\", \"escape\":\"\\\"wo//rking\" }"),
invalidHash: b(`{"foo": # this is a single line comment "bar foo", "true": false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com", "escape":"\"wo//rking" }`),
}
}
func TestToJSON(t *testing.T) {
tests := []struct {
name string
arg []byte
want []byte
wantErr bool
}{
{
name: "Test for valid block comment.",
arg: jsoncTest.validBlock,
want: jsonTest.validBlock,
}, {
name: "Test for invalid block comment.",
arg: jsoncTest.invalidBlock,
want: jsonTest.invalidBlock,
wantErr: true,
}, {
name: "Test for valid single line comment.",
arg: jsoncTest.validSingle,
want: jsonTest.validBlock,
}, {
name: "Test for invalid single line comment.",
arg: jsoncTest.invalidSingle,
want: jsonTest.invalidBlock,
wantErr: true,
}, {
name: "Test for valid single line comment started with hash.",
arg: jsoncTest.validHash,
want: jsonTest.validBlock,
}, {
name: "Test for invalid single line comment started with hash.",
arg: jsoncTest.invalidHash,
want: jsonTest.invalidBlock,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ToJSON(tt.arg)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToJSON() = %v, want %v", s(got), s(tt.want))
}
if !json.Valid(got) && !tt.wantErr {
t.Errorf("ToJSON() = %v isn't valid.", s(got))
}
})
}
}
func TestUnmarshal(t *testing.T) {
type UnmarshalTest struct {
Foo string `json:"foo"`
True bool `json:"true"`
Num int `json:"number"`
Object struct {
Test string `json:"test"`
} `json:"object"`
Array []int `json:"array"`
URL string `json:"url"`
Escape string `json:"escape"`
}
t.Run("Testing Unmarshal()", func(t *testing.T) {
un := UnmarshalTest{}
if err := Unmarshal(jsoncTest.validBlock, &un); err != nil {
t.Errorf("Unmarshal() error = %v", err)
}
mr, err := json.Marshal(un)
if err != nil {
t.Errorf("Unmarshal() unable to marshal Unmarshal(). Error = %v, got = %v, want = %v", err, s(mr), s(jsonTest.validBlock))
}
if !reflect.DeepEqual(mr, jsonTest.validBlock) {
t.Errorf("Unmarshal() didn't work correctly. Got = %v, want = %v", s(mr), s(jsonTest.validBlock))
}
})
}
func TestReadFromFile(t *testing.T) {
tmp, err := os.CreateTemp("", "ReadFromFileTest")
if err != nil {
t.Skip("Unable to create temp file.", err)
}
defer os.Remove(tmp.Name())
if _, err := tmp.Write(jsoncTest.validBlock); err != nil {
t.Skip("Unable to write to file.", err)
}
defer func() {
if err := tmp.Close(); err != nil {
t.Log("Unable to close the file.", err)
}
}()
tests := []struct {
name string
filename string
want []byte
want1 []byte
wantErr bool
}{
{
name: "Valid use of ReadFromFile()",
filename: tmp.Name(),
want: jsoncTest.validBlock,
want1: jsonTest.validBlock,
wantErr: false,
},
{
name: "Invalid use of ReadFromFile()",
filename: "NonExistentFile",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := ReadFromFile(tt.filename)
if (err != nil) != tt.wantErr {
t.Errorf("ReadFromFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReadFromFile() got = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("ReadFromFile() got1 = %v, want %v", got1, tt.want1)
}
})
}
}
func TestValid(t *testing.T) {
tests := []struct {
name string
data []byte
want bool
}{
{
name: "Valid test",
data: b(`{"foo":/*comment*/"bar"}`),
want: true,
},
{
name: "Invalid test",
data: b(`{"foo"://comment without ending`),
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Valid(tt.data); got != tt.want {
t.Errorf("Valid() = %v, want %v", got, tt.want)
}
})
}
}
func TestTrailingCommas(t *testing.T) {
tests := []struct {
name string
in []byte
want []byte
invalid bool
}{
{
name: "trailing comma in array",
in: b(`[1, 2, 3,]`),
want: b(`[1,2,3]`),
},
{
name: "trailing comma in object",
in: b(`{"a": 1, "b": 2,}`),
want: b(`{"a":1,"b":2}`),
},
{
name: "trailing comma after line comment removal",
in: b("[\n \"a\",\n // \"b\",\n]"),
want: b(`["a"]`),
},
{
name: "trailing comma after block comment removal",
in: b(`[1, /* skipped */ ]`),
want: b(`[1]`),
},
{
name: "non-trailing comma before comment is preserved",
in: b(`[1, /* comment */ 2]`),
want: b(`[1,2]`),
},
{
name: "trailing comma in nested structures",
in: b(`{"arr": [1, 2,], "obj": {"k": "v",},}`),
want: b(`{"arr":[1,2],"obj":{"k":"v"}}`),
},
{
name: "comma followed by whitespace then close",
in: b("[1,\n\t ]"),
want: b(`[1]`),
},
{
name: "no false positives on commas inside strings",
in: b(`["a,]", "b,}"]`),
want: b(`["a,]","b,}"]`),
},
{
name: "no false positives on escaped commas in strings",
in: b(`["esc\",", "ok"]`),
want: b(`["esc\",","ok"]`),
},
{
name: "non-trailing commas preserved",
in: b(`[1, 2, 3]`),
want: b(`[1,2,3]`),
},
{
name: "empty containers untouched",
in: b(`{"a": [], "b": {}}`),
want: b(`{"a":[],"b":{}}`),
},
{
name: "consecutive separators remain invalid",
in: b(`[1,, ,]`),
want: b(`[1,,]`),
invalid: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ToJSON(tt.in)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToJSON(%q) = %q, want %q", s(tt.in), s(got), s(tt.want))
}
wantValid := !tt.invalid
if gotValid := json.Valid(got); gotValid != wantValid {
t.Errorf("json.Valid(ToJSON(%q)) = %v, want %v; got %q", s(tt.in), gotValid, wantValid, s(got))
}
})
}
}
func BenchmarkTranslate(b *testing.B) {
for n := 0; n < b.N; n++ {
translate(jsoncTest.validSingle)
translate(jsoncTest.validBlock)
translate(jsoncTest.validHash)
}
}
func BenchmarkValid(b *testing.B) {
for n := 0; n < b.N; n++ {
Valid(jsoncTest.validSingle)
Valid(jsoncTest.validBlock)
Valid(jsoncTest.validHash)
}
}