Skip to content

Commit

Permalink
test(json): improve json testing (#5075)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Jun 18, 2024
1 parent 7e683c1 commit 1d0f658
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
15 changes: 6 additions & 9 deletions json/_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import type { JsonValue } from "./common.ts";
export function parse(text: string): JsonValue {
try {
return JSON.parse(text);
} catch (error: unknown) {
if (error instanceof Error) {
// Truncate the string so that it is within 30 lengths.
const truncatedText = 30 < text.length ? `${text.slice(0, 30)}...` : text;
throw new (error.constructor as ErrorConstructor)(
`${error.message} (parsing: '${truncatedText}')`,
);
}
throw error;
} catch (error) {
// Truncate the string so that it is within 30 lengths.
const truncatedText = 30 < text.length ? `${text.slice(0, 30)}...` : text;
throw new ((error as Error).constructor as ErrorConstructor)(
`${(error as Error).message} (parsing: '${truncatedText}')`,
);
}
}
8 changes: 8 additions & 0 deletions json/concatenated_json_parse_stream_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ Deno.test({
["tr", 'ue{"foo": "bar"}'],
[true, { foo: "bar" }],
);
// Invalid primitive which share some leading characters with the valid primitive
await assertInvalidParse(
ConcatenatedJsonParseStream,
["truu"],
{},
SyntaxError,
`Unexpected token 'u', \"truu\" is not valid JSON (parsing: 'truu')`,
);
},
});

Expand Down

0 comments on commit 1d0f658

Please sign in to comment.