From bbc0734ae5ee9276a89709b97494a59653e57acd Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Thu, 11 Jan 2018 23:20:46 +0100 Subject: [PATCH] Working on tests of the Tree --- CMakeLists.txt | 2 + src/core/Tree.h | 18 +++++-- src/core/TreeNode.h | 58 ++++++++++++++++------- src/core/control/AFieldFactory.cpp | 6 +-- src/core/control/AFieldFactory.h | 4 +- src/core/control/AFieldFactory.h.autosave | 19 -------- src/core/model/Field.cpp | 8 ++-- src/core/model/Structure.cpp | 12 ++++- src/core/model/Structure.cpp.autosave | 18 ------- src/editor/StructureField.cpp | 15 ++++++ src/editor/StructureField.h | 3 +- test/CMakeLists.txt | 13 +++++ test/core/test_tree.cpp | 41 ++++++++++++++++ 13 files changed, 149 insertions(+), 68 deletions(-) delete mode 100644 src/core/control/AFieldFactory.h.autosave delete mode 100644 src/core/model/Structure.cpp.autosave create mode 100644 test/CMakeLists.txt create mode 100644 test/core/test_tree.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 82fecd0..29884c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,8 @@ find_package(Qt5Widgets) add_subdirectory(src) +add_subdirectory(test) + add_executable(npc main.cpp) target_link_libraries(npc src) diff --git a/src/core/Tree.h b/src/core/Tree.h index be58e65..f78fadd 100644 --- a/src/core/Tree.h +++ b/src/core/Tree.h @@ -24,10 +24,10 @@ private: template Tree::Tree() { - root = std::make_shared>(nullptr); + root = std::make_shared>(); - root->setDataChangedSignal([this](std::shared_ptr> node){ this->nodeDataChanged(node);}); - root->setTreeChangedSignal([this](std::shared_ptr> node){ this->treeChanged(node);}); + //root->setDataChangedSignal([this](std::shared_ptr> node){ this->nodeDataChanged(node); }); + //root->setTreeChangedSignal([this](std::shared_ptr> node){ this->treeChanged(node); }); } template @@ -36,5 +36,17 @@ Tree::~Tree() } +template +void Tree::nodeDataChanged(std::shared_ptr > node) +{ + +} + +template +void Tree::treeChanged(std::shared_ptr > node) +{ + +} + #endif // EDITORTREE_H diff --git a/src/core/TreeNode.h b/src/core/TreeNode.h index 563e2c1..9726fb3 100644 --- a/src/core/TreeNode.h +++ b/src/core/TreeNode.h @@ -1,6 +1,7 @@ #ifndef EDITORNODE_H #define EDITORNODE_H +#include #include #include #include @@ -15,7 +16,7 @@ enum class TreeWalkStrategy { mixture }; template -class TreeNode +class TreeNode : public std::enable_shared_from_this> { public: explicit TreeNode(); @@ -37,6 +38,8 @@ public: bool removeChild(std::shared_ptr> child); bool remove(); + void addChildObject(std::shared_ptr obj); + uint getDistanceFromRoot(); std::shared_ptr> getParent() const; @@ -75,11 +78,13 @@ private: template TreeNode::TreeNode() { - + this->data = nullptr; + this->nodeDataChanged = nullptr; + this->treeChanged = nullptr; } template -TreeNode::TreeNode(std::shared_ptr data) +TreeNode::TreeNode(std::shared_ptr data) : TreeNode() { this->data = data; } @@ -110,7 +115,7 @@ const std::shared_ptr> TreeNode::nextSibling() { if (parent != nullptr) { auto l = parent->getChildren(); - auto it = std::find(l.begin(), l.end(), std::shared_ptr>(this)); + auto it = std::find(l.begin(), l.end(), this->shared_from_this()); if (it != l.end() && ++it != l.end()) return *(++it); //next sibling... else @@ -159,7 +164,7 @@ const std::shared_ptr> TreeNode::next(TreeWalkStrategy walkStrate return getChildren().front(); //return getChildren().first(); else { - auto node = std::shared_ptr>(this); + auto node = this->shared_from_this(); while (node->nextSibling() == nullptr && node->getParent() != nullptr) node = node->getParent(); return node->nextSibling(); @@ -181,7 +186,7 @@ const std::shared_ptr> TreeNode::next(TreeWalkStrategy walkStrate template const std::shared_ptr> TreeNode::getRoot() { - auto node = std::shared_ptr>(this); + auto node = this->shared_from_this(); while (node->getParent() != nullptr) node = node->getParent(); return node; @@ -190,9 +195,9 @@ const std::shared_ptr> TreeNode::getRoot() template void TreeNode::move(std::shared_ptr> to) { - this->getParent()->getChildren().remove(std::shared_ptr>(this)); + this->getParent()->getChildren().remove(this->shared_from_this()); //this->getParent()->getChildren().removeOne(std::shared_ptr(this)); - to->getChildren().push_back(std::shared_ptr>(this)); + to->getChildren().push_back(this->shared_from_this()); //to->getChildren().append(std::shared_ptr(this)); fireTreeChanged(); } @@ -208,8 +213,11 @@ void TreeNode::moveChildren(std::shared_ptr> to) template void TreeNode::addChild(std::shared_ptr> child) { + this->getChildren().push_back(child); - child->setParent(std::shared_ptr>(this)); + + child->setParent(this->shared_from_this()); + fireTreeChanged(); } @@ -220,7 +228,7 @@ void TreeNode::addChildren(std::shared_ptr> nodeWithChildren) for(std::shared_ptr> child : nodeWithChildren->getChildren()) { this->getChildren().push_back(child); - child->setParent(std::shared_ptr>(this)); + child->setParent(this->shared_from_this()); } fireTreeChanged(); } @@ -244,13 +252,22 @@ bool TreeNode::remove() } } if (worked && parent != nullptr) { - parent->getChildren().remove(std::shared_ptr>(this)); + parent->getChildren().remove(this->shared_from_this()); //worked &= parent->getChildren().removeOne(std::shared_ptr(this)); } if (worked) fireTreeChanged(); return worked; } +template +void TreeNode::addChildObject(std::shared_ptr obj) +{ + + auto node = std::make_shared>(obj); + + this->addChild(node); +} + template std::shared_ptr> TreeNode::getParent() const { @@ -285,15 +302,22 @@ std::list>> TreeNode::getChildren() const template void TreeNode::fireNodeDataChanged() { - if(this->getRoot()->getDataChangedSignal() != nullptr) - this->getRoot()->getDataChangedSignal()(std::shared_ptr>(this)); + auto signal = this->getRoot()->getDataChangedSignal(); + if(signal != nullptr) + signal(this->getParent()); } template void TreeNode::fireTreeChanged() { - if(this->getRoot()->getTreeChangedSignal() != nullptr) - this->getRoot()->getTreeChangedSignal()(std::shared_ptr>(this)); + + auto signal = this->getRoot()->getTreeChangedSignal(); + + if(signal != nullptr) { + + signal(this->getParent()); + + } } template @@ -303,7 +327,7 @@ std::shared_ptr> TreeNode::nextOnSameLevel() return nextSibling(); uint lvl = 1; auto node = this->getParent(); - auto pnode = std::shared_ptr>(this); + auto pnode = this->shared_from_this(); while (lvl > 0 && node != nullptr) { if (node != pnode->getParent() && node->getChildren().size() > 0) { node = node->getChildren().front(); @@ -327,7 +351,7 @@ std::shared_ptr> TreeNode::nextOnSameLevel() template uint TreeNode::getDistanceFromRoot() { - std::shared_ptr> node = std::make_shared>(this); + std::shared_ptr> node = this->shared_from_this(); uint lvl = 0; while(node->getParent() != nullptr) { diff --git a/src/core/control/AFieldFactory.cpp b/src/core/control/AFieldFactory.cpp index fa2cc98..7871873 100644 --- a/src/core/control/AFieldFactory.cpp +++ b/src/core/control/AFieldFactory.cpp @@ -1,4 +1,4 @@ -#include "FieldFactory.h" +#include "AFieldFactory.h" namespace NPC_core { namespace Control { AFieldFactory::AFieldFactory() @@ -6,7 +6,7 @@ AFieldFactory::AFieldFactory() } -std::shared_ptr AFieldFactory::createField(std::string name, std::shared_ptr parentStructure) +std::shared_ptr AFieldFactory::createField(std::string name, std::shared_ptr parentStructure) { auto f = std::make_shared(); f->setName(name); @@ -14,7 +14,7 @@ std::shared_ptr AFieldFactory::createField(std::string name, std:: return f; } -std::shared_ptr AFieldFactory::createFieldFull(std::string name, std::map syntaxes, std::shared_ptr parentStructure) +std::shared_ptr AFieldFactory::createFieldFull(std::string name, std::map syntaxes, std::shared_ptr parentStructure) { auto f = std::make_shared(); f->setName(name); diff --git a/src/core/control/AFieldFactory.h b/src/core/control/AFieldFactory.h index 4aa8ae2..2ffe97c 100644 --- a/src/core/control/AFieldFactory.h +++ b/src/core/control/AFieldFactory.h @@ -12,8 +12,8 @@ public: AFieldFactory(); virtual ~AFieldFactory(); - virtual std::shared_ptr createField(std::string name, std::shared_ptr parentStructure = nullptr); - virtual std::shared_ptr createFieldFull(std::string name, std::map syntaxes, std::shared_ptr parentStructure = nullptr); + virtual std::shared_ptr createField(std::string name, std::shared_ptr parentStructure = nullptr); + virtual std::shared_ptr createFieldFull(std::string name, std::map syntaxes, std::shared_ptr parentStructure = nullptr); }; }} #endif // AFIELDFACTORY_H diff --git a/src/core/control/AFieldFactory.h.autosave b/src/core/control/AFieldFactory.h.autosave deleted file mode 100644 index 2ffe97c..0000000 --- a/src/core/control/AFieldFactory.h.autosave +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef AFIELDFACTORY_H -#define AFIELDFACTORY_H - -#include "../model/Field.h" -#include - -namespace NPC_core { -namespace Control { -class AFieldFactory -{ -public: - AFieldFactory(); - virtual ~AFieldFactory(); - - virtual std::shared_ptr createField(std::string name, std::shared_ptr parentStructure = nullptr); - virtual std::shared_ptr createFieldFull(std::string name, std::map syntaxes, std::shared_ptr parentStructure = nullptr); -}; -}} -#endif // AFIELDFACTORY_H diff --git a/src/core/model/Field.cpp b/src/core/model/Field.cpp index e030f8d..526a3fc 100644 --- a/src/core/model/Field.cpp +++ b/src/core/model/Field.cpp @@ -1,4 +1,4 @@ -#include "AField.h" +#include "Field.h" namespace NPC_core { namespace Model { @@ -34,15 +34,15 @@ std::string Field::getValue() const void Field::setValue(const std::string &value) { - value = value; + this->value = value; } -std::shared_ptr Field::getParentStructure() const +std::shared_ptr Field::getParentStructure() const { return parentStructure; } -void Field::setParentStructure(const std::shared_ptr &value) +void Field::setParentStructure(const std::shared_ptr &value) { parentStructure = value; } diff --git a/src/core/model/Structure.cpp b/src/core/model/Structure.cpp index 098171a..a061eff 100644 --- a/src/core/model/Structure.cpp +++ b/src/core/model/Structure.cpp @@ -1,8 +1,18 @@ #include "Structure.h" namespace NPC_core { namespace Model { -AStructure::AStructure() +Structure::Structure() { } + +std::string Structure::getName() const +{ + return name; +} + +void Structure::setName(const std::string &value) +{ + name = value; +} }} diff --git a/src/core/model/Structure.cpp.autosave b/src/core/model/Structure.cpp.autosave deleted file mode 100644 index a061eff..0000000 --- a/src/core/model/Structure.cpp.autosave +++ /dev/null @@ -1,18 +0,0 @@ -#include "Structure.h" -namespace NPC_core { -namespace Model { -Structure::Structure() -{ - -} - -std::string Structure::getName() const -{ - return name; -} - -void Structure::setName(const std::string &value) -{ - name = value; -} -}} diff --git a/src/editor/StructureField.cpp b/src/editor/StructureField.cpp index 3f77d6b..25c2282 100644 --- a/src/editor/StructureField.cpp +++ b/src/editor/StructureField.cpp @@ -19,6 +19,21 @@ StructureField::StructureField(const std::shared_ptr fie this->field = field; } +void StructureField::inputChanged() +{ + +} + +void StructureField::dataChanged() +{ + +} + +void StructureField::nodeChanged() +{ + +} + void StructureField::render() { diff --git a/src/editor/StructureField.h b/src/editor/StructureField.h index e4ee9cb..32b1d20 100644 --- a/src/editor/StructureField.h +++ b/src/editor/StructureField.h @@ -7,7 +7,7 @@ #include #include #include -#include "../core/model/AField.h" +#include "../core/model/Field.h" #include namespace NPC_editor { @@ -34,6 +34,7 @@ private: // layout QVBoxLayout* layout; QLabel* title; + QLabel* syntax; QTextEdit* edit; QLineEdit* line; QMap otherWidgets; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..275686b --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,13 @@ + +# /usr/include/gtest + +enable_testing() +INCLUDE_DIRECTORIES(/usr/include/gtest) + +set(SOURCES + core/test_tree.cpp +) + +add_executable(runUnitTests ${SOURCES}) +target_link_libraries(runUnitTests gtest gtest_main) +add_test(runUnitTests runUnitTests) diff --git a/test/core/test_tree.cpp b/test/core/test_tree.cpp new file mode 100644 index 0000000..0e42eb4 --- /dev/null +++ b/test/core/test_tree.cpp @@ -0,0 +1,41 @@ + +#include +#include +#include "gtest/gtest.h" +#include "../src/core/Tree.h" +#include "../src/core/TreeNode.h" + +class TreeTest: public ::testing::Test { + protected: + virtual void SetUp(){ + for(int i = 0; i < 5; ++i) { + t2.getRoot()->addChildObject(std::make_shared("blah")); + } + } + + virtual void TearDown(){} + + Tree t; + Tree t2; + Tree t3; +}; + + +TEST_F(TreeTest, IsEmptyInitially) { + EXPECT_EQ(0, t.getRoot()->getDistanceFromRoot()); + EXPECT_EQ(0, t.getRoot()->getChildren().size()); +} + +TEST_F(TreeTest, blah) { + EXPECT_EQ(5, t2.getRoot()->getChildren().size()); + auto last = t2.getRoot()->getChildren().back(); + EXPECT_EQ(1, last->getDistanceFromRoot()); +} + + + + +TEST(sample_test_case, sample_test) +{ + EXPECT_EQ(1, 1); +}