-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathformat_test.go
More file actions
279 lines (272 loc) · 5.14 KB
/
Copy pathformat_test.go
File metadata and controls
279 lines (272 loc) · 5.14 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
// This file was ported from https://github.com/Microsoft/vscode/blob/c0bc1ace7ca3ce2d6b1aeb2bde9d1bb0f4b4bae6/src/vs/base/common/jsonFormatter.ts,
// which is licensed as follows:
//
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.
package jsonx
import (
"strings"
"testing"
)
func TestFormat(t *testing.T) {
defaultFormatOptions := FormatOptions{TabSize: 2, InsertSpaces: true, EOL: "\n"}
tests := map[string]struct {
input string
options *FormatOptions
want string
}{
"object - single property": {
input: `{"x" : 1}`,
want: `
{
"x": 1
}`},
"object - unicode": {
input: `{"你好" : 1}`,
want: `
{
"你好": 1
}`},
"object - multi-line": {
input: `{
"x": "y"
}`,
want: `
{
"x": "y"
}`},
"object - multiple properties": {
input: `{"x" : 1, "y" : "foo", "z" : true}`,
want: `{
"x": 1,
"y": "foo",
"z": true
}`},
"object - no properties ": {
input: `{"x" : { }, "y" : {}}`,
want: `{
"x": {},
"y": {}
}`},
"object - nesting": {
input: `{"x" : { "y" : { "z" : { }}, "a": true}}`,
want: `{
"x": {
"y": {
"z": {}
},
"a": true
}
}`},
"array - single items": {
input: `["[]"]`,
want: `[
"[]"
]`},
"array - multiple items": {
input: `[true,null,1.2]`,
want: `[
true,
null,
1.2
]`},
"array - no items": {
input: `[ ]`,
want: `[]`},
"array - nesting": {
input: `[ [], [ [ {} ], "a" ] ]`,
want: `[
[],
[
[
{}
],
"a"
]
]`},
"syntax errors": {
input: `[ null 1.2 ]`,
want: `[
null 1.2
]`},
"empty lines": {
input: `{
"a": true,
"b": true
}`,
options: &FormatOptions{TabSize: 2, InsertSpaces: false, EOL: "\n"},
want: `{
"a": true,
"b": true
}`},
"single line comment": {
input: `[
//comment 你好
"foo", "bar"
] `,
want: `[
//comment 你好
"foo",
"bar"
]`},
"block line comment": {
input: `[{
/*comment 你好*/
"foo" : true
}] `,
want: `[
{
/*comment 你好*/
"foo": true
}
]`},
"single line comment on same line": {
input: ` {
"a": {}// comment 你好
} `,
want: `{
"a": {} // comment 你好
}`},
"single line comment on same line 2": {
input: `{ //comment 你好
}`,
want: `{ //comment 你好
}`},
"block comment on same line": {
input: `{ "a": {}, /*comment 你好*/
/*comment 你好*/ "b": {},
"c": {/*comment 你好*/} } `,
want: `{
"a": {}, /*comment 你好*/
/*comment 你好*/ "b": {},
"c": { /*comment 你好*/}
}`},
"block comment on same line advanced": {
input: ` { "d": [
null
] /*comment 你好*/
,"e": /*comment 你好*/ [null] }`,
want: `{
"d": [
null
] /*comment 你好*/,
"e": /*comment 你好*/ [
null
]
}`},
"multiple block comments on same line": {
input: `{ "a": {} /*comment 你好*/, /*comment 你好*/
/*comment 你好*/ "b": {} /*comment 你好*/ } `,
want: `{
"a": {} /*comment 你好*/, /*comment 你好*/
/*comment 你好*/ "b": {} /*comment 你好*/
}`},
"multiple mixed comments on same line": {
input: `[ /*comment 你好*/ /*comment 你好*/ // comment
]`,
want: `[ /*comment 你好*/ /*comment 你好*/ // comment
]`},
"range": {
input: `{ "a": {},
|"b": [null, null]|
} `,
want: `{ "a": {},
"b": [
null,
null
]
} `},
"range with existing indent": {
input: `{ "a": {},
|"b": [null],
"c": {}
} |`,
want: `{ "a": {},
"b": [
null
],
"c": {}
}`},
"range with existing indent - tabs": {
input: `{ "a": {},
| "b": [null],
"c": {}
} | `,
options: &FormatOptions{TabSize: 2, InsertSpaces: false, EOL: "\n"},
want: `{ "a": {},
"b": [
null
],
"c": {}
}`},
"block comment none-line breaking symbols": {
input: `{ "a": [ 1
/* comment 你好 */
, 2
/* comment 你好 */
]
/* comment 你好 */
,
"b": true
/* comment 你好 */
}`,
want: `{
"a": [
1
/* comment 你好 */
,
2
/* comment 你好 */
]
/* comment 你好 */
,
"b": true
/* comment 你好 */
}`},
"line comment after none-line breaking symbols": {
input: `{ "a":
// comment 你好
null,
"b"
// comment 你好
: null
// comment 你好
}`,
want: `{
"a":
// comment 你好
null,
"b"
// comment 你好
: null
// comment 你好
}`},
}
for label, test := range tests {
t.Run(label, func(t *testing.T) {
options := test.options
if options == nil {
options = &defaultFormatOptions
}
var input string
var offset, length int
if strings.Count(test.input, "|") == 2 {
rangeStart := strings.Index(test.input, "|")
rangeEnd := strings.LastIndex(test.input, "|")
input = test.input[:rangeStart] + test.input[rangeStart+1:rangeEnd] + test.input[rangeEnd+1:]
offset = rangeStart
length = rangeEnd - rangeStart
} else {
input = test.input
length = len([]rune(test.input))
}
edits := FormatRange(input, offset, length, *options)
output, err := ApplyEdits(input, edits...)
if err != nil {
t.Fatal(err)
}
if want := strings.TrimPrefix(test.want, "\n"); output != want {
t.Errorf("formatted\ngot %s\nwant %s", output, want)
}
})
}
}