From 958457cc323a91a8cf04a486586fd7fa13b2ae74 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Tue, 17 Dec 2024 17:13:57 -0500 Subject: [PATCH] Move collection diffing type-erasure to an init --- .../Expectations/ExpectationContext.swift | 24 ++----------------- .../CollectionDifferenceAdditions.swift | 22 +++++++++++++++++ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Sources/Testing/Expectations/ExpectationContext.swift b/Sources/Testing/Expectations/ExpectationContext.swift index ac136cb6d..2bde68612 100644 --- a/Sources/Testing/Expectations/ExpectationContext.swift +++ b/Sources/Testing/Expectations/ExpectationContext.swift @@ -222,26 +222,6 @@ extension __ExpectationContext { // MARK: - Collection comparison and diffing extension __ExpectationContext { - /// Convert an instance of `CollectionDifference` to one that is type-erased - /// over elements of type `Any`. - /// - /// - Parameters: - /// - difference: The difference to convert. - /// - /// - Returns: A type-erased copy of `difference`. - private static func _typeEraseCollectionDifference(_ difference: CollectionDifference) -> CollectionDifference { - CollectionDifference( - difference.lazy.map { change in - switch change { - case let .insert(offset, element, associatedWith): - return .insert(offset: offset, element: element as Any, associatedWith: associatedWith) - case let .remove(offset, element, associatedWith): - return .remove(offset: offset, element: element as Any, associatedWith: associatedWith) - } - } - )! - } - /// Generate a description of a previously-computed collection difference. /// /// - Parameters: @@ -317,7 +297,7 @@ extension __ExpectationContext { if !result { differences[opID] = { [lhs, rhs] in - Self._typeEraseCollectionDifference(lhs.difference(from: rhs)) + CollectionDifference(lhs.difference(from: rhs)) } } @@ -380,7 +360,7 @@ extension __ExpectationContext { return nil } - return Self._typeEraseCollectionDifference(diff) + return CollectionDifference(diff) } } diff --git a/Sources/Testing/Support/Additions/CollectionDifferenceAdditions.swift b/Sources/Testing/Support/Additions/CollectionDifferenceAdditions.swift index 94a09b14c..df942fbca 100644 --- a/Sources/Testing/Support/Additions/CollectionDifferenceAdditions.swift +++ b/Sources/Testing/Support/Additions/CollectionDifferenceAdditions.swift @@ -8,6 +8,28 @@ // See https://swift.org/CONTRIBUTORS.txt for Swift project authors // +extension CollectionDifference { + /// Convert an instance of `CollectionDifference` to one that is type-erased + /// over elements of type `Any`. + /// + /// - Parameters: + /// - difference: The difference to convert. + init(_ difference: CollectionDifference) { + self.init( + difference.lazy.map { change in + switch change { + case let .insert(offset, element, associatedWith): + return .insert(offset: offset, element: element as Any, associatedWith: associatedWith) + case let .remove(offset, element, associatedWith): + return .remove(offset: offset, element: element as Any, associatedWith: associatedWith) + } + } + )! + } +} + +// MARK: - + extension CollectionDifference.Change { /// The element that was changed. var element: ChangeElement {