-
Notifications
You must be signed in to change notification settings - Fork 4
/
AssertionsTests.swift
154 lines (120 loc) · 3.76 KB
/
AssertionsTests.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
// Copyright (c) 2015 Rob Rix. All rights reserved.
final class AssertionsTests: XCTestCase {
func testAssertingWithOptionalArrays() {
let array: [Int]? = [1, 2, 3]
assert(array, ==, [1, 2, 3])
// XCTAssertEqual(array, [1, 2, 3]) // => error: value of optional type '[Int]?' not unwrapped; did you mean to use '!' or '?'?
}
func testAssertingWithOptionalArraysFailure() {
let array: [Int]? = [1, 2, 3]
assertFailure {
assert(array, ==, [1, 2, 3, 4])
}
}
func testAssertingWithOptionalStrings() {
let string: String? = "hello"
assert(string, ==, "hello")
}
func testAssertingWithOptionalStringsFailure() {
let string: String? = "hello"
assertFailure {
assert(string, ==, "hello!")
}
}
func testAssertingWithMethods() {
let set: Set<Int>? = Set([1, 2, 3])
assert(set, Set.contains, 3)
}
func testAssertingWithMethodsFailure() {
let set: Set<Int>? = Set([1, 2, 3])
assertFailure {
assert(set, Set.contains, 4)
}
}
func testAssertingPropertyWithPredicate() {
let string: String? = ""
assert(string, { $0.isEmpty })
}
func testAssertingPropertyWithPredicateFailure() {
let string: String? = "1"
assertFailure {
assert(string, { $0.isEmpty })
}
}
func testAssertingThrowingMatcher() {
assert(try getStringReturns(), ==, "")
}
func testAssertingThrowingMatcherFailure() {
assertFailure {
assert(try getStringThrows(), ==, "")
}
}
func testAssertingThrowingPropertyWithPredicate() {
assert(try getStringReturns(), { $0.isEmpty })
}
func testAssertingThrowingPropertyWithPredicateFailure() {
assertFailure {
assert(try getStringThrows(), { $0.isEmpty })
}
}
func testAssertingNilOfEquatableType() {
let x: Int? = nil
assert(x, ==, nil)
}
func testAssertingNilOfEquatableTypeFailure() {
let x: Int? = 1
assertFailure {
assert(x, ==, nil)
}
}
func testAssertingNonNilOfEquatableType() {
let x: Int? = 1
assert(x, !=, nil)
}
func testAssertingNonNilOfEquatableTypeFailure() {
let x: Int? = nil
assertFailure(x, !=, nil)
}
// MARK: Throwing functions
private func getStringReturns() throws -> String {
return ""
}
private func getStringThrows() throws -> String {
throw NSError(domain: "com.antitypical.Assertions", code: -1, userInfo: [:])
}
// MARK: Testing assertion failures
/// Assert that `test` causes an assertion failure.
func assertFailure<T>(file: String = __FILE__, line: UInt = __LINE__, @noescape _ test: () throws -> T) -> (message: String, file: String, line: UInt, expected: Bool)? {
do {
let previous = expectFailure
expectFailure = true
try test()
expectFailure = previous
if let result = failure {
return result
} else {
return Assertions.failure("expected the assertion to fail", file: file, line: line)
}
} catch {
return Assertions.failure("error: \(error)", file: file, line: line)
}
}
/// Workaround for http://www.openradar.me/19996972
func assertFailure<T>(@autoclosure expression1: () -> T, _ test: (T, T) -> Bool, @autoclosure _ expression2: () -> T, message: String = "", file: String = __FILE__, line: UInt = __LINE__) -> (message: String, file: String, line: UInt, expected: Bool)? {
return assertFailure {
assert(expression1(), test, expression2(), message: message, file: file, line: line)
}
}
private var expectFailure: Bool = false
private var failure: (message: String, file: String, line: UInt, expected: Bool)? = nil
override func recordFailureWithDescription(message: String, inFile file: String, atLine line: UInt, expected: Bool) {
if expectFailure {
failure = (message: message, file: file, line: line, expected: expected)
} else {
super.recordFailureWithDescription(message, inFile: file, atLine: line, expected: expected)
}
}
}
// MARK: - Imports
import Assertions
import XCTest