libtree/tests/test_base.cpp

71 lines
2.2 KiB
C++
Raw Permalink Normal View History

2024-01-28 22:45:55 +01:00
#include "Tree/tree.hpp"
#include "catch2/catch_test_macros.hpp"
#include "catch2/matchers/catch_matchers.hpp"
2019-04-03 21:21:22 +02:00
#include <memory>
2024-01-28 22:45:55 +01:00
#define CATCH_CONFIG_MAIN
#include <catch2/catch_all.hpp>
2019-04-03 21:21:22 +02:00
2024-01-28 22:45:55 +01:00
SCENARIO("Testing Base Tree Methods")
2019-04-03 21:21:22 +02:00
{
2024-01-28 22:45:55 +01:00
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");
}
}
}
}