11// Inspired by https://github.com/groue/CombineExpectations
22import XCTest
33
4- #if canImport(Darwin) // needed for XCTIssue
54/// A XCTestCase subclass that can test its own failures.
65class FailureTestCase : XCTestCase {
76 private struct Failure : Hashable {
7+ #if canImport(Darwin)
88 let issue : XCTIssue
9-
9+
1010 func issue( prefix: String = " " ) -> XCTIssue {
1111 if prefix. isEmpty {
1212 return issue
@@ -20,24 +20,28 @@ class FailureTestCase: XCTestCase {
2020 attachments: issue. attachments)
2121 }
2222 }
23-
23+
2424 private var description : String {
2525 return issue. compactDescription
2626 }
27-
27+
2828 func hash( into hasher: inout Hasher ) {
2929 hasher. combine ( 0 )
3030 }
3131
3232 static func == ( lhs: Failure , rhs: Failure ) -> Bool {
3333 lhs. description. hasPrefix ( rhs. description) || rhs. description. hasPrefix ( lhs. description)
3434 }
35+ #endif
3536 }
3637
3738 private var recordedFailures : [ Failure ] = [ ]
3839 private var isRecordingFailures = false
3940
40- func assertFailure( _ prefixes: String ... , file: StaticString = #file, line: UInt = #line, _ execute: ( ) throws -> Void ) rethrows {
41+ func assertFailure( _ prefixes: String ... , file: StaticString = #file, line: UInt = #line, _ execute: ( ) throws -> Void ) throws {
42+ #if !canImport(Darwin)
43+ throw XCTSkip ( " XCTIssue unavailable on non-Darwin platforms " )
44+ #else
4145 let recordedFailures = try recordingFailures ( execute)
4246 if prefixes. isEmpty {
4347 if recordedFailures. isEmpty {
@@ -69,22 +73,24 @@ class FailureTestCase: XCTestCase {
6973 recordedFailures: recordedFailures,
7074 expectedFailures: expectedFailures)
7175 }
76+ #endif
7277 }
7378
7479 override func setUp( ) {
7580 super. setUp ( )
7681 isRecordingFailures = false
7782 recordedFailures = [ ]
7883 }
79-
84+
85+ #if canImport(Darwin)
8086 override func record( _ issue: XCTIssue ) {
8187 if isRecordingFailures {
8288 recordedFailures. append ( Failure ( issue: issue) )
8389 } else {
8490 super. record ( issue)
8591 }
8692 }
87-
93+
8894 private func recordingFailures( _ execute: ( ) throws -> Void ) rethrows -> [ Failure ] {
8995 let oldRecordingFailures = isRecordingFailures
9096 let oldRecordedFailures = recordedFailures
@@ -121,6 +127,7 @@ class FailureTestCase: XCTestCase {
121127 }
122128 }
123129 }
130+ #endif
124131}
125132
126133// MARK: - Tests
@@ -129,83 +136,82 @@ class FailureTestCaseTests: FailureTestCase {
129136 func testEmptyTest( ) {
130137 }
131138
132- func testExpectedAnyFailure( ) {
133- assertFailure {
139+ func testExpectedAnyFailure( ) throws {
140+ try assertFailure {
134141 XCTFail ( " foo " )
135142 }
136- assertFailure {
143+ try assertFailure {
137144 XCTFail ( " foo " )
138145 XCTFail ( " bar " )
139146 }
140147 }
141148
142- func testMissingAnyFailure( ) {
143- assertFailure ( " No failure did happen " ) {
144- assertFailure {
149+ func testMissingAnyFailure( ) throws {
150+ try assertFailure ( " No failure did happen " ) {
151+ try assertFailure {
145152 }
146153 }
147154 }
148155
149- func testExpectedFailure( ) {
150- assertFailure ( " failed - foo " ) {
156+ func testExpectedFailure( ) throws {
157+ try assertFailure ( " failed - foo " ) {
151158 XCTFail ( " foo " )
152159 }
153160 }
154161
155- func testExpectedFailureMatchesOnPrefix( ) {
156- assertFailure ( " failed - foo " ) {
162+ func testExpectedFailureMatchesOnPrefix( ) throws {
163+ try assertFailure ( " failed - foo " ) {
157164 XCTFail ( " foobarbaz " )
158165 }
159166 }
160167
161- func testOrderOfExpectedFailureIsIgnored( ) {
162- assertFailure ( " failed - foo " , " failed - bar " ) {
168+ func testOrderOfExpectedFailureIsIgnored( ) throws {
169+ try assertFailure ( " failed - foo " , " failed - bar " ) {
163170 XCTFail ( " foo " )
164171 XCTFail ( " bar " )
165172 }
166- assertFailure ( " failed - bar " , " failed - foo " ) {
173+ try assertFailure ( " failed - bar " , " failed - foo " ) {
167174 XCTFail ( " foo " )
168175 XCTFail ( " bar " )
169176 }
170177 }
171178
172- func testExpectedFailureCanBeRepeated( ) {
173- assertFailure ( " failed - foo " , " failed - foo " , " failed - bar " ) {
179+ func testExpectedFailureCanBeRepeated( ) throws {
180+ try assertFailure ( " failed - foo " , " failed - foo " , " failed - bar " ) {
174181 XCTFail ( " foo " )
175182 XCTFail ( " bar " )
176183 XCTFail ( " foo " )
177184 }
178185 }
179186
180- func testExactNumberOfRepetitionIsRequired( ) {
181- assertFailure ( " Failure did not happen: failed - foo " ) {
182- assertFailure ( " failed - foo " , " failed - foo " ) {
187+ func testExactNumberOfRepetitionIsRequired( ) throws {
188+ try assertFailure ( " Failure did not happen: failed - foo " ) {
189+ try assertFailure ( " failed - foo " , " failed - foo " ) {
183190 XCTFail ( " foo " )
184191 }
185192 }
186- assertFailure ( " failed - foo " ) {
187- assertFailure ( " failed - foo " , " failed - foo " ) {
193+ try assertFailure ( " failed - foo " ) {
194+ try assertFailure ( " failed - foo " , " failed - foo " ) {
188195 XCTFail ( " foo " )
189196 XCTFail ( " foo " )
190197 XCTFail ( " foo " )
191198 }
192199 }
193200 }
194201
195- func testUnexpectedFailure( ) {
196- assertFailure ( " Failure did not happen: failed - foo " ) {
197- assertFailure ( " failed - foo " ) {
202+ func testUnexpectedFailure( ) throws {
203+ try assertFailure ( " Failure did not happen: failed - foo " ) {
204+ try assertFailure ( " failed - foo " ) {
198205 }
199206 }
200207 }
201208
202- func testMissedFailure( ) {
203- assertFailure ( " failed - bar " ) {
204- assertFailure ( " failed - foo " ) {
209+ func testMissedFailure( ) throws {
210+ try assertFailure ( " failed - bar " ) {
211+ try assertFailure ( " failed - foo " ) {
205212 XCTFail ( " foo " )
206213 XCTFail ( " bar " )
207214 }
208215 }
209216 }
210217}
211- #endif
0 commit comments