Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(yaml): add misc parse() test cases #5188

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions yaml/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ Deno.test("parse() handles %YAML directive", () => {
assertEquals(
parse(`%YAML 1.2
---
hello: world`),
{ hello: "world" },
);
// you can write comments after the directive
assertEquals(
parse(`%YAML 1.2 # comment
---
hello: world`),
{ hello: "world" },
);
Expand Down Expand Up @@ -528,3 +535,80 @@ bar: baz`),
{ foo: "\na b\nc\n - d\n - e\n\nf g\n" },
);
});

Deno.test("parse() handles BOM at the beginning of the yaml", () => {
const yaml = "\uFEFFhello: world";
assertEquals(parse(yaml), { hello: "world" });
});

Deno.test("parse() throws if there are more than one document in the yaml", () => {
assertThrows(
() => parse("hello: world\n---\nfoo: bar"),
YamlError,
"expected a single document in the stream, but found more",
);
});

Deno.test("parse() throws when the directive name is empty", () => {
assertThrows(
() =>
parse(`% 1.2
---
hello: world`),
YamlError,
"directive name must not be less than one character in length at line 1, column 2:\n % 1.2\n ^",
);
});

Deno.test("parse() ignores unknown directive", () => {
assertEquals(
parse(`%FOOBAR 1.2
---
hello: world`),
{ hello: "world" },
);
});

Deno.test("parse() handles document separator", () => {
assertEquals(
parse(`---
hello: world
...`),
{ hello: "world" },
);
});

Deno.test("parse() show warning with non-ascii line breaks if YAML version is < 1.2", () => {
const warningSpy = spy();
assertEquals(
parse(
`%YAML 1.1
---
hello: world\r\nfoo: bar\x85`,
{ onWarning: warningSpy },
),
{ hello: "world", foo: "bar\x85" },
);
assertSpyCall(warningSpy, 0);
const warning = warningSpy.calls[0]?.args[0];
assertEquals(
warning.message,
"non-ASCII line breaks are interpreted as content at line 5, column 1:\n \n ^",
);
});

Deno.test("parse() throws with directive only document", () => {
assertThrows(
() => parse(`%YAML 1.2`),
YamlError,
"directives end mark is expected at line 2, column 1:\n \n ^",
);
});

Deno.test("parse() throws with \0 in the middle of the document", () => {
assertThrows(
() => parse("hello: \0 world"),
YamlError,
"end of the stream or a document separator is expected at line 1, column 8:\n hello: \n ^",
);
});