Skip to content

Commit

Permalink
XCTestAssertNoThrow (#184)
Browse files Browse the repository at this point in the history
* Added implementation of XCTAssetNoThrow to XCTAssert.swift and tests
  • Loading branch information
ArtSabintsev authored and briancroom committed Jan 20, 2017
1 parent 1f20d9b commit 50cc074
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
13 changes: 13 additions & 0 deletions Sources/XCTest/Public/XCTAssert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ private enum _XCTAssertion {
case `false`
case fail
case throwsError
case noThrow

var name: String? {
switch(self) {
Expand All @@ -41,6 +42,7 @@ private enum _XCTAssertion {
case .`true`: return "XCTAssertTrue"
case .`false`: return "XCTAssertFalse"
case .throwsError: return "XCTAssertThrowsError"
case .noThrow: return "XCTAssertNoThrow"
case .fail: return nil
}
}
Expand Down Expand Up @@ -421,3 +423,14 @@ public func XCTAssertThrowsError<T>(_ expression: @autoclosure () throws -> T, _
}
}
}

public func XCTAssertNoThrow<T>(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
_XCTEvaluateAssertion(.noThrow, message: message, file: file, line: line) {
do {
_ = try expression()
return .success
} catch let error {
return .expectedFailure("threw error \"\(error)\"")
}
}
}
25 changes: 22 additions & 3 deletions Tests/Functional/ErrorHandling/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class ErrorHandling: XCTestCase {

// Tests for throwing assertion expressions
("test_assertionExpressionCanThrow", test_assertionExpressionCanThrow),

// Tests for XCTAssertNoThrow
("test_shouldNotThrowErrorDefiningSuccess", test_shouldNotThrowErrorDefiningSuccess),
("test_shouldThrowErrorDefiningFailure", test_shouldThrowErrorDefiningFailure),
]
}()

Expand Down Expand Up @@ -103,13 +107,28 @@ class ErrorHandling: XCTestCase {
func test_assertionExpressionCanThrow() {
XCTAssertEqual(try functionThatShouldReturnButThrows(), 1)
}


// CHECK: Test Case 'ErrorHandling.test_shouldNotThrowErrorDefiningSuccess' started at \d+:\d+:\d+\.\d+
// CHECK: Test Case 'ErrorHandling.test_shouldNotThrowErrorDefiningSuccess' passed \(\d+\.\d+ seconds\)
func test_shouldNotThrowErrorDefiningSuccess() {
XCTAssertNoThrow(try functionThatDoesNotThrowError())
}

// CHECK: Test Case 'ErrorHandling.test_shouldThrowErrorDefiningFailure' started at \d+:\d+:\d+\.\d+
// CHECK: .*/ErrorHandling/main.swift:[[@LINE+3]]: error: ErrorHandling.test_shouldThrowErrorDefiningFailure : XCTAssertNoThrow failed: threw error "anError\("an error message"\)" -
// CHECK: Test Case 'ErrorHandling.test_shouldThrowErrorDefiningFailure' failed \(\d+\.\d+ seconds\)
func test_shouldThrowErrorDefiningFailure() {
XCTAssertNoThrow(try functionThatDoesThrowError())
}
}

// CHECK: Test Suite 'ErrorHandling' failed at \d+:\d+:\d+\.\d+
// CHECK: \t Executed 6 tests, with 4 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed \d+ tests, with \d+ failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds

XCTMain([testCase(ErrorHandling.allTests)])

// CHECK: Test Suite '.*\.xctest' failed at \d+:\d+:\d+\.\d+
// CHECK: \t Executed 6 tests, with 4 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed \d+ tests, with \d+ failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: Test Suite 'All tests' failed at \d+:\d+:\d+\.\d+
// CHECK: \t Executed 6 tests, with 4 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed \d+ tests, with \d+ failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds

0 comments on commit 50cc074

Please sign in to comment.