Working on tests of the Tree

This commit is contained in:
Marcel Otte 2018-01-11 23:20:46 +01:00
parent 691e387554
commit bbc0734ae5
13 changed files with 149 additions and 68 deletions

View File

@ -13,6 +13,8 @@ find_package(Qt5Widgets)
add_subdirectory(src)
add_subdirectory(test)
add_executable(npc main.cpp)
target_link_libraries(npc src)

View File

@ -24,10 +24,10 @@ private:
template <class T>
Tree<T>::Tree()
{
root = std::make_shared<TreeNode<T>>(nullptr);
root = std::make_shared<TreeNode<T>>();
root->setDataChangedSignal([this](std::shared_ptr<TreeNode<T>> node){ this->nodeDataChanged(node);});
root->setTreeChangedSignal([this](std::shared_ptr<TreeNode<T>> node){ this->treeChanged(node);});
//root->setDataChangedSignal([this](std::shared_ptr<TreeNode<T>> node){ this->nodeDataChanged(node); });
//root->setTreeChangedSignal([this](std::shared_ptr<TreeNode<T>> node){ this->treeChanged(node); });
}
template <class T>
@ -36,5 +36,17 @@ Tree<T>::~Tree()
}
template<class T>
void Tree<T>::nodeDataChanged(std::shared_ptr<TreeNode<T> > node)
{
}
template<class T>
void Tree<T>::treeChanged(std::shared_ptr<TreeNode<T> > node)
{
}
#endif // EDITORTREE_H

View File

@ -1,6 +1,7 @@
#ifndef EDITORNODE_H
#define EDITORNODE_H
#include <iostream>
#include <memory>
#include <functional>
#include <list>
@ -15,7 +16,7 @@ enum class TreeWalkStrategy {
mixture
};
template <class T>
class TreeNode
class TreeNode : public std::enable_shared_from_this<TreeNode<T>>
{
public:
explicit TreeNode();
@ -37,6 +38,8 @@ public:
bool removeChild(std::shared_ptr<TreeNode<T>> child);
bool remove();
void addChildObject(std::shared_ptr<T> obj);
uint getDistanceFromRoot();
std::shared_ptr<TreeNode<T>> getParent() const;
@ -75,11 +78,13 @@ private:
template <class T>
TreeNode<T>::TreeNode()
{
this->data = nullptr;
this->nodeDataChanged = nullptr;
this->treeChanged = nullptr;
}
template <class T>
TreeNode<T>::TreeNode(std::shared_ptr<T> data)
TreeNode<T>::TreeNode(std::shared_ptr<T> data) : TreeNode()
{
this->data = data;
}
@ -110,7 +115,7 @@ const std::shared_ptr<TreeNode<T>> TreeNode<T>::nextSibling()
{
if (parent != nullptr) {
auto l = parent->getChildren();
auto it = std::find(l.begin(), l.end(), std::shared_ptr<TreeNode<T>>(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<T>> TreeNode<T>::next(TreeWalkStrategy walkStrate
return getChildren().front();
//return getChildren().first();
else {
auto node = std::shared_ptr<TreeNode<T>>(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<T>> TreeNode<T>::next(TreeWalkStrategy walkStrate
template <class T>
const std::shared_ptr<TreeNode<T>> TreeNode<T>::getRoot()
{
auto node = std::shared_ptr<TreeNode<T>>(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<T>> TreeNode<T>::getRoot()
template <class T>
void TreeNode<T>::move(std::shared_ptr<TreeNode<T>> to)
{
this->getParent()->getChildren().remove(std::shared_ptr<TreeNode<T>>(this));
this->getParent()->getChildren().remove(this->shared_from_this());
//this->getParent()->getChildren().removeOne(std::shared_ptr<EditorNode>(this));
to->getChildren().push_back(std::shared_ptr<TreeNode<T>>(this));
to->getChildren().push_back(this->shared_from_this());
//to->getChildren().append(std::shared_ptr<EditorNode>(this));
fireTreeChanged();
}
@ -208,8 +213,11 @@ void TreeNode<T>::moveChildren(std::shared_ptr<TreeNode<T>> to)
template <class T>
void TreeNode<T>::addChild(std::shared_ptr<TreeNode<T>> child)
{
this->getChildren().push_back(child);
child->setParent(std::shared_ptr<TreeNode<T>>(this));
child->setParent(this->shared_from_this());
fireTreeChanged();
}
@ -220,7 +228,7 @@ void TreeNode<T>::addChildren(std::shared_ptr<TreeNode<T>> nodeWithChildren)
for(std::shared_ptr<TreeNode<T>> child : nodeWithChildren->getChildren())
{
this->getChildren().push_back(child);
child->setParent(std::shared_ptr<TreeNode<T>>(this));
child->setParent(this->shared_from_this());
}
fireTreeChanged();
}
@ -244,13 +252,22 @@ bool TreeNode<T>::remove()
}
}
if (worked && parent != nullptr) {
parent->getChildren().remove(std::shared_ptr<TreeNode<T>>(this));
parent->getChildren().remove(this->shared_from_this());
//worked &= parent->getChildren().removeOne(std::shared_ptr<EditorNode>(this));
}
if (worked) fireTreeChanged();
return worked;
}
template<class T>
void TreeNode<T>::addChildObject(std::shared_ptr<T> obj)
{
auto node = std::make_shared<TreeNode<T>>(obj);
this->addChild(node);
}
template <class T>
std::shared_ptr<TreeNode<T>> TreeNode<T>::getParent() const
{
@ -285,15 +302,22 @@ std::list<std::shared_ptr<TreeNode<T>>> TreeNode<T>::getChildren() const
template <class T>
void TreeNode<T>::fireNodeDataChanged()
{
if(this->getRoot()->getDataChangedSignal() != nullptr)
this->getRoot()->getDataChangedSignal()(std::shared_ptr<TreeNode<T>>(this));
auto signal = this->getRoot()->getDataChangedSignal();
if(signal != nullptr)
signal(this->getParent());
}
template <class T>
void TreeNode<T>::fireTreeChanged()
{
if(this->getRoot()->getTreeChangedSignal() != nullptr)
this->getRoot()->getTreeChangedSignal()(std::shared_ptr<TreeNode<T>>(this));
auto signal = this->getRoot()->getTreeChangedSignal();
if(signal != nullptr) {
signal(this->getParent());
}
}
template <class T>
@ -303,7 +327,7 @@ std::shared_ptr<TreeNode<T>> TreeNode<T>::nextOnSameLevel()
return nextSibling();
uint lvl = 1;
auto node = this->getParent();
auto pnode = std::shared_ptr<TreeNode<T>>(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<T>> TreeNode<T>::nextOnSameLevel()
template <class T>
uint TreeNode<T>::getDistanceFromRoot()
{
std::shared_ptr<TreeNode<T>> node = std::make_shared<TreeNode<T>>(this);
std::shared_ptr<TreeNode<T>> node = this->shared_from_this();
uint lvl = 0;
while(node->getParent() != nullptr)
{

View File

@ -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<Model::Field> AFieldFactory::createField(std::string name, std::shared_ptr<Model::AStructure> parentStructure)
std::shared_ptr<Model::Field> AFieldFactory::createField(std::string name, std::shared_ptr<Model::Structure> parentStructure)
{
auto f = std::make_shared<Model::Field>();
f->setName(name);
@ -14,7 +14,7 @@ std::shared_ptr<Model::Field> AFieldFactory::createField(std::string name, std::
return f;
}
std::shared_ptr<Model::Field> AFieldFactory::createFieldFull(std::string name, std::map<std::string, std::string> syntaxes, std::shared_ptr<Model::AStructure> parentStructure)
std::shared_ptr<Model::Field> AFieldFactory::createFieldFull(std::string name, std::map<std::string, std::string> syntaxes, std::shared_ptr<Model::Structure> parentStructure)
{
auto f = std::make_shared<Model::Field>();
f->setName(name);

View File

@ -12,8 +12,8 @@ public:
AFieldFactory();
virtual ~AFieldFactory();
virtual std::shared_ptr<Model::Field> createField(std::string name, std::shared_ptr<Model::AStructure> parentStructure = nullptr);
virtual std::shared_ptr<Model::Field> createFieldFull(std::string name, std::map<std::string, std::string> syntaxes, std::shared_ptr<Model::AStructure> parentStructure = nullptr);
virtual std::shared_ptr<Model::Field> createField(std::string name, std::shared_ptr<Model::Structure> parentStructure = nullptr);
virtual std::shared_ptr<Model::Field> createFieldFull(std::string name, std::map<std::string, std::string> syntaxes, std::shared_ptr<Model::Structure> parentStructure = nullptr);
};
}}
#endif // AFIELDFACTORY_H

View File

@ -1,19 +0,0 @@
#ifndef AFIELDFACTORY_H
#define AFIELDFACTORY_H
#include "../model/Field.h"
#include <memory>
namespace NPC_core {
namespace Control {
class AFieldFactory
{
public:
AFieldFactory();
virtual ~AFieldFactory();
virtual std::shared_ptr<Model::Field> createField(std::string name, std::shared_ptr<Model::Structure> parentStructure = nullptr);
virtual std::shared_ptr<Model::Field> createFieldFull(std::string name, std::map<std::string, std::string> syntaxes, std::shared_ptr<Model::Structure> parentStructure = nullptr);
};
}}
#endif // AFIELDFACTORY_H

View File

@ -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<AStructure> Field::getParentStructure() const
std::shared_ptr<Structure> Field::getParentStructure() const
{
return parentStructure;
}
void Field::setParentStructure(const std::shared_ptr<AStructure> &value)
void Field::setParentStructure(const std::shared_ptr<Structure> &value)
{
parentStructure = value;
}

View File

@ -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;
}
}}

View File

@ -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;
}
}}

View File

@ -19,6 +19,21 @@ StructureField::StructureField(const std::shared_ptr<NPC_core::Model::Field> fie
this->field = field;
}
void StructureField::inputChanged()
{
}
void StructureField::dataChanged()
{
}
void StructureField::nodeChanged()
{
}
void StructureField::render()
{

View File

@ -7,7 +7,7 @@
#include <QLineEdit>
#include <QVBoxLayout>
#include <QRegExpValidator>
#include "../core/model/AField.h"
#include "../core/model/Field.h"
#include <memory>
namespace NPC_editor {
@ -34,6 +34,7 @@ private:
// layout
QVBoxLayout* layout;
QLabel* title;
QLabel* syntax;
QTextEdit* edit;
QLineEdit* line;
QMap<QString, QWidget*> otherWidgets;

13
test/CMakeLists.txt Normal file
View File

@ -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)

41
test/core/test_tree.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <iostream>
#include <string>
#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<std::string>("blah"));
}
}
virtual void TearDown(){}
Tree<std::string> t;
Tree<std::string> t2;
Tree<std::string> 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);
}