Skip to content

Commit

Permalink
Remove AssertionHandler.require., require matcher failures will be re…
Browse files Browse the repository at this point in the history
…corded as test assertion failures, which is what they are anyway.
  • Loading branch information
younata committed Dec 18, 2023
1 parent c629411 commit cf008d2
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 61 deletions.
1 change: 0 additions & 1 deletion Sources/Nimble/Adapters/AdapterProtocols.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// Protocol for the assertion handler that Nimble uses for all expectations.
public protocol AssertionHandler {
func assert(_ assertion: Bool, message: FailureMessage, location: SourceLocation)
func require(_ passed: Bool, message: FailureMessage, location: SourceLocation)
}

/// Global backing interface for assertions that Nimble creates.
Expand Down
6 changes: 0 additions & 6 deletions Sources/Nimble/Adapters/AssertionDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,4 @@ public class AssertionDispatcher: AssertionHandler {
handler.assert(assertion, message: message, location: location)
}
}

public func require(_ passed: Bool, message: FailureMessage, location: SourceLocation) {
for handler in handlers {
handler.require(passed, message: message, location: location)
}
}
}
16 changes: 2 additions & 14 deletions Sources/Nimble/Adapters/AssertionRecorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ public struct AssertionRecord: CustomStringConvertible {
/// The source location the expectation occurred on.
public let location: SourceLocation

public let issueType: IssueType

public var description: String {
return "AssertionRecord { success=\(success), message='\(message.stringValue)', location=\(location), issueType=\(issueType) }"
return "AssertionRecord { success=\(success), message='\(message.stringValue)', location=\(location) }"
}
}

Expand All @@ -33,17 +31,7 @@ public class AssertionRecorder: AssertionHandler {
AssertionRecord(
success: assertion,
message: message,
location: location,
issueType: .assertionFailure))
}

public func require(_ passed: Bool, message: FailureMessage, location: SourceLocation) {
assertions.append(
AssertionRecord(
success: passed,
message: message,
location: location,
issueType: .thrownError))
location: location))
}
}

Expand Down
40 changes: 2 additions & 38 deletions Sources/Nimble/Adapters/NimbleXCTestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ public class NimbleXCTestHandler: AssertionHandler {
recordFailure("\(message.stringValue)\n", location: location)
}
}

public func require(_ passed: Bool, message: FailureMessage, location: SourceLocation) {
if !passed {
recordFailure("\(message.stringValue)\n", issueType: .thrownError, location: location)
}
}
}

/// Alternative handler for Nimble. This assertion handler passes failures along
Expand All @@ -31,18 +25,6 @@ public class NimbleShortXCTestHandler: AssertionHandler {
recordFailure("\(msg)\n", location: location)
}
}

public func require(_ passed: Bool, message: FailureMessage, location: SourceLocation) {
if !passed {
let msg: String
if let actual = message.actualValue {
msg = "got: \(actual) \(message.postfixActual)"
} else {
msg = "expected \(message.to) \(message.postfixMessage)"
}
recordFailure("\(msg)\n", issueType: .thrownError, location: location)
}
}
}

/// Fallback handler in case XCTest is unavailable. This assertion handler will abort
Expand All @@ -51,10 +33,6 @@ class NimbleXCTestUnavailableHandler: AssertionHandler {
func assert(_ assertion: Bool, message: FailureMessage, location: SourceLocation) {
fatalError("XCTest is not available and no custom assertion handler was configured. Aborting.")
}

func require(_ passed: Bool, message: FailureMessage, location: SourceLocation) {
fatalError("XCTest is not available and no custom assertion handler was configured. Aborting.")
}
}

#if canImport(Darwin)
Expand Down Expand Up @@ -94,29 +72,15 @@ func isXCTestAvailable() -> Bool {
#endif
}

public enum IssueType {
case assertionFailure
case thrownError

#if canImport(Darwin)
var xctIssueType: XCTIssueReference.IssueType {
switch self {
case .assertionFailure: return .assertionFailure
case .thrownError: return .thrownError
}
}
#endif
}

public func recordFailure(_ message: String, issueType: IssueType = .assertionFailure, location: SourceLocation) {
public func recordFailure(_ message: String, location: SourceLocation) {
#if !canImport(Darwin)
XCTFail("\(message)", file: location.file, line: location.line)
#else
if let testCase = CurrentTestCaseTracker.sharedInstance.currentTestCase {
let line = Int(location.line)
let location = XCTSourceCodeLocation(filePath: location.file, lineNumber: line)
let sourceCodeContext = XCTSourceCodeContext(location: location)
let issue = XCTIssue(type: issueType.xctIssueType, compactDescription: message, sourceCodeContext: sourceCodeContext)
let issue = XCTIssue(type: .assertionFailure, compactDescription: message, sourceCodeContext: sourceCodeContext)
testCase.record(issue)
} else {
let msg = """
Expand Down
4 changes: 2 additions & 2 deletions Sources/Nimble/Requirement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public struct SyncRequirement<Value> {

public func verify(_ pass: Bool, _ message: FailureMessage, _ value: Value?) throws -> Value {
let handler = NimbleEnvironment.activeInstance.assertionHandler
handler.require(pass, message: message, location: expression.location)
handler.assert(pass, message: message, location: expression.location)
guard pass, let value else {
throw RequirementError(message: message.stringValue, location: self.location)
}
Expand Down Expand Up @@ -146,7 +146,7 @@ public struct AsyncRequirement<Value> {

public func verify(_ pass: Bool, _ message: FailureMessage, _ value: Value?) throws -> Value {
let handler = NimbleEnvironment.activeInstance.assertionHandler
handler.require(pass, message: message, location: expression.location)
handler.assert(pass, message: message, location: expression.location)
guard pass, let value else {
throw RequirementError(message: message.stringValue, location: self.location)
}
Expand Down

0 comments on commit cf008d2

Please sign in to comment.