blob: d6079fb2ac5135db2579a63b5f70e7024628e9ca [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át2315c732018-05-16 20:25:55 +0200104 cd_ command = parser.parseCommand(input, errorStream);
Václav Kubernátd6662962018-03-22 17:41:33 +0100105 REQUIRE(command == expected);
106 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200107 SECTION("invalid input")
Václav Kubernátd6662962018-03-22 17:41:33 +0100108 {
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200109 SECTION("missing space between a command and its arguments")
110 {
111 SECTION("cda")
112 {
113 input = "cda";
114 }
115 }
116 SECTION("garbage arguments handling")
117 {
118 SECTION("cd a garbage")
119 {
120 input = "cd a garbage";
121 }
122 SECTION("cd a/a2 garbage")
123 {
124 input = "cd a/a2 garbage";
125 }
126 }
Václav Kubernátb96eef72018-05-04 19:10:22 +0200127 SECTION("invalid identifiers")
Václav Kubernátd6662962018-03-22 17:41:33 +0100128 {
Václav Kubernátb96eef72018-05-04 19:10:22 +0200129 SECTION("nonexistent")
130 {
131 input = "cd nonexistent";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200132 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100133
Václav Kubernátb96eef72018-05-04 19:10:22 +0200134 SECTION("nonexistent/lol")
135 {
136 input = "cd nonexistent/lol";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200137 }
Václav Kubernátd6662962018-03-22 17:41:33 +0100138 }
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200139
Václav Kubernátb96eef72018-05-04 19:10:22 +0200140 SECTION("invalid list key identifiers")
141 {
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200142 SECTION("list")
143 {
144 input = "cd list";
145 }
146
147 SECTION("list[]")
148 {
149 input = "cd list[]";
150 }
151
Václav Kubernátb96eef72018-05-04 19:10:22 +0200152 SECTION("twoKeyList[invalidKey=4]")
153 {
154 input = "cd twoKeyList[invalidKey=4]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200155 }
156
157 SECTION("twoKeyList[number=4 number=5]")
158 {
159 input = "cd twoKeyList[number=4 number=5]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200160 }
161
162 SECTION("twoKeyList[number=4 name=lol number=7]")
163 {
164 input = "cd twoKeyList[number=4 name=lol number=7]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200165 }
166
167 SECTION("twoKeyList[number=4]")
168 {
169 input = "cd twoKeyList[number=4]";
Václav Kubernátb96eef72018-05-04 19:10:22 +0200170 }
171 }
Václav Kubernát2315c732018-05-16 20:25:55 +0200172 REQUIRE_THROWS(parser.parseCommand(input, errorStream));
Václav Kubernátd6662962018-03-22 17:41:33 +0100173 }
174}