Skip to content

Instantly share code, notes, and snippets.

@BalestraPatrick
Last active March 26, 2017 00:31
Show Gist options
  • Save BalestraPatrick/53a0727422a8659b4659a7abd172f9c1 to your computer and use it in GitHub Desktop.
Save BalestraPatrick/53a0727422a8659b4659a7abd172f9c1 to your computer and use it in GitHub Desktop.
let dictionary: [String: Any] = [
"color": "blue",
"width": 5.0,
]
func XCTAssertEqual(_ l: [String: Any], _ r: [String: Any]) {
l.keys.forEach { key in
if let left = l[key] as? String, let right = r[key] as? String {
XCTAssertEqual(left, right)
} else if let left = l[key] as? CGFloat, let right = r[key] as? CGFloat {
XCTAssertEqual(left, right)
} else if let left = l[key] as? Bool, let right = r[key] as? Bool {
XCTAssertEqual(left, right)
} else {
XCTFail()
}
// And on and on with all your supported types...
}
}
XCTAssertEqual(dictionary, dictionary)
@BalestraPatrick
Copy link
Author

@akosma That looks very good but doesn't work with nested arrays unfortunately.

@AndreiVidrasco
Copy link

AndreiVidrasco commented Mar 25, 2017

try this

func XCTAssertEqual(_ l: [String: Any], _ r: [String: Any]){
    XCTAssertEqual(NSDictionary(dictionary: l), NSDictionary(dictionary: r))
}

@BalestraPatrick
Copy link
Author

@AndreiVidrasco That should work but unfortunately it doesn't support nested objects, like I wrote before with AnyHashable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment