deno.land / std@0.166.0 / encoding / testdata / jsonc / node-jsonc-parser / test.ts

نووسراو ببینە
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// Ported from node-jsonc-parser// https://github.com/microsoft/node-jsonc-parser/blob/35d94cd71bd48f9784453b2439262c938e21d49b/src/test/json.test.ts/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/
import * as JSONC from "../../../jsonc.ts";import { assertEquals, assertThrows } from "../../../../testing/asserts.ts";function assertValidParse( text: string, expected: unknown, options?: JSONC.ParseOptions,) { assertEquals(JSONC.parse(text, options), expected);}function assertInvalidParse( text: string, // deno-lint-ignore no-explicit-any ErrorClass: (new (...args: any[]) => Error), msgIncludes?: string, options?: JSONC.ParseOptions,) { assertThrows( () => JSONC.parse(text, options), ErrorClass, msgIncludes, );}
Deno.test("[jsonc] parse node-jsonc-parser:literals", () => { assertValidParse("true", true); assertValidParse("false", false); assertValidParse("null", null); assertValidParse('"foo"', "foo"); assertValidParse( '"\\"-\\\\-\\/-\\b-\\f-\\n-\\r-\\t"', '"-\\-/-\b-\f-\n-\r-\t', ); assertValidParse('"\\u00DC"', "Ü"); assertValidParse("9", 9); assertValidParse("-9", -9); assertValidParse("0.129", 0.129); assertValidParse("23e3", 23e3); assertValidParse("1.2E+3", 1.2E+3); assertValidParse("1.2E-3", 1.2E-3); assertValidParse("1.2E-3 // comment", 1.2E-3);});
Deno.test("[jsonc] parse node-jsonc-parser:objects", () => { assertValidParse("{}", {}); assertValidParse('{ "foo": true }', { foo: true }); assertValidParse('{ "bar": 8, "xoo": "foo" }', { bar: 8, xoo: "foo" }); assertValidParse('{ "hello": [], "world": {} }', { hello: [], world: {} }); assertValidParse('{ "a": false, "b": true, "c": [ 7.4 ] }', { a: false, b: true, c: [7.4], }); assertValidParse( '{ "lineComment": "//", "blockComment": ["/*", "*/"], "brackets": [ ["{", "}"], ["[", "]"], ["(", ")"] ] }', { lineComment: "//", blockComment: ["/*", "*/"], brackets: [["{", "}"], ["[", "]"], ["(", ")"]], }, ); assertValidParse('{ "hello": [], "world": {} }', { hello: [], world: {} }); assertValidParse('{ "hello": { "again": { "inside": 5 }, "world": 1 }}', { hello: { again: { inside: 5 }, world: 1 }, }); assertValidParse('{ "foo": /*hello*/true }', { foo: true }); assertValidParse('{ "": true }', { "": true });});
Deno.test("[jsonc] parse node-jsonc-parser:arrays", () => { assertValidParse("[]", []); assertValidParse("[ [], [ [] ]]", [[], [[]]]); assertValidParse("[ 1, 2, 3 ]", [1, 2, 3]); assertValidParse('[ { "a": null } ]', [{ a: null }]);});
Deno.test("[jsonc] parse node-jsonc-parser:objects with errors", () => { assertInvalidParse("{,}", SyntaxError); assertInvalidParse('{ "foo": true, }', SyntaxError, undefined, { allowTrailingComma: false, }); assertInvalidParse('{ "bar": 8 "xoo": "foo" }', SyntaxError); assertInvalidParse('{ ,"bar": 8 }', SyntaxError); assertInvalidParse('{ ,"bar": 8, "foo" }', SyntaxError); assertInvalidParse('{ "bar": 8, "foo": }', SyntaxError); assertInvalidParse('{ 8, "foo": 9 }', SyntaxError);});
Deno.test("[jsonc] parse node-jsonc-parser:array with errors", () => { assertInvalidParse("[,]", SyntaxError); assertInvalidParse("[ 1 2, 3 ]", SyntaxError); assertInvalidParse("[ ,1, 2, 3 ]", SyntaxError); assertInvalidParse("[ ,1, 2, 3, ]", SyntaxError);});
Deno.test("[jsonc] parse node-jsonc-parser:errors", () => { assertInvalidParse("", SyntaxError); assertInvalidParse("1,1", SyntaxError);});
Deno.test("[jsonc] parse node-jsonc-parser:trailing comma", () => { const options = { allowTrailingComma: false }; assertValidParse('{ "hello": [], }', { hello: [] }); assertValidParse('{ "hello": [] }', { hello: [] }); assertValidParse( '{ "hello": [], "world": {}, }', { hello: [], world: {} }, ); assertValidParse( '{ "hello": [], "world": {} }', { hello: [], world: {} }, ); assertValidParse("[ 1, 2, ]", [1, 2]); assertValidParse("[ 1, 2 ]", [1, 2]);
assertInvalidParse('{ "hello": [], }', SyntaxError, undefined, options); assertInvalidParse( '{ "hello": [], "world": {}, }', SyntaxError, undefined, options, ); assertInvalidParse("[ 1, 2, ]", SyntaxError, undefined, options);});
std

Version Info

Tagged at
a year ago