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 parse() test for duplicate keys #5240

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
13 changes: 13 additions & 0 deletions yaml/parse_test.ts
Copy link
Contributor Author

@iuioiua iuioiua Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current documentation says that if ParseOptions.json is true, then duplicate keys cause parse() to throw, like JSON.parse(). But by default, ParseOptions.json is false, and duplicate keys still cause parse() to throw. In other words, ParseOptions.json currently does the exact opposite of what it should 😆

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON.parse() doesn't seem throwing with duplicated keys?

> JSON.parse('{"a":1,"a":2}')
{ a: 2 }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok - then my understanding of JSON.parse() was wrong 😆

Original file line number Diff line number Diff line change
Expand Up @@ -784,3 +784,16 @@ Deno.test("parse() throws with empty mapping key", () => {
"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 ^",
);
});

Deno.test("parse() throws on duplicate keys", () => {
assertThrows(
() =>
parse(
`name: John Doe
age: 30
name: Jane Doe`,
),
YamlError,
"duplicated mapping key at line 3, column 1:\n name: Jane Doe\n ^",
);
});