forked from mirage/ezjsonm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ml
95 lines (81 loc) · 2.39 KB
/
test.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
open Alcotest
let json_t: Ezjsonm.t Alcotest.testable =
(module struct
type t = Ezjsonm.t
let equal = (=)
let pp ppf x = Format.pp_print_string ppf (Ezjsonm.to_string x)
end)
let json_v: Ezjsonm.value Alcotest.testable =
(module struct
type t = Ezjsonm.value
let equal = (=)
let pp ppf x = Format.pp_print_string ppf Ezjsonm.(to_string @@ wrap x)
end)
let random_int i =
if i <= 1 then 0
else Random.int i
let random_string len =
let s = Bytes.create (random_int len) in
for i = 0 to Bytes.length s - 1 do
Bytes.set s i @@ Char.chr (random_int 127)
done;
s
let random_list len gen =
Array.to_list (Array.init len gen)
let random_bool () =
Random.int 2 = 0
type 'a x = {
to_json: 'a -> Ezjsonm.value;
of_json: Ezjsonm.value -> 'a;
test : 'a Alcotest.testable;
}
let test x t =
let j = x.to_json t in
let t' = x.of_json j in
Alcotest.(check x.test) "idempotent JSON conversion" t t';
let str = Ezjsonm.(to_string (wrap j)) in
let j' = Ezjsonm.(unwrap (from_string str)) in
let t' = x.of_json j' in
Alcotest.(check x.test) "idempotent string conversion" t t'
let string =
{ to_json = Ezjsonm.string;
of_json = Ezjsonm.get_string;
test = Alcotest.string; },
[""; "foo"; random_string 1024]
let list =
{ to_json = Ezjsonm.(list string);
of_json = Ezjsonm.(get_list get_string);
test = Alcotest.(list string); },
[
random_list 30 random_string;
random_list 10 (fun _ -> "foo")
]
let test_stream jsons () =
let jsons = List.map Ezjsonm.wrap jsons in
let jsons, last = match jsons with
| [] -> assert false
| h::t -> t, h
in
let string j = Ezjsonm.to_string j in
let strings = List.map (fun j -> string j ^ ",") jsons in
let strings = "[" :: strings @ [string last; "]"] in
let jsons = jsons @ [last] in
Lwt_main.run begin
let open Lwt.Infix in
let stream = Lwt_stream.of_list strings in
let stream = Ezjsonm_lwt.from_stream stream in
Lwt_stream.to_list stream >|= fun json' ->
Alcotest.(check @@ list json_v) "stream" jsons json'
end
let tests t ts () = List.iter (test t) ts
let stream0 =
random_list 42 (fun i -> Ezjsonm.(string @@ random_string i))
let () =
let suite k (t, ts) = k, ["test", `Quick, tests t ts] in
Alcotest.run "ezjsonm" [
suite "string" string;
suite "list" list;
"stream", [
"stream0", `Quick, test_stream stream0;
]
]