-
-
Notifications
You must be signed in to change notification settings - Fork 600
/
Copy pathAllPassTest.swift
110 lines (98 loc) · 3.6 KB
/
AllPassTest.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
import XCTest
import Nimble
#if SWIFT_PACKAGE
import NimbleSharedTestHelpers
#endif
/// Add operators to `Optional` for conforming `Comparable` that removed in Swift 3.0
extension Optional where Wrapped: Comparable {
static func < (lhs: Optional, rhs: Optional) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l < r
case (nil, _?):
return true
default:
return false
}
}
static func > (lhs: Optional, rhs: Optional) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l > r
default:
return rhs < lhs
}
}
static func <= (lhs: Optional, rhs: Optional) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l <= r
default:
return !(rhs < lhs)
}
}
static func >= (lhs: Optional, rhs: Optional) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l >= r
default:
return !(lhs < rhs)
}
}
}
final class AllPassTest: XCTestCase {
func testAllPassArray() {
expect([1, 2, 3, 4]).to(allPass { $0 < 5 })
expect([1, 2, 3, 4]).toNot(allPass { $0 > 5 })
failsWithErrorMessage(
"expected to all pass a condition, but failed first at element <3> in <[1, 2, 3, 4]>") {
expect([1, 2, 3, 4]).to(allPass { $0 < 3 })
}
failsWithErrorMessage("expected to not all pass a condition") {
expect([1, 2, 3, 4]).toNot(allPass { $0 < 5 })
}
failsWithErrorMessage(
"expected to all be something, but failed first at element <3> in <[1, 2, 3, 4]>") {
expect([1, 2, 3, 4]).to(allPass("be something", {$0 < 3}))
}
failsWithErrorMessage("expected to not all be something") {
expect([1, 2, 3, 4]).toNot(allPass("be something", {$0 < 5}))
}
}
func testAllPassMatcher() {
expect([1, 2, 3, 4]).to(allPass(beLessThan(5)))
expect([1, 2, 3, 4]).toNot(allPass(beGreaterThan(5)))
failsWithErrorMessage(
"expected to all be less than <3>, but failed first at element <3> in <[1, 2, 3, 4]>") {
expect([1, 2, 3, 4]).to(allPass(beLessThan(3)))
}
failsWithErrorMessage("expected to not all be less than <5>") {
expect([1, 2, 3, 4]).toNot(allPass(beLessThan(5)))
}
}
func testAllPassCollectionsWithOptionals() {
expect([nil, nil, nil] as [Int?]).to(allPass(beNil()))
expect([nil, nil, nil] as [Int?]).to(allPass { $0 == nil })
expect([nil, 1, nil] as [Int?]).toNot(allPass { $0 == nil })
expect([1, 1, 1] as [Int?]).to(allPass { $0 == 1 })
expect([1, 1, nil] as [Int?]).toNot(allPass { $0 == 1 })
expect([1, 2, 3] as [Int?]).to(allPass { $0 < 4 })
expect([1, 2, 3] as [Int?]).toNot(allPass { $0 < 3 })
expect([1, 2, nil] as [Int?]).to(allPass { $0 < 3 })
}
func testAllPassSet() {
expect(Set([1, 2, 3, 4])).to(allPass { $0 < 5 })
expect(Set([1, 2, 3, 4])).toNot(allPass { $0 > 5 })
failsWithErrorMessage("expected to not all pass a condition") {
expect(Set([1, 2, 3, 4])).toNot(allPass { $0 < 5 })
}
failsWithErrorMessage("expected to not all be something") {
expect(Set([1, 2, 3, 4])).toNot(allPass("be something") { $0 < 5 })
}
}
func testAllPassWithNilAsExpectedValue() {
failsWithErrorMessageForNil("expected to all pass") {
expect(nil as [Int]?).to(allPass(beLessThan(5)))
}
}
}