GWRodriguez GWRodriguez - 1 year ago 109
Swift Question

XCTest'ing a tuple

I am trying to build a unit test like so:

// region is a (Double, Double) tuple
XCTAssertEqual(region, (0.0, 200.0))

But Xcode is giving me an error:
Cannot invoke 'XCTAssertEqual' with an argument list of type ((Double, Double), (Double, Double))

Is there a different way to test tuples without extracting their members and testing individually?

Answer Source

XCTAssertEqual requires that the two parameters passed to it are Equatable, which you can see from the method signature. Note that expression1 returns T?, and T must be Equatable:

func XCTAssertEqual<T : Equatable>(_ expression1: @autoclosure () throws -> T?, _ expression2: @autoclosure () throws -> T?, _ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line)

But Swift tuples aren't Equatable, so you can't use them with XCTAssertEqual.

Tuples do have an == method — they just don't conform to the protocol — so you could do something like this:

let eql = region == (0.0, 200.0)

Or even:

XCTAssertTrue(region == (0.0, 200.0))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download