-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecommenter_test.go
165 lines (158 loc) · 3.87 KB
/
decommenter_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
162
163
164
165
package betterjson
import (
"testing"
)
func TestDecommenter(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "Single line comment",
input: `{"name": "Alice"} // Comment`,
expected: `{"name": "Alice"} `,
},
{
name: "Multi-line comment",
input: `{"name": "Alice" /* Comment */}`,
expected: `{"name": "Alice" }`,
},
{
name: "Nested comments",
input: `{"name": "Alice" /* Comment // Nested comment */}`,
expected: `{"name": "Alice" }`,
},
{
name: "Comment at the end of file without newline",
input: `{"name": "Alice"} // Comment`,
expected: `{"name": "Alice"} `,
},
{
name: "Comment at the end of file with newline",
input: `{"name": "Alice"} // Comment
`,
expected: `{"name": "Alice"}
`,
},
{
name: "Multiple single line comments",
input: `// Comment 1
// Comment 2
{"name": "Alice"}`,
expected: `
{"name": "Alice"}`,
},
{
name: "Multiple multi-line comments",
input: `/* Comment 1 */
/* Comment 2 */
{"name": "Alice"}`,
expected: `
{"name": "Alice"}`,
},
{
name: "Mixed comments",
input: `/* Comment 1 */
// Comment 2
{"name": "Alice"}`,
expected: `
{"name": "Alice"}`,
},
{
name: "No comments",
input: `{"name": "Alice"} `,
expected: `{"name": "Alice"} `,
},
{
name: " Single comment slashes in string",
input: `{"name": "//Alice"} `,
expected: `{"name": "//Alice"} `,
},
{
name: " Multi comment slashes in string",
input: `{"name": "/*Alice*/"} `,
expected: `{"name": "/*Alice*/"} `,
},
{
name: "Comment slashes inside string followed by actual comment",
input: `{"name": "//Alice"} // Comment`,
expected: `{"name": "//Alice"} `,
},
{
name: "Multi-line comment delimiters inside string",
input: `{"name": "/*Alice*/"} /* Comment */`,
expected: `{"name": "/*Alice*/"} `,
},
{
name: "Empty input",
input: ``,
expected: ``,
},
{
name: "Only single line comment",
input: `// Comment`,
expected: ` `,
},
{
name: "Only multi-line comment",
input: `/* Comment */`,
expected: ` `,
},
{
name: "Comment after a comma",
input: `{"name": "Alice", /* Comment */ "age": 30}`,
expected: `{"name": "Alice", "age": 30}`,
},
{
name: "Comment inside array",
input: `["Alice", /* Comment */ "Bob"]`,
expected: `["Alice", "Bob"]`,
},
{
name: "Newline inside string",
input: `{"message": "Hello
World"}`,
expected: `{"message": "Hello
World"}`,
},
{
name: "Newline inside string followed by actual comment",
input: `{"message": "Hello
World"} // Comment`,
expected: `{"message": "Hello
World"} `,
},
{
name: "Newline inside string with multi-line comment after",
input: `{"message": "Hello
World"} /* Comment */`,
expected: `{"message": "Hello
World"} `,
},
{
name: "Multiple newlines inside string & single comment at the end",
input: "{\"message\": \"Hello\n\nWorld\"}// ",
expected: "{\"message\": \"Hello\n\nWorld\"} ",
},
{
name: "Newline inside string with actual newline after",
input: `{"message": "Hello
World"}
// Comment`,
expected: `{"message": "Hello
World"}
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := decomment([]byte(tt.input))
if string(got) != tt.expected {
t.Errorf("\ntest: \"%s\"\ngot \"%s\"\nwant \"%s\"", tt.input, string(got), tt.expected)
} else {
t.Logf("test \"%s\" passed", tt.name)
}
})
}
}