Skip to content

Commit

Permalink
[SwiftLint] Remove force_try from disabled_rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesyo committed Jul 23, 2018
1 parent d7471bb commit 9abb08e
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 12 deletions.
1 change: 0 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
disabled_rules:
- todo
- force_try

included:
- Sources
Expand Down
17 changes: 14 additions & 3 deletions Sources/Nimble/Adapters/AssertionRecorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,26 @@ public class AssertionRecorder: AssertionHandler {
/// Once the closure finishes, then the original Nimble assertion handler is restored.
///
/// @see AssertionHandler
public func withAssertionHandler(_ tempAssertionHandler: AssertionHandler, closure: () throws -> Void) {
public func withAssertionHandler(_ tempAssertionHandler: AssertionHandler,
file: FileString = #file,
line: UInt = #line,
closure: () throws -> Void) {
let environment = NimbleEnvironment.activeInstance
let oldRecorder = environment.assertionHandler
let capturer = NMBExceptionCapture(handler: nil, finally: ({
environment.assertionHandler = oldRecorder
}))
environment.assertionHandler = tempAssertionHandler
capturer.tryBlock {
try! closure()

do {
try capturer.tryBlockThrows {
try closure()
}
} catch {
let failureMessage = FailureMessage()
failureMessage.stringValue = "unexpected error thrown: <\(error)>"
let location = SourceLocation(file: file, line: line)
tempAssertionHandler.assert(false, message: failureMessage, location: location)
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/Nimble/Adapters/NMBExpectation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ internal struct ObjCMatcherWrapper: Matcher {

func matches(_ actualExpression: Expression<NSObject>, failureMessage: FailureMessage) -> Bool {
return matcher.matches(
// swiftlint:disable:next force_try
({ try! actualExpression.evaluate() }),
failureMessage: failureMessage,
location: actualExpression.location)
}

func doesNotMatch(_ actualExpression: Expression<NSObject>, failureMessage: FailureMessage) -> Bool {
return matcher.doesNotMatch(
// swiftlint:disable:next force_try
({ try! actualExpression.evaluate() }),
failureMessage: failureMessage,
location: actualExpression.location)
Expand Down
1 change: 1 addition & 0 deletions Sources/Nimble/Matchers/AllPass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ extension NMBObjCMatcher {
} else {
let failureMessage = FailureMessage()
let result = matcher.matches(
// swiftlint:disable:next force_try
({ try! expr.evaluate() }),
failureMessage: failureMessage,
location: expr.location
Expand Down
26 changes: 23 additions & 3 deletions Sources/Nimble/Matchers/RaisesException.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ import Foundation
// This matcher requires the Objective-C, and being built by Xcode rather than the Swift Package Manager
#if (os(macOS) || os(iOS) || os(tvOS) || os(watchOS)) && !SWIFT_PACKAGE

extension NMBExceptionCapture {
internal func tryBlockThrows(_ unsafeBlock: () throws -> Void) throws {
var catchedError: Error?
tryBlock {
do {
try unsafeBlock()
} catch {
catchedError = error
}
}
if let error = catchedError {
throw error
}
}
}

/// A Nimble matcher that succeeds when the actual expression raises an
/// exception with the specified name, reason, and/or userInfo.
///
Expand All @@ -24,9 +40,13 @@ public func raiseException(
exception = e
}), finally: nil)

capture.tryBlock {
_ = try! actualExpression.evaluate()
return
do {
try capture.tryBlockThrows {
_ = try actualExpression.evaluate()
}
} catch {
failureMessage.stringValue = "unexpected error thrown: <\(error)>"
return false
}

setFailureMessageForException(
Expand Down
8 changes: 6 additions & 2 deletions Sources/Nimble/Matchers/SatisfyAllOf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ extension NMBObjCMatcher {
return predicate.satisfies({ try expression.evaluate() }, location: actualExpression.location).toSwift()
} else {
let failureMessage = FailureMessage()
// swiftlint:disable:next line_length
let success = matcher.matches({ try! expression.evaluate() }, failureMessage: failureMessage, location: actualExpression.location)
let success = matcher.matches(
// swiftlint:disable:next force_try
{ try! expression.evaluate() },
failureMessage: failureMessage,
location: actualExpression.location
)
return PredicateResult(bool: success, message: failureMessage.toExpectationMessage())
}
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/Nimble/Matchers/SatisfyAnyOf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ extension NMBObjCMatcher {
return predicate.satisfies({ try expression.evaluate() }, location: actualExpression.location).toSwift()
} else {
let failureMessage = FailureMessage()
// swiftlint:disable:next line_length
let success = matcher.matches({ try! expression.evaluate() }, failureMessage: failureMessage, location: actualExpression.location)
let success = matcher.matches(
// swiftlint:disable:next force_try
{ try! expression.evaluate() },
failureMessage: failureMessage,
location: actualExpression.location
)
return PredicateResult(bool: success, message: failureMessage.toExpectationMessage())
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/NimbleTests/Helpers/utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func failsWithErrorMessage(_ messages: [String], file: FileString = #file, line:
var lineNumber = line

let recorder = AssertionRecorder()
withAssertionHandler(recorder, closure: closure)
withAssertionHandler(recorder, file: file, line: line, closure: closure)

for msg in messages {
var lastFailure: AssertionRecord?
Expand Down

0 comments on commit 9abb08e

Please sign in to comment.