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 test cases of handling ? mark in YAML #5234

Merged
merged 1 commit into from
Jul 2, 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
60 changes: 60 additions & 0 deletions yaml/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,63 @@ Deno.test("parse() throws with \0 in the middle of the document", () => {
"end of the stream or a document separator is expected at line 1, column 8:\n hello: \n ^",
);
});

Deno.test("parse() handles complex mapping key", () => {
assertEquals(
parse(`? - Detroit Tigers
- Chicago cubs
: - 2001-07-23

? [ New York Yankees,
Atlanta Braves ]
: [ 2001-07-02, 2001-08-12,
2001-08-14 ]`),
{
"Detroit Tigers,Chicago cubs": [new Date("2001-07-23")],
"New York Yankees,Atlanta Braves": [
new Date("2001-07-02"),
new Date("2001-08-12"),
new Date("2001-08-14"),
],
},
);

// Nested array as key is not supported
assertThrows(
() =>
parse(`? - [ foo ]
: bar`),
YamlError,
"nested arrays are not supported inside keys at line 2, column 6:\n : bar\n ^",
);

assertEquals(
parse(`? - { foo: bar }
: baz`),
{ "[object Object]": "baz" },
);

assertEquals(
parse(`? { foo: bar }
: baz`),
{ "[object Object]": "baz" },
);
});

Deno.test("parse() handles unordered set", () => {
assertEquals(
parse(`--- !!set
? Mark McGwire
? Sammy Sosa
? Ken Griffey`),
{ "Mark McGwire": null, "Sammy Sosa": null, "Ken Griffey": null },
);
});

Deno.test("parse() throws with empty mapping key", () => {
assertThrows(
() => parse(`? : 1`),
YamlError,
"incomplete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line at line 1, column 3:\n ? : 1\n ^",
);
});