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): test sortKeys option behavior #5523

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions yaml/stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { assertEquals, assertThrows } from "@std/assert";
import { stringify } from "./stringify.ts";
import { compare, parse } from "@std/semver";

Deno.test({
name: "stringify()",
Expand Down Expand Up @@ -442,3 +443,42 @@ skills:
const actual = stringify(object);
assertEquals(actual.trim(), expected.trim());
});

Deno.test("stringify() changes the key order when the sortKeys option is specified", () => {
const object = {
"1.0.0": null,
"0.0.0-0": null,
"0.0.0": null,
"1.0.2": null,
"1.0.10": null,
};
assertEquals(
stringify(object),
`1.0.0: null
0.0.0-0: null
0.0.0: null
1.0.2: null
1.0.10: null
`,
);
// When sortKeys is true, keys are sorted in ASCII char order
assertEquals(
stringify(object, { sortKeys: true }),
`0.0.0: null
0.0.0-0: null
1.0.0: null
1.0.10: null
1.0.2: null
`,
);
// When sortKeys is a function, keys are sorted by the return value of the function
assertEquals(
stringify(object, { sortKeys: (a, b) => compare(parse(a), parse(b)) }),
`0.0.0-0: null
0.0.0: null
1.0.0: null
1.0.2: null
1.0.10: null
`,
);
});