blob: 538e4faa73a0c88a6f81f701c40a2fcd1df0d0b8 [file] [log] [blame]
Václav Kubernátd6662962018-03-22 17:41:33 +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át24df80e2018-06-06 15:18:03 +020010#include "ast_commands.hpp"
Václav Kubernát0af72572020-07-22 15:15:59 +020011#include "leaf_data_helpers.hpp"
Václav Kubernát48fc3832018-05-28 14:21:22 +020012#include "parser.hpp"
Václav Kubernátce108db2021-01-25 10:05:40 +010013#include "pretty_printers.hpp"
Václav Kubernátbddbb172018-06-13 16:27:39 +020014#include "static_schema.hpp"
Václav Kubernátd6662962018-03-22 17:41:33 +010015
16TEST_CASE("cd")
17{
Václav Kubernátbddbb172018-06-13 16:27:39 +020018 auto schema = std::make_shared<StaticSchema>();
Václav Kubernát744f57f2018-06-29 22:46:26 +020019 schema->addModule("example");
20 schema->addModule("second");
Václav Kubernátefcac932020-01-10 15:26:32 +010021 schema->addContainer("/", "example:a");
22 schema->addContainer("/", "second:a");
23 schema->addContainer("/", "example:b");
24 schema->addContainer("/example:a", "example:a2");
25 schema->addContainer("/example:b", "example:b2");
26 schema->addContainer("/example:a/example:a2", "example:a3");
27 schema->addContainer("/example:b/example:b2", "example:b3");
28 schema->addList("/", "example:list", {"number"});
Václav Kubernát3a99f002020-03-31 02:27:41 +020029 schema->addLeaf("/example:list", "example:number", yang::Int32{});
Václav Kubernátefcac932020-01-10 15:26:32 +010030 schema->addContainer("/example:list", "example:contInList");
31 schema->addList("/", "example:twoKeyList", {"number", "name"});
Václav Kubernát3a99f002020-03-31 02:27:41 +020032 schema->addLeaf("/example:twoKeyList", "example:number", yang::Int32{});
33 schema->addLeaf("/example:twoKeyList", "example:name", yang::String{});
Václav Kubernáte7248b22020-06-26 15:38:59 +020034 schema->addRpc("/", "example:launch-nukes");
Václav Kubernát0af72572020-07-22 15:15:59 +020035 schema->addList("/", "example:ports", {"name"});
36 schema->addLeaf("/example:ports", "example:name", createEnum({"A", "B", "C"}));
Václav Kubernát0d47e5e2020-11-17 12:27:05 +010037 schema->addLeaf("/", "example:myLeaf", yang::Int32{});
Václav Kubernát48fc3832018-05-28 14:21:22 +020038 Parser parser(schema);
Václav Kubernátd6662962018-03-22 17:41:33 +010039 std::string input;
Václav Kubernát2315c732018-05-16 20:25:55 +020040 std::ostringstream errorStream;
Václav Kubernátd6662962018-03-22 17:41:33 +010041
Václav Kubernát744f57f2018-06-29 22:46:26 +020042
Václav Kubernátb96eef72018-05-04 19:10:22 +020043 SECTION("valid input")
Václav Kubernátd6662962018-03-22 17:41:33 +010044 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020045 cd_ expected;
46
47 SECTION("container")
Václav Kubernátd6662962018-03-22 17:41:33 +010048 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020049 SECTION("example:a")
Václav Kubernátb96eef72018-05-04 19:10:22 +020050 {
Václav Kubernátd6fd2492018-11-19 15:11:16 +010051 SECTION("trailing slash")
52 {
53 input = "cd example:a/";
Václav Kubernátd6fd2492018-11-19 15:11:16 +010054 }
55 SECTION("no trailing slash")
56 {
57 input = "cd example:a";
58 }
Václav Kubernátfaacd022020-07-08 16:44:38 +020059 expected.m_path.m_nodes.emplace_back(module_{"example"}, container_("a"));
Václav Kubernátb96eef72018-05-04 19:10:22 +020060 }
61
Václav Kubernát744f57f2018-06-29 22:46:26 +020062 SECTION("second:a")
Václav Kubernátb96eef72018-05-04 19:10:22 +020063 {
Václav Kubernátd6fd2492018-11-19 15:11:16 +010064 SECTION("trailing slash")
65 {
66 input = "cd second:a/";
Václav Kubernátd6fd2492018-11-19 15:11:16 +010067 }
68 SECTION("no trailing slash")
69 {
70 input = "cd second:a";
71 }
Václav Kubernátfaacd022020-07-08 16:44:38 +020072 expected.m_path.m_nodes.emplace_back(module_{"second"}, container_("a"));
Václav Kubernátb96eef72018-05-04 19:10:22 +020073 }
74
Václav Kubernát744f57f2018-06-29 22:46:26 +020075 SECTION("example:b")
Václav Kubernátb96eef72018-05-04 19:10:22 +020076 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020077 input = "cd example:b";
Václav Kubernátfaacd022020-07-08 16:44:38 +020078 expected.m_path.m_nodes.emplace_back(module_{"example"}, container_("b"));
Václav Kubernátb96eef72018-05-04 19:10:22 +020079 }
80
Václav Kubernát744f57f2018-06-29 22:46:26 +020081 SECTION("example:a/a2")
Václav Kubernátb96eef72018-05-04 19:10:22 +020082 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020083 input = "cd example:a/a2";
Václav Kubernátfaacd022020-07-08 16:44:38 +020084 expected.m_path.m_nodes.emplace_back(module_{"example"}, container_("a"));
85 expected.m_path.m_nodes.emplace_back(container_("a2"));
Václav Kubernát744f57f2018-06-29 22:46:26 +020086 }
87
88 SECTION("example:a/example:a2")
89 {
90 input = "cd example:a/example:a2";
Václav Kubernátfaacd022020-07-08 16:44:38 +020091 expected.m_path.m_nodes.emplace_back(module_{"example"}, container_("a"));
92 expected.m_path.m_nodes.emplace_back(module_{"example"}, container_("a2"));
Václav Kubernát744f57f2018-06-29 22:46:26 +020093 }
94
95 SECTION("example:b/b2")
96 {
97 input = "cd example:b/b2";
Václav Kubernátfaacd022020-07-08 16:44:38 +020098 expected.m_path.m_nodes.emplace_back(module_{"example"}, container_("b"));
99 expected.m_path.m_nodes.emplace_back(container_("b2"));
Václav Kubernátb96eef72018-05-04 19:10:22 +0200100 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100101 }
102
Václav Kubernátb96eef72018-05-04 19:10:22 +0200103 SECTION("list elements")
Václav Kubernátd6662962018-03-22 17:41:33 +0100104 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200105 SECTION("example:list[number=1]")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200106 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200107 input = "cd example:list[number=1]";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100108 auto keys = ListInstance{
Václav Kubernát7707cae2020-01-16 12:04:53 +0100109 {"number", int32_t{1}}};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200110 expected.m_path.m_nodes.emplace_back(module_{"example"}, listElement_("list", keys));
Václav Kubernátb96eef72018-05-04 19:10:22 +0200111 }
112
Václav Kubernát744f57f2018-06-29 22:46:26 +0200113 SECTION("example:list[number=1]/contInList")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200114 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200115 input = "cd example:list[number=1]/contInList";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100116 auto keys = ListInstance{
Václav Kubernát7707cae2020-01-16 12:04:53 +0100117 {"number", int32_t{1}}};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200118 expected.m_path.m_nodes.emplace_back(module_{"example"}, listElement_("list", keys));
119 expected.m_path.m_nodes.emplace_back(container_("contInList"));
Václav Kubernátb96eef72018-05-04 19:10:22 +0200120 }
121
Václav Kubernát89728d82018-09-13 16:28:28 +0200122 SECTION("example:twoKeyList[number=4][name='abcd']")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200123 {
Václav Kubernát89728d82018-09-13 16:28:28 +0200124 input = "cd example:twoKeyList[number=4][name='abcd']";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100125 auto keys = ListInstance{
Václav Kubernát7707cae2020-01-16 12:04:53 +0100126 {"number", int32_t{4}},
127 {"name", std::string{"abcd"}}};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200128 expected.m_path.m_nodes.emplace_back(module_{"example"}, listElement_("twoKeyList", keys));
Václav Kubernátb96eef72018-05-04 19:10:22 +0200129 }
Václav Kubernát0af72572020-07-22 15:15:59 +0200130
131 SECTION("enum key type")
132 {
133 input = "cd example:ports[name=A]";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100134 auto keys = ListInstance{
Václav Kubernát0af72572020-07-22 15:15:59 +0200135 {"name", enum_{"A"}}};
136 expected.m_path.m_nodes.emplace_back(module_{"example"}, listElement_("ports", keys));
137 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100138 }
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200139
140 SECTION("whitespace handling")
141 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200142 SECTION(" cd example:a ")
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200143 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200144 input = " cd example:a ";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200145 expected.m_path.m_nodes.emplace_back(module_{"example"}, container_("a"));
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200146 }
147 }
148
Václav Kubernát60d6f292018-05-25 09:45:32 +0200149 SECTION("moving up")
150 {
Václav Kubernátc2bfe492020-06-15 13:19:07 +0200151 SECTION("moving up when already in root")
152 {
153 input = "cd ..";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200154 expected.m_path.m_nodes.emplace_back(nodeup_());
Václav Kubernátc2bfe492020-06-15 13:19:07 +0200155 }
156
157 SECTION("moving up TWICE when already in root")
158 {
159 input = "cd ../..";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200160 expected.m_path.m_nodes.emplace_back(nodeup_());
161 expected.m_path.m_nodes.emplace_back(nodeup_());
Václav Kubernátc2bfe492020-06-15 13:19:07 +0200162 }
163
Václav Kubernát744f57f2018-06-29 22:46:26 +0200164 SECTION("example:a/..")
Václav Kubernát60d6f292018-05-25 09:45:32 +0200165 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200166 input = "cd example:a/..";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200167 expected.m_path.m_nodes.emplace_back(module_{"example"}, container_("a"));
168 expected.m_path.m_nodes.emplace_back(nodeup_());
Václav Kubernát60d6f292018-05-25 09:45:32 +0200169 }
170
Václav Kubernát744f57f2018-06-29 22:46:26 +0200171 SECTION("example:a/../example:a")
Václav Kubernát60d6f292018-05-25 09:45:32 +0200172 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200173 input = "cd example:a/../example:a";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200174 expected.m_path.m_nodes.emplace_back(module_{"example"}, container_("a"));
175 expected.m_path.m_nodes.emplace_back(nodeup_());
176 expected.m_path.m_nodes.emplace_back(module_{"example"}, container_("a"));
Václav Kubernát60d6f292018-05-25 09:45:32 +0200177 }
178
Václav Kubernát744f57f2018-06-29 22:46:26 +0200179 SECTION("example:a/../example:a/a2")
Václav Kubernát60d6f292018-05-25 09:45:32 +0200180 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200181 input = "cd example:a/../example:a/a2";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200182 expected.m_path.m_nodes.emplace_back(module_{"example"}, container_("a"));
183 expected.m_path.m_nodes.emplace_back(nodeup_());
184 expected.m_path.m_nodes.emplace_back(module_{"example"}, container_("a"));
185 expected.m_path.m_nodes.emplace_back(container_("a2"));
Václav Kubernát60d6f292018-05-25 09:45:32 +0200186 }
187 }
188
Václav Kubernátb61336d2018-05-28 17:35:03 +0200189 command_ command = parser.parseCommand(input, errorStream);
190 REQUIRE(command.type() == typeid(cd_));
191 REQUIRE(boost::get<cd_>(command) == expected);
Václav Kubernát39f83f52021-02-19 02:52:08 +0100192 // Converting the path back to a string should never result with a path with a trailing slash, even if the
193 // original input string has it.
194 REQUIRE(pathToDataString(boost::get<cd_>(command).m_path, Prefixes::WhenNeeded).back() != '/');
Václav Kubernátd6662962018-03-22 17:41:33 +0100195 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200196 SECTION("invalid input")
Václav Kubernátd6662962018-03-22 17:41:33 +0100197 {
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200198 SECTION("missing space between a command and its arguments")
199 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200200 SECTION("cdexample:a")
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200201 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200202 input = "cdexample:a";
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200203 }
204 }
Václav Kubernát744f57f2018-06-29 22:46:26 +0200205
206 SECTION("whitespace between module and nodename")
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200207 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200208 SECTION("cd example: a")
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200209 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200210 input = "cd example: a";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200211 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100212
Václav Kubernát744f57f2018-06-29 22:46:26 +0200213 SECTION("cd example : a")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200214 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200215 input = "cd example : a";
216 }
217
218 SECTION("cd example :a")
219 {
220 input = "cd example :a";
221 }
222 }
223
224 SECTION("entering modules")
225 {
226 SECTION("cd example")
227 {
228 input = "cd example";
229 }
230
231 SECTION("cd example:")
232 {
233 input = "cd example:";
234 }
235 }
236
237 SECTION("garbage arguments handling")
238 {
239 SECTION("cd example:a garbage")
240 {
241 input = "cd example:a garbage";
242 }
243 SECTION("cd example:a/a2 garbage")
244 {
245 input = "cd example:a/a2 garbage";
246 }
247 }
248
249 SECTION("invalid node identifiers")
250 {
251 SECTION("example:nonexistent")
252 {
253 input = "cd example:nonexistent";
254 }
255
256 SECTION("example:nonexistent/lol")
257 {
258 input = "cd example:nonexistent/lol";
259 }
260 }
261
262 SECTION("invalid module identifiers")
263 {
264 SECTION("elpmaxe:nonexistent")
265 {
266 input = "cd elpmaxe:nonexistent";
267 }
268
269 SECTION("elpmaxe:nonexistent/example:lol")
270 {
271 input = "cd elpmaxe:nonexistent/example:lol";
272 }
273 }
274
275 SECTION("no top-level module")
276 {
277 SECTION("cd a")
278 {
279 input = "cd a";
280 }
281
282 SECTION("cd example:a/../a")
283 {
284 input = "cd example:a/../a";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200285 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100286 }
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200287
Václav Kubernátb96eef72018-05-04 19:10:22 +0200288 SECTION("invalid list key identifiers")
289 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200290 SECTION("example:list")
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200291 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200292 input = "cd example:list";
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200293 }
294
Václav Kubernát744f57f2018-06-29 22:46:26 +0200295 SECTION("example:list[]")
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200296 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200297 input = "cd example:list[]";
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200298 }
299
Václav Kubernát89728d82018-09-13 16:28:28 +0200300 SECTION("example:twoKeyList[invalidKey='4']")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200301 {
Václav Kubernát89728d82018-09-13 16:28:28 +0200302 input = "cd example:twoKeyList[invalidKey='4']";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200303 }
304
Václav Kubernát89728d82018-09-13 16:28:28 +0200305 SECTION("example:twoKeyList[number=4][number=5]")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200306 {
Václav Kubernát89728d82018-09-13 16:28:28 +0200307 input = "cd example:twoKeyList[number=4][number=5]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200308 }
309
Václav Kubernát89728d82018-09-13 16:28:28 +0200310 SECTION("example:twoKeyList[number=4][name='lol'][number=7]")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200311 {
Václav Kubernát89728d82018-09-13 16:28:28 +0200312 input = "cd example:twoKeyList[number=4][name='lol'][number=7]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200313 }
314
Václav Kubernát744f57f2018-06-29 22:46:26 +0200315 SECTION("example:twoKeyList[number=4]")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200316 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200317 input = "cd example:twoKeyList[number=4]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200318 }
Václav Kubernát89728d82018-09-13 16:28:28 +0200319
320 SECTION("strings must be quoted")
321 {
322 input = "cd example:twoKeyList[number=4][name=abcd]";
323 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200324 }
Václav Kubernát9fa5dca2020-06-01 03:56:41 +0200325
326 SECTION("no space between list prefix and suffix")
327 {
328 input = "cd example:list [number=10]";
329 }
330
Václav Kubernáte7248b22020-06-26 15:38:59 +0200331 SECTION("cd into rpc")
332 {
333 input = "cd example:launch-nukes";
334 }
335
Václav Kubernát0d47e5e2020-11-17 12:27:05 +0100336 SECTION("cd into a leaf")
337 {
338 input = "cd example:myLeaf";
339 }
340
Jan Kundrátc381e632019-03-14 13:39:11 +0100341 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernátd6662962018-03-22 17:41:33 +0100342 }
343}