Skip to content

Commit

Permalink
Merge pull request #172 from ElectreAAS/parsing-message-fail-test
Browse files Browse the repository at this point in the history
Added failing test on parsing failure
  • Loading branch information
Leonidas-from-XIV authored Nov 23, 2023
2 parents 24fc783 + 746f22c commit ed5c781
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

### Fixed

- Fix the error location reported in the exception. Regression in 2.1.1
(reported by @johnridesabike, regression test by @ElectreAAS, fix by
@Leonidas-from-XIV, #171, #172)

### Removed

### Security
Expand Down
7 changes: 7 additions & 0 deletions lib/lexer_utils.mll
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ rule read_junk buf n = parse
read_junk buf (n - 1) lexbuf
end
}

{
let read_junk_without_positions buf n (lexbuf : Lexing.lexbuf) =
let junk_start_pos = lexbuf.lex_start_pos in
read_junk buf n lexbuf;
lexbuf.lex_start_pos <- junk_start_pos + 1
}
5 changes: 3 additions & 2 deletions lib/read.mll
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@

let long_error descr v lexbuf =
let junk = Lexing.lexeme lexbuf in
let buf = Buffer.create 32 in
let () = Lexer_utils.read_junk buf 32 lexbuf in
let buf_size = 32 in
let buf = Buffer.create buf_size in
let () = Lexer_utils.read_junk_without_positions buf buf_size lexbuf in
let extra_junk = Buffer.contents buf in
custom_error
(sprintf "%s '%s%s'" descr junk extra_junk)
Expand Down
6 changes: 6 additions & 0 deletions test/test_read.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ let from_string () =
__LOC__ Fixtures.json_value
(Yojson.Safe.from_string Fixtures.json_string)

let from_string_fail () =
Alcotest.check_raises "Location of parsing failure is correct"
(Yojson.Json_error "Line 1, bytes 0-5:\nInvalid token 'hello'") (fun () ->
Yojson.Safe.from_string "hello" |> ignore)

let from_file () =
let input_file = Filename.temp_file "test_yojson_from_file" ".json" in
let oc = open_out input_file in
Expand Down Expand Up @@ -51,6 +56,7 @@ let map_ident_and_string () =
let single_json =
[
("from_string", `Quick, from_string);
("from_string_fail", `Quick, from_string_fail);
("from_file", `Quick, from_file);
("unquoted_from_string", `Quick, unquoted_from_string);
("map_ident/map_string", `Quick, map_ident_and_string);
Expand Down

0 comments on commit ed5c781

Please sign in to comment.