Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/jsonParse.test.ts
5248 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import assert from 'assert';
6
7
import { parse, stripComments } from '../../common/jsonc.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';
9
10
suite('JSON Parse', () => {
11
ensureNoDisposablesAreLeakedInTestSuite();
12
13
test('Line comment', () => {
14
const content: string = [
15
'{',
16
' "prop": 10 // a comment',
17
'}',
18
].join('\n');
19
const expected = [
20
'{',
21
' "prop": 10 ',
22
'}',
23
].join('\n');
24
assert.deepEqual(parse(content), JSON.parse(expected));
25
});
26
test('Line comment - EOF', () => {
27
const content: string = [
28
'{',
29
'}',
30
'// a comment'
31
].join('\n');
32
const expected = [
33
'{',
34
'}',
35
''
36
].join('\n');
37
assert.deepEqual(parse(content), JSON.parse(expected));
38
});
39
test('Line comment - \\r\\n', () => {
40
const content: string = [
41
'{',
42
' "prop": 10 // a comment',
43
'}',
44
].join('\r\n');
45
const expected = [
46
'{',
47
' "prop": 10 ',
48
'}',
49
].join('\r\n');
50
assert.deepEqual(parse(content), JSON.parse(expected));
51
});
52
test('Line comment - EOF - \\r\\n', () => {
53
const content: string = [
54
'{',
55
'}',
56
'// a comment'
57
].join('\r\n');
58
const expected = [
59
'{',
60
'}',
61
''
62
].join('\r\n');
63
assert.deepEqual(parse(content), JSON.parse(expected));
64
});
65
test('Block comment - single line', () => {
66
const content: string = [
67
'{',
68
' /* before */"prop": 10/* after */',
69
'}',
70
].join('\n');
71
const expected = [
72
'{',
73
' "prop": 10',
74
'}',
75
].join('\n');
76
assert.deepEqual(parse(content), JSON.parse(expected));
77
});
78
test('Block comment - multi line', () => {
79
const content: string = [
80
'{',
81
' /**',
82
' * Some comment',
83
' */',
84
' "prop": 10',
85
'}',
86
].join('\n');
87
const expected = [
88
'{',
89
' ',
90
' "prop": 10',
91
'}',
92
].join('\n');
93
assert.deepEqual(parse(content), JSON.parse(expected));
94
});
95
test('Block comment - shortest match', () => {
96
const content = '/* abc */ */';
97
const expected = ' */';
98
assert.strictEqual(stripComments(content), expected);
99
});
100
test('No strings - double quote', () => {
101
const content: string = [
102
'{',
103
' "/* */": 10',
104
'}'
105
].join('\n');
106
const expected: string = [
107
'{',
108
' "/* */": 10',
109
'}'
110
].join('\n');
111
assert.deepEqual(parse(content), JSON.parse(expected));
112
});
113
test('No strings - single quote', () => {
114
const content: string = [
115
'{',
116
` '/* */': 10`,
117
'}'
118
].join('\n');
119
const expected: string = [
120
'{',
121
` '/* */': 10`,
122
'}'
123
].join('\n');
124
assert.strictEqual(stripComments(content), expected);
125
});
126
test('Trailing comma in object', () => {
127
const content: string = [
128
'{',
129
` "a": 10,`,
130
'}'
131
].join('\n');
132
const expected: string = [
133
'{',
134
` "a": 10`,
135
'}'
136
].join('\n');
137
assert.deepEqual(parse(content), JSON.parse(expected));
138
});
139
test('Trailing comma in array', () => {
140
const content: string = [
141
`[ "a", "b", "c", ]`
142
].join('\n');
143
const expected: string = [
144
`[ "a", "b", "c" ]`
145
].join('\n');
146
assert.deepEqual(parse(content), JSON.parse(expected));
147
});
148
149
test('Trailing comma', () => {
150
const content: string = [
151
'{',
152
' "propA": 10, // a comment',
153
' "propB": false, // a trailing comma',
154
'}',
155
].join('\n');
156
const expected = [
157
'{',
158
' "propA": 10,',
159
' "propB": false',
160
'}',
161
].join('\n');
162
assert.deepEqual(parse(content), JSON.parse(expected));
163
});
164
165
test('Trailing comma - EOF', () => {
166
const content = `
167
// This configuration file allows you to pass permanent command line arguments to VS Code.
168
// Only a subset of arguments is currently supported to reduce the likelihood of breaking
169
// the installation.
170
//
171
// PLEASE DO NOT CHANGE WITHOUT UNDERSTANDING THE IMPACT
172
//
173
// NOTE: Changing this file requires a restart of VS Code.
174
{
175
// Use software rendering instead of hardware accelerated rendering.
176
// This can help in cases where you see rendering issues in VS Code.
177
// "disable-hardware-acceleration": true,
178
// Allows to disable crash reporting.
179
// Should restart the app if the value is changed.
180
"enable-crash-reporter": true,
181
// Unique id used for correlating crash reports sent from this instance.
182
// Do not edit this value.
183
"crash-reporter-id": "aaaaab31-7453-4506-97d0-93411b2c21c7",
184
"locale": "en",
185
// "log-level": "trace"
186
}
187
`;
188
assert.deepEqual(parse(content), {
189
'enable-crash-reporter': true,
190
'crash-reporter-id': 'aaaaab31-7453-4506-97d0-93411b2c21c7',
191
'locale': 'en'
192
});
193
});
194
});
195
196