Skip to content

Commit

Permalink
Fixing bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
winseros committed Dec 30, 2023
1 parent ca8ee51 commit 2d2bfc5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
38 changes: 38 additions & 0 deletions pbom/domain/__test__/pbonode_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,44 @@ namespace pboman3::domain::test {
ASSERT_TRUE(e1Old.isNull());
}

TEST(PboNodeTest, CreateHierarchy2_Replaces_Conflicting_Folder_With_File) {
const auto root = QSharedPointer<PboNode>::create(QString("file-name"), PboNodeType::Container, nullptr);

root->createHierarchy(PboPath("f1/f2/e1"));
const QPointer e1Old = root->at(0)->at(0);

PboNode* e1New = root->createHierarchy(PboPath("f1/f2"), ConflictResolution::Replace);

ASSERT_EQ(root->at(0)->count(), 1);
ASSERT_EQ(root->at(0)->at(0), e1New);
ASSERT_TRUE(e1Old.isNull());

ASSERT_EQ(root->at(0)->title(), QString("f1"));
ASSERT_EQ(root->at(0)->nodeType(), PboNodeType::Folder);
ASSERT_EQ(root->at(0)->at(0)->title(), QString("f2"));
ASSERT_EQ(root->at(0)->at(0)->nodeType(), PboNodeType::File);
}

TEST(PboNodeTest, CreateHierarchy2_Replaces_Conflicting_File_With_Folder) {
const auto root = QSharedPointer<PboNode>::create(QString("file-name"), PboNodeType::Container, nullptr);

root->createHierarchy(PboPath("f1/e1"));
const QPointer e1Old = root->at(0)->at(0);

PboNode* e1New = root->createHierarchy(PboPath("f1/e1/e2"), ConflictResolution::Replace);

ASSERT_EQ(root->at(0)->count(), 1);
ASSERT_EQ(root->at(0)->at(0)->at(0), e1New);
ASSERT_TRUE(e1Old.isNull());

ASSERT_EQ(root->at(0)->title(), QString("f1"));
ASSERT_EQ(root->at(0)->nodeType(), PboNodeType::Folder);
ASSERT_EQ(root->at(0)->at(0)->title(), QString("e1"));
ASSERT_EQ(root->at(0)->at(0)->nodeType(), PboNodeType::Folder);
ASSERT_EQ(root->at(0)->at(0)->at(0)->title(), QString("e2"));
ASSERT_EQ(root->at(0)->at(0)->at(0)->nodeType(), PboNodeType::File);
}

TEST(PboNodeTest, CreateHierarchy2_Emits_Once_When_Creating_Folders) {
const auto root = QSharedPointer<PboNode>::create(QString("file-name"), PboNodeType::Container, nullptr);

Expand Down
1 change: 0 additions & 1 deletion pbom/domain/abstractnode.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <QSharedPointer>
#include <set>
#include <QHash>
#include "util/qpointerlistiterator.h"

Expand Down
28 changes: 15 additions & 13 deletions pbom/domain/pbonode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,22 @@ namespace pboman3::domain {
PboNode* PboNode::createHierarchy(const PboPath& entryPath, const ConflictResolution& onConflict,
bool emitEvents) {
PboNode* node = this;
for (qsizetype i = 0; i < entryPath.length() - 1; i++) {
PboNode* folder = node->getChild(entryPath.at(i)).get();
const auto last = --entryPath.end();
auto it = entryPath.begin();
while (it != last) {
PboNode* folder = node->getChild(*it).get();
if (!folder) {
folder = node->createNode(entryPath.at(i), PboNodeType::Folder);
folder = node->createNode(*it, PboNodeType::Folder);
} else if (folder->nodeType_ == PboNodeType::File) {
switch (onConflict) {
case ConflictResolution::Replace: {
const auto p = folder->parentNode_;
p->removeNode(folder->sharedFromThis());
folder = p->createNode(entryPath.last(), PboNodeType::File);
folder = p->createNode(*it, PboNodeType::Folder);
break;
}
case ConflictResolution::Copy: {
const QString folderTitle = pickFolderTitle(node, entryPath.at(i));
const QString folderTitle = node->pickFolderTitle(*it);
folder = node->createNode(folderTitle, PboNodeType::Folder);
break;
}
Expand All @@ -116,25 +118,25 @@ namespace pboman3::domain {
}
}
node = folder;
++it;
}

PboNode* file = node->getChild(entryPath.last()).get();
PboNode* file = node->getChild(*last).get();
if (!file) {
file = node->createNode(entryPath.last(), PboNodeType::File);
file = node->createNode(*last, PboNodeType::File);
if (emitEvents)
emitHierarchyChanged();
} else {
switch (onConflict) {
case ConflictResolution::Replace: {
const auto p = file->parentNode_;
p->removeNode(file->sharedFromThis());
file = p->createNode(entryPath.last(), PboNodeType::File);
node->removeNode(file->sharedFromThis());
file = node->createNode(*last, PboNodeType::File);
if (emitEvents)
emitHierarchyChanged();
break;
}
case ConflictResolution::Copy: {
const QString fileTitle = pickFileTitle(node, entryPath.last());
const QString fileTitle = node->pickFileTitle(*last);
file = node->createNode(fileTitle, PboNodeType::File);
if (emitEvents)
emitHierarchyChanged();
Expand All @@ -148,7 +150,7 @@ namespace pboman3::domain {
return file;
}

QString PboNode::pickFolderTitle(const PboNode* parent, const QString& expectedTitle) const {
QString PboNode::pickFolderTitle(const QString& expectedTitle) const {
int index = 1;
QString attemptTitle = FileNames::getCopyFolderName(expectedTitle, index);
while (hasChild(attemptTitle)) {
Expand All @@ -158,7 +160,7 @@ namespace pboman3::domain {
return attemptTitle;
}

QString PboNode::pickFileTitle(const PboNode* parent, const QString& expectedTitle) const {
QString PboNode::pickFileTitle(const QString& expectedTitle) const {
int index = 1;
QString attemptTitle = FileNames::getCopyFileName(expectedTitle, index);
while (hasChild(attemptTitle)) {
Expand Down
4 changes: 2 additions & 2 deletions pbom/domain/pbonode.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ namespace pboman3::domain {

PboNode* createHierarchy(const PboPath& entryPath, const ConflictResolution& onConflict, bool emitEvents);

QString pickFolderTitle(const PboNode* parent, const QString& expectedTitle) const;
QString pickFolderTitle(const QString& expectedTitle) const;

QString pickFileTitle(const PboNode* parent, const QString& expectedTitle) const;
QString pickFileTitle(const QString& expectedTitle) const;

PboNode* createNode(const QString& title, PboNodeType nodeType);

Expand Down

0 comments on commit 2d2bfc5

Please sign in to comment.