This is a Swift µframework providing simple, flexible assertions for XCTest in Swift.
Requires Swift 1.2 (Xcode 6.3)
Assert that an optional array is equal to another array:
let array: [Int]? = [1, 2, 3]
assert(maybeAnArray(), ==, [1, 2, 3])
If you tried to use XCTAssertEqual
, you’d have to unwrap the optional first:
XCTAssertEqual(array, [1, 2, 3])
// => error: value of optional type '[Int]?' not unwrapped; did you mean to use '!' or '?'?
This works great with optionals of other types, too:
let string: String? = "hello"
assert(string, ==, "hello")
Here, too, XCTAssertEqual
would force you to unwrap the optional:
XCTAssertEqual(string, "hello")
// => error: cannot find an overload for 'XCTAssertEqual' that accepts an argument list of type '(String?, String)'
You can also pass in methods:
let set: Set<Int>? = Set([1, 2, 3])
assert(set, Set.contains, 3)
And you can use the predicate form for any other test you might want to write:
let string: String? = ""
assert(string, { $0.isEmpty })
These last two are approximately equivalent to using Swift’s optional chaining, but you might find them handy.
-
Add this repo as a submodule in e.g.
External/Assertions
:git submodule add https://github.com/robrix/Assertions.git External/Assertions
-
Drag
Assertions.xcodeproj
into your.xcworkspace
/.xcodeproj
. -
Add
Assertions.framework
to your test target’sLink Binary With Libraries
build phase.