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