Skip to content

Commit a5a93d5

Browse files
author
David Ungar
committed
fix tests
1 parent fc28720 commit a5a93d5

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

Sources/SwiftDriver/IncrementalCompilation/DependencyKey.swift

+6
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ public struct FingerprintedExternalDependency: Hashable, Equatable, ExternalDepe
105105
}
106106
}
107107

108+
extension FingerprintedExternalDependency: CustomStringConvertible {
109+
public var description: String {
110+
"\(externalDependency) \(fingerprint.map {"fingerprint: \($0)"} ?? "no fingerprint")"
111+
}
112+
}
113+
108114
/// A `DependencyKey` carries all of the information necessary to uniquely
109115
/// identify a dependency node in the graph, and serves as a point of identity
110116
/// for the dependency graph's map from definitions to uses.

Tests/SwiftDriverTests/DependencyGraphSerializationTests.swift

+11-5
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,18 @@ class DependencyGraphSerializationTests: XCTestCase, ModuleDependencyGraphMocker
6666
deserializedNodes.insert($0)
6767
}
6868

69-
XCTAssertTrue(originalNodes == deserializedNodes,
70-
"Round trip failed! Symmetric difference - \(originalNodes.symmetricDifference(deserializedNodes))")
69+
failIfUnequalAcrossGraphs( (name: "originalNodes", originalNodes),
70+
(name: "deserializedNodes", deserializedNodes),
71+
whatFailed: "Round trip")
7172

72-
XCTAssertEqual(graph.nodeFinder.usesByDef, deserializedGraph.nodeFinder.usesByDef)
73-
XCTAssertEqual(graph.fingerprintedExternalDependencies,
74-
deserializedGraph.fingerprintedExternalDependencies)
73+
failIfUnequalAcrossGraphs( (name: "original usesByDef", graph.nodeFinder.usesByDef),
74+
(name: "deserialized usesByDef", deserializedGraph.nodeFinder.usesByDef),
75+
whatFailed: "Round trip",
76+
compareBy: {"\($0.0.description): \($0.1.map{$0.description}.sorted())"})
77+
78+
failIfUnequalAcrossGraphs( (name: "original fingerprintedExternalDependencies", graph.fingerprintedExternalDependencies),
79+
(name: "deserialized fingerprintedExternalDependencies", deserializedGraph.fingerprintedExternalDependencies),
80+
whatFailed: "Round trip")
7581
}
7682

7783
func testRoundTripFixtures() throws {

Tests/SwiftDriverTests/Helpers/MockingIncrementalCompilation.swift

+38
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,41 @@ extension Driver {
201201
}
202202
}
203203
}
204+
205+
// MARK: - Graph comparison
206+
/// Test for equality across graphs and fail descriptively if needed.
207+
/// String interning is relative to the containing graph, so much pass in compare transformation.
208+
func failIfUnequalAcrossGraphs<LHS: Sequence, RHS: Sequence>(
209+
_ lhs: (name: String, LHS),
210+
_ rhs: (name: String, RHS),
211+
whatFailed: String,
212+
compareBy: (LHS.Element) -> String
213+
)
214+
where LHS.Element == RHS.Element
215+
{
216+
func reportDescs(presentIn present: (name: String, descs: Set<String>),
217+
butAbsentIn absent: (name: String, descs: Set<String>)
218+
) {
219+
let delta = present.descs.subtracting(absent.descs)
220+
guard delta.isEmpty else {
221+
XCTFail("\(whatFailed) failed! Present in \(present.name) but absent in \(absent.name): \(delta.sorted())")
222+
return
223+
}
224+
}
225+
226+
let lDescs = (name: lhs.name, descs: Set(lhs.1.map(compareBy)))
227+
let rDescs = (name: rhs.name, descs: Set(rhs.1.map(compareBy)))
228+
229+
reportDescs(presentIn: lDescs, butAbsentIn: rDescs)
230+
reportDescs(presentIn: rDescs, butAbsentIn: lDescs)
231+
}
232+
233+
/// Shorthand for ``failIfUnequalAcrossGraphs(_:_:whatFailed:compareBy:)`` when elements are ``CustomStringConvertible``
234+
func failIfUnequalAcrossGraphs<LHS: Sequence, RHS: Sequence>(
235+
_ lhs: (name: String, LHS),
236+
_ rhs: (name: String, RHS),
237+
whatFailed: String)
238+
where LHS.Element: CustomStringConvertible, LHS.Element == RHS.Element
239+
{
240+
failIfUnequalAcrossGraphs(lhs, rhs, whatFailed: whatFailed) {$0.description}
241+
}

0 commit comments

Comments
 (0)