|
| 1 | +import Foundation |
| 2 | + |
| 3 | +#if DEBUG && canImport(ObjectiveC) |
| 4 | + /// Instructs the test to expect a failure in an upcoming assertion, with options to customize |
| 5 | + /// expected failure checking and handling. |
| 6 | + /// |
| 7 | + /// - Parameters: |
| 8 | + /// - failureReason: An optional string that describes why the test expects a failure. |
| 9 | + /// - enabled: A Boolean value that indicates whether the test checks for the expected failure. |
| 10 | + /// - strict: A Boolean value that indicates whether the test reports an error if the expected |
| 11 | + /// failure doesn’t occur. |
| 12 | + /// - failingBlock: A block of test code and assertions where the test expects a failure. |
| 13 | + @_disfavoredOverload |
| 14 | + public func XCTExpectFailure<R>( |
| 15 | + _ failureReason: String? = nil, |
| 16 | + enabled: Bool? = nil, |
| 17 | + strict: Bool? = nil, |
| 18 | + failingBlock: () throws -> R, |
| 19 | + issueMatcher: ((_XCTIssue) -> Bool)? = nil |
| 20 | + ) rethrows -> R { |
| 21 | + guard enabled ?? true |
| 22 | + else { return try failingBlock() } |
| 23 | + guard |
| 24 | + let XCTExpectedFailureOptions = NSClassFromString("XCTExpectedFailureOptions") |
| 25 | + as Any as? NSObjectProtocol, |
| 26 | + let options = strict ?? true |
| 27 | + ? XCTExpectedFailureOptions |
| 28 | + .perform(NSSelectorFromString("alloc"))?.takeUnretainedValue() |
| 29 | + .perform(NSSelectorFromString("init"))?.takeUnretainedValue() |
| 30 | + : XCTExpectedFailureOptions |
| 31 | + .perform(NSSelectorFromString("nonStrictOptions"))?.takeUnretainedValue(), |
| 32 | + let functionBlockPointer = dlsym(dlopen(nil, RTLD_LAZY), "XCTExpectFailureWithOptionsInBlock") |
| 33 | + else { |
| 34 | + let errorString = dlerror().map { charPointer in String(cString: charPointer) } |
| 35 | + ?? "Unknown error" |
| 36 | + assertionFailure( |
| 37 | + "Failed to get symbol for XCTExpectFailureWithOptionsInBlock with error: \(errorString)." |
| 38 | + ) |
| 39 | + return try failingBlock() |
| 40 | + } |
| 41 | + |
| 42 | + if let issueMatcher = issueMatcher { |
| 43 | + let issueMatcher: @convention(block) (AnyObject) -> Bool = { issue in |
| 44 | + issueMatcher(_XCTIssue(issue)) |
| 45 | + } |
| 46 | + options.setValue(issueMatcher, forKey: "issueMatcher") |
| 47 | + } |
| 48 | + |
| 49 | + let XCTExpectFailureWithOptionsInBlock = unsafeBitCast( |
| 50 | + functionBlockPointer, |
| 51 | + to: (@convention(c) (String?, AnyObject, () -> Void) -> Void).self |
| 52 | + ) |
| 53 | + |
| 54 | + var result: Result<R, Error>! |
| 55 | + XCTExpectFailureWithOptionsInBlock(failureReason, options) { |
| 56 | + result = Result { try failingBlock() } |
| 57 | + } |
| 58 | + return try result._rethrowGet() |
| 59 | + } |
| 60 | + |
| 61 | + /// Instructs the test to expect a failure in an upcoming assertion, with options to customize |
| 62 | + /// expected failure checking and handling. |
| 63 | + /// |
| 64 | + /// - Parameters: |
| 65 | + /// - failureReason: An optional string that describes why the test expects a failure. |
| 66 | + /// - enabled: A Boolean value that indicates whether the test checks for the expected failure. |
| 67 | + /// - strict: A Boolean value that indicates whether the test reports an error if the expected |
| 68 | + /// failure doesn’t occur. |
| 69 | + @_disfavoredOverload |
| 70 | + public func XCTExpectFailure( |
| 71 | + _ failureReason: String? = nil, |
| 72 | + enabled: Bool? = nil, |
| 73 | + strict: Bool? = nil, |
| 74 | + issueMatcher: ((_XCTIssue) -> Bool)? = nil |
| 75 | + ) { |
| 76 | + guard enabled ?? true |
| 77 | + else { return } |
| 78 | + guard |
| 79 | + let XCTExpectedFailureOptions = NSClassFromString("XCTExpectedFailureOptions") |
| 80 | + as Any as? NSObjectProtocol, |
| 81 | + let options = strict ?? true |
| 82 | + ? XCTExpectedFailureOptions |
| 83 | + .perform(NSSelectorFromString("alloc"))?.takeUnretainedValue() |
| 84 | + .perform(NSSelectorFromString("init"))?.takeUnretainedValue() |
| 85 | + : XCTExpectedFailureOptions |
| 86 | + .perform(NSSelectorFromString("nonStrictOptions"))?.takeUnretainedValue(), |
| 87 | + let functionBlockPointer = dlsym(dlopen(nil, RTLD_LAZY), "XCTExpectFailureWithOptions") |
| 88 | + else { |
| 89 | + let errorString = dlerror().map { charPointer in String(cString: charPointer) } |
| 90 | + ?? "Unknown error" |
| 91 | + assertionFailure( |
| 92 | + "Failed to get symbol for XCTExpectFailureWithOptionsInBlock with error: \(errorString)." |
| 93 | + ) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + if let issueMatcher = issueMatcher { |
| 98 | + let issueMatcher: @convention(block) (AnyObject) -> Bool = { issue in |
| 99 | + issueMatcher(_XCTIssue(issue)) |
| 100 | + } |
| 101 | + options.setValue(issueMatcher, forKey: "issueMatcher") |
| 102 | + } |
| 103 | + |
| 104 | + let XCTExpectFailureWithOptions = unsafeBitCast( |
| 105 | + functionBlockPointer, |
| 106 | + to: (@convention(c) (String?, AnyObject) -> Void).self |
| 107 | + ) |
| 108 | + |
| 109 | + XCTExpectFailureWithOptions(failureReason, options) |
| 110 | + } |
| 111 | + |
| 112 | + public struct _XCTIssue: /*CustomStringConvertible, */Equatable, Hashable { |
| 113 | + public var type: IssueType |
| 114 | + public var compactDescription: String |
| 115 | + public var detailedDescription: String? |
| 116 | + |
| 117 | + // NB: This surface are has been left unimplemented for now. We can consider adopting more of it |
| 118 | + // in the future: |
| 119 | + // |
| 120 | + // var sourceCodeContext: XCTSourceCodeContext |
| 121 | + // var associatedError: Error? |
| 122 | + // var attachments: [XCTAttachment] |
| 123 | + // mutating func add(XCTAttachment) |
| 124 | + // |
| 125 | + // public var description: String { |
| 126 | + // """ |
| 127 | + // \(self.type.description) \ |
| 128 | + // at \ |
| 129 | + // \(self.sourceCodeContext.location.fileURL.lastPathComponent):\ |
| 130 | + // \(self.sourceCodeContext.location.lineNumber): \ |
| 131 | + // \(self.compactDescription) |
| 132 | + // """ |
| 133 | + // } |
| 134 | + |
| 135 | + init(_ issue: AnyObject) { |
| 136 | + self.type = IssueType(rawValue: issue.value(forKey: "type") as! Int)! |
| 137 | + self.compactDescription = issue.value(forKey: "compactDescription") as! String |
| 138 | + self.detailedDescription = issue.value(forKey: "detailedDescription") as? String |
| 139 | + } |
| 140 | + |
| 141 | + public enum IssueType: Int, Sendable { |
| 142 | + case assertionFailure = 0 |
| 143 | + case performanceRegression = 3 |
| 144 | + case system = 4 |
| 145 | + case thrownError = 1 |
| 146 | + case uncaughtException = 2 |
| 147 | + case unmatchedExpectedFailure = 5 |
| 148 | + |
| 149 | + var description: String { |
| 150 | + switch self { |
| 151 | + case .assertionFailure: |
| 152 | + return "Assertion Failure" |
| 153 | + case .performanceRegression: |
| 154 | + return "Performance Regression" |
| 155 | + case .system: |
| 156 | + return "System Error" |
| 157 | + case .thrownError: |
| 158 | + return "Thrown Error" |
| 159 | + case .uncaughtException: |
| 160 | + return "Uncaught Exception" |
| 161 | + case .unmatchedExpectedFailure: |
| 162 | + return "Unmatched ExpectedFailure" |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @rethrows |
| 169 | + private protocol _ErrorMechanism { |
| 170 | + associatedtype Output |
| 171 | + func get() throws -> Output |
| 172 | + } |
| 173 | + extension _ErrorMechanism { |
| 174 | + func _rethrowError() rethrows -> Never { |
| 175 | + _ = try _rethrowGet() |
| 176 | + fatalError() |
| 177 | + } |
| 178 | + @usableFromInline |
| 179 | + func _rethrowGet() rethrows -> Output { |
| 180 | + return try get() |
| 181 | + } |
| 182 | + } |
| 183 | + extension Result: _ErrorMechanism {} |
| 184 | +#endif |
0 commit comments