How best do I compare to deeply nested Dictionaries for equality in a unit test? They are both of type [AnyHashable:Any]
. My first pass of this:
private func compareNestedDictionaries(_ expected: [AnyHashable:Any], _ actual: [AnyHashable:Any]) -> Void {
if expected.count != actual.count {
XCTFail("The two objects are not equal")
return
}
for (key, value) in expected {
var value1 = value
var value2 = actual[key]
let type1 = type(of: value1)
let type2 = type(of: value2)
value1 = value1 as? type1
value2 = value2 as? type1
if type1 == type2 {
if let value1 = value1 as? [AnyHashable:Any], let value2 = value2 as? [AnyHashable:Any] {
compareNestedDictionaries(value1, value2)
continue
} else {
XCTAssertEqual(value1, value2)
}
} else {
XCTFail("The two objects are not equal")
return
}
}
}