Skip to content

Commit c01d1bf

Browse files
committed
Fixed #19 Cleanup previously data
1 parent 7e9308a commit c01d1bf

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

src/wsjcpp_yaml.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ WsjcppYamlItem::WsjcppYamlItem(
8383
// ---------------------------------------------------------------------
8484

8585
WsjcppYamlItem::~WsjcppYamlItem() {
86-
for (int i = 0; i < m_vObjects.size(); i++) {
86+
for (int i = 0; i < m_vObjects.size(); i++) {
8787
delete m_vObjects[i];
8888
}
8989
m_vObjects.clear();
@@ -1129,7 +1129,7 @@ WsjcppYamlCursor WsjcppYamlCursor::operator[](const std::string &sName) const {
11291129
// WsjcppYaml
11301130

11311131
WsjcppYaml::WsjcppYaml() {
1132-
m_pRoot = new WsjcppYamlItem(nullptr, WsjcppYamlPlaceInFile(), WSJCPP_YAML_ITEM_MAP);
1132+
m_pRoot = nullptr;
11331133
TAG = "WsjcppYaml";
11341134
}
11351135

@@ -1141,6 +1141,13 @@ WsjcppYaml::~WsjcppYaml() {
11411141

11421142
// ---------------------------------------------------------------------
11431143

1144+
void WsjcppYaml::clear() {
1145+
delete m_pRoot;
1146+
m_pRoot = nullptr;
1147+
}
1148+
1149+
// ---------------------------------------------------------------------
1150+
11441151
bool WsjcppYaml::loadFromFile(const std::string &sFileName, std::string &sError) {
11451152
std::string sTextContent;
11461153
if (!WsjcppCore::readTextFile(sFileName, sTextContent)) {
@@ -1220,6 +1227,11 @@ std::vector<std::string> WsjcppYaml::splitToLines(const std::string &sBuffer) {
12201227
// ---------------------------------------------------------------------
12211228

12221229
bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer, std::string &sError) {
1230+
this->clear();
1231+
if (m_pRoot == nullptr) {
1232+
m_pRoot = new WsjcppYamlItem(nullptr, WsjcppYamlPlaceInFile(), WSJCPP_YAML_ITEM_MAP);
1233+
}
1234+
12231235
std::vector<std::string> vLines = this->splitToLines(sBuffer);
12241236
WsjcppYamlParserStatus st;
12251237
st.pCurItem = m_pRoot; // TODO recreate again new root element

src/wsjcpp_yaml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ class WsjcppYaml {
254254
public:
255255
WsjcppYaml();
256256
~WsjcppYaml();
257+
void clear();
257258
bool loadFromFile(const std::string &sFileName, std::string &sError);
258259
bool saveToFile(const std::string &sFileName);
259260
bool loadFromString(const std::string &sBufferName, const std::string &sBuffer, std::string &sError);

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_read_yaml.cpp")
4949
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_read_write_file.cpp")
5050
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_cursor.cpp")
5151
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_tag_names.cpp")
52+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_cleanup.cpp")
5253

5354
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
5455

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
#include <wsjcpp_yaml.h>
5+
6+
// ---------------------------------------------------------------------
7+
// UnitTestCleanup
8+
9+
class UnitTestCleanup : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestCleanup();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
17+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestCleanup)
18+
19+
UnitTestCleanup::UnitTestCleanup()
20+
: WsjcppUnitTestBase("UnitTestCleanup") {
21+
}
22+
23+
// ---------------------------------------------------------------------
24+
25+
bool UnitTestCleanup::doBeforeTest() {
26+
// do something before test
27+
return true;
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
void UnitTestCleanup::executeTest() {
33+
std::string sTestYaml1 =
34+
"# Some comment 1\n"
35+
"test10: one\n"
36+
"test20: two # some comment 2\n"
37+
;
38+
39+
std::string sTestYaml2 =
40+
"# Some comment 1\n"
41+
"test11: one\n"
42+
"test20: two # some comment 2\n"
43+
"test22: two # some comment 2\n"
44+
;
45+
46+
WsjcppYaml yaml;
47+
std::string sError;
48+
49+
if (!compare("Error parsing 1", yaml.loadFromString("parse1", sTestYaml1, sError), true)) {
50+
WsjcppLog::err(TAG, sError);
51+
return;
52+
}
53+
54+
compare("(1) has test10", yaml.getRoot()->hasElement("test10"), true);
55+
compare("(1) has test11", yaml.getRoot()->hasElement("test11"), false);
56+
compare("(1) has test20", yaml.getRoot()->hasElement("test20"), true);
57+
compare("(1) has test22", yaml.getRoot()->hasElement("test22"), false);
58+
59+
if (!compare("Error parsing 2", yaml.loadFromString("parse2", sTestYaml2, sError), true)) {
60+
WsjcppLog::err(TAG, sError);
61+
return;
62+
}
63+
64+
compare("(2) has test10", yaml.getRoot()->hasElement("test10"), false);
65+
compare("(2) has test11", yaml.getRoot()->hasElement("test11"), true);
66+
compare("(2) has test20", yaml.getRoot()->hasElement("test20"), true);
67+
compare("(2) has test22", yaml.getRoot()->hasElement("test22"), true);
68+
69+
yaml.clear();
70+
71+
compare("(3) has root", yaml.getRoot() == nullptr, true);
72+
}
73+
74+
// ---------------------------------------------------------------------
75+
76+
bool UnitTestCleanup::doAfterTest() {
77+
// do somethig after test
78+
return true;
79+
}
80+
81+

wsjcpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,5 @@ unit-tests:
6565
description: ""
6666
- name: "TagNames"
6767
description: ""
68+
- name: "Cleanup"
69+
description: ""

0 commit comments

Comments
 (0)