Skip to content

Commit 3a18bb7

Browse files
committed
Added simple array test
1 parent b80b175 commit 3a18bb7

8 files changed

+134
-13
lines changed

src/wsjcpp_yaml.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,30 +235,37 @@ void WSJCppYAMLItem::setValue(const std::string &sValue) {
235235

236236
// ---------------------------------------------------------------------
237237

238-
std::string WSJCppYAMLItem::toString() {
238+
std::string WSJCppYAMLItem::toString(std::string sIntent) {
239239
std::string sRet = "";
240240
if (this->isValue()) {
241241
sRet = m_sValue;
242242
if (m_sComment.length() > 0) {
243243
if (sRet.length() > 0) {
244244
sRet += " ";
245245
}
246-
sRet += "#" + m_sComment;
246+
sRet += "# " + m_sComment;
247247
}
248248
} else if (this->isEmpty()) {
249249
if (m_sComment.length() > 0) {
250-
sRet += "#" + m_sComment;
250+
sRet += "# " + m_sComment;
251251
}
252252
} else if (this->isArray()) {
253253
sRet += "\n";
254254
for (int i = 0; i < m_vObjects.size(); i++) {
255-
sRet += " - " + m_vObjects[i]->toString();
255+
sRet += sIntent + "- " + m_vObjects[i]->toString();
256256
sRet += "\n";
257257
}
258258
} else if (this->isMap()) {
259259
for (int i = 0; i < m_vObjects.size(); i++) {
260-
if (m_vObjects[i]->isEmpty() ) {
261-
sRet += m_vObjects[i]->toString();
260+
WSJCppYAMLItem *pItem = m_vObjects[i];
261+
if (pItem->isEmpty() ) {
262+
sRet += pItem->toString(sIntent + " ");
263+
} else if (pItem->isArray() || pItem->isMap()) {
264+
sRet += pItem->getName() + ":";
265+
if (pItem->getComment().length() > 0) {
266+
sRet += " # " + pItem->getComment();
267+
}
268+
sRet += m_vObjects[i]->toString(sIntent + " ");
262269
} else {
263270
sRet += m_vObjects[i]->getName() + ": " + m_vObjects[i]->toString();
264271
}
@@ -267,7 +274,9 @@ std::string WSJCppYAMLItem::toString() {
267274
} else {
268275
sRet = "TODO: undefined";
269276
}
270-
277+
if (sIntent == "") {
278+
Fallen::trim(sRet);
279+
}
271280
return sRet;
272281
}
273282

@@ -436,6 +445,8 @@ void WSJCppYAMLParsebleLine::parseLine(const std::string &sLine) {
436445
m_bValueWasWithQuotes = true;
437446
m_sValue = removeStringDoubleQuotes(m_sValue);
438447
}
448+
449+
Fallen::trim(m_sComment);
439450
}
440451

441452
// ---------------------------------------------------------------------
@@ -695,7 +706,15 @@ void WSJCppYAML::process_sameIntent_emptyName_hasValue_noArrayItem(WSJCppYAMLPar
695706
// ---------------------------------------------------------------------
696707

697708
void WSJCppYAML::process_sameIntent_emptyName_emptyValue_arrayItem(WSJCppYAMLParserStatus &st) {
698-
st.logUnknownLine("TODO process_sameIntent_emptyName_emptyValue_arrayItem");
709+
if (st.pCurItem->isUndefined()) {
710+
st.pCurItem->doArray();
711+
}
712+
WSJCppYAMLItem *pItem = new WSJCppYAMLItem(st.pCurItem, st.nLine, st.sLine, WSJCppYAMLItemType::WSJCPP_YAML_ITEM_VALUE);
713+
pItem->setComment(st.line.getComment());
714+
pItem->setValue(st.line.getValue());
715+
st.pCurItem->appendElement(pItem);
716+
st.pCurItem = pItem;
717+
st.nIntent = st.nIntent + 2;
699718
}
700719

701720
// ---------------------------------------------------------------------

src/wsjcpp_yaml.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class WSJCppYAMLItem {
6262
std::string getValue();
6363
void setValue(const std::string &sValue);
6464

65-
std::string toString();
65+
std::string toString(std::string sIntent = "");
6666
std::string getItemTypeAsString();
6767

6868
private:

unit-tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ list (APPEND WSJCPP_SOURCES "./src/unit_tests.h")
2929
list (APPEND WSJCPP_SOURCES "./src/unit_tests.cpp")
3030
list (APPEND WSJCPP_SOURCES "./src/unit_test_line_parser.h")
3131
list (APPEND WSJCPP_SOURCES "./src/unit_test_line_parser.cpp")
32+
list (APPEND WSJCPP_SOURCES "./src/unit_test_yaml_parser_simple_array.h")
33+
list (APPEND WSJCPP_SOURCES "./src/unit_test_yaml_parser_simple_array.cpp")
3234
list (APPEND WSJCPP_SOURCES "./src/unit_test_yaml_parser_simple_map.h")
3335
list (APPEND WSJCPP_SOURCES "./src/unit_test_yaml_parser_simple_map.cpp")
3436
# list (APPEND WSJCPP_SOURCES "./src/unit_test_yaml_parser.h")

unit-tests/src/unit_test_line_parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ bool UnitTestLineParser::run() {
5050

5151
std::vector<LineTest> vTestLines;
5252

53-
vTestLines.push_back(LineTest(1, "# Some comment 1 ", "", false, "", "", " Some comment 1 "));
54-
vTestLines.push_back(LineTest(2, " test2: \"t\\\"wo\" # some comment 2", " ", false, "test2", "t\"wo", " some comment 2"));
53+
vTestLines.push_back(LineTest(1, "# Some comment 1 ", "", false, "", "", "Some comment 1"));
54+
vTestLines.push_back(LineTest(2, " test2: \"t\\\"wo\" # some comment 2 ", " ", false, "test2", "t\"wo", "some comment 2"));
5555
vTestLines.push_back(LineTest(3, " test3:", " ", false, "test3", "", ""));
5656
vTestLines.push_back(LineTest(4, " - test4", " ", true, "", "test4", ""));
5757
vTestLines.push_back(LineTest(5, "", "", false, "", "", ""));

unit-tests/src/unit_test_yaml_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bool UnitTestYamlParser::run() {
6767

6868
pItem = yaml.getRoot()->getElement("array30")->getElement(0);
6969
compareS(bTestSuccess, "test30_value", pItem->getValue(), "one31");
70-
compareS(bTestSuccess, "test30_comment", pItem->getComment(), " this field for test array30 ");
70+
compareS(bTestSuccess, "test30_comment", pItem->getComment(), "this field for test array30");
7171
pItem = yaml.getRoot()->getElement("array30")->getElement(1);
7272
compareS(bTestSuccess, "test40_value", pItem->getValue(), "two32");
7373
compareS(bTestSuccess, "test40_comment", pItem->getComment(), "");
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "unit_test_yaml_parser_simple_array.h"
2+
#include <vector>
3+
#include <iostream>
4+
#include <fallen.h>
5+
#include <wsjcpp_yaml.h>
6+
7+
REGISTRY_UNIT_TEST(UnitTestYamlParserSimpleArray)
8+
9+
UnitTestYamlParserSimpleArray::UnitTestYamlParserSimpleArray()
10+
: UnitTestBase("UnitTestYamlParserSimpleArray") {
11+
//
12+
}
13+
14+
// ---------------------------------------------------------------------
15+
16+
void UnitTestYamlParserSimpleArray::init() {
17+
// nothing
18+
}
19+
20+
// ---------------------------------------------------------------------
21+
22+
bool UnitTestYamlParserSimpleArray::run() {
23+
24+
std::string g_sTestYaml =
25+
"# simple array test\n"
26+
"param1: none value1 # it's value for something # olala \n"
27+
"array-test2 : # some comment 2 \n"
28+
" - value21 # comment v21 \n"
29+
" - value22 # comment v22 \n"
30+
" - true # comment true \n"
31+
" - falsesome \n"
32+
" - free@free \n"
33+
" - # empty \n"
34+
" - 1\n"
35+
;
36+
37+
bool bTestSuccess = true;
38+
39+
WSJCppYAML yaml;
40+
if (yaml.loadFromString(g_sTestYaml)) {
41+
Log::throw_err(TAG, "Error parsing");
42+
return -1;
43+
}
44+
45+
WSJCppYAMLItem *pItem = nullptr;
46+
47+
compareS(bTestSuccess, "param1-value", yaml.getRoot()->getElement("param1")->getValue(), "none value1");
48+
compareS(bTestSuccess, "param1-line", yaml.getRoot()->getElement("param1")->getOriginalLine(), "param1: none value1 # it's value for something # olala ");
49+
compareN(bTestSuccess, "param1-original-number-of-line", yaml.getRoot()->getElement("param1")->getOriginalNumberOfLine(), 1);
50+
compareS(bTestSuccess, "param1-comment", yaml.getRoot()->getElement("param1")->getComment(), "it's value for something # olala");
51+
52+
compareN(bTestSuccess, "array-test2-length", yaml.getRoot()->getElement("array-test2")->getLength(), 7);
53+
compareS(bTestSuccess, "array-test2-comment", yaml.getRoot()->getElement("array-test2")->getComment(), "some comment 2");
54+
55+
pItem = yaml.getRoot()->getElement("array-test2")->getElement(0);
56+
compareS(bTestSuccess, "array-test2-element0-value", pItem->getValue(), "value21");
57+
compareS(bTestSuccess, "array-test2-element0-comment", pItem->getComment(), "comment v21");
58+
59+
pItem = yaml.getRoot()->getElement("array-test2")->getElement(1);
60+
compareS(bTestSuccess, "array-test2-element1-value", pItem->getValue(), "value22");
61+
compareS(bTestSuccess, "array-test2-element1-comment", pItem->getComment(), "comment v22");
62+
63+
std::string sSaved = "";
64+
if (yaml.saveToString(sSaved)) {
65+
compareS(bTestSuccess, "yaml_save", sSaved,
66+
"# simple array test\n" // expected
67+
"param1: none value1 # it's value for something # olala\n"
68+
"array-test2: # some comment 2\n"
69+
" - value21 # comment v21\n"
70+
" - value22 # comment v22\n"
71+
" - true # comment true\n"
72+
" - falsesome\n"
73+
" - free@free\n"
74+
" - # empty\n"
75+
" - 1"
76+
);
77+
} else {
78+
Log::err(TAG, "Could not save to string");
79+
bTestSuccess = false;
80+
}
81+
return bTestSuccess;
82+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef UNIT_TEST_YAML_PARSER_SIMPLE_ARRAY_H
2+
#define UNIT_TEST_YAML_PARSER_SIMPLE_ARRAY_H
3+
4+
#include <unit_tests.h>
5+
6+
class UnitTestYamlParserSimpleArray : public UnitTestBase {
7+
public:
8+
UnitTestYamlParserSimpleArray();
9+
virtual void init();
10+
virtual bool run();
11+
};
12+
13+
#endif // UNIT_TEST_YAML_PARSER_SIMPLE_ARRAY_H

unit-tests/src/unit_test_yaml_parser_simple_map.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ bool UnitTestYamlParserSimpleMap::run() {
3939
WSJCppYAMLItem *pItem = nullptr;
4040
compareS(bTestSuccess, "param1", yaml.getRoot()->getElement("param1")->getValue(), "value1");
4141
compareS(bTestSuccess, "param2", yaml.getRoot()->getElement("param2")->getValue(), "value2");
42+
compareS(bTestSuccess, "param2", yaml.getRoot()->getElement("param2")->getComment(), "some comment 2");
4243

4344
std::string sSaved = "";
4445
if (yaml.saveToString(sSaved)) {
45-
compareS(bTestSuccess, "yaml_save", sSaved, g_sTestYaml);
46+
compareS(bTestSuccess, "yaml_save", sSaved,
47+
"# Some comment 1\n"
48+
"param1: value1\n"
49+
"param2: value2 # some comment 2"
50+
);
4651
} else {
4752
Log::err(TAG, "Could not save to string");
4853
bTestSuccess = false;

0 commit comments

Comments
 (0)