1
- let suites : Mt.pair_suites ref = ref []
1
+ let suites : Mt.pair_suites ref = ref []
2
+
2
3
let test_id = ref 0
4
+
3
5
let eq loc x y =
4
- incr test_id ;
6
+ incr test_id;
5
7
suites :=
6
- (loc ^ " id " ^ (string_of_int ! test_id), (fun _ -> Mt. Eq (x,y))) :: ! suites
7
-
8
-
8
+ (loc ^ " id " ^ string_of_int ! test_id, fun _ -> Mt. Eq (x, y)) :: ! suites
9
9
10
10
open Js_promise
11
11
12
12
let assert_bool b =
13
- if b then ()
14
- else
15
- raise (Invalid_argument " Assertion Failure." )
13
+ if b then () else raise (Invalid_argument " Assertion Failure." )
16
14
17
15
let fail _ =
18
16
(* assert_bool false *)
@@ -24,124 +22,128 @@ let thenTest () =
24
22
25
23
let andThenTest () =
26
24
let p = resolve 6 in
27
- p |> then_ (fun _ -> resolve (12 ))
28
- |> then_ (fun y -> resolve @@ assert_bool (y = 12 ))
25
+ p
26
+ |> then_ (fun _ -> resolve 12 )
27
+ |> then_ (fun y -> resolve @@ assert_bool (y = 12 ))
29
28
30
29
let h = resolve ()
31
30
32
31
let assertIsNotFound (x : Js_promise.error ) =
33
- match (function [@ bs.open ]
34
- | Not_found -> 0 ) x with
32
+ match (function [@ bs.open ] Not_found -> 0 ) x with
35
33
| Some _ -> h
36
34
| _ -> assert false
37
35
38
36
(* * would be nice to have [%bs.open? Stack_overflow]*)
39
37
let catchTest () =
40
38
let p = reject Not_found in
41
- p |> then_ fail
42
- |> catch (fun error ->
43
- assertIsNotFound error
44
- )
39
+ p |> then_ fail |> catch (fun error -> assertIsNotFound error)
45
40
46
41
let orResolvedTest () =
47
42
let p = resolve 42 in
48
- p |> catch (fun _ -> resolve 22 )
49
- |> then_ (fun value -> resolve @@ assert_bool (value = 42 ))
50
- |> catch fail
43
+ p
44
+ |> catch (fun _ -> resolve 22 )
45
+ |> then_ (fun value -> resolve @@ assert_bool (value = 42 ))
46
+ |> catch fail
51
47
52
48
let orRejectedTest () =
53
49
let p = reject Not_found in
54
- p |> catch (fun _ -> resolve 22 )
55
- |> then_ (fun value -> resolve @@ assert_bool (value = 22 ))
56
- |> catch fail
50
+ p
51
+ |> catch (fun _ -> resolve 22 )
52
+ |> then_ (fun value -> resolve @@ assert_bool (value = 22 ))
53
+ |> catch fail
57
54
58
55
let orElseResolvedTest () =
59
56
let p = resolve 42 in
60
- p |> catch (fun _ -> resolve 22 )
61
- |> then_ (fun value -> resolve @@ assert_bool (value = 42 ))
62
- |> catch fail
57
+ p
58
+ |> catch (fun _ -> resolve 22 )
59
+ |> then_ (fun value -> resolve @@ assert_bool (value = 42 ))
60
+ |> catch fail
63
61
64
62
let orElseRejectedResolveTest () =
65
63
let p = reject Not_found in
66
- p |> catch (fun _ -> resolve 22 )
67
- |> then_ (fun value -> resolve @@ assert_bool (value = 22 ))
68
- |> catch fail
64
+ p
65
+ |> catch (fun _ -> resolve 22 )
66
+ |> then_ (fun value -> resolve @@ assert_bool (value = 22 ))
67
+ |> catch fail
68
+
69
69
exception Stack_overflow
70
+
70
71
let orElseRejectedRejectTest () =
71
72
let p = reject Not_found in
72
- p |> catch (fun _ -> reject Stack_overflow )
73
- |> then_ fail
74
- |> catch (fun error ->
75
- match (function [@ bs.open ] Stack_overflow -> 0 ) error with
76
- | Some _ -> h
77
- | None -> assert false
78
- (* resolve @@ assert_bool (Obj.magic error == Stack_overflow) *) )
73
+ p
74
+ |> catch (fun _ -> reject Stack_overflow )
75
+ |> then_ fail
76
+ |> catch (fun error ->
77
+ match (function [@ bs.open ] Stack_overflow -> 0 ) error with
78
+ | Some _ -> h
79
+ | None -> assert false
80
+ (* resolve @@ assert_bool (Obj.magic error == Stack_overflow) *) )
79
81
80
82
let resolveTest () =
81
83
let p1 = resolve 10 in
82
84
p1 |> then_ (fun x -> resolve @@ assert_bool (x = 10 ))
83
85
84
86
let rejectTest () =
85
87
let p = reject Not_found in
86
- p |> catch
87
- (fun error ->
88
- assertIsNotFound error
89
- (* resolve @@ assert_bool (Obj.magic error == Not_found) *)
90
- )
88
+ p
89
+ |> catch (fun error ->
90
+ assertIsNotFound error
91
+ (* resolve @@ assert_bool (Obj.magic error == Not_found) *) )
91
92
92
93
let thenCatchChainResolvedTest () =
93
94
let p = resolve 20 in
94
- p |> then_ (fun value -> resolve @@ assert_bool (value = 20 ) )
95
- |> catch fail
95
+ p |> then_ (fun value -> resolve @@ assert_bool (value = 20 )) |> catch fail
96
96
97
97
let thenCatchChainRejectedTest () =
98
98
let p = reject Not_found in
99
99
p |> then_ fail
100
- |> catch (fun error ->
101
- assertIsNotFound error
102
- (* resolve @@ assert_bool (Obj.magic error == Not_found) *) )
103
-
100
+ |> catch (fun error ->
101
+ assertIsNotFound error
102
+ (* resolve @@ assert_bool (Obj.magic error == Not_found) *) )
104
103
105
104
let allResolvedTest () =
106
105
let p1 = resolve 1 in
107
106
let p2 = resolve 2 in
108
107
let p3 = resolve 3 in
109
108
let promises = [| p1; p2; p3 |] in
110
109
all promises
111
- |> then_
112
- (fun resolved ->
113
- assert_bool (resolved.(0 ) = 1 ) ;
114
- assert_bool (resolved.(1 ) = 2 ) ;
115
- assert_bool (resolved.(2 ) = 3 ) ;
116
- h
117
- )
110
+ |> then_ (fun resolved ->
111
+ assert_bool (resolved.(0 ) = 1 );
112
+ assert_bool (resolved.(1 ) = 2 );
113
+ assert_bool (resolved.(2 ) = 3 );
114
+ h)
118
115
116
+ let is_not_found (error : exn ) =
117
+ match error with Not_found -> true | _ -> false
119
118
120
119
let allRejectTest () =
121
120
let p1 = resolve 1 in
122
121
let p2 = resolve 3 in
123
122
let p3 = reject Not_found in
124
123
let promises = [| p1; p2; p3 |] in
125
- all promises
126
- |> then_ fail
127
- |> catch (fun error -> assert_bool (Obj. magic error == Not_found ) ; h)
124
+ all promises |> then_ fail
125
+ |> catch (fun error ->
126
+ assert_bool (is_not_found (Obj. magic error));
127
+ h)
128
128
129
129
let raceTest () =
130
130
let p1 = resolve " first" in
131
131
let p2 = resolve " second" in
132
132
let p3 = resolve " third" in
133
133
let promises = [| p1; p2; p3 |] in
134
- race promises
135
- |> then_ (fun resolved -> h)
136
- |> catch fail
134
+ race promises |> then_ (fun resolved -> h) |> catch fail
137
135
138
136
let createPromiseRejectTest () =
139
- make (fun ~resolve ~reject -> reject Not_found [@ bs])
140
- |> catch (fun error -> assert_bool (Obj. magic error == Not_found ); h)
137
+ make (fun ~resolve ~reject -> (reject Not_found [@ bs]))
138
+ |> catch (fun error ->
139
+ assert_bool (is_not_found (Obj. magic error));
140
+ h)
141
141
142
142
let createPromiseFulfillTest () =
143
- make (fun ~resolve ~reject :_ -> resolve " success" [@ bs])
144
- |> then_ (fun resolved -> assert_bool (Obj. magic resolved = " success" ); h)
143
+ make (fun ~resolve ~reject :_ -> (resolve " success" [@ bs]))
144
+ |> then_ (fun resolved ->
145
+ assert_bool (Obj. magic resolved = " success" );
146
+ h)
145
147
|> catch fail
146
148
147
149
let () =
@@ -164,30 +166,28 @@ let () =
164
166
(* * TODO: async tests?
165
167
*)
166
168
let () =
167
- ( Js.Promise. all2 (Js.Promise. resolve 2 , Js.Promise. resolve 3 ) )
168
- |> Js.Promise. then_ (fun (a ,b ) ->
169
- eq __LOC__ (a,b) (2 ,3 );
169
+ Js.Promise. all2 (Js.Promise. resolve 2 , Js.Promise. resolve 3 )
170
+ |> Js.Promise. then_ (fun (a , b ) ->
171
+ eq __LOC__ (a, b) (2 , 3 );
170
172
171
- Js.Promise. resolve ()
172
- )
173
- |> ignore
173
+ Js.Promise. resolve () )
174
+ |> ignore
175
+ ;;
174
176
177
+ Js. log (List. length ! suites);;
175
178
176
- ;; Js. log ( List. length ! suites)
179
+ Js. log " hey " ;;
177
180
178
- ;; Js. log " hey"
179
- ;; Mt. from_pair_suites __MODULE__ ! suites
181
+ Mt. from_pair_suites __MODULE__ ! suites
180
182
181
183
let twop = Js.Promise. resolve 2
184
+
182
185
let then_ = Js.Promise. then_
183
- let re = Js.Promise. resolve
184
186
185
- ;; Mt. from_promise_suites __MODULE__
187
+ let re = Js.Promise. resolve;;
188
+
189
+ Mt. from_promise_suites __MODULE__
186
190
[
187
- __LOC__,
188
- twop
189
- |> then_ (fun x -> re @@ Mt. Eq (x,2 ));
190
- __LOC__,
191
- twop
192
- |> then_ (fun x -> re @@ Mt. Neq (x,3 ))
193
- ]
191
+ (__LOC__, twop |> then_ (fun x -> re @@ Mt. Eq (x, 2 )));
192
+ (__LOC__, twop |> then_ (fun x -> re @@ Mt. Neq (x, 3 )));
193
+ ]
0 commit comments