1 #include "../io/entry.h"
5 #include <c++utilities/tests/testutils.h>
8 #include <cppunit/TestFixture.h>
9 #include <cppunit/extensions/HelperMacros.h>
13 using namespace CppUtilities::Literals;
15 using namespace CPPUNIT_NS;
22 CPPUNIT_TEST(testNewEntryCorrectlyInitialized);
23 CPPUNIT_TEST(testNesting);
24 CPPUNIT_TEST(testEntryByPath);
25 CPPUNIT_TEST(testUniqueLabels);
26 CPPUNIT_TEST_SUITE_END();
29 void setUp()
override;
30 void tearDown()
override;
32 void testNewEntryCorrectlyInitialized();
34 void testEntryByPath();
35 void testUniqueLabels();
54 CPPUNIT_ASSERT(!nodeEntry.
parent());
55 CPPUNIT_ASSERT_EQUAL(
string(), nodeEntry.
label());
56 CPPUNIT_ASSERT_EQUAL(0_st, nodeEntry.
children().size());
57 CPPUNIT_ASSERT_EQUAL(-1, nodeEntry.
index());
58 CPPUNIT_ASSERT_EQUAL(list<string>{
"" }, nodeEntry.
path());
60 CPPUNIT_ASSERT_EQUAL(EntryType::Node, nodeEntry.
type());
63 CPPUNIT_ASSERT(!accountEntry.
parent());
64 CPPUNIT_ASSERT_EQUAL(
string(), nodeEntry.
label());
65 CPPUNIT_ASSERT_EQUAL(0_st, accountEntry.
fields().size());
66 CPPUNIT_ASSERT_EQUAL(-1, accountEntry.
index());
67 CPPUNIT_ASSERT_EQUAL(list<string>{
"" }, accountEntry.
path());
68 CPPUNIT_ASSERT_EQUAL(EntryType::Account, accountEntry.
type());
70 const NodeEntry nodeEntryWithLabel(
"foo");
71 CPPUNIT_ASSERT(!nodeEntryWithLabel.
parent());
72 CPPUNIT_ASSERT_EQUAL(
"foo"s, nodeEntryWithLabel.
label());
73 CPPUNIT_ASSERT_EQUAL(list<string>{
"foo" }, nodeEntryWithLabel.
path());
81 auto *
const account =
new AccountEntry(
"account", &root);
82 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"account appended to root's children", vector<Entry *>{ account }, root.
children());
83 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"account's path contains parent", list<string>{
"root" CPP_UTILITIES_PP_COMMA
"account" }, account->path());
84 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index initialized", 0, account->index());
85 CPPUNIT_ASSERT_MESSAGE(
"actual assignment happened", account->parent() == &root);
88 auto *
const node =
new NodeEntry(
"node", &root);
89 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"node appended to root's children", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA node }, root.
children());
90 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"account's path contains parent", list<string>{
"root" CPP_UTILITIES_PP_COMMA
"node" }, node->path());
91 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index initialized", 1, node->index());
92 CPPUNIT_ASSERT_MESSAGE(
"actual assignment happened", node->parent() == &root);
95 node->setParent(&root);
96 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"root's children not altered", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA node }, root.
children());
97 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index not altered", 0, account->index());
98 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index not altered", 1, node->index());
101 node->setParent(&root, 0);
102 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"root's children not altered", vector<Entry *>{ node CPP_UTILITIES_PP_COMMA account }, root.
children());
103 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 1, account->index());
104 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 0, node->index());
107 node->setParent(&root, 1);
108 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"root's children not altered", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA node }, root.
children());
109 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 0, account->index());
110 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 1, node->index());
113 auto *
const anotherNode =
new NodeEntry(
"another node", &root);
114 anotherNode->setParent(&root, 2000);
115 CPPUNIT_ASSERT_EQUAL_MESSAGE(
116 "another node is at the end", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA node CPP_UTILITIES_PP_COMMA anotherNode }, root.
children());
117 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index initialized", 2, anotherNode->index());
120 node->setParent(anotherNode);
121 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index not altered", 0, account->index());
122 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 0, node->index());
123 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 1, anotherNode->index());
124 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"root's childrens updated", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA anotherNode }, root.
children());
125 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"another node's childrens updated", vector<Entry *>{ node }, anotherNode->children());
126 CPPUNIT_ASSERT_MESSAGE(
"node is still an indirect child of root", node->isIndirectChildOf(&root));
127 CPPUNIT_ASSERT_MESSAGE(
"node is direct and hence also an indirect child of another node", node->isIndirectChildOf(anotherNode));
128 CPPUNIT_ASSERT_MESSAGE(
"another node is no indirect child of node", !anotherNode->isIndirectChildOf(node));
131 auto *
const replacementNode =
new NodeEntry(
"replacement", &root);
132 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"replacement's index initialized", 2, replacementNode->index());
134 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"root's childrens updated", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA replacementNode }, root.
children());
135 CPPUNIT_ASSERT_MESSAGE(
"another node parentless", !anotherNode->parent());
136 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"another node's index updated", -1, anotherNode->index());
137 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"replacement's index updated", 1, replacementNode->index());
140 anotherNode->setParent(&root, 0);
141 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 0, anotherNode->index());
142 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 1, account->index());
143 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 2, replacementNode->index());
145 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"root's childrens updated", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA replacementNode }, root.
children());
146 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 0, account->index());
147 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 1, replacementNode->index());
154 auto createNode = EntryType::Node;
155 auto createAccount = EntryType::Account;
157 CPPUNIT_ASSERT_MESSAGE(
"nullptr for empty path", !root.
entryByPath(path));
160 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"return current instance",
static_cast<Entry *
>(&root), root.
entryByPath(path));
162 path = {
"root",
"foo" };
163 CPPUNIT_ASSERT_MESSAGE(
"nullptr for non-existant path", !root.
entryByPath(path));
165 path = {
"root",
"node" };
166 const auto *
const node = root.
entryByPath(path,
true, &createNode);
167 CPPUNIT_ASSERT_MESSAGE(
"node created", node);
168 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"actually a node", EntryType::Node, node->type());
169 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"label assigned",
"node"s, node->label());
171 path = {
"root",
"account" };
172 const auto *
const account = root.
entryByPath(path,
true, &createAccount);
173 CPPUNIT_ASSERT_MESSAGE(
"account created", account);
174 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"actually an account", EntryType::Account, account->type());
175 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"label assigned",
"account"s, account->label());
177 path = {
"root",
"account",
"foo" };
178 CPPUNIT_ASSERT_MESSAGE(
"nullptr for trying to add child to account", !root.
entryByPath(path,
true, &createAccount));
180 path = {
"root",
"node",
"foo" };
181 const auto *
const nestedAccount = root.
entryByPath(path,
true, &createAccount);
182 CPPUNIT_ASSERT_MESSAGE(
"nested account created", nestedAccount);
183 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"nested account created", EntryType::Account, nestedAccount->type());
184 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"label assigned",
"foo"s, nestedAccount->label());
185 CPPUNIT_ASSERT_EQUAL_MESSAGE(
186 "path actually correct", list<string>{
"root" CPP_UTILITIES_PP_COMMA
"node" CPP_UTILITIES_PP_COMMA
"foo" }, nestedAccount->path());
192 const auto *
const fooEntry =
new AccountEntry(
"foo", &root);
193 CPP_UTILITIES_UNUSED(fooEntry)
194 const auto *
const foo2Entry =
new AccountEntry(
"foo", &root);
195 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"2nd foo renamed to foo 2",
"foo 2"s, foo2Entry->label());
196 const auto *
const foo3Entry =
new AccountEntry(
"foo", &root);
197 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"3rd foo renamed to foo 3",
"foo 3"s, foo3Entry->label());