blob: 83a1f9867e189fa6504f5cbecd15b49f8fea8327 [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át2315c732018-05-16 20:25:55 +0200128 cd_ command = parser.parseCommand(input, errorStream);
Václav Kubernátd6662962018-03-22 17:41:33 +0100129 REQUIRE(command == expected);
130 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200131 SECTION("invalid input")
Václav Kubernátd6662962018-03-22 17:41:33 +0100132 {
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200133 SECTION("missing space between a command and its arguments")
134 {
135 SECTION("cda")
136 {
137 input = "cda";
138 }
139 }
140 SECTION("garbage arguments handling")
141 {
142 SECTION("cd a garbage")
143 {
Václav Kubernát48fc3832018-05-28 14:21:22 +0200144 input = "cd a garbage";
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200145 }
146 SECTION("cd a/a2 garbage")
147 {
148 input = "cd a/a2 garbage";
149 }
150 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200151 SECTION("invalid identifiers")
Václav Kubernátd6662962018-03-22 17:41:33 +0100152 {
Václav Kubernátb96eef72018-05-04 19:10:22 +0200153 SECTION("nonexistent")
154 {
155 input = "cd nonexistent";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200156 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100157
Václav Kubernátb96eef72018-05-04 19:10:22 +0200158 SECTION("nonexistent/lol")
159 {
160 input = "cd nonexistent/lol";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200161 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100162 }
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200163
Václav Kubernátb96eef72018-05-04 19:10:22 +0200164 SECTION("invalid list key identifiers")
165 {
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200166 SECTION("list")
167 {
168 input = "cd list";
169 }
170
171 SECTION("list[]")
172 {
173 input = "cd list[]";
174 }
175
Václav Kubernátb96eef72018-05-04 19:10:22 +0200176 SECTION("twoKeyList[invalidKey=4]")
177 {
178 input = "cd twoKeyList[invalidKey=4]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200179 }
180
181 SECTION("twoKeyList[number=4 number=5]")
182 {
183 input = "cd twoKeyList[number=4 number=5]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200184 }
185
186 SECTION("twoKeyList[number=4 name=lol number=7]")
187 {
188 input = "cd twoKeyList[number=4 name=lol number=7]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200189 }
190
191 SECTION("twoKeyList[number=4]")
192 {
193 input = "cd twoKeyList[number=4]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200194 }
195 }
Václav Kubernát2315c732018-05-16 20:25:55 +0200196 REQUIRE_THROWS(parser.parseCommand(input, errorStream));
Václav Kubernátd6662962018-03-22 17:41:33 +0100197 }
198}