blob: e0085576726dd2749cb0d64355c3964ede37cfae [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")
Václav Kubernátb4e5b182020-11-16 19:55:09 +010046 {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020047 input = "ls";
Václav Kubernátb4e5b182020-11-16 19:55:09 +010048 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020049
50 SECTION("ls --recursive")
51 {
52 input = "ls --recursive";
Václav Kubernátfaacd022020-07-08 16:44:38 +020053 expected.m_options.emplace_back(LsOption::Recursive);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020054 }
Václav Kubernát11afac72018-07-18 14:59:53 +020055 }
56
57 SECTION("with path argument")
58 {
Václav Kubernát37171a12018-08-31 17:01:48 +020059 SECTION("ls example:a")
60 {
61 input = "ls example:a";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020062 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020063 }
64
65 SECTION("ls /example:a")
66 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +010067 SECTION("cwd: /") { }
Václav Kubernátbf083ec2019-02-19 13:58:09 +010068 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020069
70 input = "ls /example:a";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020071 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020072 }
73
74 SECTION("ls /")
75 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +010076 SECTION("cwd: /") { }
Václav Kubernátbf083ec2019-02-19 13:58:09 +010077 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020078 input = "ls /";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020079 expected.m_path = dataPath_{Scope::Absolute, {}};
Václav Kubernát37171a12018-08-31 17:01:48 +020080 }
81
Václav Kubernátaed4bc72020-06-08 01:09:25 +020082 SECTION("going up from a node with no children")
83 {
84 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"second"}, container_{"a"})}});
85 REQUIRE(schema->availableNodes(parser.currentPath(), Recursion::NonRecursive) == std::set<ModuleNodePair>{});
86 input = "ls ../example:a";
87 expected.m_path = dataPath_{Scope::Relative, {
88 dataNode_{nodeup_{}},
89 dataNode_{module_{"example"}, container_{"a"}}
90 }};
91
92 }
93
Václav Kubernát37171a12018-08-31 17:01:48 +020094 SECTION("ls example:a/a2")
95 {
96 input = "ls example:a/a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020097 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
98 dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020099 }
100
101 SECTION("ls a2")
102 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200103 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}});
Václav Kubernát37171a12018-08-31 17:01:48 +0200104 input = "ls a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200105 expected.m_path = dataPath_{Scope::Relative, {dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200106 }
107
108 SECTION("ls /example:a/a2")
109 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100110 SECTION("cwd: /") { }
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100111 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +0200112 input = "ls /example:a/a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200113 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}),
114 dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200115 }
116
117 SECTION("ls example:a/example:a2")
118 {
119 input = "ls example:a/example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200120 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
121 dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200122 }
123
124 SECTION("ls example:a2")
125 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200126 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}});
Václav Kubernát37171a12018-08-31 17:01:48 +0200127 input = "ls example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200128 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200129 }
130
131 SECTION("ls /example:a/example:a2")
132 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100133 SECTION("cwd: /") { }
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100134 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +0200135
136 input = "ls /example:a/example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200137 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}),
138 dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200139 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200140
141 SECTION("ls --recursive /example:a")
142 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100143 SECTION("cwd: /") { }
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100144 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200145
146 input = "ls --recursive /example:a";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200147 expected.m_options.emplace_back(LsOption::Recursive);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200148 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200149 }
Václav Kubernát5c75b252018-10-10 18:33:47 +0200150
151 SECTION("ls example:list")
152 {
153 input = "ls example:list";
154 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, list_{"list"})}};
155 }
156
Václav Kubernát211383d2020-01-06 18:07:50 +0100157 SECTION("ls /example:list")
158 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100159 SECTION("cwd: /") { }
Václav Kubernát211383d2020-01-06 18:07:50 +0100160 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
161 input = "ls /example:list";
162 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, list_{"list"})}};
163 }
164
Václav Kubernát5c75b252018-10-10 18:33:47 +0200165 SECTION("ls example:a/example:listInCont")
166 {
167 input = "ls example:a/example:listInCont";
168 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
169 dataNode_(module_{"example"}, list_{"listInCont"})}};
170 }
171
Václav Kubernátdd8ec152020-05-13 17:06:36 +0200172 SECTION("ls example:a/example:listInCont/example:contInList")
173 {
174 input = "ls example:a/example:listInCont/example:contInList";
175 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, container_{"a"}),
176 schemaNode_(module_{"example"}, list_{"listInCont"}),
177 schemaNode_(module_{"example"}, container_{"contInList"})}};
178 }
179
Václav Kubernát5c75b252018-10-10 18:33:47 +0200180 SECTION("ls example:list[number=342]/contInList")
181 {
182 input = "ls example:list[number=342]/contInList";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100183 auto keys = ListInstance{
Václav Kubernát7707cae2020-01-16 12:04:53 +0100184 {"number", int32_t{342}}};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200185 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}),
186 dataNode_(container_{"contInList"})}};
187 }
Václav Kubernátac035d62019-02-18 10:59:08 +0100188
189 SECTION("ls example:list/contInList")
190 {
191 input = "ls example:list/contInList";
192 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}),
193 schemaNode_(container_{"contInList"})}};
194 }
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200195
Václav Kubernát88c2a612020-05-06 02:20:03 +0200196 SECTION("ls example:list/example:contInList")
197 {
198 input = "ls example:list/example:contInList";
199 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}),
200 schemaNode_(module_{"example"},container_{"contInList"})}};
201 }
202
Václav Kubernát1cd8c9c2020-05-06 01:38:59 +0200203 SECTION("ls example:list[number=343]/..")
204 {
205 input = "ls example:list[number=343]/..";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100206 auto keys = ListInstance{
Václav Kubernát1cd8c9c2020-05-06 01:38:59 +0200207 {"number", int32_t{343}}};
208 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}), dataNode_{nodeup_{}}}};
209 }
210
211 SECTION("ls example:list/..")
212 {
213 input = "ls example:list/..";
214 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}), schemaNode_{nodeup_{}}}};
215 }
216
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200217 SECTION("ls example:*")
218 {
219 input = "ls example:*";
220 expected.m_path = module_{"example"};
221 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200222 }
223
224 command_ command = parser.parseCommand(input, errorStream);
225 REQUIRE(command.type() == typeid(ls_));
226 REQUIRE(boost::get<ls_>(command) == expected);
227 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200228
Václav Kubernát11afac72018-07-18 14:59:53 +0200229 SECTION("invalid input")
230 {
231 SECTION("invalid path")
232 {
Václav Kubernát37171a12018-08-31 17:01:48 +0200233 SECTION("ls example:nonexistent")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100234 input = "ls example:nonexistent";
Václav Kubernát37171a12018-08-31 17:01:48 +0200235
236 SECTION("ls /example:nonexistent")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100237 input = "ls /example:nonexistent";
Václav Kubernát37171a12018-08-31 17:01:48 +0200238
Václav Kubernátb9621052019-03-18 18:27:49 +0100239 SECTION("ls /exa")
240 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100241 SECTION("cwd: /") { }
Václav Kubernátb9621052019-03-18 18:27:49 +0100242 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
243 input = "ls /exa";
244 }
245
Václav Kubernát37171a12018-08-31 17:01:48 +0200246 SECTION("ls /bad:nonexistent")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100247 input = "ls /bad:nonexistent";
Václav Kubernát37171a12018-08-31 17:01:48 +0200248
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200249 SECTION("ls example:a/nonexistent")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100250 input = "ls example:a/nonexistent";
Václav Kubernát37171a12018-08-31 17:01:48 +0200251
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200252 SECTION("ls /example:a/nonexistent")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100253 input = "ls /example:a/nonexistent";
Václav Kubernát11afac72018-07-18 14:59:53 +0200254 }
255
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200256 SECTION("whitespace before path")
257 {
258 SECTION("ls --recursive/")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100259 input = "ls --recursive/";
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200260
261 SECTION("ls/")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100262 input = "ls/";
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200263
264 SECTION("ls --recursive/example:a")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100265 input = "ls --recursive/example:a";
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200266
267 SECTION("ls/example:a")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100268 input = "ls/example:a";
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200269
270 SECTION("lssecond:a")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100271 input = "lssecond:a";
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200272 }
273
Jan Kundrátc381e632019-03-14 13:39:11 +0100274 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát11afac72018-07-18 14:59:53 +0200275 }
276}