-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathounit_json_tests.ml
70 lines (65 loc) · 2.13 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
let ((>::),
(>:::)) = OUnit.((>::),(>:::))
open Ext_json
let (|?) m (key, cb) =
m |> Ext_json.test key cb
exception Parse_error
let suites =
__FILE__
>:::
[
"empty_json" >:: begin fun _ ->
let v =parse_json_from_string "{}" in
match v with
| `Obj v -> OUnit.assert_equal (String_map.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 =
match v with
|`Obj 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 =
match v with
| `Arr { content = [|`Flo "1" ; `Flo "3" |] } -> ()
| _ -> OUnit.assert_failure "trailing comma array" in
test v ;
test v1
end
]