-
Notifications
You must be signed in to change notification settings - Fork 501
Fix failing duplicate identifier detection #375
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, we need to not only look in the |
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without the fix this will not throw |
||
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, "") | ||
]), | ||
|
@@ -332,7 +401,7 @@ extension AlgorithmTests { | |
] | ||
|
||
do { | ||
_ = try Diff.differencesForSectionedView(initialSections: initial, finalSections: initial) | ||
_ = try Diff.differencesForSectionedView(initialSections: [], finalSections: final) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without the fix this will not throw |
||
XCTFail("Should throw exception") | ||
} | ||
catch let exception { | ||
|
@@ -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, "") | ||
]), | ||
|
@@ -357,7 +451,7 @@ extension AlgorithmTests { | |
] | ||
|
||
do { | ||
_ = try Diff.differencesForSectionedView(initialSections: initial, finalSections: initial) | ||
_ = try Diff.differencesForSectionedView(initialSections: [], finalSections: final) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without the fix this will not throw |
||
XCTFail("Should throw exception") | ||
} | ||
catch let exception { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem here is that if the item was not present in the initial sections, we always
continue
and it's never checked if the finalSections contain items with duplicate identifiers!