-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathProperty.fs
355 lines (279 loc) · 12.8 KB
/
Property.fs
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
namespace Hedgehog
open System
[<Struct>]
type Property<'a> =
| Property of Gen<Lazy<Journal * Outcome<'a>>>
module Property =
let ofGen (x : Gen<Lazy<Journal * Outcome<'a>>>) : Property<'a> =
Property x
let toGen (Property x : Property<'a>) : Gen<Lazy<Journal * Outcome<'a>>> =
x
let tryFinally (after : unit -> unit) (m : Property<'a>) : Property<'a> =
Gen.tryFinally after (toGen m) |> ofGen
let tryWith (k : exn -> Property<'a>) (m : Property<'a>) : Property<'a> =
Gen.tryWith (toGen << k) (toGen m) |> ofGen
let delay (f : unit -> Property<'a>) : Property<'a> =
Gen.delay (toGen << f) |> ofGen
let using (x : 'a) (k : 'a -> Property<'b>) : Property<'b> when
'a :> IDisposable and
'a : null =
delay (fun () -> k x)
|> tryFinally (fun () ->
match x with
| null ->
()
| _ ->
x.Dispose ())
let filter (p : 'a -> bool) (m : Property<'a>) : Property<'a> =
m |> toGen |> GenLazyTuple.mapSnd (Outcome.filter p) |> ofGen
let ofOutcome (x : Outcome<'a>) : Property<'a> =
(Journal.empty, x) |> GenLazy.constant |> ofGen
let failure : Property<unit> =
Failure |> ofOutcome
let discard : Property<unit> =
Discard |> ofOutcome
let success (x : 'a) : Property<'a> =
Success x |> ofOutcome
let ofBool (x : bool) : Property<unit> =
if x then
success ()
else
failure
let counterexample (msg : unit -> string) : Property<unit> =
(Journal.singleton msg, Success ()) |> GenLazy.constant |> ofGen
let map (f : 'a -> 'b) (x : Property<'a>) : Property<'b> =
let g (j, outcome) =
try
(j, outcome |> Outcome.map f)
with e ->
(Journal.append j (Journal.singletonMessage (string e)), Failure)
x |> toGen |> GenLazy.map g |> ofGen
let internal set (a: 'a) (property : Property<'b>) : Property<'a> =
property |> map (fun _ -> a)
let private bindGen
(f : 'a -> Gen<Lazy<Journal * Outcome<'b>>>)
(m : Gen<Lazy<Journal * Outcome<'a>>>) : Gen<Lazy<Journal * Outcome<'b>>> =
m |> GenLazy.bind (fun (journal, result) ->
match result with
| Failure ->
GenLazy.constant (journal, Failure)
| Discard ->
GenLazy.constant (journal, Discard)
| Success a ->
GenLazyTuple.mapFst (Journal.append journal) (f a))
let bind (k : 'a -> Property<'b>) (m : Property<'a>) : Property<'b> =
let kTry a =
try
k a |> toGen
with e ->
(Journal.singletonMessage (string e), Failure) |> GenLazy.constant
m
|> toGen
|> bindGen kTry
|> ofGen
let falseToFailure p =
p |> bind ofBool
let internal printValue (value) : string =
// sprintf "%A" is not prepared for printing ResizeArray<_> (C# List<T>) so we prepare the value instead
let prepareForPrinting (value: obj) : obj =
#if FABLE_COMPILER
value
#else
if value = null then
value
else
let t = value.GetType()
// have to use TypeInfo due to targeting netstandard 1.6
let t = System.Reflection.IntrospectionExtensions.GetTypeInfo(t)
let isList = t.IsGenericType && t.GetGenericTypeDefinition() = typedefof<ResizeArray<_>>
if isList
then value :?> System.Collections.IEnumerable |> Seq.cast<obj> |> List.ofSeq :> obj
else value
#endif
value |> prepareForPrinting |> sprintf "%A"
let forAll (k : 'a -> Property<'b>) (gen : Gen<'a>) : Property<'b> =
let prepend (x : 'a) =
counterexample (fun () -> printValue x)
|> set x
|> bind k
|> toGen
gen |> Gen.bind prepend |> ofGen
let forAll' (gen : Gen<'a>) : Property<'a> =
gen |> forAll success
//
// Runner
//
let private shrinkInput
(language: Language)
(data : RecheckData)
(shrinkLimit : int<shrinks> Option) =
let rec loop
(nshrinks : int<shrinks>)
(shrinkPath : ShrinkOutcome list)
(Node (root, xs) : Tree<Lazy<Journal * Outcome<'a>>>) =
let journal = root.Value |> fst
let recheckData = { data with ShrinkPath = shrinkPath }
let failed =
Failed {
Shrinks = nshrinks
Journal = journal
RecheckInfo =
Some { Language = language
Data = recheckData } }
match shrinkLimit, xs |> Seq.indexed |> Seq.tryFind (snd >> Tree.outcome >> Lazy.value >> snd >> Outcome.isFailure) with
| Some shrinkLimit', _ when nshrinks >= shrinkLimit' -> failed
| _, None -> failed
| _, Some (idx, tree) ->
let nextShrinkPath = shrinkPath @ List.replicate idx ShrinkOutcome.Pass @ [ShrinkOutcome.Fail]
loop (nshrinks + 1<shrinks>) nextShrinkPath tree
loop 0<shrinks> []
let rec private followShrinkPath
(Node (root, children) : Tree<Lazy<Journal * Outcome<'a>>>) =
let rec skipPassedChild children shrinkPath =
match children, shrinkPath with
| _, [] ->
Failed {
Shrinks = 0<shrinks>
Journal = root.Value |> fst
RecheckInfo = None
}
| [], _ -> failwith "The shrink path lead to a dead end. This should never happen."
| _ :: childrenTail, ShrinkOutcome.Pass :: shrinkPathTail -> skipPassedChild childrenTail shrinkPathTail
| childrenHead :: _, ShrinkOutcome.Fail :: shrinkPathTail -> followShrinkPath childrenHead shrinkPathTail
skipPassedChild (Seq.toList children)
let private splitAndRun p data =
let seed1, seed2 = Seed.split data.Seed
let result = p |> toGen |> Gen.toRandom |> Random.run seed1 data.Size
result, seed2
let private reportWith' (args : PropertyArgs) (config : PropertyConfig) (p : Property<unit>) : Report =
let nextSize size =
if size >= 100 then
1
else
size + 1
let rec loop data tests discards =
if tests = config.TestLimit then
{ Tests = tests
Discards = discards
Status = OK }
elif discards >= 100<discards> then
{ Tests = tests
Discards = discards
Status = GaveUp }
else
let result, seed2 = splitAndRun p data
let nextData = {
data with
Seed = seed2
Size = nextSize data.Size
}
match snd (Tree.outcome result).Value with
| Failure ->
{ Tests = tests + 1<tests>
Discards = discards
Status = shrinkInput args.Language data config.ShrinkLimit result }
| Success () ->
loop nextData (tests + 1<tests>) discards
| Discard ->
loop nextData tests (discards + 1<discards>)
loop args.RecheckData 0<tests> 0<discards>
let reportWith (config : PropertyConfig) (p : Property<unit>) : Report =
p |> reportWith' PropertyArgs.init config
let report (p : Property<unit>) : Report =
p |> reportWith PropertyConfig.defaultConfig
let reportBoolWith (config : PropertyConfig) (p : Property<bool>) : Report =
p |> falseToFailure |> reportWith config
let reportBool (p : Property<bool>) : Report =
p |> falseToFailure |> report
let checkWith (config : PropertyConfig) (p : Property<unit>) : unit =
p |> reportWith config |> Report.tryRaise
let check (p : Property<unit>) : unit =
p |> report |> Report.tryRaise
let checkBool (g : Property<bool>) : unit =
g |> falseToFailure |> check
let checkBoolWith (config : PropertyConfig) (g : Property<bool>) : unit =
g |> falseToFailure |> checkWith config
let reportRecheckWith (recheckData: string) (config : PropertyConfig) (p : Property<unit>) : Report =
let recheckData = recheckData |> RecheckData.deserialize
let result, _ = splitAndRun p recheckData
{ Tests = 1<tests>
Discards = 0<discards>
Status = followShrinkPath result recheckData.ShrinkPath }
let reportRecheck (recheckData: string) (p : Property<unit>) : Report =
p |> reportRecheckWith recheckData PropertyConfig.defaultConfig
let reportRecheckBoolWith (recheckData: string) (config : PropertyConfig) (p : Property<bool>) : Report =
p |> falseToFailure |> reportRecheckWith recheckData config
let reportRecheckBool (recheckData: string) (p : Property<bool>) : Report =
p |> falseToFailure |> reportRecheck recheckData
let recheckWith (recheckData: string) (config : PropertyConfig) (p : Property<unit>) : unit =
p |> reportRecheckWith recheckData config |> Report.tryRaise
let recheck (recheckData: string) (p : Property<unit>) : unit =
p |> reportRecheck recheckData |> Report.tryRaise
let recheckBoolWith (recheckData: string) (config : PropertyConfig) (g : Property<bool>) : unit =
g |> falseToFailure |> recheckWith recheckData config
let recheckBool (recheckData: string) (g : Property<bool>) : unit =
g |> falseToFailure |> recheck recheckData
let renderWith (n : PropertyConfig) (p : Property<unit>) : string =
p |> reportWith n |> Report.render
let render (p : Property<unit>) : string =
p |> report |> Report.render
let renderBool (property : Property<bool>) : string =
property |> falseToFailure |> render
let renderBoolWith (config : PropertyConfig) (p : Property<bool>) : string =
p |> falseToFailure |> renderWith config
[<AutoOpen>]
module PropertyBuilder =
let rec private loop (p : unit -> bool) (m : Property<unit>) : Property<unit> =
if p () then
m |> Property.bind (fun _ -> loop p m)
else
Property.success ()
type Builder internal () =
member __.For(m : Property<'a>, k : 'a -> Property<'b>) : Property<'b> =
m |> Property.bind k
member __.For(xs : #seq<'a>, k : 'a -> Property<unit>) : Property<unit> =
let xse = xs.GetEnumerator ()
Property.using xse (fun xse ->
let mv = xse.MoveNext
let kc = Property.delay (fun () -> k xse.Current)
loop mv kc)
member __.While(p : unit -> bool, m : Property<unit>) : Property<unit> =
loop p m
member __.Yield(x : 'a) : Property<'a> =
Property.success x
member __.Combine(m : Property<unit>, n : Property<'a>) : Property<'a> =
m |> Property.bind (fun _ -> n)
member __.TryFinally(m : Property<'a>, after : unit -> unit) : Property<'a> =
m |> Property.tryFinally after
member __.TryWith(m : Property<'a>, k : exn -> Property<'a>) : Property<'a> =
m |> Property.tryWith k
member __.Using(a : 'a, k : 'a -> Property<'b>) : Property<'b> when
'a :> IDisposable and
'a : null =
Property.using a k
member __.Bind(m : Gen<'a>, k : 'a -> Property<'b>) : Property<'b> =
m |> Property.forAll k
member __.Return(b : bool) : Property<unit> =
Property.ofBool b
member __.BindReturn(m : Gen<'a>, f: 'a -> 'b) =
m
|> Gen.map (fun a -> Lazy.constant ((Journal.singleton (fun () -> Property.printValue a)), Success a))
|> Property.ofGen
|> Property.map f
member __.MergeSources(ga, gb) =
Gen.zip ga gb
member __.ReturnFrom(m : Property<'a>) : Property<'a> =
m
member __.Delay(f : unit -> Property<'a>) : Property<'a> =
Property.delay f
member __.Zero() : Property<unit> =
Property.success ()
[<CustomOperation("counterexample", MaintainsVariableSpace = true)>]
member __.Counterexample(m : Property<'a>, [<ProjectionParameter>] f : 'a -> string) : Property<'a> =
m |> Property.bind (fun a ->
Property.counterexample (fun () -> f a)
|> Property.set a)
[<CustomOperation("where", MaintainsVariableSpace = true)>]
member __.Where(m : Property<'a>, [<ProjectionParameter>] p : 'a -> bool) : Property<'a> =
Property.filter p m
let property = Builder ()