From 4b0715cdfac5314c09924e0104c2e33e8f7569bf Mon Sep 17 00:00:00 2001 From: babiabeo Date: Tue, 16 Jan 2024 18:10:50 +0700 Subject: [PATCH 1/3] test(toml): improve test coverage --- toml/_parser.ts | 2 +- toml/parse_test.ts | 64 ++++++++++++++++++++++++++++++++++++++- toml/test.ts | 21 +++++++++++-- toml/testdata/string.toml | 1 + 4 files changed, 84 insertions(+), 4 deletions(-) diff --git a/toml/_parser.ts b/toml/_parser.ts index 0107258f82f7..1981001bdd43 100644 --- a/toml/_parser.ts +++ b/toml/_parser.ts @@ -894,7 +894,7 @@ export function ParserFactory(parser: ParserComponent) { if (count > line.length) { count -= line.length + 1; } else { - return count; + break; } } return count; diff --git a/toml/parse_test.ts b/toml/parse_test.ts index 8fd88d1b1100..0c68de6e2a88 100644 --- a/toml/parse_test.ts +++ b/toml/parse_test.ts @@ -112,6 +112,14 @@ Violets are\\tblue"""`), TOMLParseError, "Invalid escape sequence: \\?", ); + assertThrows( + () => + parse(`""" +Roses are red +Violets are\\tblue`), + TOMLParseError, + "not closed", + ); }, }); @@ -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", + ); }, }); @@ -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_+-")); }, }); @@ -352,6 +369,7 @@ Deno.test({ ]`), [1, 2], ); + assertThrows(() => parse("[1, 2, 3"), TOMLParseError, "not closed"); }, }); @@ -426,6 +444,7 @@ Deno.test({ fn() { const source = { foo: {}, + bar: null, }; Utils.deepAssignWithTable( @@ -446,6 +465,7 @@ Deno.test({ }, ], }, + bar: null, }, ); Utils.deepAssignWithTable( @@ -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", + ); }, }); @@ -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]", + ); }, }); diff --git a/toml/test.ts b/toml/test.ts index d910444029bf..7c42cd63f91f 100644 --- a/toml/test.ts +++ b/toml/test.ts @@ -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: @@ -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: { @@ -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}]] @@ -762,3 +763,19 @@ Deno.test({ assertEquals(actual, expected); }, }); + +Deno.test({ + name: "stringify throws on invalid value", + fn() { + assertThrows( + () => stringify({ a: [[null]] }), + Error, + "should never reach", + ); + assertThrows( + () => stringify({ a: [[undefined]] }), + Error, + "should never reach", + ); + }, +}); diff --git a/toml/testdata/string.toml b/toml/testdata/string.toml index d5d84afe4822..dec147320b78 100644 --- a/toml/testdata/string.toml +++ b/toml/testdata/string.toml @@ -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';" From bb95ab5f5b76a1566c8fb66f138f620514bab325 Mon Sep 17 00:00:00 2001 From: babiabeo Date: Tue, 16 Jan 2024 18:51:39 +0700 Subject: [PATCH 2/3] fmt --- toml/parse_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toml/parse_test.ts b/toml/parse_test.ts index 0c68de6e2a88..2e69caa80f2f 100644 --- a/toml/parse_test.ts +++ b/toml/parse_test.ts @@ -543,7 +543,7 @@ Deno.test({ ); assertThrows( () => - ParserFactory((s) => { + ParserFactory((_s) => { throw "Custom parser"; })(""), TOMLParseError, From e2f670ace1e7fcfc88cc26d291442dd4b5d1b82f Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 16 Jan 2024 22:31:58 +0900 Subject: [PATCH 3/3] Update toml/test.ts --- toml/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toml/test.ts b/toml/test.ts index 7c42cd63f91f..2011910280c2 100644 --- a/toml/test.ts +++ b/toml/test.ts @@ -765,7 +765,7 @@ Deno.test({ }); Deno.test({ - name: "stringify throws on invalid value", + name: "stringify() throws on invalid value", fn() { assertThrows( () => stringify({ a: [[null]] }),