Created
March 14, 2017 07:52
-
-
Save lightsprint09/b4c42695d90472dd459f48b34ae11ef9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Train { | |
| let name: String | |
| let date: Date | |
| } | |
| extension Train: Equatable { | |
| static func ==(lhs: Train, rhs: Train) -> Bool { | |
| return lhs.name == rhs.name | |
| } | |
| } | |
| class TrainTest: XCTestCase { | |
| func testEquatableWithDump() { | |
| //Given | |
| let ice = Train(name: "ICE", date: Date(timeIntervalSinceNow: 20)) | |
| let ice2 = Train(name: "ICE", date: Date(timeIntervalSinceNow: 30)) | |
| //When | |
| AssertEqualWithDump(ice, ice2) | |
| } | |
| } | |
| public func AssertEqualWithDump<T : Equatable>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) { | |
| guard let lhs = try? expression1(), let rhs = try? expression2() else { | |
| XCTFail() | |
| return | |
| } | |
| if lhs == rhs { | |
| assertDumpsEqual(lhs, rhs, file: file, line: line) | |
| } else { | |
| XCTFail() | |
| } | |
| } | |
| //From https://oleb.net/blog/2017/03/dump-as-equatable-safeguard/ | |
| func assertDumpsEqual<T>(_ lhs: @autoclosure () -> T, | |
| _ rhs: @autoclosure () -> T, | |
| file: StaticString = #file, line: UInt = #line) { | |
| XCTAssert(String(dumping: lhs()) == String(dumping: rhs()), | |
| "Expected dumps to be equal.", | |
| file: file, line: line) | |
| } | |
| extension String { | |
| /** | |
| Creates a string from the `dump` output of the | |
| given value. | |
| */ | |
| init<T>(dumping x: T) { | |
| self.init() | |
| dump(x, to: &self) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thats for the detailed reply :)
The test is correct but it should fail. This tests should detect that the implementation is incorrect.