hemplate

Simple XML template engine
git clone git://git.dimitrijedobrota.com/hemplate.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

element_test.cpp (5317B)


0 #include "hemplate/element.hpp" 1 2 #include <catch2/catch_test_macros.hpp> 3 4 using namespace hemplate; // NOLINT 5 6 TEST_CASE("boolean", "[element]") 7 { 8 using tag = element_boolean<"tag">; 9 using child = element_boolean<"child">; 10 11 SECTION("empty") 12 { 13 const auto t = tag {}; 14 15 REQUIRE(std::string(t) == "<tag>\n</tag>\n"); 16 } 17 18 SECTION("attribute") 19 { 20 const auto t = tag {{{"attr", "val"}}}; 21 22 REQUIRE(std::string(t) == "<tag attr=\"val\">\n</tag>\n"); 23 } 24 25 SECTION("data") 26 { 27 const auto t = tag {"text"}; 28 29 REQUIRE(std::string(t) == "<tag>text</tag>\n"); 30 } 31 32 SECTION("attribute data") 33 { 34 const auto t = tag {{{"attr", "val"}}, "text"}; 35 36 REQUIRE(std::string(t) == "<tag attr=\"val\">text</tag>\n"); 37 } 38 39 SECTION("child") 40 { 41 const auto t = tag { 42 child {}, 43 }; 44 45 REQUIRE(std::string(t) == "<tag>\n <child>\n </child>\n</tag>\n"); 46 } 47 48 SECTION("attribute child") 49 { 50 const auto t = tag { 51 {{"attr", "val"}}, 52 child {}, 53 }; 54 55 REQUIRE( 56 std::string(t) == "<tag attr=\"val\">\n <child>\n </child>\n</tag>\n" 57 ); 58 } 59 60 SECTION("child data") 61 { 62 const auto t = tag { 63 child {"text"}, 64 }; 65 66 REQUIRE(std::string(t) == "<tag>\n <child>text</child>\n</tag>\n"); 67 } 68 69 SECTION("attribute child data") 70 { 71 const auto t = tag { 72 {{"attr", "val"}}, 73 child {"text"}, 74 }; 75 76 REQUIRE( 77 std::string(t) == "<tag attr=\"val\">\n <child>text</child>\n</tag>\n" 78 ); 79 } 80 81 SECTION("range") 82 { 83 const std::vector<std::string> vec = {"hello", "world"}; 84 const auto t = tag {vec}; 85 86 REQUIRE(std::string(t) == "<tag>\n hello\n world\n</tag>\n"); 87 } 88 89 SECTION("tag element elemetn tag") 90 { 91 const auto t = tag {element {element {tag {}}}}; 92 REQUIRE(std::string(t) == "<tag>\n <tag>\n </tag>\n</tag>\n"); 93 } 94 } 95 96 TEST_CASE("atomic", "[element]") 97 { 98 using tag = element_atomic<"tag">; 99 100 SECTION("empty") 101 { 102 const auto t = tag {}; 103 104 REQUIRE(std::string(t) == "<tag />\n"); 105 } 106 107 SECTION("attribute") 108 { 109 const auto t = tag {{{"attr", "val"}}}; 110 111 REQUIRE(std::string(t) == "<tag attr=\"val\" />\n"); 112 } 113 } 114 115 TEST_CASE("element", "[element]") 116 { 117 using tag = element_boolean<"tag">; 118 using child = element_boolean<"child">; 119 120 SECTION("empty") 121 { 122 const auto t = element {}; 123 124 REQUIRE(std::string(t) == ""); // NOLINT 125 } 126 127 SECTION("data") 128 { 129 const auto t = element {"text"}; 130 131 REQUIRE(std::string(t) == "text\n"); 132 } 133 134 SECTION("tag") 135 { 136 const auto t = element { 137 tag {}, 138 }; 139 140 REQUIRE(std::string(t) == "<tag>\n</tag>\n"); 141 } 142 143 SECTION("tag element") 144 { 145 const auto t = tag { 146 element {}, 147 }; 148 149 REQUIRE(std::string(t) == "<tag>\n</tag>\n"); 150 } 151 152 SECTION("element tag") 153 { 154 const auto t = element { 155 tag {}, 156 }; 157 158 REQUIRE(std::string(t) == "<tag>\n</tag>\n"); 159 } 160 161 SECTION("element tag data") 162 { 163 const auto t = element { 164 tag {"text"}, 165 }; 166 167 REQUIRE(std::string(t) == "<tag>text</tag>\n"); 168 } 169 170 SECTION("tag element data") 171 { 172 const auto t = tag { 173 element {"text"}, 174 }; 175 176 REQUIRE(std::string(t) == "<tag>text</tag>\n"); 177 } 178 179 SECTION("tag element data") 180 { 181 const auto t = tag { 182 element {"text"}, 183 }; 184 185 REQUIRE(std::string(t) == "<tag>text</tag>\n"); 186 } 187 188 SECTION("element tag child data") 189 { 190 const auto t = element { 191 tag { 192 child { 193 "text", 194 }, 195 }, 196 }; 197 198 REQUIRE(std::string(t) == "<tag>\n <child>text</child>\n</tag>\n"); 199 } 200 201 SECTION("element tag element child data") 202 { 203 const auto t = element { 204 tag { 205 element { 206 child { 207 "text", 208 }, 209 }, 210 }, 211 }; 212 213 REQUIRE(std::string(t) == "<tag>\n <child>text</child>\n</tag>\n"); 214 } 215 216 SECTION("element tag child element data") 217 { 218 const auto t = element { 219 tag { 220 child { 221 element { 222 "text", 223 }, 224 }, 225 }, 226 }; 227 228 REQUIRE(std::string(t) == "<tag>\n <child>text</child>\n</tag>\n"); 229 } 230 231 SECTION("element tag element child element data") 232 { 233 const auto t = element { 234 tag { 235 element { 236 child { 237 element { 238 "text", 239 }, 240 }, 241 }, 242 }, 243 }; 244 245 REQUIRE(std::string(t) == "<tag>\n <child>text</child>\n</tag>\n"); 246 } 247 248 SECTION("tag element child data") 249 { 250 const auto t = tag { 251 element { 252 child { 253 "text", 254 }, 255 }, 256 }; 257 258 REQUIRE(std::string(t) == "<tag>\n <child>text</child>\n</tag>\n"); 259 } 260 261 SECTION("tag child element data") 262 { 263 const auto t = tag { 264 child { 265 element { 266 "text", 267 }, 268 }, 269 }; 270 271 REQUIRE(std::string(t) == "<tag>\n <child>text</child>\n</tag>\n"); 272 } 273 274 SECTION("tag element child element data") 275 { 276 const auto t = tag { 277 element { 278 child { 279 element { 280 "text", 281 }, 282 }, 283 }, 284 }; 285 286 REQUIRE(std::string(t) == "<tag>\n <child>text</child>\n</tag>\n"); 287 } 288 }