libtree/tests/test_base.cpp

71 lines
2.2 KiB
C++

#include "Tree/tree.hpp"
#include "catch2/catch_test_macros.hpp"
#include "catch2/matchers/catch_matchers.hpp"
#include <memory>
#define CATCH_CONFIG_MAIN
#include <catch2/catch_all.hpp>
SCENARIO("Testing Base Tree Methods")
{
GIVEN("Nothing")
{
WHEN("Root Node is created")
{
std::shared_ptr<Tree::TreeRoot> root = nullptr;
REQUIRE_NOTHROW(root = std::make_shared<Tree::TreeRoot>());
THEN("attributed are as expected")
{
REQUIRE_FALSE(root == nullptr);
REQUIRE(root->getRoot() == root);
REQUIRE(root->getParent() == nullptr);
REQUIRE(root->getNrChildren() == 0);
REQUIRE(root->getNrDirectChildren() == 0);
REQUIRE(root->getChildrenBegin() == root->getChildrenEnd());
REQUIRE(root->type() == "Tree::TreeRoot");
REQUIRE(root->base() == "Tree::Base");
}
}
}
AND_GIVEN("A valid Root Node")
{
auto root = std::make_shared<Tree::TreeRoot>();
WHEN("adding a temporary node")
{
auto temp = std::make_shared<Tree::TempNode>();
root->addChild(temp);
THEN("the tree is correct")
{
REQUIRE_FALSE(root == nullptr);
REQUIRE(root->getRoot() == root);
REQUIRE(root->getParent() == nullptr);
REQUIRE(root->getNrChildren() == 1);
REQUIRE(root->getNrDirectChildren() == 1);
REQUIRE(root->type() == "Tree::TreeRoot");
REQUIRE(root->base() == "Tree::Base");
std::shared_ptr<Tree::TempNode> child;
REQUIRE_NOTHROW(child = std::static_pointer_cast<Tree::TempNode>(*(root->getChildrenBegin())));
REQUIRE(child->getRoot() == root);
REQUIRE(child->getParent() == root);
REQUIRE(child->getNrChildren() == 0);
REQUIRE(child->getNrDirectChildren() == 0);
REQUIRE(child->type() == "Tree::TempNode");
REQUIRE(child->base() == "Tree::Base");
}
}
}
}