Skip to content

Commit

Permalink
test(jsonc): remove dead code and improve testing (#5093)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Jun 20, 2024
1 parent bd55f3b commit 09d758a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
7 changes: 5 additions & 2 deletions jsonc/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class JSONCParser {
}
}
}

#parseJsonValue(value: Token): JsonValue {
switch (value.type) {
case "BeginObject":
Expand All @@ -217,6 +218,7 @@ class JSONCParser {
throw new SyntaxError(buildErrorMessage(value));
}
}

#parseObject(): { [key: string]: JsonValue | undefined } {
const target: { [key: string]: JsonValue | undefined } = {};
// ┌─token1
Expand Down Expand Up @@ -276,6 +278,7 @@ class JSONCParser {
}
}
}

#parseArray(): JsonValue[] {
const target: JsonValue[] = [];
// ┌─token1
Expand Down Expand Up @@ -311,6 +314,7 @@ class JSONCParser {
}
}
}

#parseString(value: {
type: "String";
sourceText: string;
Expand All @@ -328,6 +332,7 @@ class JSONCParser {
}
return parsed;
}

#parseNullOrTrueOrFalseOrNumber(value: {
type: "NullOrTrueOrFalseOrNumber";
sourceText: string;
Expand Down Expand Up @@ -384,8 +389,6 @@ function buildErrorMessage({ type, sourceText, position }: Token): string {
? `${sourceText.slice(0, 30)}...`
: sourceText;
break;
default:
throw new Error("unreachable");
}
return `Unexpected token ${token} in JSONC at position ${position}`;
}
31 changes: 31 additions & 0 deletions jsonc/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
assertThrows,
} from "@std/assert";

import "./testdata/JSONTestSuite/test.ts";
import "./testdata/node-jsonc-parser/test.ts";
import "./testdata/test262/test.ts";

// The test code for the jsonc module can also be found in the testcode directory.

function assertValidParse(
Expand Down Expand Up @@ -111,6 +115,21 @@ Deno.test({
SyntaxError,
"Unexpected token aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... in JSONC at position 1",
);
assertInvalidParse(
`}`,
SyntaxError,
"Unexpected token } in JSONC at position 0",
);
assertInvalidParse(
`]`,
SyntaxError,
"Unexpected token ] in JSONC at position 0",
);
assertInvalidParse(
`,`,
SyntaxError,
"Unexpected token , in JSONC at position 0",
);
},
});

Expand Down Expand Up @@ -200,3 +219,15 @@ Deno.test({
assert(success);
},
});

Deno.test({
name: "new parse() throws error",
fn() {
assertThrows(
// deno-lint-ignore no-explicit-any
() => new (parse as any)(""),
TypeError,
"parse is not a constructor",
);
},
});

0 comments on commit 09d758a

Please sign in to comment.