51 CPPUNIT_ASSERT(!nodeEntry.
parent());
52 CPPUNIT_ASSERT_EQUAL(
string(), nodeEntry.
label());
53 CPPUNIT_ASSERT_EQUAL(0_st, nodeEntry.
children().size());
54 CPPUNIT_ASSERT_EQUAL(-1, nodeEntry.
index());
55 CPPUNIT_ASSERT_EQUAL(list<string>{
"" }, nodeEntry.
path());
57 CPPUNIT_ASSERT_EQUAL(EntryType::Node, nodeEntry.
type());
60 CPPUNIT_ASSERT(!accountEntry.
parent());
61 CPPUNIT_ASSERT_EQUAL(
string(), nodeEntry.
label());
62 CPPUNIT_ASSERT_EQUAL(0_st, accountEntry.
fields().size());
63 CPPUNIT_ASSERT_EQUAL(-1, accountEntry.
index());
64 CPPUNIT_ASSERT_EQUAL(list<string>{
"" }, accountEntry.
path());
65 CPPUNIT_ASSERT_EQUAL(EntryType::Account, accountEntry.
type());
67 const NodeEntry nodeEntryWithLabel(
"foo");
68 CPPUNIT_ASSERT(!nodeEntryWithLabel.
parent());
69 CPPUNIT_ASSERT_EQUAL(
"foo"s, nodeEntryWithLabel.
label());
70 CPPUNIT_ASSERT_EQUAL(list<string>{
"foo" }, nodeEntryWithLabel.
path());
78 auto *
const account =
new AccountEntry(
"account", &root);
79 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"account appended to root's children", vector<Entry *>{ account }, root.
children());
80 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"account's path contains parent", list<string>{
"root" CPP_UTILITIES_PP_COMMA
"account" }, account->path());
81 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index initialized", 0, account->index());
82 CPPUNIT_ASSERT_MESSAGE(
"actual assignment happened", account->parent() == &root);
85 auto *
const node =
new NodeEntry(
"node", &root);
86 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"node appended to root's children", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA node }, root.
children());
87 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"account's path contains parent", list<string>{
"root" CPP_UTILITIES_PP_COMMA
"node" }, node->path());
88 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index initialized", 1, node->index());
89 CPPUNIT_ASSERT_MESSAGE(
"actual assignment happened", node->parent() == &root);
92 node->setParent(&root);
93 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"root's children not altered", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA node }, root.
children());
94 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index not altered", 0, account->index());
95 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index not altered", 1, node->index());
98 node->setParent(&root, 0);
99 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"root's children not altered", vector<Entry *>{ node CPP_UTILITIES_PP_COMMA account }, root.
children());
100 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 1, account->index());
101 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 0, node->index());
104 node->setParent(&root, 1);
105 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"root's children not altered", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA node }, root.
children());
106 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 0, account->index());
107 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 1, node->index());
110 auto *
const anotherNode =
new NodeEntry(
"another node", &root);
111 anotherNode->setParent(&root, 2000);
112 CPPUNIT_ASSERT_EQUAL_MESSAGE(
113 "another node is at the end", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA node CPP_UTILITIES_PP_COMMA anotherNode }, root.
children());
114 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index initialized", 2, anotherNode->index());
117 node->setParent(anotherNode);
118 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index not altered", 0, account->index());
119 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 0, node->index());
120 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 1, anotherNode->index());
121 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"root's childrens updated", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA anotherNode }, root.
children());
122 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"another node's childrens updated", vector<Entry *>{ node }, anotherNode->children());
123 CPPUNIT_ASSERT_MESSAGE(
"node is still an indirect child of root", node->isIndirectChildOf(&root));
124 CPPUNIT_ASSERT_MESSAGE(
"node is direct and hence also an indirect child of another node", node->isIndirectChildOf(anotherNode));
125 CPPUNIT_ASSERT_MESSAGE(
"another node is no indirect child of node", !anotherNode->isIndirectChildOf(node));
128 auto *
const replacementNode =
new NodeEntry(
"replacement", &root);
129 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"replacement's index initialized", 2, replacementNode->index());
131 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"root's childrens updated", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA replacementNode }, root.
children());
132 CPPUNIT_ASSERT_MESSAGE(
"another node parentless", !anotherNode->parent());
133 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"another node's index updated", -1, anotherNode->index());
134 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"replacement's index updated", 1, replacementNode->index());
137 anotherNode->setParent(&root, 0);
138 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 0, anotherNode->index());
139 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 1, account->index());
140 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 2, replacementNode->index());
142 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"root's childrens updated", vector<Entry *>{ account CPP_UTILITIES_PP_COMMA replacementNode }, root.
children());
143 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 0, account->index());
144 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"index updated", 1, replacementNode->index());
151 auto createNode = EntryType::Node;
152 auto createAccount = EntryType::Account;
154 CPPUNIT_ASSERT_MESSAGE(
"nullptr for empty path", !root.
entryByPath(path));
157 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"return current instance",
static_cast<Entry *
>(&root), root.
entryByPath(path));
159 path = {
"root",
"foo" };
160 CPPUNIT_ASSERT_MESSAGE(
"nullptr for non-existent path", !root.
entryByPath(path));
162 path = {
"root",
"node" };
163 const auto *
const node = root.
entryByPath(path,
true, &createNode);
164 CPPUNIT_ASSERT_MESSAGE(
"node created", node);
165 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"actually a node", EntryType::Node, node->type());
166 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"label assigned",
"node"s, node->label());
168 path = {
"root",
"account" };
169 const auto *
const account = root.
entryByPath(path,
true, &createAccount);
170 CPPUNIT_ASSERT_MESSAGE(
"account created", account);
171 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"actually an account", EntryType::Account, account->type());
172 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"label assigned",
"account"s, account->label());
174 path = {
"root",
"account",
"foo" };
175 CPPUNIT_ASSERT_MESSAGE(
"nullptr for trying to add child to account", !root.
entryByPath(path,
true, &createAccount));
177 path = {
"root",
"node",
"foo" };
178 const auto *
const nestedAccount = root.
entryByPath(path,
true, &createAccount);
179 CPPUNIT_ASSERT_MESSAGE(
"nested account created", nestedAccount);
180 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"nested account created", EntryType::Account, nestedAccount->type());
181 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"label assigned",
"foo"s, nestedAccount->label());
182 CPPUNIT_ASSERT_EQUAL_MESSAGE(
183 "path actually correct", list<string>{
"root" CPP_UTILITIES_PP_COMMA
"node" CPP_UTILITIES_PP_COMMA
"foo" }, nestedAccount->path());
The EntryTests class tests the Io::Entry class.
void testNewEntryCorrectlyInitialized()
Tests whether a new entry is correctly initialized (basically empty, default values set).
The exception that is thrown when a parsing error occurs.
const std::vector< Field > & fields() const
EntryType type() const override
Returns the type of the entry.
Instances of the Entry class form a hierarchic data structure used to store account information.
int index() const
Returns the index of the entry within its parent.
NodeEntry * parent() const
Returns the parent entry.
const std::string & label() const
Returns the label.
std::list< std::string > path() const
Returns the path of the entry.
The NodeEntry class acts as parent for other entries.
Entry * entryByPath(std::list< std::string > &path, bool includeThis=true, const EntryType *creationType=nullptr)
Returns an entry specified by the provided path.
void replaceChild(std::size_t at, Entry *newChild)
Replaces the child at the specified index with the specified newChild.
EntryType type() const override
Returns the type of the entry.
void deleteChildren(int begin, int end)
Deletes children from the node entry.
const std::vector< Entry * > & children() const