Skip to content

Commit

Permalink
More tests and better argument order
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonidas-from-XIV committed May 30, 2024
1 parent dbe0b81 commit 5a81430
Showing 1 changed file with 64 additions and 56 deletions.
120 changes: 64 additions & 56 deletions test_json5/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let parsing_test_case name expected input =
Alcotest.(check (result yojson any_string))
name expected (M.from_string input))

let parsing_should_succeed name expected input =
let parsing_should_succeed name input expected =
parsing_test_case name (Ok expected) input

let parsing_should_fail name input =
Expand All @@ -20,51 +20,58 @@ let parsing_tests =
[
parsing_should_fail "Unexpected line break" {|"foo
bar"|};
parsing_should_succeed "Empty object" (`Assoc []) "{}";
parsing_should_succeed "Empty list" (`List []) "[]";
parsing_should_succeed "List"
(`List [ `Int 1; `String "2"; `Float 3. ])
{|[1, "2", 3.0]|};
parsing_should_succeed "true" (`Bool true) "true";
parsing_should_succeed "false" (`Bool false) "false";
parsing_should_succeed "null" `Null "null";
parsing_should_succeed "double quotes string" (`String "hello world")
{|"hello world"|};
parsing_should_succeed "single quotes string" (`String "hello world")
{|'hello world'|};
parsing_should_succeed "float" (`Float 12345.67890) "12345.67890";
parsing_should_succeed "hex" (`Int 0x1) "0x1";
parsing_should_succeed "hex escape sequence" (`String "a") {|"\x61"|};
parsing_should_succeed "unicode escape sequence" (`String "λ") {|"\u03bb"|};
parsing_should_succeed "more string escaping" (`String "Hello λ world")
"\"Hello \\u03bb \\x77\\x6F\\x72\\x6C\\x64\"";
parsing_should_succeed "null byte string" (`String "\x00") {|"\0"|};
parsing_should_succeed "octal string" (`String "?") {|"\077"|};
parsing_should_succeed "null and octal string" (`String "\x007") {|"\07"|};
parsing_should_succeed "int" (`Int 1) "1";
parsing_should_succeed "backslash escape" (`String {|foo\bar|})
{|"foo\\bar"|};
parsing_should_succeed "line break" (`String "foobar") "\"foo\\\nbar\"";
parsing_should_succeed "string and comment" (`String "bar") "\"bar\" //foo";
parsing_should_succeed "object with double quote string"
(`Assoc [ ("foo", `String "bar") ])
{|{"foo": "bar"}|};
parsing_should_succeed "object with single quote string"
(`Assoc [ ("foo", `String "bar") ])
{|{'foo': 'bar'}|};
parsing_should_succeed "object with unquoted string"
(`Assoc [ ("foo", `String "bar") ])
{|{foo: 'bar'}|};
parsing_should_succeed "trailing comma in list"
(`List [ `Int 1; `Int 2; `Int 3 ])
"[1, 2, 3,]";
parsing_should_fail "multiple trailing commas in list" "[1, 2, 3,,]";
parsing_should_fail "just trailing commas in list" "[,,,]";
parsing_should_succeed "trailing comma in object"
(`Assoc [ ("one", `Int 1) ])
{|{"one": 1,}|};
parsing_should_succeed "true" "true" (`Bool true);
parsing_should_succeed "false" "false" (`Bool false);
parsing_should_succeed "null" "null" `Null;
parsing_should_succeed "double quotes string" {|"hello world"|}
(`String "hello world");
parsing_should_succeed "single quotes string" {|'hello world'|}
(`String "hello world");
parsing_should_succeed "float" "12345.67890" (`Float 12345.67890);
parsing_should_succeed "hex" "0x1" (`Int 0x1);
parsing_should_succeed "hex escape sequence" {|"\x61"|} (`String "a");
parsing_should_succeed "unicode escape sequence" {|"\u03bb"|} (`String "λ");
parsing_should_succeed "more string escaping"
"\"Hello \\u03bb \\x77\\x6F\\x72\\x6C\\x64\"" (`String "Hello λ world");
parsing_should_succeed "null byte string" {|"\0"|} (`String "\x00");
parsing_should_succeed "octal string" {|"\077"|} (`String "?");
parsing_should_succeed "null and octal string" {|"\07"|} (`String "\x007");
parsing_should_succeed "int" "1" (`Int 1);
parsing_should_succeed "backslash escape" {|"foo\\bar"|}
(`String {|foo\bar|});
parsing_should_succeed "line break" "\"foo\\\nbar\"" (`String "foobar");
parsing_should_succeed "string and comment" "\"bar\" //foo" (`String "bar");
(* objects *)
parsing_should_succeed "empty object" "{}" (`Assoc []);
parsing_should_succeed "object with double quote string" {|{"foo": "bar"}|}
(`Assoc [ ("foo", `String "bar") ]);
parsing_should_succeed "object with single quote string" {|{'foo': 'bar'}|}
(`Assoc [ ("foo", `String "bar") ]);
parsing_should_succeed "object with unquoted string" {|{foo: 'bar'}|}
(`Assoc [ ("foo", `String "bar") ]);
parsing_should_succeed "trailing comma in object" {|{"one": 1,}|}
(`Assoc [ ("one", `Int 1) ]);
parsing_should_succeed "colon in key" {|{"colon:": 1}|}
(`Assoc [ ("colon:", `Int 1) ]);
parsing_should_fail "multiple trailing commas in object" {|{"one": 1,,}|};
parsing_should_fail "just trailing comma in object" "{,}";
parsing_should_fail "just trailing commas in object" "{,,,}";
parsing_should_fail "multiple colons in object" {|{one :: 1}|};
parsing_should_fail "newline in key" {|{new\nline: 1}|};
(* lists *)
parsing_should_succeed "empty list" "[]" (`List []);
parsing_should_succeed "heterogenous list" {|[1, "2", 3.0]|}
(`List [ `Int 1; `String "2"; `Float 3. ]);
parsing_should_succeed "trailing comma in list" "[1, 2, 3,]"
(`List [ `Int 1; `Int 2; `Int 3 ]);
parsing_should_succeed "trailing comma with space list" "[1, 2, 3, ]"
(`List [ `Int 1; `Int 2; `Int 3 ]);
parsing_should_succeed "newlines in list" "[1, 2\n, 3]"
(`List [ `Int 1; `Int 2; `Int 3 ]);
parsing_should_fail "multiple trailing commas in list" "[1, 2, 3,,]";
parsing_should_fail "just trailing comma in list" "[,]";
parsing_should_fail "multiple trailing commas in list" "[,,,]";
(* all together *)
(let expected =
`Assoc
[
Expand All @@ -80,7 +87,7 @@ let parsing_tests =
("backwardsCompatible", `String "with JSON");
]
in
parsing_should_succeed "More elaborated" expected
parsing_should_succeed "More elaborated"
{|{
// comments
unquoted: 'and you can quote me on that',
Expand All @@ -92,24 +99,25 @@ No \\n's!",
positiveSign: +1,
trailingComma: 'in objects', andIn: ['arrays',],
"backwardsCompatible": "with JSON",
}|});
}|}
expected);
]

let writing_test_case name expected input =
let writing_test_case name input expected =
Alcotest.test_case name `Quick (fun () ->
Alcotest.(check string) name expected (M.to_string input))

let writing_tests =
[
writing_test_case "Empty object" "{}" (`Assoc []);
writing_test_case "Empty list" "[]" (`List []);
writing_test_case "true" "true" (`Bool true);
writing_test_case "false" "false" (`Bool false);
writing_test_case "null" "null" `Null;
writing_test_case "string" "\"hello world\"" (`String "hello world");
writing_test_case "float" "12345.6789" (`Float 12345.6789);
writing_test_case "hex" "1" (`Int 0x1);
writing_test_case "int" "1" (`Int 1);
writing_test_case "Empty object" (`Assoc []) "{}";
writing_test_case "Empty list" (`List []) "[]";
writing_test_case "true" (`Bool true) "true";
writing_test_case "false" (`Bool false) "false";
writing_test_case "null" `Null "null";
writing_test_case "string" (`String "hello world") "\"hello world\"";
writing_test_case "float" (`Float 12345.6789) "12345.6789";
writing_test_case "hex" (`Int 0x1) "1";
writing_test_case "int" (`Int 1) "1";
]

let () =
Expand Down

0 comments on commit 5a81430

Please sign in to comment.