Skip to content

Commit

Permalink
test(yaml): test sortKeys option behavior (#5523)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k authored Jul 24, 2024
1 parent 3f31893 commit 9679f73
Showing 1 changed file with 40 additions and 0 deletions.
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 @@ -462,6 +463,45 @@ skills:
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
`,
);
});

Deno.test("stringify() changes line wrap behavior based on lineWidth option", () => {
const object = {
message:
Expand Down

0 comments on commit 9679f73

Please sign in to comment.