|
| 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 | +#include "helpers.h" |
| 30 | + |
| 31 | +int main() { |
| 32 | + |
| 33 | + std::string sFilepath = find_test_data_file("data/parser-simple-array.yml"); |
| 34 | + WsjcppYaml yaml; |
| 35 | + std::string sError; |
| 36 | + |
| 37 | + if (!yaml.loadFromFile(sFilepath, sError)) { |
| 38 | + std::cerr << "Parsing error " << std::endl; |
| 39 | + return -1; |
| 40 | + } |
| 41 | + |
| 42 | + WsjcppYamlNode *pItem = nullptr; |
| 43 | + |
| 44 | + if (yaml.getRoot()->getElement("param1")->getValue() != "none value1") { |
| 45 | + std::cerr << "param1-value" << std::endl; |
| 46 | + return -1; |
| 47 | + } |
| 48 | + if (yaml.getRoot()->getElement("param1")->getPlaceInFile().getLine() != "param1: none value1 # it's value for something # olala ") { |
| 49 | + std::cerr << "param1-line" << std::endl; |
| 50 | + return -1; |
| 51 | + } |
| 52 | + |
| 53 | + if (yaml.getRoot()->getElement("param1")->getPlaceInFile().getNumberOfLine() != 1) { |
| 54 | + std::cerr << "param1-original-number-of-line" << std::endl; |
| 55 | + return -1; |
| 56 | + } |
| 57 | + if (yaml.getRoot()->getElement("param1")->getComment() != "it's value for something # olala") { |
| 58 | + std::cerr << "param1-comment" << std::endl; |
| 59 | + return -1; |
| 60 | + } |
| 61 | + |
| 62 | + if (yaml.getRoot()->getElement("array-test2")->getLength() != 7) { |
| 63 | + std::cerr << "array-test2-length" << std::endl; |
| 64 | + return -1; |
| 65 | + } |
| 66 | + if (yaml.getRoot()->getElement("array-test2")->getComment() != "some comment 2") { |
| 67 | + std::cerr << "array-test2-comment" << std::endl; |
| 68 | + return -1; |
| 69 | + } |
| 70 | + |
| 71 | + pItem = yaml.getRoot()->getElement("array-test2")->getElement(0); |
| 72 | + if (pItem->getValue() != "value21") { |
| 73 | + std::cerr << "array-test2-element0-value" << std::endl; |
| 74 | + return -1; |
| 75 | + } |
| 76 | + if (pItem->getComment() != "comment v21") { |
| 77 | + std::cerr << "array-test2-element0-comment" << std::endl; |
| 78 | + return -1; |
| 79 | + } |
| 80 | + |
| 81 | + pItem = yaml.getRoot()->getElement("array-test2")->getElement(1); |
| 82 | + if (pItem->getValue() != "value22") { |
| 83 | + std::cerr << "array-test2-element1-value" << std::endl; |
| 84 | + return -1; |
| 85 | + } |
| 86 | + if (pItem->getComment() != "comment v22") { |
| 87 | + std::cerr << "array-test2-element1-comment" << std::endl; |
| 88 | + return -1; |
| 89 | + } |
| 90 | + |
| 91 | + if (yaml.getRoot()->getElement("array-test2")->getElement(2)->getValue() != "true") { |
| 92 | + std::cerr << "array-test2-element2-value" << std::endl; |
| 93 | + return -1; |
| 94 | + } |
| 95 | + if (yaml.getRoot()->getElement("array-test2")->getElement(2)->getPlaceInFile().getLine() != " - true # comment true ") { |
| 96 | + std::cerr << "array-test2-element2-line" << std::endl; |
| 97 | + return -1; |
| 98 | + } |
| 99 | + if (yaml.getRoot()->getElement("array-test2")->getElement(2)->getPlaceInFile().getNumberOfLine() != 5) { |
| 100 | + std::cerr << "array-test2-element2-original-number-of-line" << std::endl; |
| 101 | + return -1; |
| 102 | + } |
| 103 | + if (yaml.getRoot()->getElement("array-test2")->getElement(2)->getComment() != "comment true") { |
| 104 | + std::cerr << "array-test2-element2-comment" << std::endl; |
| 105 | + return -1; |
| 106 | + } |
| 107 | + |
| 108 | + if (yaml.getRoot()->getElement("array-test2")->getElement(3)->getValue() != "falsesome") { |
| 109 | + std::cerr << "array-test2-element3-value" << std::endl; |
| 110 | + return -1; |
| 111 | + } |
| 112 | + if (yaml.getRoot()->getElement("array-test2")->getElement(3)->getPlaceInFile().getLine() != " - falsesome ") { |
| 113 | + std::cerr << "array-test2-element3-line" << std::endl; |
| 114 | + return -1; |
| 115 | + } |
| 116 | + if (yaml.getRoot()->getElement("array-test2")->getElement(3)->getPlaceInFile().getNumberOfLine() != 7) { |
| 117 | + std::cerr << "array-test2-element3-original-number-of-line" << std::endl; |
| 118 | + return -1; |
| 119 | + } |
| 120 | + if (yaml.getRoot()->getElement("array-test2")->getElement(3)->getComment() != "") { |
| 121 | + std::cerr << "array-test2-element3-comment" << std::endl; |
| 122 | + return -1; |
| 123 | + } |
| 124 | + |
| 125 | + if (yaml.getRoot()->getElement("array-test2")->getElement(4)->getValue() != "free@free") { |
| 126 | + std::cerr << "array-test2-element4-value" << std::endl; |
| 127 | + return -1; |
| 128 | + } |
| 129 | + if (yaml.getRoot()->getElement("array-test2")->getElement(4)->getPlaceInFile().getLine() != " - free@free ") { |
| 130 | + std::cerr << "array-test2-element4-line" << std::endl; |
| 131 | + return -1; |
| 132 | + } |
| 133 | + if (yaml.getRoot()->getElement("array-test2")->getElement(4)->getPlaceInFile().getNumberOfLine() != 8) { |
| 134 | + std::cerr << "array-test2-element4-original-number-of-line" << std::endl; |
| 135 | + return -1; |
| 136 | + } |
| 137 | + if (yaml.getRoot()->getElement("array-test2")->getElement(4)->getComment() != "") { |
| 138 | + std::cerr << "array-test2-element4-comment" << std::endl; |
| 139 | + return -1; |
| 140 | + } |
| 141 | + |
| 142 | + if (yaml.getRoot()->getElement("array-test2")->getElement(5)->getValue() != "") { |
| 143 | + std::cerr << "array-test2-element5-value" << std::endl; |
| 144 | + return -1; |
| 145 | + } |
| 146 | + if (yaml.getRoot()->getElement("array-test2")->getElement(5)->getPlaceInFile().getLine() != " - # empty ") { |
| 147 | + std::cerr << "array-test2-element5-line" << std::endl; |
| 148 | + return -1; |
| 149 | + } |
| 150 | + if (yaml.getRoot()->getElement("array-test2")->getElement(5)->getPlaceInFile().getNumberOfLine() != 9) { |
| 151 | + std::cerr << "array-test2-element5-original-number-of-line" << std::endl; |
| 152 | + return -1; |
| 153 | + } |
| 154 | + if (yaml.getRoot()->getElement("array-test2")->getElement(5)->getComment() != "empty") { |
| 155 | + std::cerr << "array-test2-element5-comment" << std::endl; |
| 156 | + return -1; |
| 157 | + } |
| 158 | + |
| 159 | + if (yaml.getRoot()->getElement("array-test2")->getElement(6)->getValue() != "1") { |
| 160 | + std::cerr << "array-test2-element6-value" << std::endl; |
| 161 | + return -1; |
| 162 | + } |
| 163 | + if (yaml.getRoot()->getElement("array-test2")->getElement(6)->getPlaceInFile().getLine() != " - 1") { |
| 164 | + std::cerr << "array-test2-element6-line" << std::endl; |
| 165 | + return -1; |
| 166 | + } |
| 167 | + if (yaml.getRoot()->getElement("array-test2")->getElement(6)->getPlaceInFile().getNumberOfLine() != 10) { |
| 168 | + std::cerr << "array-test2-element6-original-number-of-line" << std::endl; |
| 169 | + return -1; |
| 170 | + } |
| 171 | + if (yaml.getRoot()->getElement("array-test2")->getElement(6)->getComment() != "") { |
| 172 | + std::cerr << "array-test2-element6-comment" << std::endl; |
| 173 | + return -1; |
| 174 | + } |
| 175 | + |
| 176 | + if (yaml.getRoot()->getElement("param2")->getValue() != "val2") { |
| 177 | + std::cerr << "param2-value" << std::endl; |
| 178 | + return -1; |
| 179 | + } |
| 180 | + if (yaml.getRoot()->getElement("param2")->getPlaceInFile().getLine() != "param2: val2 # value 2 ") { |
| 181 | + std::cerr << "param2-line" << std::endl; |
| 182 | + return -1; |
| 183 | + } |
| 184 | + if (yaml.getRoot()->getElement("param2")->getPlaceInFile().getNumberOfLine() != 11) { |
| 185 | + std::cerr << "param2-original-number-of-line" << std::endl; |
| 186 | + return -1; |
| 187 | + } |
| 188 | + if (yaml.getRoot()->getElement("param2")->getComment() != "value 2") { |
| 189 | + std::cerr << "param2-comment" << std::endl; |
| 190 | + return -1; |
| 191 | + } |
| 192 | + |
| 193 | + std::string sSaved = ""; |
| 194 | + if (!yaml.saveToString(sSaved, sError)) { |
| 195 | + std::cerr << "save yaml" << std::endl; |
| 196 | + return -1; |
| 197 | + } |
| 198 | + |
| 199 | + std::string sExpected = "" |
| 200 | + "# simple array test\n" // expected |
| 201 | + "param1: none value1 # it's value for something # olala\n" |
| 202 | + "array-test2: # some comment 2\n" |
| 203 | + " - value21 # comment v21\n" |
| 204 | + " - value22 # comment v22\n" |
| 205 | + " - true # comment true\n" |
| 206 | + " # some\n" |
| 207 | + " - falsesome\n" |
| 208 | + " - free@free\n" |
| 209 | + " - # empty\n" |
| 210 | + " - 1\n" |
| 211 | + "param2: val2 # value 2\n" |
| 212 | + ; |
| 213 | + |
| 214 | + if (sSaved != sExpected) { |
| 215 | + std::cerr << "yaml_save unexpected" << std::endl; |
| 216 | + return -1; |
| 217 | + } |
| 218 | + return 0; |
| 219 | +} |
0 commit comments