-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathounit_json_tests.ml
185 lines (172 loc) · 5.06 KB
/
ounit_json_tests.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
let ((>::),
(>:::)) = OUnit.((>::),(>:::))
type t = Ext_json_noloc.t
let rec equal
(x : t)
(y : t) =
match x with
| Null -> (* [%p? Null _ ] *)
begin match y with
| Null -> true
| _ -> false end
| Str str ->
begin match y with
| Str str2 -> str = str2
| _ -> false end
| Flo flo
->
begin match y with
| Flo flo2 ->
flo = flo2
| _ -> false
end
| True ->
begin match y with
| True -> true
| _ -> false
end
| False ->
begin match y with
| False -> true
| _ -> false
end
| Arr content
->
begin match y with
| Arr content2
->
Ext_array.for_all2_no_exn content content2 equal
| _ -> false
end
| Obj map ->
begin match y with
| Obj map2 ->
let xs = Map_string.bindings map
|> List.sort (fun (a,_) (b,_) -> compare a b) in
let ys = Map_string.bindings map2
|> List.sort (fun (a,_) (b,_) -> compare a b) in
Ext_list.for_all2_no_exn xs ys (fun (k0,v0) (k1,v1) -> k0=k1 && equal v0 v1)
| _ -> false
end
open Ext_json_parse
let (|?) m (key, cb) =
m |> Ext_json.test key cb
let rec strip (x : Ext_json_types.t) : Ext_json_noloc.t =
let open Ext_json_noloc in
match x with
| True _ -> true_
| False _ -> false_
| Null _ -> null
| Flo {flo = s} -> flo s
| Str {str = s} -> str s
| Arr {content } -> arr (Array.map strip content)
| Obj {map} ->
obj (Map_string.map map strip)
let id_parsing_serializing x =
let normal_s =
Ext_json_noloc.to_string
@@ strip
@@ Ext_json_parse.parse_json_from_string x
in
let normal_ss =
Ext_json_noloc.to_string
@@ strip
@@ Ext_json_parse.parse_json_from_string normal_s
in
if normal_s <> normal_ss then
begin
prerr_endline "ERROR";
prerr_endline normal_s ;
prerr_endline normal_ss ;
end;
OUnit.assert_equal ~cmp:(fun (x:string) y -> x = y) normal_s normal_ss
let id_parsing_x2 x =
let stru = Ext_json_parse.parse_json_from_string x |> strip in
let normal_s = Ext_json_noloc.to_string stru in
let normal_ss = strip (Ext_json_parse.parse_json_from_string normal_s) in
if equal stru normal_ss then
true
else begin
prerr_endline "ERROR";
prerr_endline normal_s;
Format.fprintf Format.err_formatter
"%a@.%a@." Ext_obj.pp_any stru Ext_obj.pp_any normal_ss;
prerr_endline (Ext_json_noloc.to_string normal_ss);
false
end
let test_data =
[{|
{}
|};
{| [] |};
{| [1,2,3]|};
{| ["x", "y", 1,2,3 ]|};
{| { "x" : 3, "y" : "x", "z" : [1,2,3, "x"] }|};
{| {"x " : true , "y" : false , "z\"" : 1} |}
]
exception Parse_error
let suites =
__FILE__
>:::
[
__LOC__ >:: begin fun _ ->
List.iter id_parsing_serializing test_data
end;
__LOC__ >:: begin fun _ ->
List.iteri (fun i x -> OUnit.assert_bool (__LOC__ ^ string_of_int i ) (id_parsing_x2 x)) test_data
end;
"empty_json" >:: begin fun _ ->
let v =parse_json_from_string "{}" in
match v with
| Obj {map = v} -> OUnit.assert_equal (Map_string.is_empty v ) true
| _ -> OUnit.assert_failure "should be empty"
end
;
"empty_arr" >:: begin fun _ ->
let v =parse_json_from_string "[]" in
match v with
| Arr {content = [||]} -> ()
| _ -> OUnit.assert_failure "should be empty"
end
;
"empty trails" >:: begin fun _ ->
(OUnit.assert_raises Parse_error @@ fun _ ->
try parse_json_from_string {| [,]|} with _ -> raise Parse_error);
OUnit.assert_raises Parse_error @@ fun _ ->
try parse_json_from_string {| {,}|} with _ -> raise Parse_error
end;
"two trails" >:: begin fun _ ->
(OUnit.assert_raises Parse_error @@ fun _ ->
try parse_json_from_string {| [1,2,,]|} with _ -> raise Parse_error);
(OUnit.assert_raises Parse_error @@ fun _ ->
try parse_json_from_string {| { "x": 3, ,}|} with _ -> raise Parse_error)
end;
"two trails fail" >:: begin fun _ ->
(OUnit.assert_raises Parse_error @@ fun _ ->
try parse_json_from_string {| { "x": 3, 2 ,}|} with _ -> raise Parse_error)
end;
"trail comma obj" >:: begin fun _ ->
let v = parse_json_from_string {| { "x" : 3 , }|} in
let v1 = parse_json_from_string {| { "x" : 3 , }|} in
let test (v : Ext_json_types.t) =
match v with
| Obj {map = v} ->
v
|? ("x" , `Flo (fun x -> OUnit.assert_equal x "3"))
|> ignore
| _ -> OUnit.assert_failure "trail comma" in
test v ;
test v1
end
;
"trail comma arr" >:: begin fun _ ->
let v = parse_json_from_string {| [ 1, 3, ]|} in
let v1 = parse_json_from_string {| [ 1, 3 ]|} in
let test (v : Ext_json_types.t) =
match v with
| Arr { content = [| Flo {flo = "1"} ; Flo { flo = "3"} |] } -> ()
| _ -> OUnit.assert_failure "trailing comma array" in
test v ;
test v1
end
]