8
8
//
9
9
//
10
10
// XCTest.swift
11
- // a mini version XCTest
11
+ // This is the main file for the framework. It provides the entry point function
12
+ // for running tests and some infrastructure for running them.
12
13
//
13
14
14
15
#if os(Linux)
@@ -17,90 +18,13 @@ import Glibc
17
18
import Darwin
18
19
#endif
19
20
20
- public protocol XCTestCaseProvider {
21
- // In the Objective-C version of XCTest, it is possible to discover all tests when the test is executed by asking the runtime for all methods and looking for the string "test". In Swift, we ask test providers to tell us the list of tests by implementing this property.
22
- var allTests : [ ( String , ( ) -> ( ) ) ] { get }
23
- }
24
-
25
- public protocol XCTestCase : XCTestCaseProvider {
26
-
27
- }
28
-
29
- extension XCTestCase {
30
-
31
- public var continueAfterFailure : Bool {
32
- get {
33
- return true
34
- }
35
- set {
36
- // TODO: When using the Objective-C runtime, XCTest is able to throw an exception from an assert and then catch it at the frame above the test method. This enables the framework to effectively stop all execution in the current test. There is no such facility in Swift. Until we figure out how to get a compatible behavior, we have decided to hard-code the value of 'true' for continue after failure.
37
- }
38
- }
39
-
40
- public func invokeTest( ) {
41
- let tests = self . allTests
42
- var totalDuration = 0.0
43
- var totalFailures = 0
44
- for (name, test) in tests {
45
- XCTCurrentTestCase = self
46
- let method = " \( self . dynamicType) . \( name) "
47
- var duration : Double = 0.0
48
- print ( " Test Case ' \( method) ' started. " )
49
- var tv = timeval ( )
50
- let start = withUnsafeMutablePointer ( & tv, { ( t: UnsafeMutablePointer < timeval > ) -> Double in
51
- gettimeofday ( t, nil )
52
- return Double ( t. memory. tv_sec) + Double( t. memory. tv_usec) / 1000000.0
53
- } )
54
-
55
- test ( )
56
- let end = withUnsafeMutablePointer ( & tv, { ( t: UnsafeMutablePointer < timeval > ) -> Double in
57
- gettimeofday ( t, nil )
58
- return Double ( t. memory. tv_sec) + Double( t. memory. tv_usec) / 1000000.0
59
- } )
60
- duration = end - start
61
- totalDuration += duration
62
- for failure in XCTCurrentFailures {
63
- failure. emit ( method)
64
- totalFailures++
65
- }
66
- var result = " passed "
67
- if XCTCurrentFailures . count > 0 {
68
- result = " failed "
69
- }
70
- print ( " Test Case ' \( method) ' \( result) ( \( round ( duration * 1000.0 ) / 1000.0 ) seconds). " )
71
- XCTAllRuns . append ( XCTRun ( duration: duration, method: method, passed: XCTCurrentFailures . count == 0 , failures: XCTCurrentFailures) )
72
- XCTCurrentFailures . removeAll ( )
73
- XCTCurrentTestCase = nil
74
- }
75
- var testCountSuffix = " s "
76
- if tests. count == 1 {
77
- testCountSuffix = " "
78
- }
79
- var failureSuffix = " s "
80
- if totalFailures == 1 {
81
- failureSuffix = " "
82
- }
83
- let averageDuration = totalDuration / Double( tests. count)
84
-
85
-
86
- print ( " Executed \( tests. count) test \( testCountSuffix) , with \( totalFailures) failure \( failureSuffix) (0 unexpected) in \( round ( averageDuration * 1000.0 ) / 1000.0 ) ( \( round ( totalDuration * 1000.0 ) / 1000.0 ) ) seconds " )
87
- }
88
-
89
- // This function is for the use of XCTestCase only, but we must make it public or clients will get a link failure when using XCTest (23476006)
90
- public func testFailure( message: String , file: StaticString , line: UInt ) {
91
- if !continueAfterFailure {
92
- assert ( false , message, file: file, line: line)
93
- } else {
94
- XCTCurrentFailures . append ( XCTFailure ( message: message, file: file, line: line) )
95
- }
96
- }
97
-
98
- public func setUp( ) {
99
-
100
- }
21
+ struct XCTFailure {
22
+ var message : String
23
+ var file : StaticString
24
+ var line : UInt
101
25
102
- public func tearDown ( ) {
103
-
26
+ func emit ( method : String ) {
27
+ print ( " \( file ) : \( line ) : error: \( method ) : \( message ) " )
104
28
}
105
29
}
106
30
@@ -134,108 +58,7 @@ internal struct XCTRun {
134
58
exit ( totalFailures > 0 ? 1 : 0 )
135
59
}
136
60
137
- struct XCTFailure {
138
- var message : String
139
- var file : StaticString
140
- var line : UInt
141
-
142
- func emit( method: String ) {
143
- print ( " \( file) : \( line) : error: \( method) : \( message) " )
144
- }
145
- }
146
-
147
61
internal var XCTCurrentTestCase : XCTestCase ?
148
62
internal var XCTCurrentFailures = [ XCTFailure] ( )
149
63
internal var XCTAllRuns = [ XCTRun] ( )
150
64
151
- public func XCTAssert( @autoclosure expression: ( ) -> BooleanType , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
152
- if !expression( ) . boolValue {
153
- if let test = XCTCurrentTestCase {
154
- test. testFailure ( message, file: file, line: line)
155
- }
156
- }
157
- }
158
-
159
- public func XCTAssertEqual< T : Equatable > ( @autoclosure expression1: ( ) -> T ? , @autoclosure _ expression2: ( ) -> T ? , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
160
- XCTAssert ( expression1 ( ) == expression2 ( ) , message, file: file, line: line)
161
- }
162
-
163
- public func XCTAssertEqual< T : Equatable > ( @autoclosure expression1: ( ) -> ArraySlice < T > , @autoclosure _ expression2: ( ) -> ArraySlice < T > , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
164
- XCTAssert ( expression1 ( ) == expression2 ( ) , message, file: file, line: line)
165
- }
166
-
167
- public func XCTAssertEqual< T : Equatable > ( @autoclosure expression1: ( ) -> ContiguousArray < T > , @autoclosure _ expression2: ( ) -> ContiguousArray < T > , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
168
- XCTAssert ( expression1 ( ) == expression2 ( ) , message, file: file, line: line)
169
- }
170
-
171
- public func XCTAssertEqual< T : Equatable > ( @autoclosure expression1: ( ) -> [ T ] , @autoclosure _ expression2: ( ) -> [ T ] , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
172
- XCTAssert ( expression1 ( ) == expression2 ( ) , message, file: file, line: line)
173
- }
174
-
175
- public func XCTAssertEqual< T, U : Equatable > ( @autoclosure expression1: ( ) -> [ T : U ] , @autoclosure _ expression2: ( ) -> [ T : U ] , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
176
- XCTAssert ( expression1 ( ) == expression2 ( ) , message, file: file, line: line)
177
- }
178
-
179
- public func XCTAssertEqualWithAccuracy< T : FloatingPointType > ( @autoclosure expression1: ( ) -> T , @autoclosure _ expression2: ( ) -> T , accuracy: T , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
180
- XCTAssert ( abs ( expression1 ( ) . distanceTo ( expression2 ( ) ) ) <= abs ( accuracy. distanceTo ( T ( 0 ) ) ) , message, file: file, line: line)
181
- }
182
-
183
- public func XCTAssertFalse( @autoclosure expression: ( ) -> BooleanType , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
184
- XCTAssert ( !expression( ) . boolValue, message, file: file, line: line)
185
- }
186
-
187
- public func XCTAssertGreaterThan< T : Comparable > ( @autoclosure expression1: ( ) -> T , @autoclosure _ expression2: ( ) -> T , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
188
- XCTAssert ( expression1 ( ) > expression2 ( ) , message, file: file, line: line)
189
- }
190
-
191
- public func XCTAssertGreaterThanOrEqual< T : Comparable > ( @autoclosure expression1: ( ) -> T , @autoclosure _ expression2: ( ) -> T , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
192
- XCTAssert ( expression1 ( ) >= expression2 ( ) , message, file: file, line: line)
193
- }
194
-
195
- public func XCTAssertLessThan< T : Comparable > ( @autoclosure expression1: ( ) -> T , @autoclosure _ expression2: ( ) -> T , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
196
- XCTAssert ( expression1 ( ) < expression2 ( ) , message, file: file, line: line)
197
- }
198
-
199
- public func XCTAssertLessThanOrEqual< T : Comparable > ( @autoclosure expression1: ( ) -> T , @autoclosure _ expression2: ( ) -> T , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
200
- XCTAssert ( expression1 ( ) <= expression2 ( ) , message, file: file, line: line)
201
- }
202
-
203
- public func XCTAssertNil( @autoclosure expression: ( ) -> Any ? , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
204
- XCTAssert ( expression ( ) == nil , message, file: file, line: line)
205
- }
206
-
207
- public func XCTAssertNotEqual< T : Equatable > ( @autoclosure expression1: ( ) -> T ? , @autoclosure _ expression2: ( ) -> T ? , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
208
- XCTAssert ( expression1 ( ) != expression2 ( ) , message, file: file, line: line)
209
- }
210
-
211
- public func XCTAssertNotEqual< T : Equatable > ( @autoclosure expression1: ( ) -> ContiguousArray < T > , @autoclosure _ expression2: ( ) -> ContiguousArray < T > , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
212
- XCTAssert ( expression1 ( ) != expression2 ( ) , message, file: file, line: line)
213
- }
214
-
215
- public func XCTAssertNotEqual< T : Equatable > ( @autoclosure expression1: ( ) -> ArraySlice < T > , @autoclosure _ expression2: ( ) -> ArraySlice < T > , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
216
- XCTAssert ( expression1 ( ) != expression2 ( ) , message, file: file, line: line)
217
- }
218
-
219
- public func XCTAssertNotEqual< T : Equatable > ( @autoclosure expression1: ( ) -> [ T ] , @autoclosure _ expression2: ( ) -> [ T ] , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
220
- XCTAssert ( expression1 ( ) != expression2 ( ) , message, file: file, line: line)
221
- }
222
-
223
- public func XCTAssertNotEqual< T, U : Equatable > ( @autoclosure expression1: ( ) -> [ T : U ] , @autoclosure _ expression2: ( ) -> [ T : U ] , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
224
- XCTAssert ( expression1 ( ) != expression2 ( ) , message, file: file, line: line)
225
- }
226
-
227
- public func XCTAssertNotEqualWithAccuracy< T : FloatingPointType > ( @autoclosure expression1: ( ) -> T , @autoclosure _ expression2: ( ) -> T , _ accuracy: T , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
228
- XCTAssert ( abs ( expression1 ( ) . distanceTo ( expression2 ( ) ) ) > abs ( accuracy. distanceTo ( T ( 0 ) ) ) , message, file: file, line: line)
229
- }
230
-
231
- public func XCTAssertNotNil( @autoclosure expression: ( ) -> Any ? , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
232
- XCTAssert ( expression ( ) != nil , message, file: file, line: line)
233
- }
234
-
235
- public func XCTAssertTrue( @autoclosure expression: ( ) -> BooleanType , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
236
- XCTAssert ( expression ( ) . boolValue, message, file: file, line: line)
237
- }
238
-
239
- public func XCTFail( message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
240
- XCTAssert ( false , message, file: file, line: line)
241
- }
0 commit comments