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+ }
0 commit comments