-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
53 lines (51 loc) · 2.3 KB
/
json_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
package json_utils
import "testing"
func TestSanitizeJson(t *testing.T) {
cases := []struct { in, want string
}{
// validity
{ "[\"a\", \"b\", \"c\"]", "[\"a\", \"b\", \"c\"]" },
// remove comments
{ "//comment\n{\"a\":\"b\"}", "\n{\"a\":\"b\"}" },
{ "/*//comment*/{\"a\":\"b\"}", "{\"a\":\"b\"}" },
{ "{\"a\":\"b\"//comment\n}", "{\"a\":\"b\"\n}" },
{ "{\"a\":\"b\"/*comment*/}", "{\"a\":\"b\"}" },
{ "{\"a\"/*\n\n\ncomment\r\n*/:\"b\"}", "{\"a\":\"b\"}" },
{ "/*!\n * comment\n */\n{\"a\":\"b\"}", "\n{\"a\":\"b\"}" },
{ "{/*comment*/\"a\":\"b\"}", "{\"a\":\"b\"}" },
// doesn\'t strip comments inside strings
{ "{\"a\":\"b//c\"}", "{\"a\":\"b//c\"}" },
{ "{\"a\":\"b/*c*/\"}", "{\"a\":\"b/*c*/\"}" },
{ "{\"/*a\":\"b\"}", "{\"/*a\":\"b\"}" },
{ "{\"\\\"/*a\":\"b\"}", "{\"\\\"/*a\":\"b\"}" },
// escaped slashes
{ "{\"\\\\\":\"https://foobar.com\"}", "{\"\\\\\":\"https://foobar.com\"}" },
{ "{\"foo\\\"\":\"https://foobar.com\"}", "{\"foo\\\"\":\"https://foobar.com\"}" },
// line endings - no comments
{ "{\"a\":\"b\"\n}", "{\"a\":\"b\"\n}" },
{ "{\"a\":\"b\"\r\n}", "{\"a\":\"b\"\r\n}" },
// line endings - single line comment
{ "{\"a\":\"b\"//c\n}", "{\"a\":\"b\"\n}" },
{ "{\"a\":\"b\"//c\r\n}", "{\"a\":\"b\"\r\n}" },
{ "{\"a\":\"b\"//c\r}", "{\"a\":\"b\"\r}" },
// line endings - single line block comment
{ "{\"a\":\"b\"/*c*/\n}", "{\"a\":\"b\"\n}" },
{ "{\"a\":\"b\"/*c*/\r\n}", "{\"a\":\"b\"\r\n}" },
{ "{\"a\":\"b\"/*c*/\r}", "{\"a\":\"b\"\r}" },
// line endings - multi line block comment
{ "{\"a\":\"b\",/*c\nc2*/\"x\":\"y\"\n}", "{\"a\":\"b\",\"x\":\"y\"\n}" },
{ "{\"a\":\"b\",/*c\r\nc2*/\"x\":\"y\"\r\n}", "{\"a\":\"b\",\"x\":\"y\"\r\n}" },
// strips trailing commas
{ "{\"x\":true,}", "{\"x\":true}" },
{ "{\"x\":true,\n }", "{\"x\":true\n }" },
{ "[true, false,]", "[true, false]" },
{ "{\n \"array\": [\n true,\n false,\n ],\n}", "{\n \"array\": [\n true,\n false\n ]\n}" },
{ "{\n \"array\": [\n true,\n false /* comment */ ,\n /*comment*/ ],\n}", "{\n \"array\": [\n true,\n false \n ]\n}" },
}
for _, c := range cases {
got := string(stripComments([]byte(c.in)))
if got != c.want {
t.Errorf("stripComments(%q) == %q, want %q", c.in, got, c.want)
}
}
}