Skip to content

Commit a44c5f9

Browse files
committed
Redesigned unit_test_cursor.cpp -> test_cursor.cpp
1 parent ab6349a commit a44c5f9

4 files changed

Lines changed: 352 additions & 180 deletions

File tree

src/tests/test_cursor.cpp

Lines changed: 352 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,352 @@
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 <wsjcpp_yaml.h>
28+
29+
int main() {
30+
31+
std::string sTestYaml =
32+
"# Some comment 1\n"
33+
"map1: \n"
34+
" map11: \n"
35+
" sss: \n"
36+
" map111: \n"
37+
" param1111: v1111\n"
38+
" param1112: v1112\n"
39+
" map112: \n"
40+
" param1121: v1121\n"
41+
" param1122: v1122\n"
42+
" map113: \n"
43+
" param1131: v1131\n"
44+
" param1132: v1132\n"
45+
" map12: \n"
46+
" param121: v121\n"
47+
" param122: v122\n"
48+
" map123: \n"
49+
" param1231: v1231\n"
50+
" param1232: v1232\n"
51+
" param1232: v1232\n"
52+
"param2: v2 # some comment 2\n"
53+
"arr1: # some comment array 1\n"
54+
" - some1\n"
55+
" - 3\n"
56+
"\n"
57+
" - Yes\n"
58+
" # empty\n"
59+
" - no\n"
60+
" - true\n"
61+
" - False\n"
62+
"\n" // empty line
63+
;
64+
65+
WsjcppYaml yaml;
66+
std::string sError;
67+
if (!yaml.loadFromString("test_cursor", sTestYaml, sError)) {
68+
std::cerr << "Error parsing " << sError << std::endl;
69+
return -1;
70+
}
71+
72+
if (yaml["map1"].isNull() != false) {
73+
std::cerr << "map1 is null" << std::endl;
74+
return -1;
75+
}
76+
if (yaml["map1"].isUndefined() != false) {
77+
std::cerr << "map1 is undefined" << std::endl;
78+
return -1;
79+
}
80+
if (yaml["map1"].isValue() != false) {
81+
std::cerr << "map1 is value" << std::endl;
82+
return -1;
83+
}
84+
if (yaml["map1"].isArray() != false) {
85+
std::cerr << "map1 is array" << std::endl;
86+
return -1;
87+
}
88+
if (yaml["map1"].isMap() != true) {
89+
std::cerr << "map1 is map" << std::endl;
90+
return -1;
91+
}
92+
93+
if (yaml["map1-1111"].isNull() != true) {
94+
std::cerr << "map1-1111 is null" << std::endl;
95+
return -1;
96+
}
97+
if (yaml["map1-1111"].isUndefined() != false) {
98+
std::cerr << "map1 is undefined" << std::endl;
99+
return -1;
100+
}
101+
if (yaml["map1-1111"].isValue() != false) {
102+
std::cerr << "map1 is value" << std::endl;
103+
return -1;
104+
}
105+
if (yaml["map1-1111"].isArray() != false) {
106+
std::cerr << "map1-1111 is array" << std::endl;
107+
return -1;
108+
}
109+
if (yaml["map1-1111"].isMap() != false) {
110+
std::cerr << "map1-1111 is map" << std::endl;
111+
return -1;
112+
}
113+
114+
if (yaml["arr1"].isNull() != false) {
115+
std::cerr << "arr1 is null" << std::endl;
116+
return -1;
117+
}
118+
if (yaml["arr1"].isUndefined() != false) {
119+
std::cerr << "arr1 is undefined" << std::endl;
120+
return -1;
121+
}
122+
if (yaml["arr1"].isValue() != false) {
123+
std::cerr << "arr1 is value" << std::endl;
124+
return -1;
125+
}
126+
if (yaml["arr1"].isArray() != true) {
127+
std::cerr << "arr1 is array" << std::endl;
128+
return -1;
129+
}
130+
if (yaml["arr1"].isMap() != false) {
131+
std::cerr << "arr1 is map" << std::endl;
132+
return -1;
133+
}
134+
135+
if (yaml["map1"]["map11"]["sss"].isNull() != false) {
136+
std::cerr << "map1.map11.sss is null" << std::endl;
137+
return -1;
138+
}
139+
if (yaml["map1"]["map11"]["sss"].isUndefined() != true) {
140+
std::cerr << "map1.map11.sss is undefined" << std::endl;
141+
return -1;
142+
}
143+
if (yaml["map1"]["map11"]["sss"].isValue() != false) {
144+
std::cerr << "map1.map11.sss is value" << std::endl;
145+
return -1;
146+
}
147+
if (yaml["map1"]["map11"]["sss"].isArray() != false) {
148+
std::cerr << "map1.map11.sss is array" << std::endl;
149+
return -1;
150+
}
151+
if (yaml["map1"]["map11"]["sss"].isMap() != false) {
152+
std::cerr << "map1.map11.sss is map" << std::endl;
153+
return -1;
154+
}
155+
156+
if (yaml["map1"]["map11"]["map111"]["param1111"].isNull() != false) {
157+
std::cerr << "map1.map11.map111.param1111 is null" << std::endl;
158+
return -1;
159+
}
160+
if (yaml["map1"]["map11"]["map111"]["param1111"].isUndefined() != false) {
161+
std::cerr << "map1.map11.map111.param1111 is undefined" << std::endl;
162+
return -1;
163+
}
164+
if (yaml["map1"]["map11"]["map111"]["param1111"].isValue() != true) {
165+
std::cerr << "map1.map11.map111.param1111 is value" << std::endl;
166+
return -1;
167+
}
168+
if (yaml["map1"]["map11"]["map111"]["param1111"].isArray() != false) {
169+
std::cerr << "map1.map11.map111.param1111 is array" << std::endl;
170+
return -1;
171+
}
172+
if (yaml["map1"]["map11"]["map111"]["param1111"].isMap() != false) {
173+
std::cerr << "map1.map11.map111.param1111 is map" << std::endl;
174+
return -1;
175+
}
176+
177+
if (yaml["map1"][0].isNull() != true) {
178+
std::cerr << "map use as array" << std::endl;
179+
return -1;
180+
}
181+
if (yaml["arr1"]["0"].isNull() != true) {
182+
std::cerr << "array use as map" << std::endl;
183+
return -1;
184+
}
185+
if (yaml["arr1"][0].isNull() != false) {
186+
std::cerr << "array use as array" << std::endl;
187+
return -1;
188+
}
189+
190+
if (yaml["arr1"].comment() != "some comment array 1") {
191+
std::cerr << "array 1 comment" << std::endl;
192+
return -1;
193+
}
194+
if (yaml["arr1"].size() != 6) {
195+
std::cerr << "array size 6" << std::endl;
196+
return -1;
197+
}
198+
if (yaml["arr1"][0].valStr() != "some1") {
199+
std::cerr << "array el 0" << std::endl;
200+
return -1;
201+
}
202+
if (yaml["arr1"][1].valStr() != "3") {
203+
std::cerr << "array el 1" << std::endl;
204+
return -1;
205+
}
206+
if (yaml["arr1"][2].valStr() != "Yes") {
207+
std::cerr << "array el 2" << std::endl;
208+
return -1;
209+
}
210+
if (yaml["arr1"][3].valStr() != "no") {
211+
std::cerr << "array el 3" << std::endl;
212+
return -1;
213+
}
214+
if (yaml["arr1"][4].valStr() != "true") {
215+
std::cerr << "array el 4" << std::endl;
216+
return -1;
217+
}
218+
if (yaml["arr1"][5].valStr() != "False") {
219+
std::cerr << "array el 5" << std::endl;
220+
return -1;
221+
}
222+
223+
if (yaml["arr1"][2].valBool() != true) {
224+
std::cerr << "array bool el 2" << std::endl;
225+
return -1;
226+
}
227+
if (yaml["arr1"][3].valBool() != false) {
228+
std::cerr << "array bool el 3" << std::endl;
229+
return -1;
230+
}
231+
if (yaml["arr1"][4].valBool() != true) {
232+
std::cerr << "array bool el 4" << std::endl;
233+
return -1;
234+
}
235+
if (yaml["arr1"][5].valBool() != false) {
236+
std::cerr << "array bool el 5" << std::endl;
237+
return -1;
238+
}
239+
240+
// int
241+
if (yaml["arr1"][1].valInt() != 3) {
242+
std::cerr << "array 1 val 3" << std::endl;
243+
return -1;
244+
}
245+
yaml["arr1"][1].val(10);
246+
if (yaml["arr1"][1].valInt() != 10) {
247+
std::cerr << "array 1 val 10" << std::endl;
248+
return -1;
249+
}
250+
251+
// string
252+
if (yaml["arr1"][0].valStr() != "some1") {
253+
std::cerr << "array el 0 - 1" << std::endl;
254+
return -1;
255+
}
256+
257+
yaml["arr1"][0].val("different value").comment("1234");
258+
if (yaml["arr1"][0].valStr() != "different value") {
259+
std::cerr << "array el 0 - 2" << std::endl;
260+
return -1;
261+
}
262+
if (yaml["arr1"][0].comment() != "1234") {
263+
std::cerr << "array el 0 - 3" << std::endl;
264+
return -1;
265+
}
266+
267+
// bool
268+
if (yaml["arr1"][4].valStr() != "true") {
269+
std::cerr << "array bool el 4 (1)" << std::endl;
270+
return -1;
271+
}
272+
if (yaml["arr1"][4].node()->getValue() != "true") {
273+
std::cerr << "array bool el 4 (2)" << std::endl;
274+
return -1;
275+
}
276+
if (yaml["arr1"][4].valBool() != true) {
277+
std::cerr << "array bool el 4 (3)" << std::endl;
278+
return -1;
279+
}
280+
yaml["arr1"][4].val(true);
281+
if (yaml["arr1"][4].valStr() != "yes") {
282+
std::cerr << "array bool el 4 (4)" << std::endl;
283+
return -1;
284+
}
285+
if (yaml["arr1"][4].valBool() != true) {
286+
std::cerr << "array bool el 4 (5)" << std::endl;
287+
return -1;
288+
}
289+
yaml["arr1"][4].val(false);
290+
if (yaml["arr1"][4].valStr() != "no") {
291+
std::cerr << "array bool el 4 (6)" << std::endl;
292+
return -1;
293+
}
294+
if (yaml["arr1"][4].valBool() != false) {
295+
std::cerr << "array bool el 4 (7)" << std::endl;
296+
return -1;
297+
}
298+
299+
// map
300+
if (yaml["map1"]["map12"].isMap() != true) {
301+
std::cerr << "map1.map12" << std::endl;
302+
return -1;
303+
}
304+
305+
std::vector<std::string> vKeys = yaml["map1"]["map12"].keys();
306+
if (vKeys.size() != 4) {
307+
std::cerr << "map1.map12 keys size" << std::endl;
308+
return -1;
309+
}
310+
if (vKeys.size() == 4) {
311+
if (vKeys[0] != "param121") {
312+
std::cerr << "map1.map12 keys 0" << std::endl;
313+
return -1;
314+
}
315+
if (vKeys[1] != "param122") {
316+
std::cerr << "map1.map12 keys 1" << std::endl;
317+
return -1;
318+
}
319+
if (vKeys[2] != "map123") {
320+
std::cerr << "map1.map12 keys 2" << std::endl;
321+
return -1;
322+
}
323+
if (vKeys[3] != "param1232") {
324+
std::cerr << "map1.map12 keys 3" << std::endl;
325+
return -1;
326+
}
327+
}
328+
if (yaml["map1"]["map12"].hasKey("some") != false) {
329+
std::cerr << "map1.map12 has key1" << std::endl;
330+
return -1;
331+
}
332+
if (yaml["map1"]["map12"].hasKey("map123") != true) {
333+
std::cerr << "map1.map12 has key2" << std::endl;
334+
return -1;
335+
}
336+
if (yaml["map1"]["map12"]["map123"].isValue() != false) {
337+
std::cerr << "map1.map12 is value" << std::endl;
338+
return -1;
339+
}
340+
if (yaml["map1"]["map12"]["param122"].isValue() != true) {
341+
std::cerr << "map1.map12 is value" << std::endl;
342+
return -1;
343+
}
344+
345+
346+
/* WsjcppYamlCursor &push(const std::string &sVal);
347+
WsjcppYamlCursor &push(int nVal);
348+
WsjcppYamlCursor &push(bool bVal);
349+
WsjcppYamlCursor &remove(int nIdx);*/
350+
351+
return 0;
352+
}

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ list (APPEND WSJCPP_INCLUDE_DIRS "src")
3737
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_array.cpp")
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")
40-
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_cursor.cpp")
4140

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

0 commit comments

Comments
 (0)