Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Sources/Nimble/Expectation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,27 @@ public struct Expectation<T> {
////////////////// NEW API /////////////////////

/// Tests the actual value using a matcher to match.
public func to(_ predicate: Predicate<T>, description: String? = nil) {
@discardableResult
public func to(_ predicate: Predicate<T>, description: String? = nil) -> Self {
let (pass, msg) = execute(expression, .toMatch, predicate, to: "to", description: description)
verify(pass, msg)
return self
}

/// Tests the actual value using a matcher to not match.
public func toNot(_ predicate: Predicate<T>, description: String? = nil) {
@discardableResult
public func toNot(_ predicate: Predicate<T>, description: String? = nil) -> Self {
let (pass, msg) = execute(expression, .toNotMatch, predicate, to: "to not", description: description)
verify(pass, msg)
return self
}

/// Tests the actual value using a matcher to not match.
///
/// Alias to toNot().
public func notTo(_ predicate: Predicate<T>, description: String? = nil) {
toNot(predicate, description: description)
@discardableResult
public func notTo(_ predicate: Predicate<T>, description: String? = nil) -> Self {
return toNot(predicate, description: description)
}

// see:
Expand Down
6 changes: 6 additions & 0 deletions Tests/NimbleTests/Matchers/ThrowAssertionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ final class ThrowAssertionTest: XCTestCase {
expect { () -> Int in fatalError() }.to(throwAssertion())
#endif
}

func testChainOnThrowAssertion() {
#if canImport(Darwin)
expect { () -> Int in return 5 }.toNot(throwAssertion()).to(equal(5))
#endif
}
}
6 changes: 5 additions & 1 deletion Tests/NimbleTests/Matchers/ThrowErrorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ final class ThrowErrorTest: XCTestCase {
func testNegativeMatchesWithClosure() {
let moduleName = "NimbleTests"
let innerFailureMessage = "expected to equal <foo>, got <\(moduleName).NimbleError>"
let closure = { (error: Error) in
let closure = { (error: Error) -> Void in
expect(error._domain).to(equal("foo"))
}

Expand Down Expand Up @@ -164,4 +164,8 @@ final class ThrowErrorTest: XCTestCase {

// swiftlint:enable unused_closure_parameter
}

func testChainOnThrowError() {
expect { () throws -> Int in return 5 }.toNot(throwError()).to(equal(5))
}
}
12 changes: 12 additions & 0 deletions Tests/NimbleTests/SynchronousTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,16 @@ final class SynchronousTest: XCTestCase {
expect(1).notTo(MatcherFunc { _, _ in false }.predicate)
expect(1).notTo(Predicate.simple("match") { _ in .doesNotMatch })
}

// MARK: Assertion chaining

func testChain() {
expect(2).toNot(equal(1)).to(equal(2)).notTo(equal(3))
}

func testChainFailOnFirstError() {
failsWithErrorMessage("expected to not equal <2>, got <2>") {
expect(2).toNot(equal(1)).toNot(equal(2)).to(equal(3))
}
}
Comment on lines +149 to +153
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the chain does not stop on first error; all chained assertions are executed. The behavior itself should be okay but the test should be tweaked a bit.

}