Skip to content

Commit

Permalink
Ensure that root bookmark folder comparison works when one folder has… (
Browse files Browse the repository at this point in the history
#2354)

Task/Issue URL: https://app.asana.com/0/0/1206762730363884/f

**Description**:
In some cases `BookmarkFolder`s that are added to the root folder have `parentFolderUUID` equal to `nil` in some cases they have it equal to the root identifier `bookmarks_root`. Ensure that equality is satisfied when this happens.
This bug started when we changed hashable/equatable.
  • Loading branch information
alessandroboron authored Mar 11, 2024
1 parent d76c00b commit d8bb6e4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
14 changes: 13 additions & 1 deletion DuckDuckGo/Bookmarks/Model/Bookmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ final class BookmarkFolder: BaseBookmarkEntity {
return id == folder.id &&
title == folder.title &&
isFolder == folder.isFolder &&
parentFolderUUID == folder.parentFolderUUID &&
isParentFolderEqual(lhs: parentFolderUUID, rhs: folder.parentFolderUUID) &&
children == folder.children
}

Expand All @@ -166,6 +166,18 @@ final class BookmarkFolder: BaseBookmarkEntity {
hasher.combine(children)
}

// In some cases a bookmark folder that is child of the root folder has its `parentFolderUUID` set to `bookmarks_root`. In some other cases is nil. Making sure that comparing a `nil` and a `bookmarks_root` does not return false. Probably would be good idea to remove the optionality of `parentFolderUUID` in the future and set it to `bookmarks_root` when needed.
private func isParentFolderEqual(lhs: String?, rhs: String?) -> Bool {
switch (lhs, rhs) {
case (.none, .none):
return true
case (.some(let lhsValue), .some(let rhsValue)):
return lhsValue == rhsValue
case (.some(let value), .none), (.none, .some(let value)):
return value == "bookmarks_root"
}
}

}

final class Bookmark: BaseBookmarkEntity {
Expand Down
24 changes: 24 additions & 0 deletions UnitTests/Bookmarks/Model/BaseBookmarkEntityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ final class BaseBookmarkEntityTests: XCTestCase {
XCTAssertFalse(result)
}

func testTwoBookmarkFoldersAddedToRootFolderReturnTrueWhenLeftParentIsBookmarksRootAndRightIsNil() {
// GIVEN
let lhs = BookmarkFolder(id: "1", title: "A", parentFolderUUID: "bookmarks_root", children: [])
let rhs = BookmarkFolder(id: "1", title: "A", parentFolderUUID: nil, children: [])

// WHEN
let result = lhs == rhs

// THEN
XCTAssertTrue(result)
}

func testTwoBookmarkFoldersAddedToRootFolderReturnTrueWhenLeftParentIsNilAndRightParentIsRootBookmarks() {
// GIVEN
let lhs = BookmarkFolder(id: "1", title: "A", parentFolderUUID: nil, children: [])
let rhs = BookmarkFolder(id: "1", title: "A", parentFolderUUID: "bookmarks_root", children: [])

// WHEN
let result = lhs == rhs

// THEN
XCTAssertTrue(result)
}

// MARK: - Base Entity

func testDifferentBookmarkEntitiesReturnFalseWhenIsEqualCalled() {
Expand Down

0 comments on commit d8bb6e4

Please sign in to comment.