blob: 0a91dfb6321259206b70101b601f7262dded1d5b [file] [log] [blame]
Václav Kubernát11afac72018-07-18 14:59:53 +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
Václav Kubernát26b56082020-02-03 18:28:56 +01009#include "trompeloeil_doctest.hpp"
Václav Kubernát11afac72018-07-18 14:59:53 +020010#include "ast_commands.hpp"
11#include "parser.hpp"
Václav Kubernát4a58ce62020-05-14 17:58:10 +020012#include "pretty_printers.hpp"
Václav Kubernát11afac72018-07-18 14:59:53 +020013#include "static_schema.hpp"
14
15TEST_CASE("ls")
16{
17 auto schema = std::make_shared<StaticSchema>();
18 schema->addModule("example");
19 schema->addModule("second");
Václav Kubernátefcac932020-01-10 15:26:32 +010020 schema->addContainer("/", "example:a");
21 schema->addList("/example:a", "example:listInCont", {"number"});
Václav Kubernátdd8ec152020-05-13 17:06:36 +020022 schema->addContainer("/example:a/example:listInCont", "example:contInList");
Václav Kubernátefcac932020-01-10 15:26:32 +010023 schema->addContainer("/", "second:a");
24 schema->addContainer("/", "example:b");
25 schema->addContainer("/example:a", "example:a2");
26 schema->addContainer("/example:b", "example:b2");
27 schema->addContainer("/example:a/example:a2", "example:a3");
28 schema->addContainer("/example:b/example:b2", "example:b3");
29 schema->addList("/", "example:list", {"number"});
Václav Kubernát3a99f002020-03-31 02:27:41 +020030 schema->addLeaf("/example:list", "example:number", yang::Int32{});
Václav Kubernátefcac932020-01-10 15:26:32 +010031 schema->addContainer("/example:list", "example:contInList");
32 schema->addList("/", "example:twoKeyList", {"number", "name"});
Václav Kubernát3a99f002020-03-31 02:27:41 +020033 schema->addLeaf("/example:twoKeyList", "example:number", yang::Int32{});
34 schema->addLeaf("/example:twoKeyList", "example:name", yang::String{});
Václav Kubernát11afac72018-07-18 14:59:53 +020035 Parser parser(schema);
36 std::string input;
37 std::ostringstream errorStream;
38
39 SECTION("valid input")
40 {
41 ls_ expected;
42
43 SECTION("no arguments")
44 {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020045 SECTION("ls")
46 input = "ls";
47
48 SECTION("ls --recursive")
49 {
50 input = "ls --recursive";
51 expected.m_options.push_back(LsOption::Recursive);
52 }
Václav Kubernát11afac72018-07-18 14:59:53 +020053 }
54
55 SECTION("with path argument")
56 {
Václav Kubernát37171a12018-08-31 17:01:48 +020057 SECTION("ls example:a")
58 {
59 input = "ls example:a";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020060 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020061 }
62
63 SECTION("ls /example:a")
64 {
65 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +010066 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020067
68 input = "ls /example:a";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020069 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020070 }
71
72 SECTION("ls /")
73 {
74 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +010075 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020076 input = "ls /";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020077 expected.m_path = dataPath_{Scope::Absolute, {}};
Václav Kubernát37171a12018-08-31 17:01:48 +020078 }
79
Václav Kubernátaed4bc72020-06-08 01:09:25 +020080 SECTION("going up from a node with no children")
81 {
82 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"second"}, container_{"a"})}});
83 REQUIRE(schema->availableNodes(parser.currentPath(), Recursion::NonRecursive) == std::set<ModuleNodePair>{});
84 input = "ls ../example:a";
85 expected.m_path = dataPath_{Scope::Relative, {
86 dataNode_{nodeup_{}},
87 dataNode_{module_{"example"}, container_{"a"}}
88 }};
89
90 }
91
Václav Kubernát37171a12018-08-31 17:01:48 +020092 SECTION("ls example:a/a2")
93 {
94 input = "ls example:a/a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020095 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
96 dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020097 }
98
99 SECTION("ls a2")
100 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200101 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}});
Václav Kubernát37171a12018-08-31 17:01:48 +0200102 input = "ls a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200103 expected.m_path = dataPath_{Scope::Relative, {dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200104 }
105
106 SECTION("ls /example:a/a2")
107 {
108 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100109 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +0200110 input = "ls /example:a/a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200111 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}),
112 dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200113 }
114
115 SECTION("ls example:a/example:a2")
116 {
117 input = "ls example:a/example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200118 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
119 dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200120 }
121
122 SECTION("ls example:a2")
123 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200124 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}});
Václav Kubernát37171a12018-08-31 17:01:48 +0200125 input = "ls example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200126 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200127 }
128
129 SECTION("ls /example:a/example:a2")
130 {
131 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100132 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +0200133
134 input = "ls /example:a/example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200135 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}),
136 dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200137 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200138
139 SECTION("ls --recursive /example:a")
140 {
141 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100142 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200143
144 input = "ls --recursive /example:a";
145 expected.m_options.push_back(LsOption::Recursive);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200146 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200147 }
Václav Kubernát5c75b252018-10-10 18:33:47 +0200148
149 SECTION("ls example:list")
150 {
151 input = "ls example:list";
152 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, list_{"list"})}};
153 }
154
Václav Kubernát211383d2020-01-06 18:07:50 +0100155 SECTION("ls /example:list")
156 {
157 SECTION("cwd: /") {}
158 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
159 input = "ls /example:list";
160 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, list_{"list"})}};
161 }
162
Václav Kubernát5c75b252018-10-10 18:33:47 +0200163 SECTION("ls example:a/example:listInCont")
164 {
165 input = "ls example:a/example:listInCont";
166 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
167 dataNode_(module_{"example"}, list_{"listInCont"})}};
168 }
169
Václav Kubernátdd8ec152020-05-13 17:06:36 +0200170 SECTION("ls example:a/example:listInCont/example:contInList")
171 {
172 input = "ls example:a/example:listInCont/example:contInList";
173 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, container_{"a"}),
174 schemaNode_(module_{"example"}, list_{"listInCont"}),
175 schemaNode_(module_{"example"}, container_{"contInList"})}};
176 }
177
Václav Kubernát5c75b252018-10-10 18:33:47 +0200178 SECTION("ls example:list[number=342]/contInList")
179 {
180 input = "ls example:list[number=342]/contInList";
Václav Kubernátc15fe822020-06-04 11:28:39 +0200181 auto keys = ListInstance {
Václav Kubernát7707cae2020-01-16 12:04:53 +0100182 {"number", int32_t{342}}};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200183 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}),
184 dataNode_(container_{"contInList"})}};
185 }
Václav Kubernátac035d62019-02-18 10:59:08 +0100186
187 SECTION("ls example:list/contInList")
188 {
189 input = "ls example:list/contInList";
190 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}),
191 schemaNode_(container_{"contInList"})}};
192 }
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200193
Václav Kubernát88c2a612020-05-06 02:20:03 +0200194 SECTION("ls example:list/example:contInList")
195 {
196 input = "ls example:list/example:contInList";
197 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}),
198 schemaNode_(module_{"example"},container_{"contInList"})}};
199 }
200
Václav Kubernát1cd8c9c2020-05-06 01:38:59 +0200201 SECTION("ls example:list[number=343]/..")
202 {
203 input = "ls example:list[number=343]/..";
Václav Kubernátc15fe822020-06-04 11:28:39 +0200204 auto keys = ListInstance {
Václav Kubernát1cd8c9c2020-05-06 01:38:59 +0200205 {"number", int32_t{343}}};
206 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}), dataNode_{nodeup_{}}}};
207 }
208
209 SECTION("ls example:list/..")
210 {
211 input = "ls example:list/..";
212 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}), schemaNode_{nodeup_{}}}};
213 }
214
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200215 SECTION("ls example:*")
216 {
217 input = "ls example:*";
218 expected.m_path = module_{"example"};
219 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200220 }
221
222 command_ command = parser.parseCommand(input, errorStream);
223 REQUIRE(command.type() == typeid(ls_));
224 REQUIRE(boost::get<ls_>(command) == expected);
225 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200226
Václav Kubernát11afac72018-07-18 14:59:53 +0200227 SECTION("invalid input")
228 {
229 SECTION("invalid path")
230 {
Václav Kubernát37171a12018-08-31 17:01:48 +0200231 SECTION("ls example:nonexistent")
232 input = "ls example:nonexistent";
233
234 SECTION("ls /example:nonexistent")
235 input = "ls /example:nonexistent";
236
Václav Kubernátb9621052019-03-18 18:27:49 +0100237 SECTION("ls /exa")
238 {
239 SECTION("cwd: /") {}
240 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
241 input = "ls /exa";
242 }
243
Václav Kubernát37171a12018-08-31 17:01:48 +0200244 SECTION("ls /bad:nonexistent")
245 input = "ls /bad:nonexistent";
246
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200247 SECTION("ls example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200248 input = "ls example:a/nonexistent";
249
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200250 SECTION("ls /example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200251 input = "ls /example:a/nonexistent";
Václav Kubernát11afac72018-07-18 14:59:53 +0200252 }
253
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200254 SECTION("whitespace before path")
255 {
256 SECTION("ls --recursive/")
257 input = "ls --recursive/";
258
259 SECTION("ls/")
260 input = "ls/";
261
262 SECTION("ls --recursive/example:a")
263 input = "ls --recursive/example:a";
264
265 SECTION("ls/example:a")
266 input = "ls/example:a";
267
268 SECTION("lssecond:a")
269 input = "lssecond:a";
270 }
271
Jan Kundrátc381e632019-03-14 13:39:11 +0100272 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát11afac72018-07-18 14:59:53 +0200273 }
274}