blob: e18cf25852265f7de97b17de7c148f4f128ee2da [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
Jan Kundráta33cf082019-03-28 11:55:57 +01009#include "trompeloeil_doctest.h"
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");
19 schema->addContainer("", "example:a");
Václav Kubernát5c75b252018-10-10 18:33:47 +020020 schema->addList("example:a", "example:listInCont", {"number"});
Václav Kubernát11afac72018-07-18 14:59:53 +020021 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"});
28 schema->addContainer("example:list", "example:contInList");
29 schema->addList("", "example:twoKeyList", {"number", "name"});
30 Parser parser(schema);
31 std::string input;
32 std::ostringstream errorStream;
33
34 SECTION("valid input")
35 {
36 ls_ expected;
37
38 SECTION("no arguments")
39 {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020040 SECTION("ls")
41 input = "ls";
42
43 SECTION("ls --recursive")
44 {
45 input = "ls --recursive";
46 expected.m_options.push_back(LsOption::Recursive);
47 }
Václav Kubernát11afac72018-07-18 14:59:53 +020048 }
49
50 SECTION("with path argument")
51 {
Václav Kubernát37171a12018-08-31 17:01:48 +020052 SECTION("ls example:a")
53 {
54 input = "ls example:a";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020055 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020056 }
57
58 SECTION("ls /example:a")
59 {
60 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +010061 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020062
63 input = "ls /example:a";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020064 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020065 }
66
67 SECTION("ls /")
68 {
69 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +010070 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020071 input = "ls /";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020072 expected.m_path = dataPath_{Scope::Absolute, {}};
Václav Kubernát37171a12018-08-31 17:01:48 +020073 }
74
75 SECTION("ls example:a/a2")
76 {
77 input = "ls example:a/a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020078 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
79 dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020080 }
81
82 SECTION("ls a2")
83 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +020084 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}});
Václav Kubernát37171a12018-08-31 17:01:48 +020085 input = "ls a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020086 expected.m_path = dataPath_{Scope::Relative, {dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020087 }
88
89 SECTION("ls /example:a/a2")
90 {
91 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +010092 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +020093 input = "ls /example:a/a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020094 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}),
95 dataNode_(container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +020096 }
97
98 SECTION("ls example:a/example:a2")
99 {
100 input = "ls example:a/example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200101 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
102 dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200103 }
104
105 SECTION("ls example:a2")
106 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200107 parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}});
Václav Kubernát37171a12018-08-31 17:01:48 +0200108 input = "ls example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200109 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200110 }
111
112 SECTION("ls /example:a/example:a2")
113 {
114 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100115 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernát37171a12018-08-31 17:01:48 +0200116
117 input = "ls /example:a/example:a2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200118 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}),
119 dataNode_(module_{"example"}, container_{"a2"})}};
Václav Kubernát37171a12018-08-31 17:01:48 +0200120 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200121
122 SECTION("ls --recursive /example:a")
123 {
124 SECTION("cwd: /") {}
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100125 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200126
127 input = "ls --recursive /example:a";
128 expected.m_options.push_back(LsOption::Recursive);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200129 expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"})}};
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200130 }
Václav Kubernát5c75b252018-10-10 18:33:47 +0200131
132 SECTION("ls example:list")
133 {
134 input = "ls example:list";
135 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, list_{"list"})}};
136 }
137
138 SECTION("ls example:a/example:listInCont")
139 {
140 input = "ls example:a/example:listInCont";
141 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}),
142 dataNode_(module_{"example"}, list_{"listInCont"})}};
143 }
144
145 SECTION("ls example:list[number=342]/contInList")
146 {
147 input = "ls example:list[number=342]/contInList";
148 auto keys = std::map<std::string, std::string>{
149 {"number", "342"}};
150 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}),
151 dataNode_(container_{"contInList"})}};
152 }
Václav Kubernátac035d62019-02-18 10:59:08 +0100153
154 SECTION("ls example:list/contInList")
155 {
156 input = "ls example:list/contInList";
157 expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}),
158 schemaNode_(container_{"contInList"})}};
159 }
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200160
161 SECTION("ls example:*")
162 {
163 input = "ls example:*";
164 expected.m_path = module_{"example"};
165 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200166 }
167
168 command_ command = parser.parseCommand(input, errorStream);
169 REQUIRE(command.type() == typeid(ls_));
170 REQUIRE(boost::get<ls_>(command) == expected);
171 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200172
Václav Kubernát11afac72018-07-18 14:59:53 +0200173 SECTION("invalid input")
174 {
175 SECTION("invalid path")
176 {
Václav Kubernát37171a12018-08-31 17:01:48 +0200177 SECTION("ls example:nonexistent")
178 input = "ls example:nonexistent";
179
180 SECTION("ls /example:nonexistent")
181 input = "ls /example:nonexistent";
182
Václav Kubernátb9621052019-03-18 18:27:49 +0100183 SECTION("ls /exa")
184 {
185 SECTION("cwd: /") {}
186 SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); }
187 input = "ls /exa";
188 }
189
Václav Kubernát37171a12018-08-31 17:01:48 +0200190 SECTION("ls /bad:nonexistent")
191 input = "ls /bad:nonexistent";
192
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200193 SECTION("ls example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200194 input = "ls example:a/nonexistent";
195
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200196 SECTION("ls /example:a/nonexistent")
Václav Kubernát37171a12018-08-31 17:01:48 +0200197 input = "ls /example:a/nonexistent";
Václav Kubernát11afac72018-07-18 14:59:53 +0200198 }
199
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200200 SECTION("whitespace before path")
201 {
202 SECTION("ls --recursive/")
203 input = "ls --recursive/";
204
205 SECTION("ls/")
206 input = "ls/";
207
208 SECTION("ls --recursive/example:a")
209 input = "ls --recursive/example:a";
210
211 SECTION("ls/example:a")
212 input = "ls/example:a";
213
214 SECTION("lssecond:a")
215 input = "lssecond:a";
216 }
217
Jan Kundrátc381e632019-03-14 13:39:11 +0100218 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát11afac72018-07-18 14:59:53 +0200219 }
220}