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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
type pos = Loc.pos
type 'a printer = Format.formatter -> 'a -> unit
type 'a testable = 'a Testable.t
let fail_equality ?__POS__ ?msg ?not_ ~expected actual =
raise
(Failure.Check_failure
(Failure.equality ?loc:(Loc.resolve ?__POS__ ()) ?msg ?not_ ~expected
~actual ()))
let fail_predicate ?__POS__ ?msg ~claim value =
raise
(Failure.Check_failure
(Failure.predicate ?loc:(Loc.resolve ?__POS__ ()) ?msg ~claim value))
let rendering pp v =
match pp with Some pp -> Pp.to_string pp v | None -> Pp.abstract
let equal ?__POS__ ?msg t expected actual =
if not (Testable.equal t expected actual) then
fail_equality ?__POS__ ?msg
~expected:(Testable.to_string t expected)
(Testable.to_string t actual)
let not_equal ?__POS__ ?msg t a b =
if Testable.equal t a b then
let rendered = Testable.to_string t a in
fail_equality ?__POS__ ?msg ~not_:true ~expected:rendered rendered
let is_true ?__POS__ ?msg b =
if not b then fail_equality ?__POS__ ?msg ~expected:"true" "false"
let is_false ?__POS__ ?msg b =
if b then fail_equality ?__POS__ ?msg ~expected:"false" "true"
let is_none ?__POS__ ?msg ?pp = function
| None -> ()
| Some v ->
fail_equality ?__POS__ ?msg ~expected:"None" ("Some " ^ rendering pp v)
let require_some ?__POS__ ?msg = function
| Some v -> v
| None -> fail_equality ?__POS__ ?msg ~expected:"Some _" "None"
let require_ok ?__POS__ ?msg ?pp = function
| Ok v -> v
| Error e ->
fail_equality ?__POS__ ?msg ~expected:"Ok _" ("Error " ^ rendering pp e)
let require_error ?__POS__ ?msg ?pp = function
| Error e -> e
| Ok v ->
fail_equality ?__POS__ ?msg ~expected:"Error _" ("Ok " ^ rendering pp v)
let is_some ?__POS__ ?msg o = ignore (require_some ?__POS__ ?msg o)
let is_ok ?__POS__ ?msg ?pp r = ignore (require_ok ?__POS__ ?msg ?pp r)
let is_error ?__POS__ ?msg ?pp r = ignore (require_error ?__POS__ ?msg ?pp r)
let require_match ?__POS__ ?msg ?pp v =
match extract v with
| Some b -> b
| None -> fail_predicate ?__POS__ ?msg ~claim:"a match" (rendering pp v)
let satisfies ?__POS__ ?msg ?(claim = "value satisfying the predicate") t pred v
=
if not (pred v) then
fail_predicate ?__POS__ ?msg ~claim (Testable.to_string t v)
let mem ?__POS__ ?msg t x xs =
if not (List.exists (Testable.equal t x) xs) then
fail_predicate ?__POS__ ?msg
~claim:(Pp.str "a list containing %s" (Testable.to_string t x))
(Testable.to_string (Testable.list t) xs)
let ordered verb ~relation ~holds ?__POS__ ?msg t ~than v =
match Testable.compare t with
| None ->
invalid_arg
(Pp.str
"Windtrap.%s: the witness has no order; give it one with \
Testable.with_compare"
verb)
| Some compare ->
if not (holds (compare v than)) then
fail_predicate ?__POS__ ?msg
~claim:(Pp.str "%s %s" relation (Testable.to_string t than))
(Testable.to_string t v)
let less ?__POS__ ?msg t ~than v =
ordered "less" ~relation:"less than"
~holds:(fun c -> c < 0)
?__POS__ ?msg t ~than v
let at_most ?__POS__ ?msg t ~than v =
ordered "at_most" ~relation:"at most"
~holds:(fun c -> c <= 0)
?__POS__ ?msg t ~than v
let greater ?__POS__ ?msg t ~than v =
ordered "greater" ~relation:"greater than"
~holds:(fun c -> c > 0)
?__POS__ ?msg t ~than v
let at_least ?__POS__ ?msg t ~than v =
ordered "at_least" ~relation:"at least"
~holds:(fun c -> c >= 0)
?__POS__ ?msg t ~than v
let fail_containment ?__POS__ ?msg ~demand ~needle haystack =
raise
(Failure.Check_failure
(Failure.containment ?loc:(Loc.resolve ?__POS__ ()) ?msg
?found_at:(Text.first_occurrence ~pattern:needle haystack)
~demand ~needle ~haystack ()))
let contains ?__POS__ ?msg ~sub haystack =
if not (Text.contains_substring ~pattern:sub haystack) then
fail_containment ?__POS__ ?msg ~demand:Failure.Anywhere ~needle:sub haystack
let not_contains ?__POS__ ?msg ~sub haystack =
if Text.contains_substring ~pattern:sub haystack then
fail_containment ?__POS__ ?msg ~demand:Failure.Anywhere ~needle:sub haystack
let starts_with ?__POS__ ?msg ~affix haystack =
if not (String.starts_with ~prefix:affix haystack) then
fail_containment ?__POS__ ?msg ~demand:Failure.Prefix ~needle:affix haystack
let ends_with ?__POS__ ?msg ~affix haystack =
if not (String.ends_with ~suffix:affix haystack) then
fail_containment ?__POS__ ?msg ~demand:Failure.Suffix ~needle:affix haystack
let in_order ?__POS__ ?msg ~subs haystack =
if subs = [] then invalid_arg "Windtrap.in_order: subs is empty";
let rec walk index cursor = function
| [] -> ()
| sub :: rest -> (
match Text.first_occurrence ~start:cursor ~pattern:sub haystack with
| Some at -> walk (index + 1) (at + String.length sub) rest
| None ->
fail_containment ?__POS__ ?msg
~demand:(Failure.Ordered { index; resumed_at = cursor })
~needle:sub haystack)
in
walk 0 0 subs
let raised_by fn =
match Failure.catch fn with
| Ok _ -> None
| Error (`Exception raised) -> Some raised
| Error c -> Failure.reraise c
let message_diff expected raised =
let diff constructor e a =
Some
{
Failure.constructor;
expected_message = Failure.text e;
actual_message = Failure.text a;
}
in
match (expected, raised) with
| Some (Invalid_argument e), Some (Invalid_argument a, _) ->
diff "Invalid_argument" e a
| Some (Stdlib.Failure e), Some (Stdlib.Failure a, _) -> diff "Failure" e a
| Some (Sys_error e), Some (Sys_error a, _) -> diff "Sys_error" e a
| _ -> None
let fail_raise ?__POS__ ?msg ?expected ~predicate raised =
raise
(Failure.Check_failure
(Failure.raised ?loc:(Loc.resolve ?__POS__ ()) ?msg
?expected:(Option.map Failure.exn_to_string expected)
?actual:
(Option.map (fun (exn, _) -> Failure.exn_to_string exn) raised)
~predicate
?backtrace:
(Option.map (fun (_, bt) -> Failure.backtrace_to_string bt) raised)
?message_diff:(message_diff expected raised)
()))
let raises ?__POS__ ?msg expected fn =
match raised_by fn with
| Some (exn, _) when exn = expected -> ()
| raised -> fail_raise ?__POS__ ?msg ~expected ~predicate:false raised
let raises_match ?__POS__ ?msg pred fn =
match raised_by fn with
| Some (exn, _) when pred exn -> ()
| raised -> fail_raise ?__POS__ ?msg ~predicate:true raised
module Exn = struct
let has substring m =
match substring with
| None -> true
| Some pattern -> Text.contains_substring ~pattern m
let invalid_arg ?substring = function
| Invalid_argument m -> has substring m
| _ -> false
let failure ?substring = function
| Stdlib.Failure m -> has substring m
| _ -> false
let sys_error ?substring = function
| Sys_error m -> has substring m
| _ -> false
end
let fail ?__POS__ msg =
raise
(Failure.Check_failure (Failure.message ?loc:(Loc.resolve ?__POS__ ()) msg))
let failf ?__POS__ fmt = Format.kasprintf (fun msg -> fail ?__POS__ msg) fmt
let skip ?reason () = raise (Failure.Control (`Skip reason))