Skip to content

Commit

Permalink
Add more tests for JSON5
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilst committed Sep 25, 2022
1 parent 6e623ce commit 7bb5eb4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/json5/lexer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ let json5_int =
let unicode_escape_sequence =
[%sedlex.regexp? 'u', hex_digit, hex_digit, hex_digit, hex_digit]

let single_escape_character = [%sedlex.regexp? Chars {|'"\bfnrtv|}]
let single_escape_character = [%sedlex.regexp? Chars {|'"\\bfnrtv|}]
let escape_character =
[%sedlex.regexp? single_escape_character | decimal_digit | 'x' | 'u']
Expand Down Expand Up @@ -181,7 +181,8 @@ let string_lex_single lexbuf strbuf =
let* s = Unescape.unescape (lexeme lexbuf) in
Buffer.add_string strbuf s;
lex lexbuf strbuf
| Sub (source_character, '\'') ->
| line_continuation -> lex lexbuf strbuf
| Sub (source_character, ('\'' | line_terminator)) ->
Buffer.add_string strbuf (lexeme lexbuf);
lex lexbuf strbuf
| _ ->
Expand All @@ -203,7 +204,8 @@ let string_lex_double lexbuf strbuf =
let* s = Unescape.unescape (lexeme lexbuf) in
Buffer.add_string strbuf s;
lex lexbuf strbuf
| Sub (source_character, '"') ->
| line_continuation -> lex lexbuf strbuf
| Sub (source_character, ('"' | line_terminator)) ->
Buffer.add_string strbuf (lexeme lexbuf);
lex lexbuf strbuf
| _ ->
Expand Down
3 changes: 2 additions & 1 deletion lib/json5/unescape.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ let unescape str =
| None -> Error (Format.sprintf "bad escape sequence %s" escape_chars)
in
utf_8_string_of_unicode as_int
| '\\' | '"' | 'n' | 't' -> Ok str
| '"' | '\'' | 'b' | 'f' | 'n' | 'r' | 't' | 'v' -> Ok str
| '\\' -> Ok {|\|}
| '0' ->
if String.length str = 2 then Ok "\x00"
else if String.length str = 4 then
Expand Down
19 changes: 16 additions & 3 deletions test_json5/test.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
module M = struct
include Yojson_json5.Safe

let from_string_err s =
match from_string s with
| Ok x ->
failwith
(Format.sprintf "Test didn't failed when should: %s" (to_string x))
| Error e -> e

let from_string s =
match from_string s with
| Ok t -> t
Expand Down Expand Up @@ -37,6 +44,10 @@ let test_from_string () =
Alcotest.(check yojson_json5)
"more string escaping" (`String "Hello λ world")
(M.from_string "\"Hello \\u03bb \\x77\\x6F\\x72\\x6C\\x64\"");
Alcotest.(check string)
"string unescaped linebreak fails" "Unexpected character: "
(M.from_string_err {|"foo
bar"|});
Alcotest.(check yojson_json5)
"null byte string" (`String "\x00") (M.from_string {|"\0"|});
Alcotest.(check yojson_json5)
Expand All @@ -45,7 +56,10 @@ let test_from_string () =
"null and octal string" (`String "\x007") (M.from_string {|"\07"|});
Alcotest.(check yojson_json5) "int" (`Int 1) (M.from_string "1");
Alcotest.(check yojson_json5)
"line break" (`String "foo\\\nbar")
"backslash escape" (`String {|foo\bar|})
(M.from_string {|"foo\\bar"|});
Alcotest.(check yojson_json5)
"line break" (`String "foobar")
(M.from_string "\"foo\\\nbar\"");
Alcotest.(check yojson_json5)
"string and comment" (`String "bar")
Expand All @@ -55,8 +69,7 @@ let test_from_string () =
[
("unquoted", `String "and you can quote me on that");
("singleQuotes", `String "I can use \"double quotes\" here");
("lineBreaks", `String {|Look, Mom! \
No \\n's!|});
("lineBreaks", `String {|Look, Mom! No \n's!|});
("hexadecimal", `Int 0xdecaf);
("leadingDecimalPoint", `Float 0.8675309);
("andTrailing", `Float 8675309.0);
Expand Down

0 comments on commit 7bb5eb4

Please sign in to comment.