Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
| 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
| 4 | * |
| 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
Václav Kubernát | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 9 | #include "trompeloeil_doctest.hpp" |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 10 | #include "completion.hpp" |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 11 | #include "leaf_data_helpers.hpp" |
Václav Kubernát | 2e4cafe | 2020-11-05 01:53:21 +0100 | [diff] [blame] | 12 | #include "libyang_utils.hpp" |
| 13 | #include "pretty_printers.hpp" |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 14 | #include "utils.hpp" |
| 15 | |
| 16 | TEST_CASE("utils") |
| 17 | { |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 18 | SECTION("filterByPrefix") |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 19 | { |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 20 | std::set<Completion> set{{"ahoj"}, {"coze"}, {"copak"}, {"aha"}, {"polivka"}}; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 21 | |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 22 | REQUIRE((filterByPrefix(set, "a") == std::set<Completion>{{"ahoj"}, {"aha"}})); |
| 23 | REQUIRE((filterByPrefix(set, "ah") == std::set<Completion>{{"ahoj"}, {"aha"}})); |
| 24 | REQUIRE((filterByPrefix(set, "aho") == std::set<Completion>{{"ahoj"}})); |
| 25 | REQUIRE((filterByPrefix(set, "polivka") == std::set<Completion>{{"polivka"}})); |
| 26 | REQUIRE((filterByPrefix(set, "polivkax") == std::set<Completion>{})); |
| 27 | REQUIRE((filterByPrefix(set, "co") == std::set<Completion>{{"copak"}, {"coze"}})); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 28 | } |
Václav Kubernát | a44bdf2 | 2020-01-24 12:15:31 +0100 | [diff] [blame] | 29 | |
| 30 | SECTION("joinPaths") { |
| 31 | std::string prefix, suffix, result; |
| 32 | |
| 33 | SECTION("regular") { |
| 34 | prefix = "/example:a"; |
| 35 | suffix = "leaf"; |
| 36 | result = "/example:a/leaf"; |
| 37 | } |
| 38 | |
| 39 | SECTION("no prefix - absolute path") { |
| 40 | suffix = "/example:a/leaf"; |
| 41 | result = "/example:a/leaf"; |
| 42 | } |
| 43 | |
| 44 | SECTION("no prefix - relative path") { |
| 45 | suffix = "example:a/leaf"; |
| 46 | result = "example:a/leaf"; |
| 47 | } |
| 48 | |
| 49 | SECTION("no suffix") { |
| 50 | prefix = "/example:a/leaf"; |
| 51 | result = "/example:a/leaf"; |
| 52 | } |
| 53 | |
| 54 | SECTION("at root") { |
| 55 | prefix = "/"; |
| 56 | suffix = "example:a"; |
| 57 | result = "/example:a"; |
| 58 | } |
| 59 | |
| 60 | SECTION("trailing slash") { |
| 61 | prefix = "/example:a"; |
| 62 | suffix = "/"; |
| 63 | result = "/example:a/"; |
| 64 | } |
| 65 | |
| 66 | SECTION("prefix ends with slash") { |
| 67 | prefix = "/example:a/"; |
| 68 | suffix = "leaf"; |
| 69 | result = "/example:a/leaf"; |
| 70 | } |
| 71 | |
| 72 | SECTION("suffix starts with slash") { |
| 73 | prefix = "/example:a"; |
| 74 | suffix = "/leaf"; |
| 75 | result = "/example:a/leaf"; |
| 76 | } |
| 77 | |
| 78 | SECTION("slashes all the way to eleven") { |
| 79 | prefix = "/example:a/"; |
| 80 | suffix = "/leaf"; |
| 81 | result = "/example:a/leaf"; |
| 82 | } |
| 83 | |
| 84 | REQUIRE(joinPaths(prefix, suffix) == result); |
| 85 | } |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 86 | |
| 87 | SECTION("leafDataTypeToString") |
| 88 | { |
| 89 | yang::LeafDataType type; |
| 90 | std::string expected; |
| 91 | SECTION("union") |
| 92 | { |
| 93 | type = yang::Union{{ |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 94 | yang::TypeInfo{yang::String{}}, |
| 95 | yang::TypeInfo{createEnum({"foo", "bar"})}, |
| 96 | yang::TypeInfo{yang::Int8{}}, |
| 97 | yang::TypeInfo{yang::Int64{}}, |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 98 | }}; |
| 99 | expected = "a string, an enum, an 8-bit integer, a 64-bit integer"; |
| 100 | } |
| 101 | |
| 102 | REQUIRE(leafDataTypeToString(type) == expected); |
| 103 | |
| 104 | } |
| 105 | |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 106 | } |
Václav Kubernát | 2e4cafe | 2020-11-05 01:53:21 +0100 | [diff] [blame] | 107 | |
| 108 | const auto schema = R"( |
| 109 | module test-schema { |
| 110 | namespace "http://example.com/ayyyy"; |
| 111 | prefix AHOJ; |
| 112 | |
| 113 | leaf int8 { |
| 114 | type int8; |
| 115 | } |
| 116 | leaf int16 { |
| 117 | type int16; |
| 118 | } |
| 119 | leaf int32 { |
| 120 | type int32; |
| 121 | } |
| 122 | leaf int64 { |
| 123 | type int64; |
| 124 | } |
| 125 | leaf uint8 { |
| 126 | type uint8; |
| 127 | } |
| 128 | leaf uint16 { |
| 129 | type uint16; |
| 130 | } |
| 131 | leaf uint32 { |
| 132 | type uint32; |
| 133 | } |
| 134 | leaf uint64 { |
| 135 | type uint64; |
| 136 | } |
| 137 | leaf boolean { |
| 138 | type boolean; |
| 139 | } |
| 140 | leaf string { |
| 141 | type string; |
| 142 | } |
| 143 | leaf enum { |
| 144 | type enumeration { |
| 145 | enum A; |
| 146 | enum B; |
| 147 | enum C; |
| 148 | } |
| 149 | } |
| 150 | identity food; |
| 151 | identity apple { |
| 152 | base "food"; |
| 153 | } |
| 154 | leaf identityRef { |
| 155 | type identityref { |
| 156 | base "food"; |
| 157 | } |
| 158 | } |
| 159 | leaf binary { |
| 160 | type binary; |
| 161 | } |
| 162 | leaf empty { |
| 163 | type empty; |
| 164 | } |
| 165 | leaf bits { |
| 166 | type bits { |
| 167 | bit a; |
| 168 | bit b; |
| 169 | bit AHOJ; |
| 170 | } |
| 171 | } |
| 172 | leaf dec64 { |
| 173 | type decimal64 { |
| 174 | fraction-digits 5; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | list stuff { |
| 179 | key "name"; |
| 180 | leaf name { |
| 181 | type string; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | leaf leafRefPresent { |
| 186 | type leafref { |
| 187 | path ../stuff/name; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | leaf leafRefNonPresent { |
| 192 | type leafref { |
| 193 | path ../stuff/name; |
| 194 | } |
| 195 | } |
Václav Kubernát | 61d92d7 | 2020-11-06 03:14:18 +0100 | [diff] [blame^] | 196 | |
| 197 | container users { |
| 198 | config false; |
| 199 | list userList { |
| 200 | leaf name { |
| 201 | type string; |
| 202 | } |
| 203 | } |
| 204 | } |
Václav Kubernát | 2e4cafe | 2020-11-05 01:53:21 +0100 | [diff] [blame] | 205 | } |
| 206 | )"; |
| 207 | |
| 208 | const auto data = R"( |
| 209 | { |
| 210 | "test-schema:int8": 8, |
| 211 | "test-schema:int16": 300, |
| 212 | "test-schema:int32": -300, |
| 213 | "test-schema:int64": -999999999999999, |
| 214 | "test-schema:uint8": 8, |
| 215 | "test-schema:uint16": 300, |
| 216 | "test-schema:uint32": 300, |
| 217 | "test-schema:uint64": 999999999999999, |
| 218 | "test-schema:boolean": true, |
| 219 | "test-schema:string": "AHOJ", |
| 220 | "test-schema:enum": "A", |
| 221 | "test-schema:identityRef": "apple", |
| 222 | "test-schema:binary": "QUhPSgo=", |
| 223 | "test-schema:empty": "", |
| 224 | "test-schema:bits": "a AHOJ", |
| 225 | "test-schema:dec64": "43242.43260", |
| 226 | "test-schema:stuff": [ |
| 227 | { |
| 228 | "name": "Xaver" |
| 229 | } |
| 230 | ], |
| 231 | "test-schema:leafRefPresent": "Xaver", |
Václav Kubernát | 61d92d7 | 2020-11-06 03:14:18 +0100 | [diff] [blame^] | 232 | "test-schema:leafRefNonPresent": "Lucas", |
| 233 | "test-schema:users": { |
| 234 | "userList": [ |
| 235 | { |
| 236 | "name": "John" |
| 237 | }, |
| 238 | { |
| 239 | "name": "Aneta" |
| 240 | }, |
| 241 | { |
| 242 | "name": "Aneta" |
| 243 | } |
| 244 | ] |
| 245 | } |
Václav Kubernát | 2e4cafe | 2020-11-05 01:53:21 +0100 | [diff] [blame] | 246 | } |
| 247 | )"; |
| 248 | |
| 249 | |
| 250 | TEST_CASE("libyang_utils") |
| 251 | { |
| 252 | auto ctx = std::make_shared<libyang::Context>(); |
| 253 | ctx->parse_module_mem(schema, LYS_IN_YANG); |
Václav Kubernát | 61d92d7 | 2020-11-06 03:14:18 +0100 | [diff] [blame^] | 254 | auto dataNode = ctx->parse_data_mem(data, LYD_JSON, LYD_OPT_DATA_NO_YANGLIB | LYD_OPT_NOEXTDEPS | LYD_OPT_STRICT); |
Václav Kubernát | 2e4cafe | 2020-11-05 01:53:21 +0100 | [diff] [blame] | 255 | |
Václav Kubernát | 61d92d7 | 2020-11-06 03:14:18 +0100 | [diff] [blame^] | 256 | SECTION("leafValueFromNode") { |
| 257 | std::string path; |
| 258 | leaf_data_ expectedLeafData; |
Václav Kubernát | 2e4cafe | 2020-11-05 01:53:21 +0100 | [diff] [blame] | 259 | |
Václav Kubernát | 61d92d7 | 2020-11-06 03:14:18 +0100 | [diff] [blame^] | 260 | SECTION("test-schema:int8") { |
| 261 | path = "test-schema:int8"; |
| 262 | expectedLeafData = int8_t{8}; |
| 263 | } |
| 264 | SECTION("test-schema:int16") { |
| 265 | path = "test-schema:int16"; |
| 266 | expectedLeafData = int16_t{300}; |
| 267 | } |
| 268 | SECTION("test-schema:int32") { |
| 269 | path = "test-schema:int32"; |
| 270 | expectedLeafData = int32_t{-300}; |
| 271 | } |
| 272 | SECTION("test-schema:int64") { |
| 273 | path = "test-schema:int64"; |
| 274 | expectedLeafData = int64_t{-999999999999999}; |
| 275 | } |
| 276 | SECTION("test-schema:uint8") { |
| 277 | path = "test-schema:uint8"; |
| 278 | expectedLeafData = uint8_t{8}; |
| 279 | } |
| 280 | SECTION("test-schema:uint16") { |
| 281 | path = "test-schema:uint16"; |
| 282 | expectedLeafData = uint16_t{300}; |
| 283 | } |
| 284 | SECTION("test-schema:uint32") { |
| 285 | path = "test-schema:uint32"; |
| 286 | expectedLeafData = uint32_t{300}; |
| 287 | } |
| 288 | SECTION("test-schema:uint64") { |
| 289 | path = "test-schema:uint64"; |
| 290 | expectedLeafData = uint64_t{999999999999999}; |
| 291 | } |
| 292 | SECTION("test-schema:boolean") { |
| 293 | path = "test-schema:boolean"; |
| 294 | expectedLeafData = true; |
| 295 | } |
| 296 | SECTION("test-schema:string") { |
| 297 | path = "test-schema:string"; |
| 298 | expectedLeafData = std::string{"AHOJ"}; |
| 299 | } |
| 300 | SECTION("test-schema:enum") { |
| 301 | path = "test-schema:enum"; |
| 302 | expectedLeafData = enum_{"A"}; |
| 303 | } |
| 304 | SECTION("test-schema:identityRef") { |
| 305 | path = "test-schema:identityRef"; |
| 306 | expectedLeafData = identityRef_{"test-schema", "apple"}; |
| 307 | } |
| 308 | SECTION("test-schema:binary") { |
| 309 | path = "test-schema:binary"; |
| 310 | expectedLeafData = binary_{"QUhPSgo="}; |
| 311 | } |
| 312 | SECTION("test-schema:empty") { |
| 313 | path = "test-schema:empty"; |
| 314 | expectedLeafData = empty_{}; |
| 315 | } |
| 316 | SECTION("test-schema:bits") { |
| 317 | path = "test-schema:bits"; |
| 318 | expectedLeafData = bits_{{"a", "AHOJ"}}; |
| 319 | } |
| 320 | SECTION("test-schema:dec64") { |
| 321 | path = "test-schema:dec64"; |
| 322 | expectedLeafData = 43242.43260; |
| 323 | } |
| 324 | SECTION("test-schema:leafRefPresent") { |
| 325 | path = "test-schema:leafRefPresent"; |
| 326 | expectedLeafData = std::string{"Xaver"}; |
| 327 | } |
| 328 | SECTION("test-schema:leafRefNonPresent") { |
| 329 | path = "test-schema:leafRefNonPresent"; |
| 330 | expectedLeafData = std::string{"Lucas"}; |
| 331 | } |
Václav Kubernát | 2e4cafe | 2020-11-05 01:53:21 +0100 | [diff] [blame] | 332 | |
Václav Kubernát | 61d92d7 | 2020-11-06 03:14:18 +0100 | [diff] [blame^] | 333 | auto leaf = dataNode->find_path(("/" + path).c_str()); |
| 334 | REQUIRE(leaf->number() == 1); |
| 335 | auto firstLeaf = std::make_shared<libyang::Data_Node_Leaf_List>(leaf->data().front()); |
| 336 | REQUIRE(leafValueFromNode(firstLeaf) == expectedLeafData); |
| 337 | |
Václav Kubernát | 2e4cafe | 2020-11-05 01:53:21 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Václav Kubernát | 61d92d7 | 2020-11-06 03:14:18 +0100 | [diff] [blame^] | 340 | SECTION("lyNodesToTree") { |
| 341 | DatastoreAccess::Tree expected { |
| 342 | {"/test-schema:int8", int8_t{8}}, |
| 343 | {"/test-schema:int16", int16_t{300}}, |
| 344 | {"/test-schema:int32", int32_t{-300}}, |
| 345 | {"/test-schema:int64", int64_t{-999999999999999}}, |
| 346 | {"/test-schema:uint8", uint8_t{8}}, |
| 347 | {"/test-schema:uint16", uint16_t{300}}, |
| 348 | {"/test-schema:uint32", uint32_t{300}}, |
| 349 | {"/test-schema:uint64", uint64_t{999999999999999}}, |
| 350 | {"/test-schema:boolean", true}, |
| 351 | {"/test-schema:string", std::string{"AHOJ"}}, |
| 352 | {"/test-schema:enum", enum_{"A"}}, |
| 353 | {"/test-schema:identityRef", identityRef_{"test-schema", "apple"}}, |
| 354 | {"/test-schema:binary", binary_{"QUhPSgo="}}, |
| 355 | {"/test-schema:empty", empty_{}}, |
| 356 | {"/test-schema:bits", bits_{{"a", "AHOJ"}}}, |
| 357 | {"/test-schema:dec64", 43242.432600}, |
| 358 | {"/test-schema:stuff[name='Xaver']", special_{SpecialValue::List}}, |
| 359 | {"/test-schema:stuff[name='Xaver']/name", std::string{"Xaver"}}, |
| 360 | {"/test-schema:leafRefPresent", std::string{"Xaver"}}, |
| 361 | {"/test-schema:leafRefNonPresent", std::string{"Lucas"}}, |
| 362 | {"/test-schema:users/userList[1]", special_{SpecialValue::List}}, |
| 363 | {"/test-schema:users/userList[1]/name", std::string{"John"}}, |
| 364 | {"/test-schema:users/userList[2]", special_{SpecialValue::List}}, |
| 365 | {"/test-schema:users/userList[2]/name", std::string{"Aneta"}}, |
| 366 | {"/test-schema:users/userList[3]", special_{SpecialValue::List}}, |
| 367 | {"/test-schema:users/userList[3]/name", std::string{"Aneta"}}, |
| 368 | }; |
| 369 | |
| 370 | DatastoreAccess::Tree tree; |
| 371 | lyNodesToTree(tree, {dataNode->tree_for()}); |
| 372 | REQUIRE(tree == expected); |
| 373 | } |
Václav Kubernát | 2e4cafe | 2020-11-05 01:53:21 +0100 | [diff] [blame] | 374 | } |