blob: b3be4735a06702ddc9b400688cdb51f907abbe53 [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"});
21 schema->addContainer("/", "second:a");
22 schema->addContainer("/", "example:b");
23 schema->addContainer("/example:a", "example:a2");
24 schema->addContainer("/example:b", "example:b2");
25 schema->addContainer("/example:a/example:a2", "example:a3");
26 schema->addContainer("/example:b/example:b2", "example:b3");
27 schema->addList("/", "example:list", {"number"});
Václav Kubernát3a99f002020-03-31 02:27:41 +020028 schema->addLeaf("/example:list", "example:number", yang::Int32{});
Václav Kubernátefcac932020-01-10 15:26:32 +010029 schema->addContainer("/example:list", "example:contInList");
30 schema->addList("/", "example:twoKeyList", {"number", "name"});
Václav Kubernát3a99f002020-03-31 02:27:41 +020031 schema->addLeaf("/example:twoKeyList", "example:number", yang::Int32{});
32 schema->addLeaf("/example:twoKeyList", "example:name", yang::String{});
Václav Kubernát11afac72018-07-18 14:59:53 +020033 Parser parser(schema);
34 std::string input;
35 std::ostringstream errorStream;
36
37 SECTION("valid input")
38 {
39 ls_ expected;
40
41 SECTION("no arguments")
42 {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020043 SECTION("ls")
44 input = "ls";
45
46 SECTION("ls --recursive")
47 {
48 input = "ls --recursive";
49 expected.m_options.push_back(LsOption::Recursive);
50 }
Václav Kubernát11afac72018-07-18 14:59:53 +020051 }
52
53 SECTION("with path argument")
54 {
Václav Kubernát37171a12018-08-31 17:01:48 +020055 SECTION("ls example:a")
56 {
57 input = "ls example:a";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020058 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020059 }
60
61 SECTION("ls /example:a")
62 {
63 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +010064 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020065
66 input = "ls /example:a";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020067 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020068 }
69
70 SECTION("ls /")
71 {
72 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +010073 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020074 input = "ls /";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020075 expected.m_path = dataPath_{Scope::Absolute, {}};
Václav Kubernát37171a12018-08-31 17:01:48 +020076 }
77
78 SECTION("ls example:a/a2")
79 {
80 input = "ls example:a/a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020081 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
82 dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020083 }
84
85 SECTION("ls a2")
86 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +020087 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}});
Václav Kubernát37171a12018-08-31 17:01:48 +020088 input = "ls a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020089 expected.m_path = dataPath_{Scope::Relative, {dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020090 }
91
92 SECTION("ls /example:a/a2")
93 {
94 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +010095 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020096 input = "ls /example:a/a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020097 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}),
98 dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020099 }
100
101 SECTION("ls example:a/example:a2")
102 {
103 input = "ls example:a/example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200104 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
105 dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200106 }
107
108 SECTION("ls example:a2")
109 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200110 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}});
Václav Kubernát37171a12018-08-31 17:01:48 +0200111 input = "ls example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200112 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200113 }
114
115 SECTION("ls /example:a/example:a2")
116 {
117 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100118 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +0200119
120 input = "ls /example:a/example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200121 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}),
122 dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200123 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200124
125 SECTION("ls --recursive /example:a")
126 {
127 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100128 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200129
130 input = "ls --recursive /example:a";
131 expected.m_options.push_back(LsOption::Recursive);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200132 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200133 }
Václav Kubernát5c75b252018-10-10 18:33:47 +0200134
135 SECTION("ls example:list")
136 {
137 input = "ls example:list";
138 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, list_{"list"})}};
139 }
140
Václav Kubernát211383d2020-01-06 18:07:50 +0100141 SECTION("ls /example:list")
142 {
143 SECTION("cwd: /") {}
144 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
145 input = "ls /example:list";
146 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, list_{"list"})}};
147 }
148
Václav Kubernát5c75b252018-10-10 18:33:47 +0200149 SECTION("ls example:a/example:listInCont")
150 {
151 input = "ls example:a/example:listInCont";
152 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
153 dataNode_(module_{"example"}, list_{"listInCont"})}};
154 }
155
156 SECTION("ls example:list[number=342]/contInList")
157 {
158 input = "ls example:list[number=342]/contInList";
Václav Kubernát7707cae2020-01-16 12:04:53 +0100159 auto keys = std::map<std::string, leaf_data_>{
160 {"number", int32_t{342}}};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200161 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}),
162 dataNode_(container_{"contInList"})}};
163 }
Václav Kubernátac035d62019-02-18 10:59:08 +0100164
165 SECTION("ls example:list/contInList")
166 {
167 input = "ls example:list/contInList";
168 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}),
169 schemaNode_(container_{"contInList"})}};
170 }
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200171
Václav Kubernát1cd8c9c2020-05-06 01:38:59 +0200172 SECTION("ls example:list[number=343]/..")
173 {
174 input = "ls example:list[number=343]/..";
175 auto keys = std::map<std::string, leaf_data_>{
176 {"number", int32_t{343}}};
177 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}), dataNode_{nodeup_{}}}};
178 }
179
180 SECTION("ls example:list/..")
181 {
182 input = "ls example:list/..";
183 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}), schemaNode_{nodeup_{}}}};
184 }
185
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200186 SECTION("ls example:*")
187 {
188 input = "ls example:*";
189 expected.m_path = module_{"example"};
190 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200191 }
192
193 command_ command = parser.parseCommand(input, errorStream);
194 REQUIRE(command.type() == typeid(ls_));
195 REQUIRE(boost::get<ls_>(command) == expected);
196 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200197
Václav Kubernát11afac72018-07-18 14:59:53 +0200198 SECTION("invalid input")
199 {
200 SECTION("invalid path")
201 {
Václav Kubernát37171a12018-08-31 17:01:48 +0200202 SECTION("ls example:nonexistent")
203 input = "ls example:nonexistent";
204
205 SECTION("ls /example:nonexistent")
206 input = "ls /example:nonexistent";
207
Václav Kubernátb9621052019-03-18 18:27:49 +0100208 SECTION("ls /exa")
209 {
210 SECTION("cwd: /") {}
211 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
212 input = "ls /exa";
213 }
214
Václav Kubernát37171a12018-08-31 17:01:48 +0200215 SECTION("ls /bad:nonexistent")
216 input = "ls /bad:nonexistent";
217
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200218 SECTION("ls example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200219 input = "ls example:a/nonexistent";
220
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200221 SECTION("ls /example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200222 input = "ls /example:a/nonexistent";
Václav Kubernát11afac72018-07-18 14:59:53 +0200223 }
224
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200225 SECTION("whitespace before path")
226 {
227 SECTION("ls --recursive/")
228 input = "ls --recursive/";
229
230 SECTION("ls/")
231 input = "ls/";
232
233 SECTION("ls --recursive/example:a")
234 input = "ls --recursive/example:a";
235
236 SECTION("ls/example:a")
237 input = "ls/example:a";
238
239 SECTION("lssecond:a")
240 input = "lssecond:a";
241 }
242
Jan Kundrátc381e632019-03-14 13:39:11 +0100243 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát11afac72018-07-18 14:59:53 +0200244 }
245}