-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPropertyTests.fs
More file actions
800 lines (669 loc) · 30.3 KB
/
PropertyTests.fs
File metadata and controls
800 lines (669 loc) · 30.3 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
namespace Hedgehog.Xunit.Tests.FSharp
open Xunit
open System
open Hedgehog
open Hedgehog.Xunit
module Common =
let [<Literal>] skipReason = "Skipping because it's just here to be the target of a [<Fact>] test"
open Common
type Int13 = static member __ = GenX.defaults |> AutoGenConfig.addGenerator (Gen.constant 13)
type Int5() =
inherit GenAttribute<int>()
override _.Generator = Gen.constant 5
type Int6() =
inherit GenAttribute<int>()
override _.Generator = Gen.constant 6
type IntConstantRange(max: int, min: int) =
inherit GenAttribute<int>()
override _.Generator = Range.constant max min |> Gen.int32
module ``Property module tests`` =
type private Marker = class end
let getMethod = typeof<Marker>.DeclaringType.GetMethod
let assertShrunk methodName expected =
let report = InternalLogic.report (getMethod methodName) typeof<Marker>.DeclaringType null
match report.Status with
| Status.Failed r ->
Assert.Equal(expected, r.Journal |> Journal.eval |> Seq.head)
| _ -> failwith "impossible"
[<Property(Skip = skipReason)>]
let ``fails for false, skipped`` (_: int) = false
[<Fact>]
let ``fails for false`` () =
assertShrunk (nameof ``fails for false, skipped``) "[0]"
[<Property(Skip = skipReason)>]
let ``Result with Error shrinks, skipped`` (i: int) =
if i > 10 then
Error ()
else
Ok ()
[<Fact>]
let ``Result with Error shrinks`` () =
assertShrunk (nameof ``Result with Error shrinks, skipped``) "[11]"
[<Property(Skip = skipReason)>]
let ``Result with Error reports exception with Error value, skipped`` (i: int) =
if i > 10 then
Error "Too many digits!"
else
Ok ()
[<Fact>]
let ``Result with Error reports exception with Error value`` () =
let report = InternalLogic.report (nameof ``Result with Error reports exception with Error value, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
match report.Status with
| Status.Failed r ->
let errorMessage = r.Journal |> Journal.eval |> Seq.skip 1 |> Seq.exactlyOne
Assert.Contains($"System.Exception: Result is in the Error case with the following value:{Environment.NewLine}\"Too many digits!\"", errorMessage)
| _ -> failwith "impossible"
[<Property>]
let ``Can generate an int`` (i: int) =
printfn "Test input: %i" i
[<Property(Skip = skipReason)>]
let ``Can shrink an int, skipped`` (i: int) =
if i >= 50 then failwith "Some error."
[<Fact>]
let ``Can shrink an int`` () =
assertShrunk (nameof ``Can shrink an int, skipped``) "[50]"
[<Property>]
let ``Can generate two ints`` (i1: int, i2: int) =
printfn "Test input: %i, %i" i1 i2
[<Property(Skip = skipReason)>]
let ``Can shrink both ints, skipped`` (i1: int, i2: int) =
if i1 >= 10 &&
i2 >= 20 then failwith "Some error."
[<Fact>]
let ``Can shrink both ints`` () =
assertShrunk (nameof ``Can shrink both ints, skipped``) "[10; 20]"
[<Property>]
let ``Can generate an int and string`` (i: int, s: string) =
printfn "Test input: %i, %s" i s
[<Property(Tests = 1000<tests>, Skip = skipReason)>]
let ``Can shrink an int and string, skipped`` (i: int, s: string) =
if i >= 2 && s.Contains "b" then failwith "Some error."
[<Fact>]
let ``Can shrink an int and string`` () =
assertShrunk (nameof ``Can shrink an int and string, skipped``) "[2; \"b\"]"
[<Property(typeof<Int13>, 1<tests>)>]
let ``runs with 13 once`` () = ()
[<Fact>]
let ``Tests 'runs with 13 once'`` () =
let config, tests, shrinks, _, _ = InternalLogic.parseAttributes (nameof ``runs with 13 once`` |> getMethod) typeof<Marker>.DeclaringType
Assert.Equal(None, shrinks)
Assert.Equal(Some 1<tests>, tests)
let generated = GenX.autoWith config |> Gen.sample 1 1 |> Seq.exactlyOne
Assert.Equal(13, generated)
type CustomRecord = { Herp: int; Derp: string }
[<Property>]
let ``Works up to 26 parameters`` (
a: string,
b: char,
c: double,
d: bool,
e: DateTime,
f: DateTimeOffset,
g: string list,
h: char list,
i: int,
j: int array,
k: char array,
l: DateTime array,
m: DateTimeOffset array,
n: CustomRecord option,
o: DateTime option,
p: Result<string, string>,
q: Result<string, int>,
r: Result<int, int>,
s: Result<int, string>,
t: Result<DateTime, string>,
u: Result<CustomRecord, string>,
v: Result<DateTimeOffset, DateTimeOffset>,
w: Result<double, DateTimeOffset>,
x: Result<double, bool>,
y: CustomRecord,
z: int list list) =
printfn "%A %A %A %A %A %A %A %A %A %A %A %A %A %A %A %A %A %A %A %A %A %A %A %A %A %A "
a b c d e f g h i j k l m n o p q r s t u v w x y z
[<Property>]
let ``multiple unresolved generics works`` _ _ = ()
[<Property>]
let ``mixed unresolved generics works 1`` (_: int) _ = ()
[<Property>]
let ``mixed unresolved generics works 2`` _ (_: int) = ()
[<Property>]
let ``unresolved nested generics works`` (_: _ list) (_: Result<_, _>) = ()
[<Property>]
let ``mixed nested generics works`` (_: int) _ (_: _ list) (_: Result<_, _>) = ()
[<Property>]
let ``returning unresolved generic works`` (x: 'a) = x
[<Property>]
let ``returning unresolved nested generic works`` () : Result<unit, 'a> = Ok ()
[<Property>]
let ``0 parameters passes`` () = ()
type ``Property class tests``(output: Xunit.Abstractions.ITestOutputHelper) =
[<Property>]
let ``Can generate an int`` (i: int) =
sprintf "Test input: %i" i |> output.WriteLine
[<Property>]
let ``Can generate two ints`` (i1: int, i2: int) =
sprintf "Test input: %i, %i" i1 i2 |> output.WriteLine
[<Property>]
let ``Can generate an int and string`` (i: int, s: string) =
sprintf "Test input: %i, %s" i s |> output.WriteLine
type ConfigArg = static member __ a = GenX.defaults |> AutoGenConfig.addGenerator (Gen.constant a)
[<Properties(AutoGenConfig = typeof<ConfigArg>, AutoGenConfigArgs = [|'a'|])>]
module ``AutoGenConfigArgs tests`` =
let [<Property>] ``PropertiesAttribute passes its AutoGenConfigArgs`` a = a = 'a'
let config a b =
GenX.defaults
|> AutoGenConfig.addGenerator (Gen.constant a)
|> AutoGenConfig.addGenerator (Gen.constant b)
type ConfigGenericArgs = static member __ (a: 'a) (b: 'b) = config a b
type ConfigArgs = static member __ (a: string) (b: int) = config a b
type ConfigMixedArgsA = static member __ (a: 'a) (b: int) = config a b
type ConfigMixedArgsB = static member __ (a: string) (b: 'b) = config a b
let test s i = s = "foo" && i = 13
let [<Property(AutoGenConfig = typeof<ConfigGenericArgs>, AutoGenConfigArgs = [|"foo"; 13|])>] ``all generics`` s i = test s i
let [<Property(AutoGenConfig = typeof<ConfigArgs> , AutoGenConfigArgs = [|"foo"; 13|])>] ``all non-generics`` s i = test s i
let [<Property(AutoGenConfig = typeof<ConfigMixedArgsA> , AutoGenConfigArgs = [|"foo"; 13|])>] ``mixed generics, 1`` s i = test s i
let [<Property(AutoGenConfig = typeof<ConfigMixedArgsB> , AutoGenConfigArgs = [|"foo"; 13|])>] ``mixed generics, 2`` s i = test s i
module ``Property module with AutoGenConfig tests`` =
module NormalTests =
[<Property( typeof<Int13>)>]
let ``Uses custom Int gen`` i = i = 13
[<Property(AutoGenConfig = typeof<Int13>)>]
let ``Uses custom Int gen with named arg`` i = i = 13
module FailingTests =
type private Marker = class end
type NonstaticProperty = member _.__ = GenX.defaults
[<Property(typeof<NonstaticProperty>, Skip = skipReason)>]
let ``Instance property fails, skipped`` () = ()
[<Fact>]
let ``Instance property fails`` () =
let testMethod = typeof<Marker>.DeclaringType.GetMethod(nameof ``Instance property fails, skipped``)
let e = Assert.Throws<Exception>(fun () -> InternalLogic.parseAttributes testMethod typeof<Marker>.DeclaringType |> ignore)
Assert.Equal("Hedgehog.Xunit.Tests.FSharp.Property module with AutoGenConfig tests+FailingTests+NonstaticProperty must have exactly one public static property that returns an AutoGenConfig.
An example type definition:
type NonstaticProperty =
static member __ =
GenX.defaults |> AutoGenConfig.addGenerator (Gen.constant 13)
", e.Message)
type NonAutoGenConfig = static member __ = ()
[<Property(typeof<NonAutoGenConfig>, Skip = skipReason)>]
let ``Non AutoGenConfig static property fails, skipped`` () = ()
[<Fact>]
let ``Non AutoGenConfig static property fails`` () =
let testMethod = typeof<Marker>.DeclaringType.GetMethod(nameof ``Non AutoGenConfig static property fails, skipped``)
let e = Assert.Throws<Exception>(fun () -> InternalLogic.parseAttributes testMethod typeof<Marker>.DeclaringType |> ignore)
Assert.Equal("Hedgehog.Xunit.Tests.FSharp.Property module with AutoGenConfig tests+FailingTests+NonAutoGenConfig must have exactly one public static property that returns an AutoGenConfig.
An example type definition:
type NonAutoGenConfig =
static member __ =
GenX.defaults |> AutoGenConfig.addGenerator (Gen.constant 13)
", e.Message)
type Int2718 = static member __ = AutoGenConfig.defaults |> AutoGenConfig.addGenerator (Gen.constant 2718)
[<Properties(typeof<Int13>, 200<tests>)>]
module ``Module with <Properties> tests`` =
[<Property>]
let ``Module <Properties> works`` (i: int) =
i = 13
[<Property(typeof<Int2718>)>]
let ``Module <Properties> is overriden by Method <Property>`` (i: int) =
i = 2718
type private Marker = class end
let getMethod = typeof<Marker>.DeclaringType.GetMethod
[<Fact>]
let ``Module <Properties> tests (count) works`` () =
let testMethod = getMethod (nameof ``Module <Properties> works``)
let _, tests, _, _, _ = InternalLogic.parseAttributes testMethod typeof<Marker>.DeclaringType
Assert.Equal(Some 200<tests>, tests)
[<Property(300<tests>)>]
let ``Module <Properties> tests (count) is overriden by Method <Property>, skipped`` (_: int) = ()
[<Fact>]
let ``Module <Properties> tests (count) is overriden by Method <Property>`` () =
let testMethod = getMethod (nameof ``Module <Properties> tests (count) is overriden by Method <Property>, skipped``)
let _, tests, _, _, _ = InternalLogic.parseAttributes testMethod typeof<Marker>.DeclaringType
Assert.Equal(Some 300<tests>, tests)
[<Properties(typeof<Int13>)>]
type ``Class with <Properties> tests``(output: Xunit.Abstractions.ITestOutputHelper) =
[<Property>]
let ``Class <Properties> works`` (i: int) =
i = 13
[<Property(typeof<Int2718>)>]
let ``Class <Properties> is overriden by Method level <Property>`` (i: int) =
i = 2718
type PropertyInt13Attribute() = inherit PropertyAttribute(typeof<Int13>)
module ``Property inheritance tests`` =
[<PropertyInt13>]
let ``Property inheritance works`` (i: int) =
i = 13
type PropertiesInt13Attribute() = inherit PropertiesAttribute(typeof<Int13>)
[<PropertiesInt13>]
module ``Properties inheritance tests`` =
[<Property>]
let ``Properties inheritance works`` (i: int) =
i = 13
[<Properties(Tests = 1<tests>, AutoGenConfig = typeof<Int13>)>]
module ``Properties named arg tests`` =
type private Marker = class end
let getMethod = typeof<Marker>.DeclaringType.GetMethod
[<Property>]
let ``runs once with 13`` () = ()
[<Fact>]
let ``Tests 'runs once with 13'`` () =
let config, tests, _, _, _ = InternalLogic.parseAttributes (nameof ``runs once with 13`` |> getMethod) typeof<Marker>.DeclaringType
Assert.Equal(Some 1<tests>, tests)
let generated = GenX.autoWith config |> Gen.sample 1 1 |> Seq.exactlyOne
Assert.Equal(13, generated)
[<Properties(1<tests>)>]
module ``Properties (tests count) tests`` =
type private Marker = class end
let getMethod = typeof<Marker>.DeclaringType.GetMethod
[<Property>]
let ``runs once`` () = ()
[<Fact>]
let ``Tests 'runs once'`` () =
let _, tests, _, _, _ = InternalLogic.parseAttributes (nameof ``runs once`` |> getMethod) typeof<Marker>.DeclaringType
Assert.Equal(Some 1<tests>, tests)
module ``Asynchronous tests`` =
type private Marker = class end
let getMethod = typeof<Marker>.DeclaringType.GetMethod
let assertShrunk methodName expected =
let report = InternalLogic.report (getMethod methodName) typeof<Marker>.DeclaringType null
match report.Status with
| Status.Failed r ->
Assert.Equal(expected, r.Journal |> Journal.eval |> Seq.head)
| _ -> failwith "impossible"
open System.Threading.Tasks
let FooAsync() =
Task.Delay 100
[<Property(Skip = skipReason)>]
let ``Returning Task with exception fails, skipped`` (i: int) : Task =
if i > 10 then
Exception() |> Task.FromException
else FooAsync()
[<Fact>]
let ``Returning Task with exception fails`` () =
assertShrunk (nameof ``Returning Task with exception fails, skipped``) "[11]"
open FSharp.Control.Tasks
[<Property(Skip = skipReason)>]
let ``TaskBuilder (returning Task<unit>) with exception shrinks, skipped`` (i: int) : Task<unit> =
task {
do! FooAsync()
if i > 10 then
raise <| Exception()
}
[<Fact>]
let ``TaskBuilder (returning Task<unit>) with exception shrinks`` () =
assertShrunk (nameof ``TaskBuilder (returning Task<unit>) with exception shrinks, skipped``) "[11]"
[<Property(Skip = skipReason)>]
let ``Async with exception shrinks, skipped`` (i: int) =
async {
do! Async.Sleep 100
if i > 10 then
raise <| Exception()
}
[<Fact>]
let ``Async with exception shrinks`` () =
assertShrunk (nameof ``Async with exception shrinks, skipped``) "[11]"
[<Property(Skip = skipReason)>]
let ``AsyncResult with Error shrinks, skipped`` (i: int) =
async {
do! Async.Sleep 100
if i > 10 then
return Error ()
else
return Ok ()
}
[<Fact>]
let ``AsyncResult with Error shrinks`` () =
assertShrunk (nameof ``AsyncResult with Error shrinks, skipped``) "[11]"
[<Property(Skip = skipReason)>]
let ``TaskResult with Error shrinks, skipped`` (i: int) =
task {
do! FooAsync()
if i > 10 then
return Error ()
else
return Ok ()
}
[<Fact>]
let ``TaskResult with Error shrinks`` () =
assertShrunk (nameof ``TaskResult with Error shrinks, skipped``) "[11]"
[<Property(Skip = skipReason)>]
let ``Non-unit TaskResult with Error shrinks, skipped`` (i: int) =
task {
do! FooAsync()
if i > 10 then
return Error "Test fails"
else
return Ok 1
}
[<Fact>]
let ``Non-unit TaskResult with Error shrinks`` () =
assertShrunk (nameof ``Non-unit TaskResult with Error shrinks, skipped``) "[11]"
module ``IDisposable test module`` =
let mutable runs = 0
let mutable disposes = 0
type DisposableImplementation() =
interface IDisposable with
member _.Dispose() =
disposes <- disposes + 1
let getMethod = typeof<DisposableImplementation>.DeclaringType.GetMethod
[<Property(Skip = skipReason)>]
let ``IDisposable arg get disposed even if exception thrown, skipped`` (_: DisposableImplementation) (i: int) =
runs <- runs + 1
if i > 10 then raise <| Exception()
[<Fact>]
let ``IDisposable arg get disposed even if exception thrown`` () =
let report = InternalLogic.report (getMethod (nameof ``IDisposable arg get disposed even if exception thrown, skipped``)) typeof<DisposableImplementation>.DeclaringType null
match report.Status with
| Status.Failed _ ->
Assert.NotEqual(0, runs)
Assert.Equal(runs, disposes)
| _ -> failwith "impossible"
module ``The PropertyTestCaseDiscoverer works`` =
let mutable runs = 0
[<Property>]
let ``increment runs`` () =
runs <- runs + 1
// This assumes that ``increment runs`` runs before this test runs. The tests *seem* to run in alphabetical order.
// https://github.com/asherber/Xunit.Priority doesn't seem to work; perhaps modules are treated differently. Ref: https://stackoverflow.com/questions/9210281/
[<Fact>]
let ``PropertyAttribute is discovered and run`` () =
Assert.True(runs > 0)
module TupleTests =
[<Fact>]
let ``Non-Hedgehog.Xunit passes`` () =
Property.check <| property {
let! a, b =
GenX.defaults
|> AutoGenConfig.addGenerator (Gen.constant (1, 2))
|> GenX.autoWith<int*int>
Assert.Equal(1, a)
Assert.Equal(2, b)
}
type CustomTupleGen = static member __ = GenX.defaults |> AutoGenConfig.addGenerator (Gen.constant (1, 2))
[<Property(typeof<CustomTupleGen>)>]
let ``Hedgehog.Xunit requires another param to pass`` (((a,b) : int*int), _: bool) =
Assert.Equal(1, a)
Assert.Equal(2, b)
module ShrinkTests =
type private Marker = class end
let getMethod = typeof<Marker>.DeclaringType.GetMethod
[<Property(100<tests>, 0<shrinks>, Skip = skipReason)>]
let ``0 shrinks, skipped`` i =
i < 2500
[<Fact>]
let ``0 shrinks`` () =
let _, _, shrinks, _, _ = InternalLogic.parseAttributes (nameof ``0 shrinks, skipped`` |> getMethod) typeof<Marker>.DeclaringType
Assert.Equal(Some 0<shrinks>, shrinks)
[<Property(Shrinks = 1<shrinks>, Skip = skipReason)>]
let ``1 shrinks, run, skipped`` i =
i < 2500
[<Fact>]
let ``1 shrinks, run`` () =
let report = InternalLogic.report (nameof ``1 shrinks, run, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
match report.Status with
| Failed data ->
Assert.Equal(1<shrinks>, data.Shrinks)
| _ -> failwith "impossible"
[<Property(typeof<Int13>, 100<tests>, 0<shrinks>, Skip = skipReason)>]
let ``0 shrinks, run, skipped`` () : unit =
failwith "oops"
[<Fact>]
let ``0 shrinks, run`` () =
let report = InternalLogic.report (nameof ``0 shrinks, run, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
match report.Status with
| Failed data ->
Assert.Equal(0<shrinks>, data.Shrinks)
| _ -> failwith "impossible"
type Forever = static member __ = GenX.defaults |> AutoGenConfig.addGenerator (Gen.constant "...")
[<Properties(typeof<Forever>, 100<tests>, 0<shrinks>)>]
module ``Module with <Properties> tests, 0 shrinks`` =
type private Marker = class end
let getMethod = typeof<Marker>.DeclaringType.GetMethod
[<Property(Skip = skipReason)>]
let ``0 shrinks, skipped`` i =
i < 2500
[<Fact>]
let ``0 shrinks`` () =
let _, _, shrinks, _, _ = InternalLogic.parseAttributes (nameof ``0 shrinks, skipped`` |> getMethod) typeof<Marker>.DeclaringType
Assert.Equal(Some 0<shrinks>, shrinks)
[<Property(Shrinks = 1<shrinks>, Skip = skipReason)>]
let ``1 shrinks, run, skipped`` i =
i < 2500
[<Fact>]
let ``1 shrinks, run`` () =
let report = InternalLogic.report (nameof ``1 shrinks, run, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
match report.Status with
| Failed data ->
Assert.Equal(1<shrinks>, data.Shrinks)
| _ -> failwith "impossible"
[<Properties(Shrinks = 0<shrinks>)>]
module ``Module with <Properties> tests, 0 shrinks manual`` =
type private Marker = class end
let getMethod = typeof<Marker>.DeclaringType.GetMethod
[<Property(Skip = skipReason)>]
let ``0 shrinks, skipped`` i =
i < 2500
[<Fact>]
let ``0 shrinks`` () =
let _, _, shrinks, _, _ = InternalLogic.parseAttributes (nameof ``0 shrinks, skipped`` |> getMethod) typeof<Marker>.DeclaringType
Assert.Equal(Some 0<shrinks>, shrinks)
[<Property(Shrinks = 1<shrinks>, Skip = skipReason)>]
let ``1 shrinks, run, skipped`` i =
i < 2500
[<Fact>]
let ``1 shrinks, run`` () =
let report = InternalLogic.report (nameof ``1 shrinks, run, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
match report.Status with
| Failed data ->
Assert.Equal(1<shrinks>, data.Shrinks)
| _ -> failwith "impossible"
[<Properties(100<tests>, 0<shrinks>)>]
module ``Module with <Properties> tests, whatever tests and 0 shrinks`` =
type private Marker = class end
let getMethod = typeof<Marker>.DeclaringType.GetMethod
[<Property(Skip = skipReason)>]
let ``0 shrinks, skipped`` i =
i < 2500
[<Fact>]
let ``0 shrinks`` () =
let _, _, shrinks, _, _ = InternalLogic.parseAttributes (nameof ``0 shrinks, skipped`` |> getMethod) typeof<Marker>.DeclaringType
Assert.Equal(Some 0<shrinks>, shrinks)
[<Property(Shrinks = 1<shrinks>, Skip = skipReason)>]
let ``1 shrinks, run, skipped`` i =
i < 2500
[<Fact>]
let ``1 shrinks, run`` () =
let report = InternalLogic.report (nameof ``1 shrinks, run, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
match report.Status with
| Failed data ->
Assert.Equal(1<shrinks>, data.Shrinks)
| _ -> failwith "impossible"
module RecheckTests =
type private Marker = class end
let getMethod = typeof<Marker>.DeclaringType.GetMethod
let [<Literal>] expectedRecheckData = "0_16700074754810023652_2867022503662193831_"
[<Property(Skip = skipReason)>]
[<Recheck(expectedRecheckData)>]
let ``recheck, skipped`` () = ()
[<Fact>]
let ``recheck`` () =
let _, _, _, recheck, _ = InternalLogic.parseAttributes (nameof ``recheck, skipped`` |> getMethod) typeof<Marker>.DeclaringType
match recheck with
| None -> failwith "impossible"
| Some actualRecheckData ->
Assert.Equal(expectedRecheckData, actualRecheckData)
let mutable runs = 0
[<Property>]
[<Recheck("33_6868290028601943647_16954941586199361379_10110110111101111110")>]
let ``recheck runs once`` (i: int) =
runs <- runs + 1
Assert.Equal(1, runs)
//Assert.True(i < 100) // used to generate the Recheck Data
[<Recheck("99_99_99_")>]
let [<Property(Size = 1, Skip = skipReason)>] ``Recheck's Size overrides Property's Size, skipped, 99`` (_: int) = false
[<Recheck("1_1_1_")>]
let [<Property(Size = 99, Skip = skipReason)>] ``Recheck's Size overrides Property's Size, skipped, 1`` (_: int) = false
[<Fact>]
let ``Recheck's Size overrides Property's Size`` () =
let lastGennedInt test =
let report = InternalLogic.report (test |> getMethod) typeof<Marker>.DeclaringType null
let exn = Assert.Throws<Exception>(fun () -> InternalLogic.tryRaise report)
System.Text.RegularExpressions.Regex.Match(exn.ToString(), "\[-?(\d+)\]").Groups.Item(1).Value |> Int32.Parse
let gennedWithSize99 = nameof ``Recheck's Size overrides Property's Size, skipped, 99`` |> lastGennedInt
gennedWithSize99 > 999_999_999 |> Assert.True
let gennedWithSize1 = nameof ``Recheck's Size overrides Property's Size, skipped, 1`` |> lastGennedInt
gennedWithSize1 < 100 |> Assert.True
[<Properties(Size=1)>]
module SizeTests =
type private Marker = class end
let getMethod = typeof<Marker>.DeclaringType.GetMethod
[<Property(Size = 2)>]
let ``property size, actual`` () = ()
[<Fact>]
let ``property size`` () =
let _, _, _, _, size = InternalLogic.parseAttributes (nameof ``property size, actual`` |> getMethod) typeof<Marker>.DeclaringType
match size with
| None -> failwith "impossible"
| Some size ->
Assert.Equal(2, size)
[<Property>]
let ``properites size, actual`` () = ()
[<Fact>]
let ``properites size`` () =
let _, _, _, _, size = InternalLogic.parseAttributes (nameof ``properites size, actual`` |> getMethod) typeof<Marker>.DeclaringType
match size with
| None -> failwith "impossible"
| Some size ->
Assert.Equal(1, size)
module ``tryRaise tests`` =
type private Marker = class end
let getMethod = typeof<Marker>.DeclaringType.GetMethod
[<Property(Skip = skipReason)>]
let ``always fails, skipped`` () = false
[<Fact>]
let ``always fails`` () =
let report = InternalLogic.report (nameof ``always fails, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
let actual = Assert.Throws<Exception>(fun () -> InternalLogic.tryRaise report)
let expectedMessage = """*** Failed! Falsifiable (after 1 test):
[]"""
actual.Message.Contains(expectedMessage) |> Assert.True
let expectedMessage = """
This failure can be reproduced by running:
> Property.recheck "0_"""
actual.Message.Contains(expectedMessage) |> Assert.True
module ``returning a property runs it`` =
type private Marker = class end
let getMethod = typeof<Marker>.DeclaringType.GetMethod
[<Property(Skip = skipReason)>]
let ``returning a passing property with internal gen passes, skipped`` () = property {
let! a = Gen.constant 13
Assert.Equal(13, a)
}
[<Fact>]
let ``returning a passing property with internal gen passes`` () =
let report = InternalLogic.report (nameof ``returning a passing property with internal gen passes, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
Assert.Equal(100<tests>, report.Tests)
[<Property(Skip = skipReason)>]
let ``returning a failing property with internal gen fails and shrinks, skipped`` () = property {
let! a = Gen.int32 (Range.constant 1 100)
Assert.True(a <= 50)
}
[<Fact>]
let ``returning a failing property with internal gen fails and shrinks`` () =
let report = InternalLogic.report (nameof ``returning a failing property with internal gen fails and shrinks, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
let actual = Assert.Throws<Exception>(fun () -> InternalLogic.tryRaise report)
actual.Message.Contains("51") |> Assert.True
[<Property(typeof<Int13>, Skip = skipReason)>]
let ``returning a passing property with external gen passes, skipped`` i = property {
Assert.Equal(13, i)
}
[<Fact>]
let ``returning a passing property with external gen passes`` () =
let report = InternalLogic.report (nameof ``returning a passing property with external gen passes, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
Assert.Equal(100<tests>, report.Tests)
[<Property(Skip = skipReason)>]
let ``returning a failing property with external gen fails and shrinks, skipped`` i = property {
let! _50 = Gen.constant 50
Assert.True(i <= _50)
}
[<Fact>]
let ``returning a failing property with external gen fails and shrinks`` () =
let report = InternalLogic.report (nameof ``returning a failing property with external gen fails and shrinks, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
let actual = Assert.Throws<Exception>(fun () -> InternalLogic.tryRaise report)
actual.Message.Contains("51") |> Assert.True
[<Property(Skip = skipReason)>]
let ``returning a passing property<bool> with internal gen passes, skipped`` () = property {
let! a = Gen.constant 13
return 13 = a
}
[<Fact>]
let ``returning a passing property<bool> with internal gen passes`` () =
let report = InternalLogic.report (nameof ``returning a passing property<bool> with internal gen passes, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
Assert.Equal(100<tests>, report.Tests)
[<Property(Skip = skipReason)>]
let ``returning a failing property<bool> with internal gen fails and shrinks, skipped`` () = property {
let! a = Gen.int32 (Range.constant 1 100)
return a <= 50
}
[<Fact>]
let ``returning a failing property<bool> with internal gen fails and shrinks`` () =
let report = InternalLogic.report (nameof ``returning a failing property<bool> with internal gen fails and shrinks, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
let actual = Assert.Throws<Exception>(fun () -> InternalLogic.tryRaise report)
actual.Message.Contains("51") |> Assert.True
[<Property(typeof<Int13>, Skip = skipReason)>]
let ``returning a passing property<bool> with external gen passes, skipped`` i = property {
return 13 = i
}
[<Fact>]
let ``returning a passing property<bool> with external gen passes`` () =
let report = InternalLogic.report (nameof ``returning a passing property<bool> with external gen passes, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
Assert.Equal(100<tests>, report.Tests)
[<Property(Skip = skipReason)>]
let ``returning a failing property<bool> with external gen fails and shrinks, skipped`` i = property {
let! _50 = Gen.constant 50
return i <= _50
}
[<Fact>]
let ``returning a failing property<bool> with external gen fails and shrinks`` () =
let report = InternalLogic.report (nameof ``returning a failing property<bool> with external gen fails and shrinks, skipped`` |> getMethod) typeof<Marker>.DeclaringType null
let actual = Assert.Throws<Exception>(fun () -> InternalLogic.tryRaise report)
actual.Message.Contains("51") |> Assert.True
module ``GenAttribute Tests`` =
[<Property>]
let ``can set parameter as 5`` ([<Int5>] i) =
Assert.StrictEqual(5, i)
[<Property(typeof<Int13>)>]
let ``overrides Property's autoGenConfig`` ([<Int5>] i) =
Assert.StrictEqual(5, i)
[<Property>]
let ``can have different generators for the same parameter type`` ([<Int5>] five) ([<Int6>] six) =
five = 5 && six = 6
[<Property>]
let ``can restrict on range`` ([<IntConstantRange(min = 0, max = 5)>] i) =
i >= 0 && i <= 5
type OtherAttribute() = inherit Attribute()
[<Property>]
let ``Doesn't error with OtherAttribute`` ([<OtherAttribute>][<Int5>] i) =
i = 5
[<Properties(typeof<Int13>)>]
module ``GenAttribute with Properties Tests`` =
[<Property>]
let ``overrides Properties' autoGenConfig`` ([<Int5>] i) =
Assert.StrictEqual(5, i)
[<Property(typeof<Int13>)>]
let ``overrides Properties' and Property's autoGenConfig`` ([<Int5>] i) =
Assert.StrictEqual(5, i)
type Int13A =
static member __ =
AutoGenConfig.defaults
|> AutoGenConfig.addGenerator (Gen.constant 13)
|> AutoGenConfig.addGenerator (Gen.constant "A")
[<Properties(typeof<Int13A>)>]
module ``Module with <Properties(typeof<Int13A>)>`` =
[<Property>]
let ``Module's <Properties> works`` (i, s) =
i = 13 && s = "A"
[<Property(typeof<Int2718>)>]
let ``Module's <Properties> merges with Method level <Property>`` (i, s) =
i = 2718 && s = "A"