Skip to content

Commit 66b20e3

Browse files
committed
Redesign unit-tests.wsjcpp/src/unit_test_append_elements.cpp to src/tests/test_append_elements.cpp
1 parent 2c59999 commit 66b20e3

4 files changed

Lines changed: 86 additions & 76 deletions

File tree

src/tests/test_append_elements.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2019-2025 wsjcpp
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
24+
Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
25+
*/
26+
27+
#include <iostream>
28+
#include <wsjcpp_yaml.h>
29+
30+
int main() {
31+
WsjcppYaml yml;
32+
yml.getRoot()->setElementValue("p1", "val1");
33+
yml.getRoot()->createElementMap("some");
34+
WsjcppYamlNode *pSome = yml.getRoot()->getElement("some");
35+
pSome->setElementValue("p2", "val2");
36+
pSome->createElementMap("sub-some");
37+
WsjcppYamlNode *pSubSome = pSome->getElement("sub-some");
38+
pSubSome->setElementValue("p3", "val3");
39+
pSome->createElementArray("arr-some");
40+
WsjcppYamlNode *pArrSome = pSome->getElement("arr-some");
41+
pArrSome->setComment("some array here");
42+
pArrSome->appendElementValue("1234");
43+
WsjcppYamlPlaceInFile placeInFile;
44+
WsjcppYamlNode *pItemMap = new WsjcppYamlNode(pArrSome, &yml, placeInFile, WSJCPP_YAML_NODE_MAP);
45+
pItemMap->setComment("some comment for a map"); // why not work ????
46+
pArrSome->appendElement(pItemMap);
47+
pItemMap->setElementValue("p4", "val4");
48+
pItemMap->setElementValue("p5", "val5");
49+
yml.getRoot()->setElementValue("p6", "val6");
50+
51+
int ret = 0;
52+
53+
std::string sExpectedYaml = ""
54+
"p1: val1\n"
55+
"some:\n"
56+
" p2: val2\n"
57+
" sub-some:\n"
58+
" p3: val3\n"
59+
" arr-some: # some array here\n"
60+
" - 1234\n"
61+
" - p4: val4\n"
62+
" p5: val5\n"
63+
"p6: val6";
64+
65+
std::string sSaved = yml.getRoot()->toString();
66+
67+
if (sSaved != sExpectedYaml) {
68+
std::cerr << "ERROR (1). Not equal. Expected: \n---\n" << sExpectedYaml << "\n---\n, but got \n---\n" << sSaved << "\n---\n" << std::endl;
69+
ret = -1;
70+
}
71+
72+
std::string sError = "";
73+
std::string sSaved2 = "";
74+
std::string sExpectedYaml2 = sExpectedYaml + "\n";
75+
if (!yml.saveToString(sSaved2, sError)) {
76+
std::cerr << "Error save: " << sError << std::endl;
77+
return -1;
78+
}
79+
80+
if (sSaved2 != sExpectedYaml2) {
81+
std::cerr << "ERROR (2). Not equal. Expected: \n---\n" << sExpectedYaml2 << "\n---\n, but got \n---\n" << sSaved2 << "\n---\n" << std::endl;
82+
ret = -1;
83+
}
84+
85+
return ret;
86+
}

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_t
3838
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_map.cpp")
3939
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_read_yaml.cpp")
4040
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_cursor.cpp")
41-
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_append_elements.cpp")
4241

4342
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
4443

unit-tests.wsjcpp/src/unit_test_append_elements.cpp

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

wsjcpp.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,5 @@ unit-tests:
4646
description: ""
4747
- name: "Cursor"
4848
description: ""
49-
- name: "AppendElements"
50-
description: ""
5149
- name: "ReadWsjcppHoldYml"
5250
description: ""

0 commit comments

Comments
 (0)