blob: 6f63bfc17baaa03be68f07c044fdc4ae5bf876f0 [file] [log] [blame]
Václav Kubernát0d4db442018-07-18 17:18:43 +02001/*
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
9#include "trompeloeil_catch.h"
10#include "yang_schema.hpp"
11
12const char* schema = R"(
13module example-schema {
14 namespace "http://example.com/example-sports";
15 prefix coze;
16
17 container a {
18 container a2 {
19 container a3 {
20 presence true;
21 }
22 }
23
24 leaf leafa {
25 type string;
26 }
27 }
28
29 container b {
30 container b2 {
31 presence true;
32 container b3 {
33 }
34 }
35 }
36
37 leaf leafString {
38 type string;
39 }
40
41 leaf leafDecimal {
42 type decimal64 {
43 fraction-digits 5;
44 }
45 }
46
47 leaf leafBool {
48 type boolean;
49 }
50
51 leaf leafInt {
52 type int32;
53 }
54
55 leaf leafUint {
56 type uint32;
57 }
58
59 leaf leafEnum {
60 type enumeration {
61 enum lol;
62 enum data;
63 enum coze;
64 }
65 }
66
67 list _list {
68 key number;
69
70 leaf number {
71 type int32;
72 }
73
74 container contInList {
75 presence true;
76 }
77 }
78
79 list twoKeyList {
80 key "name number";
81
82 leaf number {
83 type int32;
84 }
85
86 leaf name {
87 type string;
88 }
89 }
90})";
91
92TEST_CASE("yangschema")
93{
94 YangSchema ys;
95 ys.addSchemaString(schema);
96 path_ path;
97 ModuleNodePair node;
98
99 SECTION("positive")
100 {
101 SECTION("isContainer")
102 {
103 SECTION("example-schema:a")
104 {
105 node.first = "example-schema";
106 node.second = "a";
107 }
108
109 SECTION("example-schema:a/a2")
110 {
111 path.m_nodes.push_back(node_(module_{"example-schema"}, container_("a")));
112 node.second = "a2";
113 }
114
115 REQUIRE(ys.isContainer(path, node));
116 }
117 SECTION("isLeaf")
118 {
119 SECTION("example-schema:leafString")
120 {
121 node.first = "example-schema";
122 node.second = "leafString";
123 }
124
125 SECTION("example-schema:a/leafa")
126 {
127 path.m_nodes.push_back(node_(module_{"example-schema"}, container_("a")));
128 node.first = "example-schema";
129 node.second = "leafa";
130 }
131
132 REQUIRE(ys.isLeaf(path, node));
133 }
134 SECTION("isModule")
135 {
136 REQUIRE(ys.isModule(path, "example-schema"));
137 }
138 SECTION("isList")
139 {
140 SECTION("example-schema:_list")
141 {
142 node.first = "example-schema";
143 node.second = "_list";
144 }
145
146 SECTION("example-schema:twoKeyList")
147 {
148 node.first = "example-schema";
149 node.second = "twoKeyList";
150 }
151
152 REQUIRE(ys.isList(path, node));
153 }
154 SECTION("isPresenceContainer")
155 {
156 SECTION("example-schema:a/a2/a3")
157 {
158 path.m_nodes.push_back(node_(module_{"example-schema"}, container_("a")));
159 path.m_nodes.push_back(node_(module_{"example-schema"}, container_("a2")));
160 node.second = "a3";
161 }
162
163 REQUIRE(ys.isPresenceContainer(path, node));
164 }
165 SECTION("leafEnumHasValue")
166 {
167 node.first = "example-schema";
168 node.second = "leafEnum";
169 std::string value;
170
171 SECTION("lol")
172 value = "lol";
173
174 SECTION("data")
175 value = "data";
176
177 SECTION("coze")
178 value = "coze";
179
180 REQUIRE(ys.leafEnumHasValue(path, node, value));
181 }
182 SECTION("listHasKey")
183 {
184 std::string key;
185
186 SECTION("_list")
187 {
188 node.first = "example-schema";
189 node.second = "_list";
190 SECTION("number")
191 key = "number";
192 }
193
194 SECTION("twoKeyList")
195 {
196 node.first = "example-schema";
197 node.second = "twoKeyList";
198 SECTION("number")
199 key = "number";
200 SECTION("name")
201 key = "name";
202 }
203
204 REQUIRE(ys.listHasKey(path, node, key));
205 }
206 SECTION("listKeys")
207 {
208 std::set<std::string> set;
209
210 SECTION("_list")
211 {
212 set = {"number"};
213 node.first = "example-schema";
214 node.second = "_list";
215 }
216
217 SECTION("twoKeyList")
218 {
219 set = {"number", "name"};
220 node.first = "example-schema";
221 node.second = "twoKeyList";
222 }
223
224 REQUIRE(ys.listKeys(path, node) == set);
225 }
226 SECTION("leafType")
227 {
228 yang::LeafDataTypes type;
229
230 SECTION("leafString")
231 {
232 node.first = "example-schema";
233 node.second = "leafString";
234 type = yang::LeafDataTypes::String;
235 }
236
237 SECTION("leafDecimal")
238 {
239 node.first = "example-schema";
240 node.second = "leafDecimal";
241 type = yang::LeafDataTypes::Decimal;
242 }
243
244 SECTION("leafBool")
245 {
246 node.first = "example-schema";
247 node.second = "leafBool";
248 type = yang::LeafDataTypes::Bool;
249 }
250
251 SECTION("leafInt")
252 {
253 node.first = "example-schema";
254 node.second = "leafInt";
255 type = yang::LeafDataTypes::Int;
256 }
257
258 SECTION("leafUint")
259 {
260 node.first = "example-schema";
261 node.second = "leafUint";
262 type = yang::LeafDataTypes::Uint;
263 }
264
265 SECTION("leafEnum")
266 {
267 node.first = "example-schema";
268 node.second = "leafEnum";
269 type = yang::LeafDataTypes::Enum;
270 }
271
272 REQUIRE(ys.leafType(path, node) == type);
273 }
274 SECTION("childNodes")
275 {
276 std::set<std::string> set;
277
278 SECTION("<root>")
279 {
280 set = {"example-schema:a", "example-schema:b", "example-schema:leafString",
281 "example-schema:leafDecimal", "example-schema:leafBool", "example-schema:leafInt",
282 "example-schema:leafUint", "example-schema:leafEnum", "example-schema:_list", "example-schema:twoKeyList"};
283 }
284
285 SECTION("a")
286 {
287 path.m_nodes.push_back(node_(module_{"example-schema"}, container_("a")));
288 set = {"example-schema:a2", "example-schema:leafa"};
289 }
290
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200291 REQUIRE(ys.childNodes(path, Recursion::NonRecursive) == set);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200292 }
293 }
294
295 SECTION("negative")
296 {
297 SECTION("nonexistent nodes")
298 {
299 SECTION("example-schema:coze")
300 {
301 node.first = "example-schema";
302 node.second = "coze";
303 }
304
305 SECTION("example-schema:a/nevim")
306 {
307 path.m_nodes.push_back(node_(module_{"example-schema"}, container_("a")));
308 node.second = "nevim";
309 }
310
311 SECTION("modul:a/nevim")
312 {
313 path.m_nodes.push_back(node_(module_{"modul"}, container_("a")));
314 node.second = "nevim";
315 }
316
317 REQUIRE(!ys.isPresenceContainer(path, node));
318 REQUIRE(!ys.isList(path, node));
319 REQUIRE(!ys.isLeaf(path, node));
320 REQUIRE(!ys.isContainer(path, node));
321 }
322
323 SECTION("\"is\" methods return false for existing nodes for different nodetypes")
324 {
325 SECTION("example-schema:a")
326 {
327 node.first = "example-schema";
328 node.second = "a";
329 }
330
331 SECTION("example-schema:a/a2")
332 {
333 path.m_nodes.push_back(node_(module_{"example-schema"}, container_("a")));
334 node.second = "a2";
335 }
336
337 REQUIRE(!ys.isPresenceContainer(path, node));
338 REQUIRE(!ys.isList(path, node));
339 REQUIRE(!ys.isLeaf(path, node));
340 }
341
342 SECTION("nodetype-specific methods called with different nodetypes")
343 {
344 path.m_nodes.push_back(node_(module_{"example-schema"}, container_("a")));
345 node.second = "a2";
346
347 REQUIRE(!ys.leafEnumHasValue(path, node, "haha"));
348 REQUIRE(!ys.listHasKey(path, node, "chacha"));
349 }
350
351 SECTION("nonexistent module")
352 {
353 REQUIRE(!ys.isModule(path, "notAModule"));
354 }
355 }
356}