blob: bcc1dab5a48544a1b8e8cd6989bba3d6e85b446c [file] [log] [blame]
Václav Kubernátd6662962018-03-22 17:41:33 +01001/*
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
9#include "trompeloeil_catch.h"
Václav Kubernát24df80e2018-06-06 15:18:03 +020010#include "ast_commands.hpp"
Václav Kubernát48fc3832018-05-28 14:21:22 +020011#include "parser.hpp"
Václav Kubernátbddbb172018-06-13 16:27:39 +020012#include "static_schema.hpp"
Václav Kubernátd6662962018-03-22 17:41:33 +010013
14TEST_CASE("cd")
15{
Václav Kubernátbddbb172018-06-13 16:27:39 +020016 auto schema = std::make_shared<StaticSchema>();
Václav Kubernát744f57f2018-06-29 22:46:26 +020017 schema->addModule("example");
18 schema->addModule("second");
19 schema->addContainer("", "example:a");
20 schema->addContainer("", "second:a");
21 schema->addContainer("", "example:b");
22 schema->addContainer("example:a", "example:a2");
23 schema->addContainer("example:b", "example:b2");
24 schema->addContainer("example:a/example:a2", "example:a3");
25 schema->addContainer("example:b/example:b2", "example:b3");
26 schema->addList("", "example:list", {"number"});
27 schema->addContainer("example:list", "example:contInList");
28 schema->addList("", "example:twoKeyList", {"number", "name"});
Václav Kubernát48fc3832018-05-28 14:21:22 +020029 Parser parser(schema);
Václav Kubernátd6662962018-03-22 17:41:33 +010030 std::string input;
Václav Kubernát2315c732018-05-16 20:25:55 +020031 std::ostringstream errorStream;
Václav Kubernátd6662962018-03-22 17:41:33 +010032
Václav Kubernát744f57f2018-06-29 22:46:26 +020033
Václav Kubernátb96eef72018-05-04 19:10:22 +020034 SECTION("valid input")
Václav Kubernátd6662962018-03-22 17:41:33 +010035 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020036 cd_ expected;
37
38 SECTION("container")
Václav Kubernátd6662962018-03-22 17:41:33 +010039 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020040 SECTION("example:a")
Václav Kubernátb96eef72018-05-04 19:10:22 +020041 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020042 input = "cd example:a";
43 expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("a")));
Václav Kubernátb96eef72018-05-04 19:10:22 +020044 }
45
Václav Kubernát744f57f2018-06-29 22:46:26 +020046 SECTION("second:a")
Václav Kubernátb96eef72018-05-04 19:10:22 +020047 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020048 input = "cd second:a";
49 expected.m_path.m_nodes.push_back(node_(module_{"second"}, container_("a")));
Václav Kubernátb96eef72018-05-04 19:10:22 +020050 }
51
Václav Kubernát744f57f2018-06-29 22:46:26 +020052 SECTION("example:b")
Václav Kubernátb96eef72018-05-04 19:10:22 +020053 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020054 input = "cd example:b";
55 expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("b")));
Václav Kubernátb96eef72018-05-04 19:10:22 +020056 }
57
Václav Kubernát744f57f2018-06-29 22:46:26 +020058 SECTION("example:a/a2")
Václav Kubernátb96eef72018-05-04 19:10:22 +020059 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020060 input = "cd example:a/a2";
61 expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("a")));
62 expected.m_path.m_nodes.push_back(node_(container_("a2")));
63 }
64
65 SECTION("example:a/example:a2")
66 {
67 input = "cd example:a/example:a2";
68 expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("a")));
69 expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("a2")));
70 }
71
72 SECTION("example:b/b2")
73 {
74 input = "cd example:b/b2";
75 expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("b")));
76 expected.m_path.m_nodes.push_back(node_(container_("b2")));
Václav Kubernátb96eef72018-05-04 19:10:22 +020077 }
Václav Kubernátd6662962018-03-22 17:41:33 +010078 }
79
Václav Kubernátb96eef72018-05-04 19:10:22 +020080 SECTION("list elements")
Václav Kubernátd6662962018-03-22 17:41:33 +010081 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020082 SECTION("example:list[number=1]")
Václav Kubernátb96eef72018-05-04 19:10:22 +020083 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020084 input = "cd example:list[number=1]";
Václav Kubernátb96eef72018-05-04 19:10:22 +020085 auto keys = std::map<std::string, std::string>{
Václav Kubernát48fc3832018-05-28 14:21:22 +020086 {"number", "1"}};
Václav Kubernát744f57f2018-06-29 22:46:26 +020087 expected.m_path.m_nodes.push_back(node_(module_{"example"}, listElement_("list", keys)));
Václav Kubernátb96eef72018-05-04 19:10:22 +020088 }
89
Václav Kubernát744f57f2018-06-29 22:46:26 +020090 SECTION("example:list[number=1]/contInList")
Václav Kubernátb96eef72018-05-04 19:10:22 +020091 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020092 input = "cd example:list[number=1]/contInList";
Václav Kubernátb96eef72018-05-04 19:10:22 +020093 auto keys = std::map<std::string, std::string>{
Václav Kubernát48fc3832018-05-28 14:21:22 +020094 {"number", "1"}};
Václav Kubernát744f57f2018-06-29 22:46:26 +020095 expected.m_path.m_nodes.push_back(node_(module_{"example"}, listElement_("list", keys)));
96 expected.m_path.m_nodes.push_back(node_(container_("contInList")));
Václav Kubernátb96eef72018-05-04 19:10:22 +020097 }
98
Václav Kubernát89728d82018-09-13 16:28:28 +020099 SECTION("example:twoKeyList[number=4][name='abcd']")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200100 {
Václav Kubernát89728d82018-09-13 16:28:28 +0200101 input = "cd example:twoKeyList[number=4][name='abcd']";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200102 auto keys = std::map<std::string, std::string>{
Václav Kubernát48fc3832018-05-28 14:21:22 +0200103 {"number", "4"},
104 {"name", "abcd"}};
Václav Kubernát744f57f2018-06-29 22:46:26 +0200105 expected.m_path.m_nodes.push_back(node_(module_{"example"}, listElement_("twoKeyList", keys)));
Václav Kubernátb96eef72018-05-04 19:10:22 +0200106 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100107 }
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200108
109 SECTION("whitespace handling")
110 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200111 SECTION(" cd example:a ")
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200112 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200113 input = " cd example:a ";
114 expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("a")));
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200115 }
116 }
117
Václav Kubernát60d6f292018-05-25 09:45:32 +0200118 SECTION("moving up")
119 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200120 SECTION("example:a/..")
Václav Kubernát60d6f292018-05-25 09:45:32 +0200121 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200122 input = "cd example:a/..";
123 expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("a")));
124 expected.m_path.m_nodes.push_back(node_(nodeup_()));
Václav Kubernát60d6f292018-05-25 09:45:32 +0200125 }
126
Václav Kubernát744f57f2018-06-29 22:46:26 +0200127 SECTION("example:a/../example:a")
Václav Kubernát60d6f292018-05-25 09:45:32 +0200128 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200129 input = "cd example:a/../example:a";
130 expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("a")));
131 expected.m_path.m_nodes.push_back(node_(nodeup_()));
132 expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("a")));
Václav Kubernát60d6f292018-05-25 09:45:32 +0200133 }
134
Václav Kubernát744f57f2018-06-29 22:46:26 +0200135 SECTION("example:a/../example:a/a2")
Václav Kubernát60d6f292018-05-25 09:45:32 +0200136 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200137 input = "cd example:a/../example:a/a2";
138 expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("a")));
139 expected.m_path.m_nodes.push_back(node_(nodeup_()));
140 expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("a")));
141 expected.m_path.m_nodes.push_back(node_(container_("a2")));
Václav Kubernát60d6f292018-05-25 09:45:32 +0200142 }
143 }
144
Václav Kubernátb61336d2018-05-28 17:35:03 +0200145 command_ command = parser.parseCommand(input, errorStream);
146 REQUIRE(command.type() == typeid(cd_));
147 REQUIRE(boost::get<cd_>(command) == expected);
Václav Kubernátd6662962018-03-22 17:41:33 +0100148 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200149 SECTION("invalid input")
Václav Kubernátd6662962018-03-22 17:41:33 +0100150 {
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200151 SECTION("missing space between a command and its arguments")
152 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200153 SECTION("cdexample:a")
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200154 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200155 input = "cdexample:a";
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200156 }
157 }
Václav Kubernát744f57f2018-06-29 22:46:26 +0200158
159 SECTION("whitespace between module and nodename")
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200160 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200161 SECTION("cd example: a")
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200162 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200163 input = "cd example: a";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200164 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100165
Václav Kubernát744f57f2018-06-29 22:46:26 +0200166 SECTION("cd example : a")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200167 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200168 input = "cd example : a";
169 }
170
171 SECTION("cd example :a")
172 {
173 input = "cd example :a";
174 }
175 }
176
177 SECTION("entering modules")
178 {
179 SECTION("cd example")
180 {
181 input = "cd example";
182 }
183
184 SECTION("cd example:")
185 {
186 input = "cd example:";
187 }
188 }
189
190 SECTION("garbage arguments handling")
191 {
192 SECTION("cd example:a garbage")
193 {
194 input = "cd example:a garbage";
195 }
196 SECTION("cd example:a/a2 garbage")
197 {
198 input = "cd example:a/a2 garbage";
199 }
200 }
201
202 SECTION("invalid node identifiers")
203 {
204 SECTION("example:nonexistent")
205 {
206 input = "cd example:nonexistent";
207 }
208
209 SECTION("example:nonexistent/lol")
210 {
211 input = "cd example:nonexistent/lol";
212 }
213 }
214
215 SECTION("invalid module identifiers")
216 {
217 SECTION("elpmaxe:nonexistent")
218 {
219 input = "cd elpmaxe:nonexistent";
220 }
221
222 SECTION("elpmaxe:nonexistent/example:lol")
223 {
224 input = "cd elpmaxe:nonexistent/example:lol";
225 }
226 }
227
228 SECTION("no top-level module")
229 {
230 SECTION("cd a")
231 {
232 input = "cd a";
233 }
234
235 SECTION("cd example:a/../a")
236 {
237 input = "cd example:a/../a";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200238 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100239 }
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200240
Václav Kubernátb96eef72018-05-04 19:10:22 +0200241 SECTION("invalid list key identifiers")
242 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200243 SECTION("example:list")
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200244 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200245 input = "cd example:list";
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200246 }
247
Václav Kubernát744f57f2018-06-29 22:46:26 +0200248 SECTION("example:list[]")
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200249 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200250 input = "cd example:list[]";
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200251 }
252
Václav Kubernát89728d82018-09-13 16:28:28 +0200253 SECTION("example:twoKeyList[invalidKey='4']")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200254 {
Václav Kubernát89728d82018-09-13 16:28:28 +0200255 input = "cd example:twoKeyList[invalidKey='4']";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200256 }
257
Václav Kubernát89728d82018-09-13 16:28:28 +0200258 SECTION("example:twoKeyList[number=4][number=5]")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200259 {
Václav Kubernát89728d82018-09-13 16:28:28 +0200260 input = "cd example:twoKeyList[number=4][number=5]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200261 }
262
Václav Kubernát89728d82018-09-13 16:28:28 +0200263 SECTION("example:twoKeyList[number=4][name='lol'][number=7]")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200264 {
Václav Kubernát89728d82018-09-13 16:28:28 +0200265 input = "cd example:twoKeyList[number=4][name='lol'][number=7]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200266 }
267
Václav Kubernát744f57f2018-06-29 22:46:26 +0200268 SECTION("example:twoKeyList[number=4]")
Václav Kubernátb96eef72018-05-04 19:10:22 +0200269 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200270 input = "cd example:twoKeyList[number=4]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200271 }
Václav Kubernát89728d82018-09-13 16:28:28 +0200272
273 SECTION("strings must be quoted")
274 {
275 input = "cd example:twoKeyList[number=4][name=abcd]";
276 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200277 }
Václav Kubernát2315c732018-05-16 20:25:55 +0200278 REQUIRE_THROWS(parser.parseCommand(input, errorStream));
Václav Kubernátd6662962018-03-22 17:41:33 +0100279 }
280}