Skip to content

Commit 8e44b09

Browse files
committed
Fixed #4 implemented removeElement for map
1 parent e75781f commit 8e44b09

File tree

9 files changed

+164
-53
lines changed

9 files changed

+164
-53
lines changed

src/wsjcpp_yaml.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ WsjcppYamlItem::WsjcppYamlItem(
8282
// ---------------------------------------------------------------------
8383

8484
WsjcppYamlItem::~WsjcppYamlItem() {
85+
for (int i = 0; i < m_vObjects.size(); i++) {
86+
delete m_vObjects[i];
87+
}
8588
m_vObjects.clear();
8689
}
8790

@@ -249,7 +252,15 @@ bool WsjcppYamlItem::removeElement(const std::string &sName) {
249252
if (m_nItemType != WSJCPP_YAML_ITEM_MAP) {
250253
WsjcppLog::throw_err(TAG, "removeElement: Element must be map");
251254
}
252-
// TODO erase
255+
std::vector<WsjcppYamlItem *>::iterator it;
256+
for (it = m_vObjects.begin(); it != m_vObjects.end(); ++it) {
257+
WsjcppYamlItem *pItem = *it;
258+
if (pItem->getName() == sName) {
259+
m_vObjects.erase(it);
260+
delete pItem;
261+
return true;
262+
}
263+
}
253264
return false;
254265
}
255266

@@ -431,6 +442,7 @@ bool WsjcppYamlItem::removeElement(int i) {
431442
std::vector<WsjcppYamlItem *>::iterator it;
432443
for (it = m_vObjects.begin(); it != m_vObjects.end(); ++it) {
433444
if (*it == pItem) {
445+
delete pItem;
434446
m_vObjects.erase(it);
435447
return true;
436448
}

src/wsjcpp_yaml.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,24 @@ class WsjcppYaml {
213213
WsjcppYamlItem *m_pRoot;
214214
};
215215

216+
// ---------------------------------------------------------------------
217+
218+
/*
219+
class WsjcppYamlCursor {
220+
public:
221+
WsjcppYamlCursor(WsjcppYaml *pYaml);
222+
~WsjcppYamlCursor();
223+
WsjcppYamlCursor(WsjcppYaml *pYaml, WsjcppYamlItem *pCurrentNode);
224+
WsjcppYamlCursor &operator[](int idx) {
225+
return WsjcppYamlCursor(pYaml, pYaml->getRoot()->getElement(idx)); // will be call destructor ?
226+
}
227+
WsjcppYamlCursor &operator[](const std::string &sName) { return *(getRoot()->getElement(sName)); }
228+
229+
private:
230+
WsjcppYaml *m_pYaml;
231+
WsjcppYamlItem *m_pCurrentNode;
232+
};
233+
*/
234+
216235
#endif // WSJCPP_YAML_H
217236

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,15 @@ list (APPEND WSJCPP_SOURCES "../src/wsjcpp_yaml.h")
3434

3535
# unit-tests
3636
list (APPEND WSJCPP_INCLUDE_DIRS "src")
37-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_line_parser.h")
3837
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_line_parser.cpp")
39-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_array.h")
4038
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_array.cpp")
41-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_map.h")
4239
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_map.cpp")
43-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_hierarchical_map.h")
4440
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_hierarchical_map.cpp")
45-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_quotes.h")
4641
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_quotes.cpp")
47-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_comments.h")
4842
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_comments.cpp")
49-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_all.h")
5043
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp")
51-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.h")
5244
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.cpp")
45+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_remove_element_for_map.cpp")
5346

5447
# required-libraries
5548
list (APPEND WSJCPP_LIBRARIES "-lpthread")

unit-tests.wsjcpp/src/unit_test_line_parser.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
#include "unit_test_line_parser.h"
1+
#include <wsjcpp_unit_tests.h>
22
#include <vector>
33
#include <iostream>
44
#include <wsjcpp_yaml.h>
55

6+
// ---------------------------------------------------------------------
7+
// UnitTestLineParser
8+
9+
class UnitTestLineParser : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestLineParser();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
617
REGISTRY_WSJCPP_UNIT_TEST(UnitTestLineParser)
718

819
UnitTestLineParser::UnitTestLineParser()
@@ -13,7 +24,7 @@ UnitTestLineParser::UnitTestLineParser()
1324
// ---------------------------------------------------------------------
1425

1526
bool UnitTestLineParser::doBeforeTest() {
16-
// nothing
27+
// do something before test
1728
return true;
1829
}
1930

@@ -84,6 +95,6 @@ void UnitTestLineParser::executeTest() {
8495
// ---------------------------------------------------------------------
8596

8697
bool UnitTestLineParser::doAfterTest() {
87-
// nothing
98+
// do somethig after test
8899
return true;
89100
}

unit-tests.wsjcpp/src/unit_test_line_parser.h

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
#include <wsjcpp_yaml.h>
5+
6+
// ---------------------------------------------------------------------
7+
// UnitTestRemoveElementForMap
8+
9+
class UnitTestRemoveElementForMap : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestRemoveElementForMap();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
17+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestRemoveElementForMap)
18+
19+
UnitTestRemoveElementForMap::UnitTestRemoveElementForMap()
20+
: WsjcppUnitTestBase("UnitTestRemoveElementForMap") {
21+
}
22+
23+
// ---------------------------------------------------------------------
24+
25+
bool UnitTestRemoveElementForMap::doBeforeTest() {
26+
// do something before test
27+
return true;
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
void UnitTestRemoveElementForMap::executeTest() {
33+
34+
std::string sTestYaml =
35+
"# Some comment 1\n"
36+
"map1: \n"
37+
" map11: \n"
38+
" map111: \n"
39+
" param1111: v1111\n"
40+
" param1112: v1112\n"
41+
" map112: \n"
42+
" param1121: v1121\n"
43+
" param1122: v1122\n"
44+
" map113: \n"
45+
" param1131: v1131\n"
46+
" param1132: v1132\n"
47+
" map12: \n"
48+
" param121: v121\n"
49+
" param122: v122\n"
50+
" map123: \n"
51+
" param1231: v1231\n"
52+
" param1232: v1232\n"
53+
"param2: v2 # some comment 2\n"
54+
"\n" // empty line
55+
;
56+
57+
WsjcppYaml yaml;
58+
if (!compare("Error parsing", yaml.loadFromString(sTestYaml), true)) {
59+
return;
60+
}
61+
62+
WsjcppYamlItem *pMap1 = yaml.getRoot()->getElement("map1");
63+
WsjcppYamlItem *pMap11 = pMap1->getElement("map11");
64+
65+
compare("has map111", pMap11->hasElement("map111"), true);
66+
compare("has map112", pMap11->hasElement("map112"), true);
67+
compare("has map113", pMap11->hasElement("map113"), true);
68+
pMap11->removeElement("map112");
69+
70+
compare("has map111", pMap11->hasElement("map111"), true);
71+
compare("has map112", pMap11->hasElement("map112"), false);
72+
compare("has map113", pMap11->hasElement("map113"), true);
73+
pMap11->removeElement("map111");
74+
75+
compare("has map111", pMap11->hasElement("map111"), false);
76+
compare("has map112", pMap11->hasElement("map112"), false);
77+
compare("has map113", pMap11->hasElement("map113"), true);
78+
79+
compare("has map11", pMap1->hasElement("map11"), true);
80+
yaml.getRoot()->getElement("map1")->removeElement("map11");
81+
compare("has map11", pMap1->hasElement("map11"), false);
82+
}
83+
84+
// ---------------------------------------------------------------------
85+
86+
bool UnitTestRemoveElementForMap::doAfterTest() {
87+
// do somethig after test
88+
return true;
89+
}
90+
91+

unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
#include "unit_test_yaml_parser_all.h"
1+
#include <wsjcpp_unit_tests.h>
22
#include <vector>
33
#include <iostream>
4-
#include <wsjcpp_core.h>
54
#include <wsjcpp_yaml.h>
65

6+
// ---------------------------------------------------------------------
7+
// UnitTestYamlParserAll
8+
9+
class UnitTestYamlParserAll : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestYamlParserAll();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
17+
718
REGISTRY_WSJCPP_UNIT_TEST(UnitTestYamlParserAll)
819

920
UnitTestYamlParserAll::UnitTestYamlParserAll()
@@ -14,15 +25,15 @@ UnitTestYamlParserAll::UnitTestYamlParserAll()
1425
// ---------------------------------------------------------------------
1526

1627
bool UnitTestYamlParserAll::doBeforeTest() {
17-
// nothing
28+
// do something before test
1829
return true;
1930
}
2031

2132
// ---------------------------------------------------------------------
2233

2334
void UnitTestYamlParserAll::executeTest() {
2435

25-
std::string g_sTestYaml =
36+
std::string sTestYaml =
2637
"# Some comment 1\n"
2738
"test10: one\n"
2839
"test20: two # some comment 2\n"
@@ -48,13 +59,14 @@ void UnitTestYamlParserAll::executeTest() {
4859
;
4960

5061
WsjcppYaml yaml;
51-
if (!compare("Error parsing", yaml.loadFromString(g_sTestYaml), true)) {
62+
if (!compare("Error parsing", yaml.loadFromString(sTestYaml), true)) {
5263
return;
5364
}
5465

55-
std::string sSaved = "";
56-
if (yaml.saveToString(sSaved)) {
57-
WsjcppLog::info(TAG, "\n>>>>\n" + sSaved);
66+
std::string sSaved1 = "";
67+
if (!compare("Error saving", yaml.saveToString(sSaved1), true)) {
68+
compare("yaml_saved 2-test", sSaved1, sTestYaml);
69+
return;
5870
}
5971

6072
WsjcppYamlItem *pItem = nullptr;
@@ -83,15 +95,15 @@ void UnitTestYamlParserAll::executeTest() {
8395
pItem = yaml.getRoot()->getElement("map60")->getElement("test80");
8496
compare("test80_comment", pItem->getValue(), "opa2");
8597

86-
sSaved = "";
87-
if (compare("save yaml", yaml.saveToString(sSaved), true)) {
88-
compare("yaml_save", sSaved, g_sTestYaml);
98+
std::string sSaved2 = "";
99+
if (compare("saving yaml", yaml.saveToString(sSaved2), true)) {
100+
compare("yaml_saved 1-2", sSaved1, sSaved2);
89101
}
90102
}
91103

92104
// ---------------------------------------------------------------------
93105

94106
bool UnitTestYamlParserAll::doAfterTest() {
95-
// nothing
107+
// do something after test
96108
return true;
97109
}

unit-tests.wsjcpp/src/unit_test_yaml_parser_all.h

Lines changed: 0 additions & 14 deletions
This file was deleted.

wsjcpp.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ unit-tests:
5858
description: YamlParserAll
5959
- name: YamlParserArrayIncludedMap
6060
description: YamlParserArrayIncludedMap
61-
61+
- name: "RemoveElementForMap"
62+
description: ""

0 commit comments

Comments
 (0)