blob: 8ab4c40b4e7deff9670e17b4a2dfc34559adeeb9 [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"
10#include "CParser.hpp"
11#include "CTree.hpp"
12#include "ast.hpp"
13
14TEST_CASE("cd")
15{
16 CTree tree;
17 tree.addContainer("", "a");
18 tree.addContainer("", "b");
19 tree.addContainer("a", "a2");
20 tree.addContainer("b", "b2");
21 tree.addContainer("a/a2", "a3");
22 tree.addContainer("b/b2", "b3");
Václav Kubernátb96eef72018-05-04 19:10:22 +020023 tree.addList("", "list", {"number"});
24 tree.addContainer("list", "contInList");
25 tree.addList("", "twoKeyList", {"number", "name"});
Václav Kubernátd6662962018-03-22 17:41:33 +010026 CParser parser(tree);
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 }
61
Václav Kubernátd6662962018-03-22 17:41:33 +010062 }
63
Václav Kubernátb96eef72018-05-04 19:10:22 +020064 SECTION("list elements")
Václav Kubernátd6662962018-03-22 17:41:33 +010065 {
Václav Kubernátb96eef72018-05-04 19:10:22 +020066 SECTION("list[number=1]")
67 {
68 input = "cd list[number=1]";
69 auto keys = std::map<std::string, std::string>{
70 {"number", "1"}
71 };
72 expected.m_path.m_nodes.push_back(listElement_("list", keys));
73 }
74
75 SECTION("list[number=1]/contInList")
76 {
77 input = "cd list[number=1]/contInList";
78 auto keys = std::map<std::string, std::string>{
79 {"number", "1"}};
80 expected.m_path.m_nodes.push_back(listElement_("list", keys));
81 expected.m_path.m_nodes.push_back(container_("contInList"));
82 }
83
84 SECTION("twoKeyList[number=4 name=abcd]")
85 {
86 input = "cd twoKeyList[number=4 name=abcd]";
87 auto keys = std::map<std::string, std::string>{
88 {"number", "4"},
89 {"name", "abcd"}};
90 expected.m_path.m_nodes.push_back(listElement_("twoKeyList", keys));
91 }
92
Václav Kubernátd6662962018-03-22 17:41:33 +010093 }
Václav Kubernát7e4e82f2018-05-14 20:04:58 +020094
95 SECTION("whitespace handling")
96 {
97 SECTION(" cd a ")
98 {
99 input = " cd a ";
100 expected.m_path.m_nodes.push_back(container_("a"));
101 }
102 }
103
Václav Kubernát60d6f292018-05-25 09:45:32 +0200104 SECTION("moving up")
105 {
106 SECTION("a/..")
107 {
108 input = "cd a/..";
109 expected.m_path.m_nodes.push_back(container_("a"));
110 expected.m_path.m_nodes.push_back(nodeup_());
111 }
112
113 SECTION("a/../a")
114 {
115 input = "cd a/../a";
116 expected.m_path.m_nodes.push_back(container_("a"));
117 expected.m_path.m_nodes.push_back(nodeup_());
118 expected.m_path.m_nodes.push_back(container_("a"));
119 }
120
121 SECTION("a/../a/a2")
122 {
123 input = "cd a/../a/a2";
124 expected.m_path.m_nodes.push_back(container_("a"));
125 expected.m_path.m_nodes.push_back(nodeup_());
126 expected.m_path.m_nodes.push_back(container_("a"));
127 expected.m_path.m_nodes.push_back(container_("a2"));
128 }
129 }
130
Václav Kubernát2315c732018-05-16 20:25:55 +0200131 cd_ command = parser.parseCommand(input, errorStream);
Václav Kubernátd6662962018-03-22 17:41:33 +0100132 REQUIRE(command == expected);
133 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200134 SECTION("invalid input")
Václav Kubernátd6662962018-03-22 17:41:33 +0100135 {
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200136 SECTION("missing space between a command and its arguments")
137 {
138 SECTION("cda")
139 {
140 input = "cda";
141 }
142 }
143 SECTION("garbage arguments handling")
144 {
145 SECTION("cd a garbage")
146 {
147 input = "cd a garbage";
148 }
149 SECTION("cd a/a2 garbage")
150 {
151 input = "cd a/a2 garbage";
152 }
153 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200154 SECTION("invalid identifiers")
Václav Kubernátd6662962018-03-22 17:41:33 +0100155 {
Václav Kubernátb96eef72018-05-04 19:10:22 +0200156 SECTION("nonexistent")
157 {
158 input = "cd nonexistent";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200159 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100160
Václav Kubernátb96eef72018-05-04 19:10:22 +0200161 SECTION("nonexistent/lol")
162 {
163 input = "cd nonexistent/lol";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200164 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100165 }
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200166
Václav Kubernátb96eef72018-05-04 19:10:22 +0200167 SECTION("invalid list key identifiers")
168 {
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200169 SECTION("list")
170 {
171 input = "cd list";
172 }
173
174 SECTION("list[]")
175 {
176 input = "cd list[]";
177 }
178
Václav Kubernátb96eef72018-05-04 19:10:22 +0200179 SECTION("twoKeyList[invalidKey=4]")
180 {
181 input = "cd twoKeyList[invalidKey=4]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200182 }
183
184 SECTION("twoKeyList[number=4 number=5]")
185 {
186 input = "cd twoKeyList[number=4 number=5]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200187 }
188
189 SECTION("twoKeyList[number=4 name=lol number=7]")
190 {
191 input = "cd twoKeyList[number=4 name=lol number=7]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200192 }
193
194 SECTION("twoKeyList[number=4]")
195 {
196 input = "cd twoKeyList[number=4]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200197 }
198 }
Václav Kubernát2315c732018-05-16 20:25:55 +0200199 REQUIRE_THROWS(parser.parseCommand(input, errorStream));
Václav Kubernátd6662962018-03-22 17:41:33 +0100200 }
201}