Skip to content

Commit

Permalink
test(yaml): test float handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed May 24, 2024
1 parent 860cf0e commit ce47eac
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
31 changes: 31 additions & 0 deletions yaml/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,34 @@ Deno.test({
});
},
});

Deno.test({
name: "parse() handles float types",
fn() {
const yaml = `
- 3.14
- -3.14
- .inf
- -.inf
- .nan
- 12e03
- 1:15
- 1:15:20
- -1:15:20
- !!float 12000
`;

assertEquals(parse(yaml), [
3.14,
-3.14,
Infinity,
-Infinity,
NaN,
12000,
75,
4520,
-4520,
12000,
]);
},
});
48 changes: 48 additions & 0 deletions yaml/stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,51 @@ Deno.test({
assertEquals(stringify(object, { schema: SPACE_SCHEMA }), expected);
},
});

Deno.test({
name: "stringify() handles float types",
fn() {
const floats = [
4.1,
-1.473,
6.82e-5,
6.82e-12,
5e-12,
0,
-0,
];
assertEquals(
stringify(floats),
`- 4.1
- -1.473
- 0.0000682
- 6.82e-12
- 5.e-12
- 0
- -0.0
`,
);
const infNaN = [Infinity, -Infinity, NaN];
assertEquals(
stringify(infNaN),
`- .inf
- -.inf
- .nan
`,
);
assertEquals(
stringify(infNaN, { styles: { "tag:yaml.org,2002:float": "uppercase" } }),
`- .INF
- -.INF
- .NAN
`,
);
assertEquals(
stringify(infNaN, { styles: { "tag:yaml.org,2002:float": "camelcase" } }),
`- .Inf
- -.Inf
- .NaN
`,
);
},
});

0 comments on commit ce47eac

Please sign in to comment.