@@ -80,15 +80,43 @@ ExternalGenericMetadataBuilderTests.test("JSON output") {
80
80
let outputJSONObject = try! JSONSerialization.jsonObject(with: outputJSON!.data(using: .utf8)!)
81
81
let expectedJSONObject = try! JSONSerialization.jsonObject(with: expectedJSON.data(using: .utf8)!)
82
82
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
84
111
expectNotNil(outputJSONDictionary)
85
- let expectedJSONDictionary = expectedJSONObject as? NSDictionary
112
+ let expectedJSONDictionary = expectedJSONPrepped as? NSDictionary
86
113
expectNotNil(expectedJSONDictionary)
87
114
88
115
// Don't use expectEqual, as it will print the strings on one line with \n
89
116
// escapes, which is unreadable here.
90
117
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!)")
92
120
93
121
swift_externalMetadataBuilder_destroy(builder)
94
122
}
0 commit comments