Skip to content

Commit

Permalink
Merge pull request #4 from antitypical/test-assertion-failures
Browse files Browse the repository at this point in the history
Test assertion failures
  • Loading branch information
robrix committed Feb 28, 2015
2 parents e973aae + fec057f commit 19bac03
Showing 1 changed file with 85 additions and 3 deletions.
88 changes: 85 additions & 3 deletions AssertionsTests/AssertionsTests.swift
Original file line number Diff line number Diff line change
@@ -1,38 +1,120 @@
// Copyright (c) 2015 Rob Rix. All rights reserved.

import Assertions
import XCTest

final class AssertionsTests: XCTestCase {
func testAssertingWithOptionalArrays() {
let array: [Int]? = [1, 2, 3]
assert(array, ==, [1, 2, 3])
// XCTAssertEqual(array, [1, 2, 3]) // => error: value of optional type '[Int]?' not unwrapped; did you mean to use '!' or '?'?
}

func testAssertingWithOptionalArraysFailure() {
let array: [Int]? = [1, 2, 3]
assertFailure {
assert(array, ==, [1, 2, 3, 4])
}
}


func testAssertingWithOptionalStrings() {
let string: String? = "hello"
assert(string, ==, "hello")
// XCTAssertEqual(string, "hello") // => error: cannot find an overload for 'XCTAssertEqual' that accepts an argument list of type '(String?, String)'
}

func testAssertingWithOptionalStringsFailure() {
let string: String? = "hello"
assertFailure {
assert(string, ==, "hello!")
}
}


func testAssertingWithMethods() {
let set: Set<Int>? = Set([1, 2, 3])
assert(set, Set.contains, 3)
}

func testAssertingWithMethodsFailure() {
let set: Set<Int>? = Set([1, 2, 3])
assertFailure {
assert(set, Set.contains, 4)
}
}


func testAssertingPropertyWithPredicate() {
let string: String? = ""
assert(string, { $0.isEmpty })
}

func testAssertingPropertyWithPredicateFailure() {
let string: String? = "1"
assertFailure {
assert(string, { $0.isEmpty })
}
}


func testAssertingNilOfEquatableType() {
let x: Int? = nil
assert(x, ==, nil)
}

func testAssertingNilOfEquatableTypeFailure() {
let x: Int? = 1
assertFailure {
assert(x, ==, nil)
}
}

func testAssertingNonNilOfEquatableType() {
let x: Int? = 1
assert(x, !=, nil)
}

func testAssertingNonNilOfEquatableTypeFailure() {
let x: Int? = nil
assertFailure(x, !=, nil)
}


// MARK: Testing assertion failures

/// Assert that `test` causes an assertion failure.
func assertFailure(file: String = __FILE__, line: UInt = __LINE__, @noescape _ test: () -> ()) -> (message: String, file: String, line: UInt, expected: Bool)? {
let previous = expectFailure
expectFailure = true
test()
expectFailure = previous
if let result = failure {
return result
} else {
XCTFail("expected the assertion to fail", file: file, line: line)
return nil
}
}

/// Workaround for http://www.openradar.me/19996972
func assertFailure<T>(@autoclosure expression1: () -> T, _ test: (T, T) -> Bool, @autoclosure _ expression2: () -> T, message: String = "", file: String = __FILE__, line: UInt = __LINE__) -> (message: String, file: String, line: UInt, expected: Bool)? {
return assertFailure {
assert(expression1(), test, expression2(), message: message, file: file, line: line)
}
}

private var expectFailure: Bool = false
private var failure: (message: String, file: String, line: UInt, expected: Bool)? = nil

override func recordFailureWithDescription(message: String!, inFile file: String!, atLine line: UInt, expected: Bool) {
if expectFailure {
failure = (message: message, file: file, line: line, expected: expected)
} else {
super.recordFailureWithDescription(message, inFile: file, atLine: line, expected: expected)
}
}
}


// MARK: - Imports

import Assertions
import XCTest

0 comments on commit 19bac03

Please sign in to comment.