Skip to content

Commit 3e5e206

Browse files
committed
Added unit-test for create yaml
1 parent 9b1dbfc commit 3e5e206

File tree

6 files changed

+101
-17
lines changed

6 files changed

+101
-17
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ int main(int argc, char* argv[]) {
5353
return -1;
5454
}
5555

56-
std::cout << "yaml is " << yaml["yaml1"].getValue() << std::endl;
57-
std::cout << "some-map is " << yaml["some-map"].getValue() << std::endl;
58-
std::cout << "some-map2 is " << yaml["some-map2"].getValue() << std::endl;
59-
std::cout << "some-array has " << std::to_string(yaml["some-array"].getLength()) << std::endl;
60-
std::cout << "some-array element 0 is " << yaml["some-array"][0].getValue() << std::endl;
61-
std::cout << "some-array element 1 is " << yaml["some-array"][1].getValue() << std::endl;
62-
std::cout << "some-am has " << std::to_string(yaml["some-am"].getLength()) << std::endl;
56+
std::cout << "yaml is " << yaml["yaml1"].valStr() << std::endl;
57+
std::cout << "some-map is " << yaml["some-map"].valStr() << std::endl;
58+
std::cout << "some-map2 is " << yaml["some-map2"].valStr() << std::endl;
59+
std::cout << "some-array has " << std::to_string(yaml["some-array"].valStr()) << std::endl;
60+
std::cout << "some-array element 0 is " << yaml["some-array"][0].valStr() << std::endl;
61+
std::cout << "some-array element 1 is " << yaml["some-array"][1].valStr() << std::endl;
62+
std::cout << "some-am has " << std::to_string(yaml["some-am"].size()) << std::endl;
6363
std::cout << "some-am is array: " << (yaml["some-am"].isArray() ? "yes" : "no") << std::endl;
64-
std::cout << "some-am has comment " << yaml["some-am"].getComment() << std::endl;
65-
std::cout << "some-am element 0 : p1 is " << yaml["some-am"][0]["p1"].getValue() << std::endl;
66-
std::cout << "some-am element 0 : p2 is " << yaml["some-am"][0]["p2"].getValue() << std::endl;
67-
std::cout << "some-am element 1 : p1 is " << yaml["some-am"][1]["p1"].getValue() << std::endl;
68-
std::cout << "some-am element 1 : p2 is " << yaml["some-am"][1]["p2"].getValue() << std::endl;
64+
std::cout << "some-am has comment " << yaml["some-am"].comment() << std::endl;
65+
std::cout << "some-am element 0 : p1 is " << yaml["some-am"][0]["p1"].valStr() << std::endl;
66+
std::cout << "some-am element 0 : p2 is " << yaml["some-am"][0]["p2"].valStr() << std::endl;
67+
std::cout << "some-am element 1 : p1 is " << yaml["some-am"][1]["p1"].valStr() << std::endl;
68+
std::cout << "some-am element 1 : p2 is " << yaml["some-am"][1]["p2"].valStr() << std::endl;
6969

7070
return 0;
7171
}

src/wsjcpp_yaml.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,15 @@ WsjcppYamlNode::WsjcppYamlNode(
7777
m_nItemType = nItemType;
7878
m_nValueQuotes = WSJCPP_YAML_QUOTES_NONE;
7979
m_nNameQuotes = WSJCPP_YAML_QUOTES_NONE;
80-
m_nNodeDiffIntent = 0;
81-
m_sNodeDiffIntent = "";
80+
// TODO get child intent
81+
if (m_pParent != nullptr && m_pParent->getParent() != nullptr) {
82+
m_nNodeDiffIntent = 2;
83+
m_sNodeDiffIntent = " ";
84+
} else {
85+
m_nNodeDiffIntent = 0;
86+
m_sNodeDiffIntent = "";
87+
}
88+
8289
TAG = "WsjcppYamlNode";
8390
}
8491

@@ -327,6 +334,7 @@ bool WsjcppYamlNode::createElementMap(const std::string &sName, WsjcppYamlQuotes
327334
WsjcppYamlPlaceInFile pl;
328335
WsjcppYamlNode *pNewItem = new WsjcppYamlNode(this, pl, WSJCPP_YAML_NODE_MAP);
329336
pNewItem->setName(sName, nNameQuotes);
337+
// pNewItem->setNodeIntents({2});
330338
this->setElement(sName, pNewItem);
331339
return true;
332340
}
@@ -1203,7 +1211,7 @@ WsjcppYamlCursor WsjcppYamlCursor::operator[](const std::string &sName) const {
12031211
// WsjcppYaml
12041212

12051213
WsjcppYaml::WsjcppYaml() {
1206-
m_pRoot = nullptr;
1214+
m_pRoot = new WsjcppYamlNode(nullptr, WsjcppYamlPlaceInFile(), WSJCPP_YAML_NODE_MAP);
12071215
TAG = "WsjcppYaml";
12081216
}
12091217

src/wsjcpp_yaml.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ class WsjcppYamlNode {
9999
WsjcppYamlQuotes nValueQuotes = WSJCPP_YAML_QUOTES_NONE
100100
);
101101

102-
bool createElementMap(const std::string &sName, WsjcppYamlQuotes nNameQuotes);
102+
bool createElementMap(const std::string &sName, WsjcppYamlQuotes nNameQuotes = WSJCPP_YAML_QUOTES_NONE);
103103
WsjcppYamlNode *createElementMap();
104-
bool createElementArray(const std::string &sName, WsjcppYamlQuotes nNameQuotes);
104+
bool createElementArray(const std::string &sName, WsjcppYamlQuotes nNameQuotes = WSJCPP_YAML_QUOTES_NONE);
105105

106106
bool isArray();
107107
int getLength();

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_read_write_file.
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")
5252
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_cleanup.cpp")
53+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_append_elements.cpp")
5354

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
#include <wsjcpp_yaml.h>
5+
6+
// ---------------------------------------------------------------------
7+
// UnitTestAppendElements
8+
9+
class UnitTestAppendElements : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestAppendElements();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
17+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestAppendElements)
18+
19+
UnitTestAppendElements::UnitTestAppendElements()
20+
: WsjcppUnitTestBase("UnitTestAppendElements") {
21+
}
22+
23+
// ---------------------------------------------------------------------
24+
25+
bool UnitTestAppendElements::doBeforeTest() {
26+
// do something before test
27+
return true;
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
void UnitTestAppendElements::executeTest() {
33+
34+
WsjcppYaml yml;
35+
yml.getRoot()->setElementValue("p1", "val1");
36+
yml.getRoot()->createElementMap("some");
37+
WsjcppYamlNode *pSome = yml.getRoot()->getElement("some");
38+
pSome->setElementValue("p2", "val2");
39+
pSome->createElementMap("sub-some");
40+
WsjcppYamlNode *pSubSome = pSome->getElement("sub-some");
41+
pSubSome->setElementValue("p3", "val3");
42+
pSome->createElementArray("arr-some");
43+
WsjcppYamlNode *pArrSome = pSome->getElement("arr-some");
44+
pArrSome->appendElementValue("1234");
45+
WsjcppYamlPlaceInFile placeInFile;
46+
WsjcppYamlNode *pItemMap = new WsjcppYamlNode(pArrSome, placeInFile, WSJCPP_YAML_NODE_MAP);
47+
pArrSome->appendElement(pItemMap);
48+
pItemMap->setElementValue("p4", "val4");
49+
pItemMap->setElementValue("p5", "val5");
50+
yml.getRoot()->setElementValue("p6", "val6");
51+
52+
compare("created yaml 1", yml.getRoot()->toString(),
53+
"p1: val1\n"
54+
"some:\n"
55+
" p2: val2\n"
56+
" sub-some:\n"
57+
" p3: val3\n"
58+
" arr-some:\n"
59+
" - 1234\n"
60+
" - p4: val4\n"
61+
" p5: val5\n"
62+
"p6: val6"
63+
);
64+
}
65+
66+
// ---------------------------------------------------------------------
67+
68+
bool UnitTestAppendElements::doAfterTest() {
69+
// do somethig after test
70+
return true;
71+
}
72+
73+

wsjcpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,5 @@ unit-tests:
6767
description: ""
6868
- name: "Cleanup"
6969
description: ""
70+
- name: "AppendElements"
71+
description: ""

0 commit comments

Comments
 (0)