blob: 2cef615e3e290946e379b825d8d9c40d6ed20b48 [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átd6662962018-03-22 17:41:33 +010010#include "ast.hpp"
Václav Kubernát48fc3832018-05-28 14:21:22 +020011#include "parser.hpp"
12#include "schema.hpp"
Václav Kubernátd6662962018-03-22 17:41:33 +010013
14TEST_CASE("cd")
15{
Václav Kubernát48fc3832018-05-28 14:21:22 +020016 Schema schema;
17 schema.addContainer("", "a");
18 schema.addContainer("", "b");
19 schema.addContainer("a", "a2");
20 schema.addContainer("b", "b2");
21 schema.addContainer("a/a2", "a3");
22 schema.addContainer("b/b2", "b3");
23 schema.addList("", "list", {"number"});
24 schema.addContainer("list", "contInList");
25 schema.addList("", "twoKeyList", {"number", "name"});
26 Parser parser(schema);
Václav Kubernátd6662962018-03-22 17:41:33 +010027 std::string input;
Václav Kubernát2315c732018-05-16 20:25:55 +020028 std::ostringstream errorStream;
Václav Kubernátd6662962018-03-22 17:41:33 +010029
Václav Kubernátb96eef72018-05-04 19:10:22 +020030 SECTION("valid input")
Václav Kubernátd6662962018-03-22 17:41:33 +010031 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020032 cd_ expected;
33
34 SECTION("container")
Václav Kubernátd6662962018-03-22 17:41:33 +010035 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020036 SECTION("a")
37 {
38 input = "cd a";
39 expected.m_path.m_nodes.push_back(container_("a"));
40 }
41
42 SECTION("b")
43 {
44 input = "cd b";
45 expected.m_path.m_nodes.push_back(container_("b"));
46 }
47
48 SECTION("a/a2")
49 {
50 input = "cd a/a2";
51 expected.m_path.m_nodes.push_back(container_("a"));
52 expected.m_path.m_nodes.push_back(container_("a2"));
53 }
54
55 SECTION("b/b2")
56 {
57 input = "cd b/b2";
58 expected.m_path.m_nodes.push_back(container_("b"));
59 expected.m_path.m_nodes.push_back(container_("b2"));
60 }
Václav Kubernátd6662962018-03-22 17:41:33 +010061 }
62
Václav Kubernátb96eef72018-05-04 19:10:22 +020063 SECTION("list elements")
Václav Kubernátd6662962018-03-22 17:41:33 +010064 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020065 SECTION("list[number=1]")
66 {
67 input = "cd list[number=1]";
68 auto keys = std::map<std::string, std::string>{
Václav Kubernát48fc3832018-05-28 14:21:22 +020069 {"number", "1"}};
Václav Kubernátb96eef72018-05-04 19:10:22 +020070 expected.m_path.m_nodes.push_back(listElement_("list", keys));
71 }
72
73 SECTION("list[number=1]/contInList")
74 {
75 input = "cd list[number=1]/contInList";
76 auto keys = std::map<std::string, std::string>{
Václav Kubernát48fc3832018-05-28 14:21:22 +020077 {"number", "1"}};
Václav Kubernátb96eef72018-05-04 19:10:22 +020078 expected.m_path.m_nodes.push_back(listElement_("list", keys));
79 expected.m_path.m_nodes.push_back(container_("contInList"));
80 }
81
82 SECTION("twoKeyList[number=4 name=abcd]")
83 {
84 input = "cd twoKeyList[number=4 name=abcd]";
85 auto keys = std::map<std::string, std::string>{
Václav Kubernát48fc3832018-05-28 14:21:22 +020086 {"number", "4"},
87 {"name", "abcd"}};
Václav Kubernátb96eef72018-05-04 19:10:22 +020088 expected.m_path.m_nodes.push_back(listElement_("twoKeyList", keys));
89 }
Václav Kubernátd6662962018-03-22 17:41:33 +010090 }
Václav Kubernát7e4e82f2018-05-14 20:04:58 +020091
92 SECTION("whitespace handling")
93 {
94 SECTION(" cd a ")
95 {
96 input = " cd a ";
97 expected.m_path.m_nodes.push_back(container_("a"));
98 }
99 }
100
Václav Kubernát60d6f292018-05-25 09:45:32 +0200101 SECTION("moving up")
102 {
103 SECTION("a/..")
104 {
105 input = "cd a/..";
106 expected.m_path.m_nodes.push_back(container_("a"));
107 expected.m_path.m_nodes.push_back(nodeup_());
108 }
109
110 SECTION("a/../a")
111 {
112 input = "cd a/../a";
113 expected.m_path.m_nodes.push_back(container_("a"));
114 expected.m_path.m_nodes.push_back(nodeup_());
115 expected.m_path.m_nodes.push_back(container_("a"));
116 }
117
118 SECTION("a/../a/a2")
119 {
120 input = "cd a/../a/a2";
121 expected.m_path.m_nodes.push_back(container_("a"));
122 expected.m_path.m_nodes.push_back(nodeup_());
123 expected.m_path.m_nodes.push_back(container_("a"));
124 expected.m_path.m_nodes.push_back(container_("a2"));
125 }
126 }
127
Václav Kubernátb61336d2018-05-28 17:35:03 +0200128 command_ command = parser.parseCommand(input, errorStream);
129 REQUIRE(command.type() == typeid(cd_));
130 REQUIRE(boost::get<cd_>(command) == expected);
Václav Kubernátd6662962018-03-22 17:41:33 +0100131 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200132 SECTION("invalid input")
Václav Kubernátd6662962018-03-22 17:41:33 +0100133 {
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200134 SECTION("missing space between a command and its arguments")
135 {
136 SECTION("cda")
137 {
138 input = "cda";
139 }
140 }
141 SECTION("garbage arguments handling")
142 {
143 SECTION("cd a garbage")
144 {
Václav Kubernát48fc3832018-05-28 14:21:22 +0200145 input = "cd a garbage";
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200146 }
147 SECTION("cd a/a2 garbage")
148 {
149 input = "cd a/a2 garbage";
150 }
151 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200152 SECTION("invalid identifiers")
Václav Kubernátd6662962018-03-22 17:41:33 +0100153 {
Václav Kubernátb96eef72018-05-04 19:10:22 +0200154 SECTION("nonexistent")
155 {
156 input = "cd nonexistent";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200157 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100158
Václav Kubernátb96eef72018-05-04 19:10:22 +0200159 SECTION("nonexistent/lol")
160 {
161 input = "cd nonexistent/lol";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200162 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100163 }
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200164
Václav Kubernátb96eef72018-05-04 19:10:22 +0200165 SECTION("invalid list key identifiers")
166 {
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200167 SECTION("list")
168 {
169 input = "cd list";
170 }
171
172 SECTION("list[]")
173 {
174 input = "cd list[]";
175 }
176
Václav Kubernátb96eef72018-05-04 19:10:22 +0200177 SECTION("twoKeyList[invalidKey=4]")
178 {
179 input = "cd twoKeyList[invalidKey=4]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200180 }
181
182 SECTION("twoKeyList[number=4 number=5]")
183 {
184 input = "cd twoKeyList[number=4 number=5]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200185 }
186
187 SECTION("twoKeyList[number=4 name=lol number=7]")
188 {
189 input = "cd twoKeyList[number=4 name=lol number=7]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200190 }
191
192 SECTION("twoKeyList[number=4]")
193 {
194 input = "cd twoKeyList[number=4]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200195 }
196 }
Václav Kubernát2315c732018-05-16 20:25:55 +0200197 REQUIRE_THROWS(parser.parseCommand(input, errorStream));
Václav Kubernátd6662962018-03-22 17:41:33 +0100198 }
199}