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(toml): improve test coverage #4211

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion toml/_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ export function ParserFactory<T>(parser: ParserComponent<T>) {
if (count > line.length) {
count -= line.length + 1;
} else {
return count;
break;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I use break here because subStr.split("\n") always returns a non-empty array. Therefore, the for loop is always called, which makes the line 900 unreachable.

}
}
return count;
Expand Down
64 changes: 63 additions & 1 deletion toml/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ Violets are\\tblue"""`),
TOMLParseError,
"Invalid escape sequence: \\?",
);
assertThrows(
() =>
parse(`"""
Roses are red
Violets are\\tblue`),
TOMLParseError,
"not closed",
);
},
});

Expand Down Expand Up @@ -146,6 +154,14 @@ Roses are red
Violets are\\tblue'''`),
"Roses are red\nViolets are\\tblue",
);
assertThrows(
() =>
parse(`'''
Roses are red
Violets are\\tblue`),
TOMLParseError,
"not closed",
);
},
});

Expand Down Expand Up @@ -252,6 +268,7 @@ Deno.test({
assertEquals(parse("224_617.445_991_228"), 224_617.445_991_228);
assertThrows(() => parse(""));
assertThrows(() => parse("X"));
assertThrows(() => parse("e_+-"));
},
});

Expand Down Expand Up @@ -352,6 +369,7 @@ Deno.test({
]`),
[1, 2],
);
assertThrows(() => parse("[1, 2, 3"), TOMLParseError, "not closed");
},
});

Expand Down Expand Up @@ -426,6 +444,7 @@ Deno.test({
fn() {
const source = {
foo: {},
bar: null,
};

Utils.deepAssignWithTable(
Expand All @@ -446,6 +465,7 @@ Deno.test({
},
],
},
bar: null,
},
);
Utils.deepAssignWithTable(
Expand All @@ -469,8 +489,37 @@ Deno.test({
},
],
},
bar: null,
},
);

assertThrows(
() =>
Utils.deepAssignWithTable(
source,
{
type: "TableArray",
key: [],
value: { email: "sub@example.com" },
},
),
Error,
"Unexpected key length",
);

assertThrows(
() =>
Utils.deepAssignWithTable(
source,
{
type: "TableArray",
key: ["bar", "items"],
value: { email: "mail@example.com" },
},
),
Error,
"Unexpected assign",
);
},
});

Expand All @@ -480,12 +529,25 @@ Deno.test({
assertThrows(
() => parse("foo = 1\nbar ="),
TOMLParseError,
"on line 2, column 5",
"line 2, column 5",
);
assertThrows(
() => parse("foo = 1\nbar = 'foo\nbaz=1"),
TOMLParseError,
"line 2, column 10",
);
assertThrows(
() => parse(""),
TOMLParseError,
"line 1, column 0",
);
assertThrows(
() =>
ParserFactory((_s) => {
throw "Custom parser";
})(""),
TOMLParseError,
"[non-error thrown]",
);
},
});
21 changes: 19 additions & 2 deletions toml/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Deno.test({
literal3: "\\n\\t is 'literal'\\\n",
literal4: 'Here are fifteen quotation marks: """""""""""""""',
literal5: "Here are fifteen apostrophes: '''''''''''''''",
literal6: "'That,' she said, 'is still pointless.'",
withApostrophe: "What if it's not?",
withSemicolon: `const message = 'hello world';`,
withHexNumberLiteral:
Expand Down Expand Up @@ -522,7 +523,7 @@ Deno.test({
emptyArray: [],
mixedArray1: [1, { b: 2 }],
mixedArray2: [{ b: 2 }, 1],
nestedArray1: [[{ b: 1 }]],
nestedArray1: [[{ b: 1, date: new Date("2022-05-13") }]],
nestedArray2: [[[{ b: 1 }]]],
nestedArray3: [[], [{ b: 1 }]],
deepNested: {
Expand All @@ -534,7 +535,7 @@ Deno.test({
const expected = `emptyArray = []
mixedArray1 = [1,{b = 2}]
mixedArray2 = [{b = 2},1]
nestedArray1 = [[{b = 1}]]
nestedArray1 = [[{b = 1,date = "2022-05-13T00:00:00.000"}]]
nestedArray2 = [[[{b = 1}]]]
nestedArray3 = [[],[{b = 1}]]

Expand Down Expand Up @@ -762,3 +763,19 @@ Deno.test({
assertEquals(actual, expected);
},
});

Deno.test({
name: "stringify throws on invalid value",
kt3k marked this conversation as resolved.
Show resolved Hide resolved
fn() {
assertThrows(
() => stringify({ a: [[null]] }),
Error,
"should never reach",
);
assertThrows(
() => stringify({ a: [[undefined]] }),
Error,
"should never reach",
);
},
});
1 change: 1 addition & 0 deletions toml/testdata/string.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ literal3 = '''
'''
literal4 = '''Here are fifteen quotation marks: """""""""""""""'''
literal5 = "Here are fifteen apostrophes: '''''''''''''''"
literal6 = ''''That,' she said, 'is still pointless.''''

withApostrophe = "What if it's not?"
withSemicolon = "const message = 'hello world';"
Expand Down