Skip to content

Commit

Permalink
Remove the comparison of id (KeyPath) in IdentifiedArray Equatable im…
Browse files Browse the repository at this point in the history
…plementation (#25)

Resolves issue at:
#24 (comment)
  • Loading branch information
sroche27r authored Oct 8, 2021
1 parent 90628a5 commit dd7402f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extension IdentifiedArray: Equatable where Element: Equatable {
@inlinable
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.id == rhs.id && lhs.elementsEqual(rhs)
lhs.elementsEqual(rhs)
}
}
38 changes: 38 additions & 0 deletions Tests/IdentifiedCollectionsTests/IdentifiedArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,42 @@ final class IdentifiedArrayTests: XCTestCase {
array.remove(atOffsets: [0, 2])
XCTAssertEqual(array, [2])
}
func testEquatable() {
struct Foo: Identifiable, Equatable {
var id: String = "id"
var value: String = "value"
}
// Create arrays using all of the initializers
var arrays: [IdentifiedArray<String, Foo>] = [
IdentifiedArray<String, Foo>(),
IdentifiedArray<String, Foo>(uncheckedUniqueElements: [], id: \.id),
IdentifiedArray<String, Foo>(uniqueElements: [], id: \.id),
IdentifiedArray<String, Foo>(uncheckedUniqueElements: []),
IdentifiedArray<String, Foo>(uniqueElements: [])
]
arrays.forEach({ lhs in
arrays.forEach({ rhs in
XCTAssertEqual(lhs, rhs)
})
})
// add an element to each array
arrays.indices.forEach({
arrays[$0].append(Foo())
})
arrays.forEach({ lhs in
arrays.forEach({ rhs in
XCTAssertEqual(lhs, rhs)
})
})
// modify all arrays
arrays.indices.forEach({
arrays[$0].append(Foo(id: "id2", value: "\($0)"))
})
arrays.enumerated().forEach({ lhsIndex, lhs in
arrays.enumerated().forEach({ rhsIndex, rhs in
guard rhsIndex != lhsIndex else { return }
XCTAssertNotEqual(lhs, rhs)
})
})
}
}

0 comments on commit dd7402f

Please sign in to comment.