-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
suf
separator to configure what terminates JSON values
This is a generalization of `newline`, but it works for all writing functions and can be overridden.
- Loading branch information
1 parent
700a222
commit 0330104
Showing
4 changed files
with
88 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,61 @@ | ||
let to_string () = | ||
Alcotest.(check string) __LOC__ Fixtures.json_string (Yojson.Safe.to_string Fixtures.json_value) | ||
let to_string_tests = | ||
let test ?suf expected = | ||
Alcotest.(check string) __LOC__ expected (Yojson.Safe.to_string ?suf Fixtures.json_value) | ||
in | ||
[ | ||
"to_string with default settings", `Quick, (fun () -> test Fixtures.json_string); | ||
"to_string with newline", `Quick, (fun () -> test ~suf:"\n" Fixtures.json_string_newline); | ||
"to_string without newline", `Quick, (fun () -> test ~suf:"" Fixtures.json_string); | ||
] | ||
|
||
let to_file () = | ||
let test ?newline () = | ||
let to_file_tests = | ||
let test ?suf expected = | ||
let output_file = Filename.temp_file "test_yojson_to_file" ".json" in | ||
let correction = match newline with | ||
| None -> | ||
Yojson.Safe.to_file output_file Fixtures.json_value; | ||
Fixtures.json_string_newline | ||
| Some newline -> | ||
Yojson.Safe.to_file ~newline output_file Fixtures.json_value; | ||
if newline then | ||
Fixtures.json_string_newline | ||
else | ||
Fixtures.json_string | ||
in | ||
Yojson.Safe.to_file ?suf output_file Fixtures.json_value; | ||
let file_content = | ||
let ic = open_in output_file in | ||
let length = in_channel_length ic in | ||
let s = really_input_string ic length in | ||
close_in ic; | ||
s | ||
in | ||
Alcotest.(check string) __LOC__ correction file_content; | ||
Sys.remove output_file | ||
Sys.remove output_file; | ||
Alcotest.(check string) __LOC__ expected file_content | ||
in | ||
test (); | ||
test ~newline:true (); | ||
test ~newline:false () | ||
[ | ||
"to_file with default settings", `Quick, (fun () -> test Fixtures.json_string_newline); | ||
"to_file with newline", `Quick, (fun () -> test ~suf:"\n" Fixtures.json_string_newline); | ||
"to_file without newline", `Quick, (fun () -> test ~suf:"" Fixtures.json_string); | ||
] | ||
|
||
(* List.to_seq is not available on old OCaml versions. *) | ||
let rec list_to_seq = function | ||
| [] -> (fun () -> Seq.Nil) | ||
| x :: xs -> (fun () -> Seq.Cons (x, list_to_seq xs)) | ||
|
||
let seq_to_file () = | ||
let output_file = Filename.temp_file "test_yojson_seq_to_file" ".json" in | ||
let data = [`String "foo"; `String "bar"] in | ||
Yojson.Safe.seq_to_file output_file (list_to_seq data); | ||
let read_data = | ||
let seq = Yojson.Safe.seq_from_file output_file in | ||
let acc = ref [] in | ||
Seq.iter (fun v -> acc := v :: !acc) seq; | ||
List.rev !acc | ||
let seq_to_file_tests = | ||
let test ?suf () = | ||
let output_file = Filename.temp_file "test_yojson_seq_to_file" ".json" in | ||
let data = [`String "foo"; `String "bar"] in | ||
Yojson.Safe.seq_to_file ?suf output_file (list_to_seq data); | ||
let read_data = | ||
let seq = Yojson.Safe.seq_from_file output_file in | ||
let acc = ref [] in | ||
Seq.iter (fun v -> acc := v :: !acc) seq; | ||
List.rev !acc | ||
in | ||
Sys.remove output_file; | ||
Alcotest.(check (list Testable.yojson)) "seq_{to,from}_file roundtrip" data read_data | ||
in | ||
Sys.remove output_file; | ||
if data <> read_data then | ||
(* TODO: it would be nice to use Alcotest.check, | ||
but we don't have a 'testable' instance for JSON values. *) | ||
Alcotest.fail "seq_{to,from}_file roundtrip failure" | ||
[ | ||
"seq_to_file with default settings", `Quick, (fun () -> test ()); | ||
"seq_to_file with newline", `Quick, (fun () -> test ~suf:"\n" ()); | ||
"seq_to_file without newline", `Quick, (fun () -> test ~suf:"" ()); | ||
] | ||
|
||
let single_json = [ | ||
"to_string", `Quick, to_string; | ||
"to_file", `Quick, to_file; | ||
"seq_to_file", `Quick, seq_to_file; | ||
] | ||
let single_json = | ||
List.flatten [ | ||
to_file_tests; | ||
to_string_tests; | ||
seq_to_file_tests; | ||
] |