-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathXCTest.swift
354 lines (292 loc) · 12.1 KB
/
XCTest.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
// RUN: %target-run-stdlib-swift
// REQUIRES: executable_test
// REQUIRES: objc_interop
// REQUIRES: OS=macosx
// watchOS 2.0 does not have an XCTest module.
// XFAIL: OS=watchos
import StdlibUnittest
import XCTest
var XCTestTestSuite = TestSuite("XCTest")
// NOTE: When instantiating test cases for a particular test method using the
// XCTestCase(selector:) initializer, those test methods must be marked
// as dynamic. Objective-C XCTest uses runtime introspection to
// instantiate an NSInvocation with the given selector.
func execute(observers: [XCTestObservation] = [], _ run: () -> Void) {
for observer in observers {
XCTestObservationCenter.shared().addTestObserver(observer)
}
run()
for observer in observers {
XCTestObservationCenter.shared().removeTestObserver(observer)
}
}
class FailureDescriptionObserver: NSObject, XCTestObservation {
var failureDescription: String?
func testCase(_ testCase: XCTestCase, didFailWithDescription description: String, inFile filePath: String?, atLine lineNumber: UInt) {
failureDescription = description
}
}
XCTestTestSuite.test("exceptions") {
class ExceptionTestCase: XCTestCase {
dynamic func test_raises() {
NSException(name: NSExceptionName(rawValue: "XCTestTestSuiteException"), reason: nil, userInfo: nil).raise()
}
}
let testCase = ExceptionTestCase(selector: #selector(ExceptionTestCase.test_raises))
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(0, testRun.failureCount)
expectEqual(1, testRun.unexpectedExceptionCount)
expectEqual(1, testRun.totalFailureCount)
expectFalse(testRun.hasSucceeded)
}
XCTestTestSuite.test("XCTAssertEqual/T") {
class AssertEqualTestCase: XCTestCase {
dynamic func test_whenEqual_passes() {
XCTAssertEqual(1, 1)
}
dynamic func test_whenNotEqual_fails() {
XCTAssertEqual(1, 2)
}
}
let passingTestCase = AssertEqualTestCase(selector: #selector(AssertEqualTestCase.test_whenEqual_passes))
execute(passingTestCase.run)
let passingTestRun = passingTestCase.testRun!
expectTrue(passingTestRun.hasSucceeded)
let failingTestCase = AssertEqualTestCase(selector: #selector(AssertEqualTestCase.test_whenNotEqual_fails))
let observer = FailureDescriptionObserver()
execute(observers: [observer], failingTestCase.run)
let failingTestRun = failingTestCase.testRun!
expectEqual(1, failingTestRun.failureCount)
expectEqual(0, failingTestRun.unexpectedExceptionCount)
expectEqual(observer.failureDescription, "XCTAssertEqual failed: (\"1\") is not equal to (\"2\") - ")
}
XCTestTestSuite.test("XCTAssertEqual/Optional<T>") {
class AssertEqualOptionalTestCase: XCTestCase {
dynamic func test_whenOptionalsAreEqual_passes() {
XCTAssertEqual(Optional(1),Optional(1))
}
dynamic func test_whenOptionalsAreNotEqual_fails() {
XCTAssertEqual(Optional(1),Optional(2))
}
}
let passingTestCase = AssertEqualOptionalTestCase(selector: #selector(AssertEqualOptionalTestCase.test_whenOptionalsAreEqual_passes))
execute(passingTestCase.run)
let passingTestRun = passingTestCase.testRun!
expectEqual(1, passingTestRun.testCaseCount)
expectEqual(1, passingTestRun.executionCount)
expectEqual(0, passingTestRun.failureCount)
expectEqual(0, passingTestRun.unexpectedExceptionCount)
expectEqual(0, passingTestRun.totalFailureCount)
expectTrue(passingTestRun.hasSucceeded)
let failingTestCase = AssertEqualOptionalTestCase(selector: #selector(AssertEqualOptionalTestCase.test_whenOptionalsAreNotEqual_fails))
let observer = FailureDescriptionObserver()
execute(observers: [observer], failingTestCase.run)
let failingTestRun = failingTestCase.testRun!
expectEqual(1, failingTestRun.testCaseCount)
expectEqual(1, failingTestRun.executionCount)
expectEqual(1, failingTestRun.failureCount)
expectEqual(0, failingTestRun.unexpectedExceptionCount)
expectEqual(1, failingTestRun.totalFailureCount)
expectFalse(failingTestRun.hasSucceeded)
expectEqual(observer.failureDescription, "XCTAssertEqual failed: (\"Optional(1)\") is not equal to (\"Optional(2)\") - ")
}
XCTestTestSuite.test("XCTAssertEqual/Array<T>") {
class AssertEqualArrayTestCase: XCTestCase {
dynamic func test_whenArraysAreEqual_passes() {
XCTAssertEqual(["foo", "bar", "baz"],
["foo", "bar", "baz"])
}
dynamic func test_whenArraysAreNotEqual_fails() {
XCTAssertEqual(["foo", "baz", "bar"],
["foo", "bar", "baz"])
}
}
let passingTestCase = AssertEqualArrayTestCase(selector: #selector(AssertEqualArrayTestCase.test_whenArraysAreEqual_passes))
execute(passingTestCase.run)
let passingTestRun = passingTestCase.testRun!
expectEqual(1, passingTestRun.testCaseCount)
expectEqual(1, passingTestRun.executionCount)
expectEqual(0, passingTestRun.failureCount)
expectEqual(0, passingTestRun.unexpectedExceptionCount)
expectEqual(0, passingTestRun.totalFailureCount)
expectTrue(passingTestRun.hasSucceeded)
let failingTestCase = AssertEqualArrayTestCase(selector: #selector(AssertEqualArrayTestCase.test_whenArraysAreNotEqual_fails))
execute(failingTestCase.run)
let failingTestRun = failingTestCase.testRun!
expectEqual(1, failingTestRun.testCaseCount)
expectEqual(1, failingTestRun.executionCount)
expectEqual(1, failingTestRun.failureCount)
expectEqual(0, failingTestRun.unexpectedExceptionCount)
expectEqual(1, failingTestRun.totalFailureCount)
expectFalse(failingTestRun.hasSucceeded)
}
XCTestTestSuite.test("XCTAssertEqual/Dictionary<T, U>") {
class AssertEqualDictionaryTestCase: XCTestCase {
dynamic func test_whenDictionariesAreEqual_passes() {
XCTAssertEqual(["foo": "bar", "baz": "flim"],
["baz": "flim", "foo": "bar"])
}
dynamic func test_whenDictionariesAreNotEqual_fails() {
XCTAssertEqual(["foo": ["bar": "baz"]],
["foo": ["bar": "flim"]])
}
}
let passingTestCase = AssertEqualDictionaryTestCase(selector: #selector(AssertEqualDictionaryTestCase.test_whenDictionariesAreEqual_passes))
execute(passingTestCase.run)
let passingTestRun = passingTestCase.testRun!
expectEqual(1, passingTestRun.testCaseCount)
expectEqual(1, passingTestRun.executionCount)
expectEqual(0, passingTestRun.failureCount)
expectEqual(0, passingTestRun.unexpectedExceptionCount)
expectEqual(0, passingTestRun.totalFailureCount)
expectTrue(passingTestRun.hasSucceeded)
let failingTestCase = AssertEqualDictionaryTestCase(selector: #selector(AssertEqualDictionaryTestCase.test_whenDictionariesAreNotEqual_fails))
execute(failingTestCase.run)
let failingTestRun = failingTestCase.testRun!
expectEqual(1, failingTestRun.testCaseCount)
expectEqual(1, failingTestRun.executionCount)
expectEqual(1, failingTestRun.failureCount)
expectEqual(0, failingTestRun.unexpectedExceptionCount)
expectEqual(1, failingTestRun.totalFailureCount)
expectFalse(failingTestRun.hasSucceeded)
}
XCTestTestSuite.test("XCTAssertThrowsError") {
class ErrorTestCase: XCTestCase {
var doThrow = true
var errorCode = 42
dynamic func throwSomething() throws {
if doThrow {
throw NSError(domain: "MyDomain", code: errorCode, userInfo: nil)
}
}
dynamic func test_throws() {
XCTAssertThrowsError(try throwSomething()) {
error in
let nserror = error as NSError
XCTAssertEqual(nserror.domain, "MyDomain")
XCTAssertEqual(nserror.code, 42)
}
}
}
// Try success case
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(0, testRun.failureCount)
expectEqual(0, testRun.unexpectedExceptionCount)
expectEqual(0, testRun.totalFailureCount)
expectTrue(testRun.hasSucceeded)
}
// Now try when it does not throw
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
testCase.doThrow = false
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(1, testRun.failureCount)
expectEqual(0, testRun.unexpectedExceptionCount)
expectEqual(1, testRun.totalFailureCount)
expectFalse(testRun.hasSucceeded)
}
// Now try when it throws the wrong thing
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
testCase.errorCode = 23
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(1, testRun.failureCount)
expectEqual(0, testRun.unexpectedExceptionCount)
expectEqual(1, testRun.totalFailureCount)
expectFalse(testRun.hasSucceeded)
}
}
XCTestTestSuite.test("XCTAsserts with throwing expressions") {
class ErrorTestCase: XCTestCase {
var doThrow = true
var errorCode = 42
dynamic func throwSomething() throws -> String {
if doThrow {
throw NSError(domain: "MyDomain", code: errorCode, userInfo: nil)
}
return "Hello"
}
dynamic func test_withThrowing() {
XCTAssertEqual(try throwSomething(), "Hello")
}
}
// Try success case
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
testCase.doThrow = false
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(0, testRun.failureCount)
expectEqual(0, testRun.unexpectedExceptionCount)
expectEqual(0, testRun.totalFailureCount)
expectTrue(testRun.hasSucceeded)
}
// Now try when the expression throws
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(0, testRun.failureCount)
expectEqual(1, testRun.unexpectedExceptionCount)
expectEqual(1, testRun.totalFailureCount)
expectFalse(testRun.hasSucceeded)
}
}
XCTestTestSuite.test("Test methods that wind up throwing") {
class ErrorTestCase: XCTestCase {
var doThrow = true
var errorCode = 42
dynamic func throwSomething() throws {
if doThrow {
throw NSError(domain: "MyDomain", code: errorCode, userInfo: nil)
}
}
dynamic func test_withThrowing() throws {
try throwSomething()
}
}
// Try success case
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
testCase.doThrow = false
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(0, testRun.failureCount)
expectEqual(0, testRun.unexpectedExceptionCount)
expectEqual(0, testRun.totalFailureCount)
expectTrue(testRun.hasSucceeded)
}
// Now try when the expression throws
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(0, testRun.failureCount)
expectEqual(1, testRun.unexpectedExceptionCount)
expectEqual(1, testRun.totalFailureCount)
expectFalse(testRun.hasSucceeded)
}
}
runAllTests()