From 9b7ac7d2e832311248f1a698507c44f6b0f5290a Mon Sep 17 00:00:00 2001 From: Guillaume Cluzel Date: Sun, 9 Jun 2024 11:30:49 +0200 Subject: [PATCH] Add tests for the new JSON5 error messages --- test_json5/test.ml | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/test_json5/test.ml b/test_json5/test.ml index 354c04e..e0821f2 100644 --- a/test_json5/test.ml +++ b/test_json5/test.ml @@ -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 "" 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 = [ @@ -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 =