Skip to content

Commit

Permalink
Add tests for the new JSON5 error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gcluzel committed Jun 9, 2024
1 parent d324654 commit 9b7ac7d
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions test_json5/test.ml
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
module M = Yojson_five.Safe

let yojson = Alcotest.testable M.pp M.equal
let equal_string = Alcotest.testable Fmt.string String.equal
let ignore_string_content = Alcotest.testable Fmt.string (fun _ _ -> true)

let parsing_test_case name expected input =
let parsing_test_case name expected input testable_str =
Alcotest.test_case name `Quick (fun () ->
(* any error message will do *)
let any_string = Alcotest.testable Fmt.string (fun _ _ -> true) in
Alcotest.(check (result yojson any_string))
Alcotest.(check (result yojson testable_str))
name expected (M.from_string input))

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

let parsing_should_fail name input =
let failure = Error "<anything>" in
parsing_test_case name failure input
parsing_test_case name failure input ignore_string_content

let parsing_should_fail_with_error name input expected =
parsing_test_case name (Error expected) input equal_string

let parsing_tests =
[
Expand Down Expand Up @@ -101,6 +105,14 @@ No \\n's!",
"backwardsCompatible": "with JSON",
}|}
expected);
parsing_should_fail_with_error "unexpected eof in list" "[1, 2,"
"Line 1: Unexpected end of input";
parsing_should_fail_with_error "unexpected eof in assoc" {|{"foo": 1,|}
"Line 1: Unexpected end of input";
parsing_should_fail_with_error "missing colon in assoc" {|{"foo"}|}
"Line 1: Expected ':' but found '}'";
parsing_should_fail_with_error "bad identifier in assoc" {|{[0]}|}
"Line 1: Expected string or identifier but found '['";
]

let writing_test_case name input expected =
Expand Down

0 comments on commit 9b7ac7d

Please sign in to comment.