Skip to content

Commit b25e4d2

Browse files
committed
Prepare WsjcppYamlCursor #13
1 parent 80c7997 commit b25e4d2

File tree

6 files changed

+228
-25
lines changed

6 files changed

+228
-25
lines changed

src/wsjcpp_yaml.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,67 @@ void WsjcppYamlParserStatus::logUnknownLine(const std::string &sPrefix) {
982982
);
983983
}
984984

985+
// ---------------------------------------------------------------------
986+
// WsjcppYamlCursor
987+
988+
WsjcppYamlCursor::WsjcppYamlCursor(WsjcppYamlItem *pCurrentNode) {
989+
m_pCurrentNode = pCurrentNode;
990+
}
991+
992+
// ---------------------------------------------------------------------
993+
994+
WsjcppYamlCursor::WsjcppYamlCursor() {
995+
m_pCurrentNode = nullptr;
996+
}
997+
998+
// ---------------------------------------------------------------------
999+
1000+
WsjcppYamlCursor::~WsjcppYamlCursor() {
1001+
// do nothing
1002+
}
1003+
1004+
// ---------------------------------------------------------------------
1005+
1006+
bool WsjcppYamlCursor::isNull() const {
1007+
return m_pCurrentNode == nullptr;
1008+
}
1009+
1010+
// ---------------------------------------------------------------------
1011+
1012+
bool WsjcppYamlCursor::isArray() const {
1013+
return m_pCurrentNode != nullptr && m_pCurrentNode->isArray();
1014+
}
1015+
1016+
// ---------------------------------------------------------------------
1017+
1018+
size_t WsjcppYamlCursor::size() const {
1019+
return m_pCurrentNode != nullptr && m_pCurrentNode->isArray() ? m_pCurrentNode->getLength() : -1;
1020+
}
1021+
1022+
// ---------------------------------------------------------------------
1023+
1024+
bool WsjcppYamlCursor::isMap() const {
1025+
return m_pCurrentNode != nullptr && m_pCurrentNode->isMap();
1026+
}
1027+
1028+
// ---------------------------------------------------------------------
1029+
1030+
WsjcppYamlCursor WsjcppYamlCursor::operator[](int idx) const {
1031+
if (m_pCurrentNode != nullptr && m_pCurrentNode->isArray() && idx < m_pCurrentNode->getLength() && idx >= 0) {
1032+
return WsjcppYamlCursor(m_pCurrentNode->getElement(idx));
1033+
}
1034+
return WsjcppYamlCursor();
1035+
}
1036+
1037+
// ---------------------------------------------------------------------
1038+
1039+
WsjcppYamlCursor WsjcppYamlCursor::operator[](const std::string &sName) const {
1040+
if (m_pCurrentNode != nullptr && m_pCurrentNode->isMap() && m_pCurrentNode->hasElement(sName)) {
1041+
return WsjcppYamlCursor(m_pCurrentNode->getElement(sName));
1042+
}
1043+
return WsjcppYamlCursor();
1044+
}
1045+
9851046
// ---------------------------------------------------------------------
9861047
// WsjcppYaml
9871048

@@ -1037,6 +1098,24 @@ WsjcppYamlItem *WsjcppYaml::getRoot() {
10371098

10381099
// ---------------------------------------------------------------------
10391100

1101+
WsjcppYamlCursor WsjcppYaml::getCursor() const {
1102+
return WsjcppYamlCursor(m_pRoot);
1103+
}
1104+
1105+
// ---------------------------------------------------------------------
1106+
1107+
WsjcppYamlCursor WsjcppYaml::operator[](int idx) const {
1108+
return this->getCursor()[idx];
1109+
}
1110+
1111+
// ---------------------------------------------------------------------
1112+
1113+
WsjcppYamlCursor WsjcppYaml::operator[](const std::string &sName) const {
1114+
return this->getCursor()[sName];
1115+
}
1116+
1117+
// ---------------------------------------------------------------------
1118+
10401119
std::vector<std::string> WsjcppYaml::splitToLines(const std::string &sBuffer) {
10411120
std::vector<std::string> vLines;
10421121
std::string sLine = "";

src/wsjcpp_yaml.h

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,55 @@ class WsjcppYamlParserStatus {
210210
void logUnknownLine(const std::string &sPrefix);
211211
};
212212

213+
214+
// ---------------------------------------------------------------------
215+
216+
class WsjcppYamlCursor {
217+
public:
218+
WsjcppYamlCursor(WsjcppYamlItem *pCurrentNode);
219+
WsjcppYamlCursor();
220+
~WsjcppYamlCursor();
221+
222+
// null or undefined
223+
bool isNull() const;
224+
225+
// array
226+
bool isArray() const;
227+
size_t size() const;
228+
WsjcppYamlCursor &push(const std::string &sVal);
229+
WsjcppYamlCursor &push(int nVal);
230+
WsjcppYamlCursor &push(bool bVal);
231+
WsjcppYamlCursor &remove(int nIdx);
232+
233+
// map
234+
bool isMap() const;
235+
std::vector<std::string> keys();
236+
WsjcppYamlCursor &set(const std::string &sName, const std::string &sValue);
237+
WsjcppYamlCursor &set(const std::string &sName, int nValue);
238+
WsjcppYamlCursor &set(const std::string &sName, bool bValue);
239+
WsjcppYamlCursor &remove(const std::string &sKey);
240+
241+
// comment
242+
std::string comment();
243+
WsjcppYamlCursor &comment(const std::string& sComment);
244+
245+
// val
246+
std::string valStr();
247+
WsjcppYamlCursor &val(const std::string &sValue);
248+
bool valInt();
249+
WsjcppYamlCursor &val(int nValue);
250+
bool valBool();
251+
WsjcppYamlCursor &val(bool bValue);
252+
253+
254+
WsjcppYamlCursor operator[](int idx) const;
255+
WsjcppYamlCursor operator[](const std::string &sName) const;
256+
257+
private:
258+
WsjcppYamlItem *m_pCurrentNode;
259+
};
260+
261+
213262
// ---------------------------------------------------------------------
214263

215264
class WsjcppYaml {
@@ -220,9 +269,12 @@ class WsjcppYaml {
220269
bool saveToFile(const std::string &sFileName);
221270
bool loadFromString(const std::string &sBufferName, const std::string &sBuffer, std::string &sError);
222271
bool saveToString(std::string &sBuffer);
223-
224272
WsjcppYamlItem *getRoot();
225273

274+
WsjcppYamlCursor getCursor() const;
275+
WsjcppYamlCursor operator[](int idx) const;
276+
WsjcppYamlCursor operator[](const std::string &sName) const;
277+
226278
private:
227279
std::string TAG;
228280

@@ -242,27 +294,5 @@ class WsjcppYaml {
242294
WsjcppYamlItem *m_pRoot;
243295
};
244296

245-
// ---------------------------------------------------------------------
246-
247-
/*
248-
class WsjcppYamlCursor {
249-
public:
250-
WsjcppYamlCursor(WsjcppYaml *pYaml);
251-
~WsjcppYamlCursor();
252-
WsjcppYamlCursor(WsjcppYaml *pYaml, WsjcppYamlItem *pCurrentNode);
253-
WsjcppYamlCursor &operator[](int idx) {
254-
return WsjcppYamlCursor(pYaml, pYaml->getRoot()->getElement(idx)); // will be call destructor ?
255-
}
256-
WsjcppYamlCursor &operator[](const std::string &sName) { return *(getRoot()->getElement(sName)); }
257-
258-
WsjcppYamlItem &operator[](int idx) { return *(getRoot()->getElement(idx)); }
259-
WsjcppYamlItem &operator[](const std::string &sName) { return *(getRoot()->getElement(sName)); }
260-
261-
private:
262-
WsjcppYaml *m_pYaml;
263-
WsjcppYamlItem *m_pCurrentNode;
264-
};
265-
*/
266-
267297
#endif // WSJCPP_YAML_H
268298

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_remove_element_i
4747
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_memory_leaks.cpp")
4848
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")
50+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_cursor.cpp")
5051

5152
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
5253

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
#include <wsjcpp_yaml.h>
5+
6+
// ---------------------------------------------------------------------
7+
// UnitTestCursor
8+
9+
class UnitTestCursor : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestCursor();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
17+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestCursor)
18+
19+
UnitTestCursor::UnitTestCursor()
20+
: WsjcppUnitTestBase("UnitTestCursor") {
21+
}
22+
23+
// ---------------------------------------------------------------------
24+
25+
bool UnitTestCursor::doBeforeTest() {
26+
// do something before test
27+
return true;
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
void UnitTestCursor::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+
"arr1: # some comment 2\n"
55+
" - some1\n"
56+
" - some2\n"
57+
" - some3\n"
58+
"\n" // empty line
59+
;
60+
61+
WsjcppYaml yaml;
62+
std::string sError;
63+
if (!compare("Error parsing", yaml.loadFromString("test_cursor", sTestYaml, sError), true)) {
64+
WsjcppLog::err(TAG, sError);
65+
return;
66+
}
67+
68+
compare("map1 is null", yaml["map1"].isNull(), false);
69+
compare("map1 is array", yaml["map1"].isArray(), false);
70+
compare("map1 is map", yaml["map1"].isMap(), true);
71+
72+
compare("map1-1111 is null", yaml["map1-1111"].isNull(), true);
73+
compare("map1-1111 is array", yaml["map1-1111"].isArray(), false);
74+
compare("map1-1111 is map", yaml["map1-1111"].isMap(), false);
75+
76+
compare("arr1 is null", yaml["arr1"].isNull(), false);
77+
compare("arr1 is array", yaml["arr1"].isArray(), true);
78+
compare("arr1 is map", yaml["arr1"].isMap(), false);
79+
80+
81+
compare("map use as array", yaml["map1"][0].isNull(), true);
82+
compare("array use as map", yaml["arr1"]["0"].isNull(), true);
83+
84+
}
85+
86+
// ---------------------------------------------------------------------
87+
88+
bool UnitTestCursor::doAfterTest() {
89+
// do somethig after test
90+
return true;
91+
}
92+
93+

unit-tests.wsjcpp/src/unit_test_read_yaml.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ void UnitTestReadYaml::executeTest() {
4545

4646
compare("version-value", yaml.getRoot()->getElement("version")->getStringValue(), "3");
4747
compare("version-value", yaml.getRoot()->getElement("version")->getIntValue(), 3); // wrong or not ?
48-
49-
// TODO unit test source code here
5048
}
5149

5250
// ---------------------------------------------------------------------

wsjcpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ unit-tests:
6161
description: ""
6262
- name: "ReadWriteFile"
6363
description: ""
64+
- name: "Cursor"
65+
description: ""

0 commit comments

Comments
 (0)