blob: 6d704148dcf524ae05240ff27d69e089dadd3e99 [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
80 SECTION("ls example:a/a2")
81 {
82 input = "ls example:a/a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020083 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
84 dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020085 }
86
87 SECTION("ls a2")
88 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +020089 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}});
Václav Kubernát37171a12018-08-31 17:01:48 +020090 input = "ls a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020091 expected.m_path = dataPath_{Scope::Relative, {dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020092 }
93
94 SECTION("ls /example:a/a2")
95 {
96 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +010097 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020098 input = "ls /example:a/a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020099 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}),
100 dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200101 }
102
103 SECTION("ls example:a/example:a2")
104 {
105 input = "ls example:a/example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200106 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
107 dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200108 }
109
110 SECTION("ls example:a2")
111 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200112 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}});
Václav Kubernát37171a12018-08-31 17:01:48 +0200113 input = "ls example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200114 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200115 }
116
117 SECTION("ls /example:a/example:a2")
118 {
119 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100120 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +0200121
122 input = "ls /example:a/example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200123 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}),
124 dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200125 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200126
127 SECTION("ls --recursive /example:a")
128 {
129 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100130 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200131
132 input = "ls --recursive /example:a";
133 expected.m_options.push_back(LsOption::Recursive);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200134 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200135 }
Václav Kubernát5c75b252018-10-10 18:33:47 +0200136
137 SECTION("ls example:list")
138 {
139 input = "ls example:list";
140 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, list_{"list"})}};
141 }
142
Václav Kubernát211383d2020-01-06 18:07:50 +0100143 SECTION("ls /example:list")
144 {
145 SECTION("cwd: /") {}
146 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
147 input = "ls /example:list";
148 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, list_{"list"})}};
149 }
150
Václav Kubernát5c75b252018-10-10 18:33:47 +0200151 SECTION("ls example:a/example:listInCont")
152 {
153 input = "ls example:a/example:listInCont";
154 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
155 dataNode_(module_{"example"}, list_{"listInCont"})}};
156 }
157
Václav Kubernátdd8ec152020-05-13 17:06:36 +0200158 SECTION("ls example:a/example:listInCont/example:contInList")
159 {
160 input = "ls example:a/example:listInCont/example:contInList";
161 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, container_{"a"}),
162 schemaNode_(module_{"example"}, list_{"listInCont"}),
163 schemaNode_(module_{"example"}, container_{"contInList"})}};
164 }
165
Václav Kubernát5c75b252018-10-10 18:33:47 +0200166 SECTION("ls example:list[number=342]/contInList")
167 {
168 input = "ls example:list[number=342]/contInList";
Václav Kubernát7707cae2020-01-16 12:04:53 +0100169 auto keys = std::map<std::string, leaf_data_>{
170 {"number", int32_t{342}}};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200171 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}),
172 dataNode_(container_{"contInList"})}};
173 }
Václav Kubernátac035d62019-02-18 10:59:08 +0100174
175 SECTION("ls example:list/contInList")
176 {
177 input = "ls example:list/contInList";
178 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}),
179 schemaNode_(container_{"contInList"})}};
180 }
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200181
Václav Kubernát88c2a612020-05-06 02:20:03 +0200182 SECTION("ls example:list/example:contInList")
183 {
184 input = "ls example:list/example:contInList";
185 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}),
186 schemaNode_(module_{"example"},container_{"contInList"})}};
187 }
188
Václav Kubernát1cd8c9c2020-05-06 01:38:59 +0200189 SECTION("ls example:list[number=343]/..")
190 {
191 input = "ls example:list[number=343]/..";
192 auto keys = std::map<std::string, leaf_data_>{
193 {"number", int32_t{343}}};
194 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}), dataNode_{nodeup_{}}}};
195 }
196
197 SECTION("ls example:list/..")
198 {
199 input = "ls example:list/..";
200 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}), schemaNode_{nodeup_{}}}};
201 }
202
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200203 SECTION("ls example:*")
204 {
205 input = "ls example:*";
206 expected.m_path = module_{"example"};
207 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200208 }
209
210 command_ command = parser.parseCommand(input, errorStream);
211 REQUIRE(command.type() == typeid(ls_));
212 REQUIRE(boost::get<ls_>(command) == expected);
213 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200214
Václav Kubernát11afac72018-07-18 14:59:53 +0200215 SECTION("invalid input")
216 {
217 SECTION("invalid path")
218 {
Václav Kubernát37171a12018-08-31 17:01:48 +0200219 SECTION("ls example:nonexistent")
220 input = "ls example:nonexistent";
221
222 SECTION("ls /example:nonexistent")
223 input = "ls /example:nonexistent";
224
Václav Kubernátb9621052019-03-18 18:27:49 +0100225 SECTION("ls /exa")
226 {
227 SECTION("cwd: /") {}
228 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
229 input = "ls /exa";
230 }
231
Václav Kubernát37171a12018-08-31 17:01:48 +0200232 SECTION("ls /bad:nonexistent")
233 input = "ls /bad:nonexistent";
234
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200235 SECTION("ls example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200236 input = "ls example:a/nonexistent";
237
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200238 SECTION("ls /example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200239 input = "ls /example:a/nonexistent";
Václav Kubernát11afac72018-07-18 14:59:53 +0200240 }
241
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200242 SECTION("whitespace before path")
243 {
244 SECTION("ls --recursive/")
245 input = "ls --recursive/";
246
247 SECTION("ls/")
248 input = "ls/";
249
250 SECTION("ls --recursive/example:a")
251 input = "ls --recursive/example:a";
252
253 SECTION("ls/example:a")
254 input = "ls/example:a";
255
256 SECTION("lssecond:a")
257 input = "lssecond:a";
258 }
259
Jan Kundrátc381e632019-03-14 13:39:11 +0100260 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát11afac72018-07-18 14:59:53 +0200261 }
262}