Skip to content

Commit ea8c39a

Browse files
committed
test(toml): Test with the official toml-test suite
1 parent 6b92bff commit ea8c39a

File tree

1,076 files changed

+7345
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,076 files changed

+7345
-0
lines changed

.github/typos.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extend-exclude = [
1717
"http/testdata",
1818
"http/user_agent.ts",
1919
"jsonc/testdata",
20+
"toml/testdata",
2021
"ulid/*.ts",
2122
"crypto/_wasm"
2223
]

deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"exclude": [
3737
".git",
3838
"jsonc/testdata",
39+
"toml/testdata",
3940
"_tools/node_test_runner"
4041
],
4142
"lint": {

toml/_test_util.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2018-2025 the Deno authors. MIT license.
2+
3+
type TestCaseValue = {
4+
type:
5+
| "string"
6+
| "integer"
7+
| "float"
8+
| "bool"
9+
| "datetime"
10+
| "datetime-local"
11+
| "date-local"
12+
| "time-local";
13+
value: string;
14+
};
15+
export type TestCase =
16+
| TestCaseValue
17+
| TestCase[]
18+
| { [key: string]: TestCase };
19+
20+
function isTestCaseValue(v: unknown): v is TestCaseValue {
21+
return typeof v === "object" && v !== null &&
22+
"type" in v && typeof v.type === "string" &&
23+
"value" in v && typeof v.value === "string";
24+
}
25+
26+
export function convertTestCase(v: TestCase): unknown {
27+
if (isTestCaseValue(v)) {
28+
switch (v.type) {
29+
case "string":
30+
return v.value;
31+
case "integer":
32+
return parseInt(v.value, 10);
33+
case "float":
34+
return parseFloat(v.value.replace("inf", "Infinity"));
35+
case "bool":
36+
return v.value === "true";
37+
// TODO: https://github.com/denoland/std/issues/6591
38+
case "datetime":
39+
case "datetime-local":
40+
case "date-local":
41+
return new Date(v.value);
42+
case "time-local":
43+
return v.value;
44+
}
45+
} else if (Array.isArray(v)) {
46+
return v.map(convertTestCase);
47+
} else {
48+
const obj: Record<string, unknown> = {};
49+
for (const [key, value] of Object.entries(v)) {
50+
obj[key] = convertTestCase(value);
51+
}
52+
return obj;
53+
}
54+
}

toml/official_parse_test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2018-2025 the Deno authors. MIT license.
2+
3+
import { assertEquals, assertThrows } from "../assert/mod.ts";
4+
import { convertTestCase, type TestCase } from "./_test_util.ts";
5+
import { parse } from "./parse.ts";
6+
7+
const testCases = await Deno.readTextFile(
8+
new URL("./testdata/files-toml-1.0.0", import.meta.url),
9+
);
10+
11+
const ignored = [
12+
"valid/integer/long",
13+
];
14+
15+
for (
16+
const testCase of testCases.split("\n")
17+
.filter((line) => line.endsWith(".toml"))
18+
.map((line) => line.slice(0, -5))
19+
) {
20+
const input = await Deno.readTextFile(
21+
new URL(`./testdata/${testCase}.toml`, import.meta.url),
22+
);
23+
24+
if (testCase.startsWith("invalid/")) {
25+
Deno.test({
26+
name: `[toml] parse ${testCase}`,
27+
ignore: ignored.some((v) => testCase.startsWith(v)),
28+
fn() {
29+
assertThrows(() => parse(input), `TOML input:\n${input}`);
30+
},
31+
});
32+
} else {
33+
const json: Record<string, TestCase> = JSON.parse(
34+
await Deno.readTextFile(
35+
new URL(`./testdata/${testCase}.json`, import.meta.url),
36+
),
37+
);
38+
const expected: Record<string, unknown> = {};
39+
for (const [key, value] of Object.entries(json)) {
40+
expected[key] = convertTestCase(value);
41+
}
42+
43+
Deno.test({
44+
name: `[toml] parse ${testCase}`,
45+
ignore: ignored.some((v) => testCase.startsWith(v)),
46+
fn() {
47+
const parsed = parse(input);
48+
assertEquals(parsed, expected);
49+
},
50+
});
51+
}
52+
}

toml/testdata/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.toml -text

toml/testdata/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
This directory contains the test cases copied from the official
2+
[toml-lang/toml-test](https://github.com/toml-lang/toml-test) repository at
3+
[`1d35870ef6783d86366ba55d7df703f3f60b3b55`](https://github.com/toml-lang/toml-test/tree/1d35870ef6783d86366ba55d7df703f3f60b3b55).
4+
5+
The license of the copied data is as follows.
6+
7+
https://github.com/toml-lang/toml-test/blob/1d35870ef6783d86366ba55d7df703f3f60b3b55/LICENSE
8+
9+
```
10+
The MIT License (MIT)
11+
12+
Copyright (c) 2018 TOML authors
13+
14+
Permission is hereby granted, free of charge, to any person obtaining a copy
15+
of this software and associated documentation files (the "Software"), to deal
16+
in the Software without restriction, including without limitation the rights
17+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18+
copies of the Software, and to permit persons to whom the Software is
19+
furnished to do so, subject to the following conditions:
20+
21+
The above copyright notice and this permission notice shall be included in
22+
all copies or substantial portions of the Software.
23+
24+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30+
THE SOFTWARE.
31+
```

0 commit comments

Comments
 (0)