Skip to content

Commit 1691786

Browse files
authored
Merge pull request #71477 from mikeash/externalgenericmetadatabuilder-test-more-lenient
[Test] Make the ExternalGenericMetadataBuilder test more lenient with equality.
2 parents d82b836 + d49cb49 commit 1691786

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

test/ExternalGenericMetadataBuilder/ExternalGenericMetadataBuilder.swift

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,43 @@ ExternalGenericMetadataBuilderTests.test("JSON output") {
8080
let outputJSONObject = try! JSONSerialization.jsonObject(with: outputJSON!.data(using: .utf8)!)
8181
let expectedJSONObject = try! JSONSerialization.jsonObject(with: expectedJSON.data(using: .utf8)!)
8282

83-
let outputJSONDictionary = outputJSONObject as? NSDictionary
83+
// Before comparing the JSONs, strip out things that might not be consistent
84+
// from one build to the next. In particular, pointer targets with large
85+
// addends are things that will depend on the specific layout of data within
86+
// the binary, because we've ended up referring to an adjacent symbol, so we
87+
// should replace those with something generic.
88+
func prepareForComparison(_ value: Any) -> Any {
89+
if let array = value as? [Any] {
90+
return array.map(prepareForComparison)
91+
}
92+
93+
if let dictionary = value as? [String: Any] {
94+
// See if this dictionary contains a large addend.
95+
if let addend = dictionary["addend"] as? Int64 {
96+
if !(-8...8).contains(addend) {
97+
// Return a placeholder value that will always match.
98+
return "Target with large addend removed."
99+
}
100+
}
101+
102+
return dictionary.mapValues(prepareForComparison)
103+
}
104+
return value;
105+
}
106+
107+
let outputJSONPrepped = prepareForComparison(outputJSONObject)
108+
let expectedJSONPrepped = prepareForComparison(expectedJSONObject)
109+
110+
let outputJSONDictionary = outputJSONPrepped as? NSDictionary
84111
expectNotNil(outputJSONDictionary)
85-
let expectedJSONDictionary = expectedJSONObject as? NSDictionary
112+
let expectedJSONDictionary = expectedJSONPrepped as? NSDictionary
86113
expectNotNil(expectedJSONDictionary)
87114

88115
// Don't use expectEqual, as it will print the strings on one line with \n
89116
// escapes, which is unreadable here.
90117
expectTrue(outputJSONDictionary!.isEqual(expectedJSONDictionary),
91-
"Output JSON does not match expected:\n\(outputJSON!)")
118+
"Output JSON does not match expected:\n\(outputJSONDictionary!)" +
119+
"\nExpected:\n\(expectedJSONDictionary!)")
92120

93121
swift_externalMetadataBuilder_destroy(builder)
94122
}

0 commit comments

Comments
 (0)