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+ std::string sTestYaml =
32+ " # Some comment 1\n "
33+ " param1: \" value1\" # v1\n "
34+ " param2: \" #$!!!value2\" # val 2\n "
35+ " \" param3 olala\" : val 3 # val 3*** \n "
36+ " param4: ' #$!!!value4' # val 4\n "
37+ " 'param5 aha': ' #$!!!value5' # val 5\n "
38+ " url: \" https://github.com/wsjcpp/wsjcpp-yaml\"\n "
39+ " issues: https://github.com/wsjcpp/wsjcpp-yaml/issues\n "
40+ " empty: \"\"\n "
41+ " array:\n "
42+ " - \" https://github.com/wsjcpp/wsjcpp-core:v0.0.1\"\n "
43+ ;
44+
45+ WsjcppYaml yaml;
46+ std::string sError ;
47+ if (!yaml.loadFromString (" parse_quotes" , sTestYaml , sError )) {
48+ std::cerr << " Error parsing: " << sError << std::endl;
49+ return -1 ;
50+ }
51+
52+ int ret = 0 ;
53+
54+ // TODO: .findLine(0)
55+
56+ if (yaml.getRoot ()->getElement (" param1" )->getValue () != " value1" ) {
57+ std::cerr << " Check failed: param1" << std::endl;
58+ ret = -1 ;
59+ }
60+
61+ if (yaml.getRoot ()->getElement (" param1" )->getComment () != " v1" ) {
62+ std::cerr << " Check failed: param1-comment" << std::endl;
63+ ret = -1 ;
64+ }
65+
66+ if (yaml.getRoot ()->getElement (" param2" )->getValue () != " #$!!!value2" ) {
67+ std::cerr << " Check failed: param2" << std::endl;
68+ ret = -1 ;
69+ }
70+
71+ if (yaml.getRoot ()->getElement (" param2" )->getComment () != " val 2" ) {
72+ std::cerr << " Check failed: param2-comment" << std::endl;
73+ ret = -1 ;
74+ }
75+
76+ if (yaml.getRoot ()->getElement (" param3 olala" )->getValue () != " val 3" ) {
77+ std::cerr << " Check failed: param3 olala" << std::endl;
78+ ret = -1 ;
79+ }
80+
81+ if (yaml.getRoot ()->getElement (" param3 olala" )->getComment () != " val 3***" ) {
82+ std::cerr << " Check failed: param3 olala comment" << std::endl;
83+ ret = -1 ;
84+ }
85+
86+ if (yaml.getRoot ()->getElement (" param4" )->getValue () != " #$!!!value4" ) {
87+ std::cerr << " Check failed: param4 val" << std::endl;
88+ ret = -1 ;
89+ }
90+
91+ if (yaml.getRoot ()->getElement (" param4" )->getComment () != " val 4" ) {
92+ std::cerr << " Check failed: param4 val" << std::endl;
93+ ret = -1 ;
94+ }
95+
96+ if (yaml.getRoot ()->getElement (" url" )->getValue () != " https://github.com/wsjcpp/wsjcpp-yaml" ) {
97+ std::cerr << " Check failed: param4 url-value" << std::endl;
98+ ret = -1 ;
99+ }
100+
101+ if (yaml.getRoot ()->getElement (" issues" )->getValue () != " https://github.com/wsjcpp/wsjcpp-yaml/issues" ) {
102+ std::cerr << " Check failed: param4 issues-value" << std::endl;
103+ ret = -1 ;
104+ }
105+
106+ if (yaml.getRoot ()->getElement (" empty" )->getValue () != " " ) {
107+ std::cerr << " Check failed: param4 empty-value" << std::endl;
108+ ret = -1 ;
109+ }
110+
111+ if (yaml.getRoot ()->getElement (" array" )->getElement (0 )->getValue () != " https://github.com/wsjcpp/wsjcpp-core:v0.0.1" ) {
112+ std::cerr << " Check failed: array-element0-value" << std::endl;
113+ ret = -1 ;
114+ }
115+
116+ std::string sSaved = " " ;
117+ if (!yaml.saveToString (sSaved , sError )) {
118+ std::cerr << " Error save: " << sError << std::endl;
119+ return -1 ;
120+ }
121+ std::string sExpected =
122+ " # Some comment 1\n "
123+ " param1: \" value1\" # v1\n "
124+ " param2: \" #$!!!value2\" # val 2\n "
125+ " \" param3 olala\" : val 3 # val 3***\n "
126+ " param4: ' #$!!!value4' # val 4\n "
127+ " 'param5 aha': ' #$!!!value5' # val 5\n "
128+ " url: \" https://github.com/wsjcpp/wsjcpp-yaml\"\n "
129+ " issues: https://github.com/wsjcpp/wsjcpp-yaml/issues\n "
130+ " empty: \"\"\n "
131+ " array:\n "
132+ " - \" https://github.com/wsjcpp/wsjcpp-core:v0.0.1\"\n "
133+ ;
134+ if (sSaved != sExpected ) {
135+ std::cerr << " Check failed: not equal" << std::endl;
136+ ret = -1 ;
137+ }
138+
139+ return ret;
140+ }
0 commit comments