blob: e5740a75a5c0cd5c5977e0ac1cbd4154aa9e0337 [file] [log] [blame]
Václav Kubernát4108e0d2018-10-29 13:32:22 +01001/*
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át26b56082020-02-03 18:28:56 +01009#include "trompeloeil_doctest.hpp"
Václav Kubernátcb3af402020-02-12 16:49:17 +010010#include "completion.hpp"
Václav Kubernát2984f442020-02-20 17:43:35 +010011#include "leaf_data_helpers.hpp"
Václav Kubernát2e4cafe2020-11-05 01:53:21 +010012#include "libyang_utils.hpp"
13#include "pretty_printers.hpp"
Václav Kubernát4108e0d2018-10-29 13:32:22 +010014#include "utils.hpp"
15
16TEST_CASE("utils")
17{
Václav Kubernáta395d332019-02-13 16:49:20 +010018 SECTION("filterByPrefix")
Václav Kubernát4108e0d2018-10-29 13:32:22 +010019 {
Václav Kubernátcb3af402020-02-12 16:49:17 +010020 std::set<Completion> set{{"ahoj"}, {"coze"}, {"copak"}, {"aha"}, {"polivka"}};
Václav Kubernát4108e0d2018-10-29 13:32:22 +010021
Václav Kubernátcb3af402020-02-12 16:49:17 +010022 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át4108e0d2018-10-29 13:32:22 +010028 }
Václav Kubernáta44bdf22020-01-24 12:15:31 +010029
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át2984f442020-02-20 17:43:35 +010086
87 SECTION("leafDataTypeToString")
88 {
89 yang::LeafDataType type;
90 std::string expected;
91 SECTION("union")
92 {
93 type = yang::Union{{
Václav Kubernát13b23d72020-04-16 21:49:51 +020094 yang::TypeInfo{yang::String{}},
95 yang::TypeInfo{createEnum({"foo", "bar"})},
96 yang::TypeInfo{yang::Int8{}},
97 yang::TypeInfo{yang::Int64{}},
Václav Kubernát2984f442020-02-20 17:43:35 +010098 }};
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át4108e0d2018-10-29 13:32:22 +0100106}
Václav Kubernát2e4cafe2020-11-05 01:53:21 +0100107
108const auto schema = R"(
109module 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 }
196}
197)";
198
199const auto data = R"(
200{
201 "test-schema:int8": 8,
202 "test-schema:int16": 300,
203 "test-schema:int32": -300,
204 "test-schema:int64": -999999999999999,
205 "test-schema:uint8": 8,
206 "test-schema:uint16": 300,
207 "test-schema:uint32": 300,
208 "test-schema:uint64": 999999999999999,
209 "test-schema:boolean": true,
210 "test-schema:string": "AHOJ",
211 "test-schema:enum": "A",
212 "test-schema:identityRef": "apple",
213 "test-schema:binary": "QUhPSgo=",
214 "test-schema:empty": "",
215 "test-schema:bits": "a AHOJ",
216 "test-schema:dec64": "43242.43260",
217 "test-schema:stuff": [
218 {
219 "name": "Xaver"
220 }
221 ],
222 "test-schema:leafRefPresent": "Xaver",
223 "test-schema:leafRefNonPresent": "Lucas"
224}
225)";
226
227
228TEST_CASE("libyang_utils")
229{
230 auto ctx = std::make_shared<libyang::Context>();
231 ctx->parse_module_mem(schema, LYS_IN_YANG);
232 auto dataNode = ctx->parse_data_mem(data, LYD_JSON, LYD_OPT_DATA_NO_YANGLIB | LYD_OPT_NOEXTDEPS);
233
234 std::string path;
235 leaf_data_ expectedLeafData;
236
237
238 SECTION("test-schema:int8") {
239 path = "test-schema:int8";
240 expectedLeafData = int8_t{8};
241 }
242 SECTION("test-schema:int16") {
243 path = "test-schema:int16";
244 expectedLeafData = int16_t{300};
245 }
246 SECTION("test-schema:int32") {
247 path = "test-schema:int32";
248 expectedLeafData = int32_t{-300};
249 }
250 SECTION("test-schema:int64") {
251 path = "test-schema:int64";
252 expectedLeafData = int64_t{-999999999999999};
253 }
254 SECTION("test-schema:uint8") {
255 path = "test-schema:uint8";
256 expectedLeafData = uint8_t{8};
257 }
258 SECTION("test-schema:uint16") {
259 path = "test-schema:uint16";
260 expectedLeafData = uint16_t{300};
261 }
262 SECTION("test-schema:uint32") {
263 path = "test-schema:uint32";
264 expectedLeafData = uint32_t{300};
265 }
266 SECTION("test-schema:uint64") {
267 path = "test-schema:uint64";
268 expectedLeafData = uint64_t{999999999999999};
269 }
270 SECTION("test-schema:boolean") {
271 path = "test-schema:boolean";
272 expectedLeafData = true;
273 }
274 SECTION("test-schema:string") {
275 path = "test-schema:string";
276 expectedLeafData = std::string{"AHOJ"};
277 }
278 SECTION("test-schema:enum") {
279 path = "test-schema:enum";
280 expectedLeafData = enum_{"A"};
281 }
282 SECTION("test-schema:identityRef") {
283 path = "test-schema:identityRef";
284 expectedLeafData = identityRef_{"test-schema", "apple"};
285 }
286 SECTION("test-schema:binary") {
287 path = "test-schema:binary";
288 expectedLeafData = binary_{"QUhPSgo="};
289 }
290 SECTION("test-schema:empty") {
291 path = "test-schema:empty";
292 expectedLeafData = empty_{};
293 }
294 SECTION("test-schema:bits") {
295 path = "test-schema:bits";
296 expectedLeafData = bits_{{"a", "AHOJ"}};
297 }
298 SECTION("test-schema:dec64") {
299 path = "test-schema:dec64";
300 expectedLeafData = 43242.43260;
301 }
302 SECTION("test-schema:leafRefPresent") {
303 path = "test-schema:leafRefPresent";
304 expectedLeafData = std::string{"Xaver"};
305 }
306 SECTION("test-schema:leafRefNonPresent") {
307 path = "test-schema:leafRefNonPresent";
308 expectedLeafData = std::string{"Lucas"};
309 }
310
311 auto leaf = dataNode->find_path(("/" + path).c_str());
312 REQUIRE(leaf->number() == 1);
313 auto firstLeaf = std::make_shared<libyang::Data_Node_Leaf_List>(leaf->data().front());
314 REQUIRE(leafValueFromNode(firstLeaf) == expectedLeafData);
315}