forked from typelift/SwiftCheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProperty.swift
652 lines (594 loc) · 20.1 KB
/
Property.swift
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
//
// Property.swift
// SwiftCheck
//
// Created by Robert Widmann on 7/31/14.
// Copyright (c) 2015 TypeLift. All rights reserved.
//
/// Takes the conjunction of multiple properties and reports all successes and
/// failures as one combined property. That is, this property holds when all
/// sub-properties hold and fails when one or more sub-properties fail.
///
/// Conjoined properties are each tested normally but are collected and labelled
/// together. This can mean multiple failures in distinct sub-properties are
/// masked. If fine-grained error reporting is needed, use a combination of
/// `disjoin(_:)` and `verbose(_:)`.
///
/// When conjoining properties all calls to `expectFailure` will fail.
public func conjoin(ps : Testable...) -> Property {
return Property(sequence(ps.map({ (p : Testable) in
return p.property.unProperty.map { $0.unProp }
})).flatMap({ roses in
return Gen.pure(Prop(unProp: conj(id, xs: roses)))
}))
}
/// Takes the disjunction of multiple properties and reports all successes and
/// failures of each sub-property distinctly. That is, this property holds when
/// any one of its sub-properties holds and fails when all of its sub-properties
/// fail simultaneously.
///
/// Disjoined properties, when used in conjunction with labelling, cause
/// SwiftCheck to print a distribution map of the success rate of each sub-
/// property.
///
/// When disjoining properties all calls to `expectFailure` will fail. You can,
/// however, `invert` the property.
public func disjoin(ps : Testable...) -> Property {
return Property(sequence(ps.map({ (p : Testable) in
return p.property.unProperty.map { $0.unProp }
})).flatMap({ roses in
return Gen.pure(Prop(unProp: roses.reduce(.MkRose({ TestResult.failed() }, { [] }), combine: disj)))
}))
}
/// Takes the nondeterministic conjunction of multiple properties and treats
/// them as a single large property.
///
/// The resulting property makes 100 random choices to test any of the given
/// properties. Thus, running multiple test cases will result in distinct
/// arbitrary sequences of each property being tested.
public func conjamb(ps : () -> Testable...) -> Property {
let ls = ps.lazy.map { $0().property.unProperty }
return Property(Gen.oneOf(ls))
}
extension Testable {
/// Modifies a property so it will not shrink when it fails.
public var noShrinking : Property {
return self.mapRoseResult { rs in
return rs.onRose { res, _ in
return .MkRose({ res }, { [] })
}
}
}
/// Inverts the result of a test. That is, test cases that would pass now
/// fail and vice versa.
///
/// Discarded tests remain discarded under inversion.
public var invert : Property {
return self.mapResult { res in
return TestResult(ok: res.ok.map(!)
, expect: res.expect
, reason: res.reason
, theException: res.theException
, labels: res.labels
, stamp: res.stamp
, callbacks: res.callbacks
, abort: res.abort
, quantifier: res.quantifier)
}
}
/// Modifies a property so that it only will be tested once.
public var once : Property {
return self.mapResult { res in
return TestResult(ok: res.ok
, expect: res.expect
, reason: res.reason
, theException: res.theException
, labels: res.labels
, stamp: res.stamp
, callbacks: res.callbacks
, abort: true
, quantifier: res.quantifier)
}
}
/// Attaches a callback to a test case.
public func withCallback(cb : Callback) -> Property {
return self.mapResult { (res) in
return TestResult(ok: res.ok
, expect: res.expect
, reason: res.reason
, theException: res.theException
, labels: res.labels
, stamp: res.stamp
, callbacks: [cb] + res.callbacks
, abort: res.abort
, quantifier: res.quantifier)
}
}
/// Adds the given string to the counterexamples of a failing property.
public func counterexample(s : String) -> Property {
return self.withCallback(Callback.AfterFinalFailure(kind: .Counterexample) { _ in
return print(s)
})
}
/// Executes an action after the last failure of the property.
public func whenFail(m : () -> ()) -> Property {
return self.withCallback(Callback.AfterFinalFailure(kind: .NotCounterexample) { _ in
return m()
})
}
/// Executes an action after the every failure of the property.
///
/// Because the action is executed after every failing test it can be used
/// to track the list of failures generated by the shrinking mechanism.
public func whenEachFail(m : () -> ()) -> Property {
return self.withCallback(Callback.AfterFinalFailure(kind: .NotCounterexample) { (st, res) in
if res.ok == .Some(false) {
m()
}
})
}
/// Modifies a property so it prints out every generated test case and the
/// result of the property every time it is tested.
///
/// This function maps AfterFinalFailure callbacks that have the
/// `.Counterexample` kind to `.AfterTest` callbacks.
public var verbose : Property {
func chattyCallbacks(cbs : [Callback]) -> [Callback] {
let c = Callback.AfterTest(kind: .Counterexample) { (st, res) in
switch res.ok {
case .Some(true):
print("\nPassed: ", terminator: "")
printLabels(res)
case .Some(false):
print("\nFailed: ", terminator: "")
printLabels(res)
print("Pass the seed values \(st.randomSeedGenerator) to replay the test.", terminator: "\n\n")
default:
print("\nDiscarded: ", terminator: "")
printLabels(res)
}
}
return [c] + cbs.map { (c : Callback) -> Callback in
switch c {
case let .AfterFinalFailure(.Counterexample, f):
return .AfterTest(kind: .Counterexample, f: f)
default:
return c
}
}
}
return self.mapResult { res in
return TestResult(ok: res.ok
, expect: res.expect
, reason: res.reason
, theException: res.theException
, labels: res.labels
, stamp: res.stamp
, callbacks: res.callbacks + chattyCallbacks(res.callbacks)
, abort: res.abort
, quantifier: res.quantifier)
}
}
/// Modifies a property to indicate that it is expected to fail.
///
/// If the property does not fail, SwiftCheck will report an error.
public var expectFailure : Property {
return self.mapTotalResult { res in
return TestResult(ok: res.ok
, expect: false
, reason: res.reason
, theException: res.theException
, labels: res.labels
, stamp: res.stamp
, callbacks: res.callbacks
, abort: res.abort
, quantifier: res.quantifier)
}
}
/// Attaches a label to a property.
///
/// Labelled properties aid in testing conjunctions and disjunctions, or any
/// other cases where test cases need to be distinct from one another. In
/// addition to shrunken test cases, upon failure SwiftCheck will print a
/// distribution map for the property that shows a percentage success rate
/// for the property.
public func label(s : String) -> Property {
return self.classify(true, label: s)
}
/// Labels a property with a printable value.
public func collect<A>(x : A) -> Property {
return self.label(String(x))
}
/// Conditionally labels a property with a value.
public func classify(b : Bool, label : String) -> Property {
return self.cover(b, percentage: 0, label: label)
}
/// Checks that at least the given proportion of successful test cases
/// belong to the given class.
///
/// Discarded tests (i.e. ones with a false precondition) do not affect
/// coverage.
public func cover(b : Bool, percentage : Int, label : String) -> Property {
if b {
return self.mapResult { res in
return TestResult(ok: res.ok
, expect: res.expect
, reason: res.reason
, theException: res.theException
, labels: insertWith(max, k: label, v: percentage, m: res.labels)
, stamp: res.stamp.union([label])
, callbacks: res.callbacks
, abort: res.abort
, quantifier: res.quantifier)
}
}
return self.property
}
/// Applies a function that modifies the property generator's inner `Prop`.
///
/// This function can be used to completely change the evaluation schema of
/// generated test cases by replacing the test's rose tree with a custom
/// one.
public func mapProp(f : Prop -> Prop) -> Property {
return Property(f <^> self.property.unProperty)
}
/// Applies a function that modifies the test case generator's size.
public func mapSize(f : Int -> Int) -> Property {
return Property(Gen.sized { n in
return self.property.unProperty.resize(f(n))
})
}
/// Applies a function that modifies the result of a test case.
public func mapTotalResult(f : TestResult -> TestResult) -> Property {
return self.mapRoseResult { rs in
return protectResults(f <^> rs)
}
}
/// Applies a function that modifies the result of a test case.
public func mapResult(f : TestResult -> TestResult) -> Property {
return self.mapRoseResult { rs in
return f <^> rs
}
}
/// Applies a function that modifies the underlying Rose Tree that a test
/// case has generated.
public func mapRoseResult(f : Rose<TestResult> -> Rose<TestResult>) -> Property {
return self.mapProp { t in
return Prop(unProp: f(t.unProp))
}
}
}
/// Using a shrinking function, shrinks a given argument to a property if it
/// fails.
///
/// Shrinking is handled automatically by SwiftCheck. Invoking this function is
/// only necessary when you must override the default behavior.
public func shrinking<A>(shrinker : A -> [A], initial : A, prop : A -> Testable) -> Property {
return Property(promote(props(shrinker, original: initial, pf: prop)).map { rs in
return Prop(unProp: joinRose(rs.map { x in
return x.unProp
}))
})
}
/// A `Callback` is a block of code that can be run after a test case has
/// finished. They consist of a kind and the callback block itself, which is
/// given the state SwiftCheck ran the test case with and the result of the test
/// to do with as it sees fit.
public enum Callback {
/// A callback that is posted after a test case has completed.
case AfterTest(kind : CallbackKind, f : (CheckerState, TestResult) -> ())
/// The callback is posted after all cases in the test have failed.
case AfterFinalFailure(kind : CallbackKind, f : (CheckerState, TestResult) -> ())
}
/// The type of callbacks SwiftCheck can dispatch.
public enum CallbackKind {
/// Affected by the verbose combinator.
case Counterexample
/// Not affected by the verbose combinator
case NotCounterexample
}
/// The types of quantification SwiftCheck can perform.
public enum Quantification {
/// Universal Quantification ("for all").
case Universal
/// Existential Quanfication ("there exists").
case Existential
/// Uniqueness Quantification ("there exists one and only one")
// case Uniqueness
/// Counting Quantification ("there exist exactly k")
// case Counting
}
/// A `TestResult` represents the result of performing a single test.
public struct TestResult {
/// The result of executing the test case. For Discarded test cases the
/// value of this property is `.None`.
let ok : Optional<Bool>
/// Indicates what the expected result of the property is.
let expect : Bool
/// A message indicating the reason a test case failed.
let reason : String
/// The exception that was thrown if one occured during testing.
let theException : Optional<String>
/// All the labels used during the test case.
let labels : Dictionary<String, Int>
/// The collected values for the test case.
let stamp : Set<String>
/// Callbacks attached to the test case.
let callbacks : [Callback]
/// Indicates that any further testing of the property should cease.
let abort : Bool
/// The quantifier being applied to this test case.
let quantifier : Quantification
/// Provides a pattern-match-friendly view of the current state of a test
/// result.
public enum TestResultMatcher {
/// A case-able view of the current state of a test result.
case MatchResult( ok : Optional<Bool>
, expect : Bool
, reason : String
, theException : Optional<String>
, labels : Dictionary<String, Int>
, stamp : Set<String>
, callbacks : Array<Callback>
, abort : Bool
, quantifier : Quantification
)
}
/// Destructures a test case into a matcher that can be used in switch
/// statement.
public var match : TestResultMatcher {
return .MatchResult(ok: ok, expect: expect, reason: reason, theException: theException, labels: labels, stamp: stamp, callbacks: callbacks, abort: abort, quantifier: quantifier)
}
/// Creates and returns a new test result initialized with the given
/// parameters.
public init( ok : Optional<Bool>
, expect : Bool
, reason : String
, theException : Optional<String>
, labels : Dictionary<String, Int>
, stamp : Set<String>
, callbacks : [Callback]
, abort : Bool
, quantifier : Quantification)
{
self.ok = ok
self.expect = expect
self.reason = reason
self.theException = theException
self.labels = labels
self.stamp = stamp
self.callbacks = callbacks
self.abort = abort
self.quantifier = quantifier
}
/// Convenience constructor for a passing `TestResult`.
public static var succeeded : TestResult {
return result(Optional.Some(true))
}
/// Convenience constructor for a failing `TestResult`.
public static func failed(reason : String = "") -> TestResult {
return result(Optional.Some(false), reason: reason)
}
/// Convenience constructor for a discarded `TestResult`.
public static var rejected : TestResult {
return result(Optional.None)
}
/// Lifts a `Bool`ean value to a TestResult by mapping true to
/// `TestResult.suceeded` and false to `TestResult.failed`.
public static func liftBool(b : Bool) -> TestResult {
if b {
return TestResult.succeeded
}
return result(Optional.Some(false), reason: "Falsifiable")
}
}
// MARK: - Implementation Details
private func exception(msg : String) -> ErrorType -> TestResult {
return { e in TestResult.failed(String(e)) }
}
private func props<A>(shrinker : A -> [A], original : A, pf : A -> Testable) -> Rose<Gen<Prop>> {
return .MkRose({ pf(original).property.unProperty }, { shrinker(original).map { x1 in
return props(shrinker, original: x1, pf: pf)
}})
}
private func result(ok : Bool?, reason : String = "") -> TestResult {
return TestResult( ok: ok
, expect: true
, reason: reason
, theException: .None
, labels: [:]
, stamp: Set()
, callbacks: []
, abort: false
, quantifier: .Universal
)
}
private func protectResults(rs : Rose<TestResult>) -> Rose<TestResult> {
return rs.onRose { x, rs in
return .IORose({
return .MkRose(protectResult({ x }), { rs.map(protectResults) })
})
}
}
//internal func protectRose(f : () throws -> Rose<TestResult>) -> (() -> Rose<TestResult>) {
// return { protect(Rose.pure • exception("Exception"), x: f) }
//}
internal func protect<A>(f : ErrorType -> A, x : () throws -> A) -> A {
do {
return try x()
} catch let e {
return f(e)
}
}
internal func id<A>(x : A) -> A {
return x
}
internal func • <A, B, C>(f : B -> C, g : A -> B) -> A -> C {
return { f(g($0)) }
}
private func protectResult(r : () throws -> TestResult) -> (() -> TestResult) {
return { protect(exception("Exception"), x: r) }
}
internal func unionWith<K : Hashable, V>(f : (V, V) -> V, l : Dictionary<K, V>, r : Dictionary<K, V>) -> Dictionary<K, V> {
var map = l
r.forEach { (k, v) in
if let val = map.updateValue(v, forKey: k) {
map.updateValue(f(val, v), forKey: k)
}
}
return map
}
private func insertWith<K : Hashable, V>(f : (V, V) -> V, k : K, v : V, m : Dictionary<K, V>) -> Dictionary<K, V> {
var res = m
let oldV = res[k]
if let existV = oldV {
res[k] = f(existV, v)
} else {
res[k] = v
}
return res
}
private func sep(l : String, r : String) -> String {
if l.isEmpty {
return r
}
if r.isEmpty {
return l
}
return l + ", " + r
}
private func mplus(l : Optional<String>, r : Optional<String>) -> Optional<String> {
if let ls = l, rs = r {
return .Some(ls + rs)
}
if l == nil {
return r
}
return l
}
private func addCallbacks(result : TestResult) -> TestResult -> TestResult {
return { res in
return TestResult(ok: res.ok
, expect: res.expect
, reason: res.reason
, theException: res.theException
, labels: res.labels
, stamp: res.stamp
, callbacks: result.callbacks + res.callbacks
, abort: res.abort
, quantifier: res.quantifier)
}
}
private func addLabels(result : TestResult) -> TestResult -> TestResult {
return { res in
return TestResult(ok: res.ok
, expect: res.expect
, reason: res.reason
, theException: res.theException
, labels: unionWith(max, l: res.labels, r: result.labels)
, stamp: res.stamp.union(result.stamp)
, callbacks: res.callbacks
, abort: res.abort
, quantifier: res.quantifier)
}
}
private func printLabels(st : TestResult) {
if st.labels.isEmpty {
print("(.)")
} else if st.labels.count == 1, let pt = st.labels.first {
print("(\(pt.0))")
} else {
let gAllLabels = st.labels.map({ (l, _) in
return l + ", "
}).reduce("", combine: +)
print("(" + gAllLabels[gAllLabels.startIndex..<gAllLabels.endIndex.advancedBy(-2)] + ")")
}
}
private func conj(k : TestResult -> TestResult, xs : [Rose<TestResult>]) -> Rose<TestResult> {
if xs.isEmpty {
return Rose.MkRose({ k(TestResult.succeeded) }, { [] })
} else if let p = xs.first {
return .IORose(/*protectRose*/({
let rose = p.reduce
switch rose {
case .MkRose(let result, _):
if !result().expect {
return Rose.pure(TestResult.failed("expectFailure may not occur inside a conjunction"))
}
switch result().ok {
case .Some(true):
return conj(addLabels(result()) • addCallbacks(result()) • k, xs: [Rose<TestResult>](xs[1..<xs.endIndex]))
case .Some(false):
return rose
case .None:
let rose2 = conj(addCallbacks(result()) • k, xs: [Rose<TestResult>](xs[1..<xs.endIndex])).reduce
switch rose2 {
case .MkRose(let result2, _):
switch result2().ok {
case .Some(true):
return Rose.MkRose({ TestResult.rejected }, { [] })
case .Some(false):
return rose2
case .None:
return rose2
}
default:
fatalError("Rose should not have reduced to IORose")
}
}
default:
fatalError("Rose should not have reduced to IORose")
}
}))
}
fatalError("Non-exhaustive if-else statement reached")
}
private func disj(p : Rose<TestResult>, q : Rose<TestResult>) -> Rose<TestResult> {
return p.flatMap { result1 in
if !result1.expect {
return Rose<TestResult>.pure(TestResult.failed("expectFailure may not occur inside a disjunction"))
}
switch result1.ok {
case .Some(true):
return Rose<TestResult>.pure(result1)
case .Some(false):
return q.flatMap { (result2 : TestResult) in
if !result2.expect {
return Rose<TestResult>.pure(TestResult.failed("expectFailure may not occur inside a disjunction"))
}
switch result2.ok {
case .Some(true):
return Rose<TestResult>.pure(result2)
case .Some(false):
let callbacks : [Callback] = [.AfterFinalFailure(kind: .Counterexample,
f: { _ in
return print("")
})]
return Rose<TestResult>.pure(TestResult(ok: .Some(false),
expect: true,
reason: sep(result1.reason, r: result2.reason),
theException: mplus(result1.theException, r: result2.theException),
labels: [:],
stamp: Set(),
callbacks: result1.callbacks + callbacks + result2.callbacks,
abort: false,
quantifier: .Universal))
case .None:
return Rose<TestResult>.pure(result2)
}
}
case .None:
return q.flatMap { (result2 : TestResult) in
if !result2.expect {
return Rose<TestResult>.pure(TestResult.failed("expectFailure may not occur inside a disjunction"))
}
switch result2.ok {
case .Some(true):
return Rose<TestResult>.pure(result2)
default:
return Rose<TestResult>.pure(result1)
}
}
}
}
}