blob: 2c72041000b0197259c60a8bd68d5975bbec7641 [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át7707cae2020-01-16 12:04:53 +010028 schema->addLeaf("/example:list", "example:number", yang::LeafDataTypes::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át7707cae2020-01-16 12:04:53 +010031 schema->addLeaf("/example:twoKeyList", "example:number", yang::LeafDataTypes::Int32);
32 schema->addLeaf("/example:twoKeyList", "example:name", yang::LeafDataTypes::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
172 SECTION("ls example:*")
173 {
174 input = "ls example:*";
175 expected.m_path = module_{"example"};
176 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200177 }
178
179 command_ command = parser.parseCommand(input, errorStream);
180 REQUIRE(command.type() == typeid(ls_));
181 REQUIRE(boost::get<ls_>(command) == expected);
182 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200183
Václav Kubernát11afac72018-07-18 14:59:53 +0200184 SECTION("invalid input")
185 {
186 SECTION("invalid path")
187 {
Václav Kubernát37171a12018-08-31 17:01:48 +0200188 SECTION("ls example:nonexistent")
189 input = "ls example:nonexistent";
190
191 SECTION("ls /example:nonexistent")
192 input = "ls /example:nonexistent";
193
Václav Kubernátb9621052019-03-18 18:27:49 +0100194 SECTION("ls /exa")
195 {
196 SECTION("cwd: /") {}
197 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
198 input = "ls /exa";
199 }
200
Václav Kubernát37171a12018-08-31 17:01:48 +0200201 SECTION("ls /bad:nonexistent")
202 input = "ls /bad:nonexistent";
203
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200204 SECTION("ls example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200205 input = "ls example:a/nonexistent";
206
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200207 SECTION("ls /example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200208 input = "ls /example:a/nonexistent";
Václav Kubernát11afac72018-07-18 14:59:53 +0200209 }
210
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200211 SECTION("whitespace before path")
212 {
213 SECTION("ls --recursive/")
214 input = "ls --recursive/";
215
216 SECTION("ls/")
217 input = "ls/";
218
219 SECTION("ls --recursive/example:a")
220 input = "ls --recursive/example:a";
221
222 SECTION("ls/example:a")
223 input = "ls/example:a";
224
225 SECTION("lssecond:a")
226 input = "lssecond:a";
227 }
228
Jan Kundrátc381e632019-03-14 13:39:11 +0100229 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát11afac72018-07-18 14:59:53 +0200230 }
231}