Created
August 1, 2018 03:06
-
-
Save nh7a/955d459e355ca84adfdb10935df11cc7 to your computer and use it in GitHub Desktop.
This file contains 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
import XCTest | |
func XCTAssertEqual2(_ lhs: Any, _ rhs: Any, context: [String] = []) { | |
func displayStyle(_ value: Any) -> Mirror.DisplayStyle? { | |
return Mirror(reflecting: value).displayStyle | |
} | |
let arrL = Array(Mirror(reflecting: lhs).children).sorted { $0.label! < $1.label! } | |
let arrR = Array(Mirror(reflecting: rhs).children).sorted { $0.label! < $1.label! } | |
guard type(of: lhs) == type(of: rhs), arrL.count == arrR.count else { | |
return XCTFail("Types mismatched!") | |
} | |
for i in 0..<arrL.count { | |
if displayStyle(arrL[i].value) != nil { | |
var context = context | |
context.append(arrL[i].label!) | |
XCTAssertEqual2(arrL[i].value, arrR[i].value, context: context) | |
} else { | |
var error = false | |
switch (arrL[i].value, arrR[i].value) { | |
case (let l as String, let r as String): error = l != r | |
case (let l as Bool, let r as Bool): error = l != r | |
case (let l as Int, let r as Int): error = l != r | |
case (let l as UInt, let r as UInt): error = l != r | |
case (let l as Float, let r as Float): error = l != r | |
case (let l as Double, let r as Double): error = l != r | |
case (let l as CGFloat, let r as CGFloat): error = l != r | |
default: | |
XCTFail("Unknown Type!: \(type(of: arrL[i].value))") | |
} | |
if error { | |
XCTFail("Values mismatched: \(context.joined(separator: ".")).\(arrL[i].label!) / \"\(arrL[i].value)\" != \"\(arrR[i].value)\"") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment