blob: f862309ee4251038ad0ce763393b076900e327c8 [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át88c2a612020-05-06 02:20:03 +0200172 SECTION("ls example:list/example:contInList")
173 {
174 input = "ls example:list/example:contInList";
175 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}),
176 schemaNode_(module_{"example"},container_{"contInList"})}};
177 }
178
Václav Kubernát1cd8c9c2020-05-06 01:38:59 +0200179 SECTION("ls example:list[number=343]/..")
180 {
181 input = "ls example:list[number=343]/..";
182 auto keys = std::map<std::string, leaf_data_>{
183 {"number", int32_t{343}}};
184 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}), dataNode_{nodeup_{}}}};
185 }
186
187 SECTION("ls example:list/..")
188 {
189 input = "ls example:list/..";
190 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}), schemaNode_{nodeup_{}}}};
191 }
192
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200193 SECTION("ls example:*")
194 {
195 input = "ls example:*";
196 expected.m_path = module_{"example"};
197 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200198 }
199
200 command_ command = parser.parseCommand(input, errorStream);
201 REQUIRE(command.type() == typeid(ls_));
202 REQUIRE(boost::get<ls_>(command) == expected);
203 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200204
Václav Kubernát11afac72018-07-18 14:59:53 +0200205 SECTION("invalid input")
206 {
207 SECTION("invalid path")
208 {
Václav Kubernát37171a12018-08-31 17:01:48 +0200209 SECTION("ls example:nonexistent")
210 input = "ls example:nonexistent";
211
212 SECTION("ls /example:nonexistent")
213 input = "ls /example:nonexistent";
214
Václav Kubernátb9621052019-03-18 18:27:49 +0100215 SECTION("ls /exa")
216 {
217 SECTION("cwd: /") {}
218 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
219 input = "ls /exa";
220 }
221
Václav Kubernát37171a12018-08-31 17:01:48 +0200222 SECTION("ls /bad:nonexistent")
223 input = "ls /bad:nonexistent";
224
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200225 SECTION("ls example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200226 input = "ls example:a/nonexistent";
227
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200228 SECTION("ls /example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200229 input = "ls /example:a/nonexistent";
Václav Kubernát11afac72018-07-18 14:59:53 +0200230 }
231
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200232 SECTION("whitespace before path")
233 {
234 SECTION("ls --recursive/")
235 input = "ls --recursive/";
236
237 SECTION("ls/")
238 input = "ls/";
239
240 SECTION("ls --recursive/example:a")
241 input = "ls --recursive/example:a";
242
243 SECTION("ls/example:a")
244 input = "ls/example:a";
245
246 SECTION("lssecond:a")
247 input = "lssecond:a";
248 }
249
Jan Kundrátc381e632019-03-14 13:39:11 +0100250 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát11afac72018-07-18 14:59:53 +0200251 }
252}