blob: 93ec75baf1c1da9357d3a5b138f6744aa37435ee [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"
12#include "static_schema.hpp"
13
14TEST_CASE("ls")
15{
16 auto schema = std::make_shared<StaticSchema>();
17 schema->addModule("example");
18 schema->addModule("second");
Václav Kubernátefcac932020-01-10 15:26:32 +010019 schema->addContainer("/", "example:a");
20 schema->addList("/example:a", "example:listInCont", {"number"});
Václav Kubernátdd8ec152020-05-13 17:06:36 +020021 schema->addContainer("/example:a/example:listInCont", "example:contInList");
Václav Kubernátefcac932020-01-10 15:26:32 +010022 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át11afac72018-07-18 14:59:53 +020034 Parser parser(schema);
35 std::string input;
36 std::ostringstream errorStream;
37
38 SECTION("valid input")
39 {
40 ls_ expected;
41
42 SECTION("no arguments")
43 {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020044 SECTION("ls")
45 input = "ls";
46
47 SECTION("ls --recursive")
48 {
49 input = "ls --recursive";
50 expected.m_options.push_back(LsOption::Recursive);
51 }
Václav Kubernát11afac72018-07-18 14:59:53 +020052 }
53
54 SECTION("with path argument")
55 {
Václav Kubernát37171a12018-08-31 17:01:48 +020056 SECTION("ls example:a")
57 {
58 input = "ls example:a";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020059 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020060 }
61
62 SECTION("ls /example:a")
63 {
64 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +010065 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020066
67 input = "ls /example:a";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020068 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020069 }
70
71 SECTION("ls /")
72 {
73 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +010074 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020075 input = "ls /";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020076 expected.m_path = dataPath_{Scope::Absolute, {}};
Václav Kubernát37171a12018-08-31 17:01:48 +020077 }
78
79 SECTION("ls example:a/a2")
80 {
81 input = "ls example:a/a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020082 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
83 dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020084 }
85
86 SECTION("ls a2")
87 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +020088 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}});
Václav Kubernát37171a12018-08-31 17:01:48 +020089 input = "ls a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020090 expected.m_path = dataPath_{Scope::Relative, {dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020091 }
92
93 SECTION("ls /example:a/a2")
94 {
95 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +010096 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020097 input = "ls /example:a/a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020098 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}),
99 dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200100 }
101
102 SECTION("ls example:a/example:a2")
103 {
104 input = "ls example:a/example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200105 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
106 dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200107 }
108
109 SECTION("ls example:a2")
110 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200111 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}});
Václav Kubernát37171a12018-08-31 17:01:48 +0200112 input = "ls example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200113 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200114 }
115
116 SECTION("ls /example:a/example:a2")
117 {
118 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100119 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +0200120
121 input = "ls /example:a/example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200122 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}),
123 dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200124 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200125
126 SECTION("ls --recursive /example:a")
127 {
128 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100129 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200130
131 input = "ls --recursive /example:a";
132 expected.m_options.push_back(LsOption::Recursive);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200133 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200134 }
Václav Kubernát5c75b252018-10-10 18:33:47 +0200135
136 SECTION("ls example:list")
137 {
138 input = "ls example:list";
139 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, list_{"list"})}};
140 }
141
Václav Kubernát211383d2020-01-06 18:07:50 +0100142 SECTION("ls /example:list")
143 {
144 SECTION("cwd: /") {}
145 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
146 input = "ls /example:list";
147 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, list_{"list"})}};
148 }
149
Václav Kubernát5c75b252018-10-10 18:33:47 +0200150 SECTION("ls example:a/example:listInCont")
151 {
152 input = "ls example:a/example:listInCont";
153 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
154 dataNode_(module_{"example"}, list_{"listInCont"})}};
155 }
156
Václav Kubernátdd8ec152020-05-13 17:06:36 +0200157 SECTION("ls example:a/example:listInCont/example:contInList")
158 {
159 input = "ls example:a/example:listInCont/example:contInList";
160 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, container_{"a"}),
161 schemaNode_(module_{"example"}, list_{"listInCont"}),
162 schemaNode_(module_{"example"}, container_{"contInList"})}};
163 }
164
Václav Kubernát5c75b252018-10-10 18:33:47 +0200165 SECTION("ls example:list[number=342]/contInList")
166 {
167 input = "ls example:list[number=342]/contInList";
Václav Kubernát7707cae2020-01-16 12:04:53 +0100168 auto keys = std::map<std::string, leaf_data_>{
169 {"number", int32_t{342}}};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200170 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}),
171 dataNode_(container_{"contInList"})}};
172 }
Václav Kubernátac035d62019-02-18 10:59:08 +0100173
174 SECTION("ls example:list/contInList")
175 {
176 input = "ls example:list/contInList";
177 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}),
178 schemaNode_(container_{"contInList"})}};
179 }
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200180
Václav Kubernát88c2a612020-05-06 02:20:03 +0200181 SECTION("ls example:list/example:contInList")
182 {
183 input = "ls example:list/example:contInList";
184 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}),
185 schemaNode_(module_{"example"},container_{"contInList"})}};
186 }
187
Václav Kubernát1cd8c9c2020-05-06 01:38:59 +0200188 SECTION("ls example:list[number=343]/..")
189 {
190 input = "ls example:list[number=343]/..";
191 auto keys = std::map<std::string, leaf_data_>{
192 {"number", int32_t{343}}};
193 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}), dataNode_{nodeup_{}}}};
194 }
195
196 SECTION("ls example:list/..")
197 {
198 input = "ls example:list/..";
199 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}), schemaNode_{nodeup_{}}}};
200 }
201
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200202 SECTION("ls example:*")
203 {
204 input = "ls example:*";
205 expected.m_path = module_{"example"};
206 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200207 }
208
209 command_ command = parser.parseCommand(input, errorStream);
210 REQUIRE(command.type() == typeid(ls_));
211 REQUIRE(boost::get<ls_>(command) == expected);
212 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200213
Václav Kubernát11afac72018-07-18 14:59:53 +0200214 SECTION("invalid input")
215 {
216 SECTION("invalid path")
217 {
Václav Kubernát37171a12018-08-31 17:01:48 +0200218 SECTION("ls example:nonexistent")
219 input = "ls example:nonexistent";
220
221 SECTION("ls /example:nonexistent")
222 input = "ls /example:nonexistent";
223
Václav Kubernátb9621052019-03-18 18:27:49 +0100224 SECTION("ls /exa")
225 {
226 SECTION("cwd: /") {}
227 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
228 input = "ls /exa";
229 }
230
Václav Kubernát37171a12018-08-31 17:01:48 +0200231 SECTION("ls /bad:nonexistent")
232 input = "ls /bad:nonexistent";
233
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200234 SECTION("ls example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200235 input = "ls example:a/nonexistent";
236
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200237 SECTION("ls /example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200238 input = "ls /example:a/nonexistent";
Václav Kubernát11afac72018-07-18 14:59:53 +0200239 }
240
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200241 SECTION("whitespace before path")
242 {
243 SECTION("ls --recursive/")
244 input = "ls --recursive/";
245
246 SECTION("ls/")
247 input = "ls/";
248
249 SECTION("ls --recursive/example:a")
250 input = "ls --recursive/example:a";
251
252 SECTION("ls/example:a")
253 input = "ls/example:a";
254
255 SECTION("lssecond:a")
256 input = "lssecond:a";
257 }
258
Jan Kundrátc381e632019-03-14 13:39:11 +0100259 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát11afac72018-07-18 14:59:53 +0200260 }
261}