Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing duplicate identifier detection #390

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.

* Adds support of mutable CellViewModels.
* Changes `TableViewSectionedDataSource` default parameters `canEditRowAtIndexPath` and `canMoveRowAtIndexPath` to align with iOS default behavior #383
* Fixes failing duplicate identity detection

## [4.0.1](https://github.com/RxSwiftCommunity/RxDataSources/releases/tag/4.0.1)

Expand Down
16 changes: 16 additions & 0 deletions Sources/Differentiator/Diff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,19 @@ public enum Diff {
dictionary[key] = i
}

var finalKeys = Set<OptimizedIdentity<Identity>>()

for (i, items) in finalItemCache.enumerated() {
for j in 0 ..< items.count {
let item = items[j]
var identity = item.identity
let key = OptimizedIdentity(&identity)

if finalKeys.contains(key) {
throw Error.duplicateItem(item: item)
}
finalKeys.insert(key)

guard let initialItemPathIndex = dictionary[key] else {
continue
}
Expand Down Expand Up @@ -518,8 +526,16 @@ public enum Diff {
var initialSectionData = ContiguousArray<SectionAssociatedData>(repeating: SectionAssociatedData.initial, count: initialSections.count)
var finalSectionData = ContiguousArray<SectionAssociatedData>(repeating: SectionAssociatedData.initial, count: finalSections.count)

var finalSectionIdentities = Set<Section.Identity>()

for (i, section) in finalSections.enumerated() {
finalSectionData[i].itemCount = finalSections[i].items.count

if finalSectionIdentities.contains(section.identity) {
throw Error.duplicateSection(section: section)
}
finalSectionIdentities.insert(section.identity)

guard let initialSectionIndex = initialSectionIndexes[section.identity] else {
continue
}
Expand Down
106 changes: 100 additions & 6 deletions Tests/RxDataSourcesTests/AlgorithmTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,77 @@ extension AlgorithmTests {

// errors
extension AlgorithmTests {
func testThrowsErrorOnDuplicateItem() {
let initial: [s] = [
func testThrowsErrorOnDuplicateItem_inInitialSections() {
let sections: [s] = [
s(1, [
i(1111, ""),
i(1111, "")
])
]

do {
_ = try Diff.differencesForSectionedView(initialSections: sections, finalSections: sections)
XCTFail("Should throw exception")
}
catch let exception {
guard case let .duplicateItem(item) = exception as! Diff.Error else {
XCTFail("Not required error")
return
}

XCTAssertEqual(item as! i, i(1111, ""))
}
}

func testThrowsErrorOnDuplicateItem_inFinalSections() {
let final: [s] = [
s(1, [
i(1111, ""),
i(1111, "")
])
]

do {
_ = try Diff.differencesForSectionedView(initialSections: [], finalSections: final)
XCTFail("Should throw exception")
}
catch let exception {
guard case let .duplicateItem(item) = exception as! Diff.Error else {
XCTFail("Not required error")
return
}

XCTAssertEqual(item as! i, i(1111, ""))
}
}

func testThrowsErrorOnDuplicateItemInDifferentSection_inInitialSections() {
let sections: [s] = [
s(1, [
i(1111, "")
]),
s(2, [
i(1111, "")
])

]

do {
_ = try Diff.differencesForSectionedView(initialSections: sections, finalSections: sections)
XCTFail("Should throw exception")
}
catch let exception {
guard case let .duplicateItem(item) = exception as! Diff.Error else {
XCTFail("Not required error")
return
}

XCTAssertEqual(item as! i, i(1111, ""))
}
}

func testThrowsErrorOnDuplicateItemInDifferentSection_inFinalSections() {
let final: [s] = [
s(1, [
i(1111, "")
]),
Expand All @@ -332,7 +401,7 @@ extension AlgorithmTests {
]

do {
_ = try Diff.differencesForSectionedView(initialSections: initial, finalSections: initial)
_ = try Diff.differencesForSectionedView(initialSections: [], finalSections: final)
XCTFail("Should throw exception")
}
catch let exception {
Expand All @@ -345,8 +414,33 @@ extension AlgorithmTests {
}
}

func testThrowsErrorOnDuplicateSection() {
let initial: [s] = [
func testThrowsErrorOnDuplicateSection_inInitialSections() {
let sections: [s] = [
s(1, [
i(1111, "")
]),
s(1, [
i(1112, "")
])
]

do {
_ = try Diff.differencesForSectionedView(initialSections: sections, finalSections: sections)
XCTFail("Should throw exception")
}
catch let exception {
guard case let .duplicateSection(section) = exception as! Diff.Error else {
XCTFail("Not required error")
return
}

XCTAssertEqual(section as! s, s(1, [
i(1112, "")
]))
}
}
func testThrowsErrorOnDuplicateSection_inFinalSections() {
let final: [s] = [
s(1, [
i(1111, "")
]),
Expand All @@ -357,7 +451,7 @@ extension AlgorithmTests {
]

do {
_ = try Diff.differencesForSectionedView(initialSections: initial, finalSections: initial)
_ = try Diff.differencesForSectionedView(initialSections: [], finalSections: final)
XCTFail("Should throw exception")
}
catch let exception {
Expand Down