Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 1 | /* |
| 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át | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 10 | #include "ast_commands.hpp" |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 11 | #include "parser.hpp" |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 12 | #include "static_schema.hpp" |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 13 | |
| 14 | TEST_CASE("cd") |
| 15 | { |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 16 | auto schema = std::make_shared<StaticSchema>(); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 17 | 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át | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 29 | Parser parser(schema); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 30 | std::string input; |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame] | 31 | std::ostringstream errorStream; |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 32 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 33 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 34 | SECTION("valid input") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 35 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 36 | cd_ expected; |
| 37 | |
| 38 | SECTION("container") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 39 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 40 | SECTION("example:a") |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 41 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 42 | input = "cd example:a"; |
| 43 | expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("a"))); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 46 | SECTION("second:a") |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 47 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 48 | input = "cd second:a"; |
| 49 | expected.m_path.m_nodes.push_back(node_(module_{"second"}, container_("a"))); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 52 | SECTION("example:b") |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 53 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 54 | input = "cd example:b"; |
| 55 | expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("b"))); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 58 | SECTION("example:a/a2") |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 59 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 60 | 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át | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 77 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 80 | SECTION("list elements") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 81 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 82 | SECTION("example:list[number=1]") |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 83 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 84 | input = "cd example:list[number=1]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 85 | auto keys = std::map<std::string, std::string>{ |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 86 | {"number", "1"}}; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 87 | expected.m_path.m_nodes.push_back(node_(module_{"example"}, listElement_("list", keys))); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 90 | SECTION("example:list[number=1]/contInList") |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 91 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 92 | input = "cd example:list[number=1]/contInList"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 93 | auto keys = std::map<std::string, std::string>{ |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 94 | {"number", "1"}}; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 95 | 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át | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame^] | 99 | SECTION("example:twoKeyList[number=4][name='abcd']") |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 100 | { |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame^] | 101 | input = "cd example:twoKeyList[number=4][name='abcd']"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 102 | auto keys = std::map<std::string, std::string>{ |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 103 | {"number", "4"}, |
| 104 | {"name", "abcd"}}; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 105 | expected.m_path.m_nodes.push_back(node_(module_{"example"}, listElement_("twoKeyList", keys))); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 106 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 107 | } |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 108 | |
| 109 | SECTION("whitespace handling") |
| 110 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 111 | SECTION(" cd example:a ") |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 112 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 113 | input = " cd example:a "; |
| 114 | expected.m_path.m_nodes.push_back(node_(module_{"example"}, container_("a"))); |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 118 | SECTION("moving up") |
| 119 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 120 | SECTION("example:a/..") |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 121 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 122 | 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át | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 127 | SECTION("example:a/../example:a") |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 128 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 129 | 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át | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 135 | SECTION("example:a/../example:a/a2") |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 136 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 137 | 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át | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 145 | command_ command = parser.parseCommand(input, errorStream); |
| 146 | REQUIRE(command.type() == typeid(cd_)); |
| 147 | REQUIRE(boost::get<cd_>(command) == expected); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 148 | } |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 149 | SECTION("invalid input") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 150 | { |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 151 | SECTION("missing space between a command and its arguments") |
| 152 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 153 | SECTION("cdexample:a") |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 154 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 155 | input = "cdexample:a"; |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 156 | } |
| 157 | } |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 158 | |
| 159 | SECTION("whitespace between module and nodename") |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 160 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 161 | SECTION("cd example: a") |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 162 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 163 | input = "cd example: a"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 164 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 165 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 166 | SECTION("cd example : a") |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 167 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 168 | 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át | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 238 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 239 | } |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 240 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 241 | SECTION("invalid list key identifiers") |
| 242 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 243 | SECTION("example:list") |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 244 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 245 | input = "cd example:list"; |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 248 | SECTION("example:list[]") |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 249 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 250 | input = "cd example:list[]"; |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame^] | 253 | SECTION("example:twoKeyList[invalidKey='4']") |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 254 | { |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame^] | 255 | input = "cd example:twoKeyList[invalidKey='4']"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame^] | 258 | SECTION("example:twoKeyList[number=4][number=5]") |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 259 | { |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame^] | 260 | input = "cd example:twoKeyList[number=4][number=5]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame^] | 263 | SECTION("example:twoKeyList[number=4][name='lol'][number=7]") |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 264 | { |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame^] | 265 | input = "cd example:twoKeyList[number=4][name='lol'][number=7]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 268 | SECTION("example:twoKeyList[number=4]") |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 269 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 270 | input = "cd example:twoKeyList[number=4]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 271 | } |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame^] | 272 | |
| 273 | SECTION("strings must be quoted") |
| 274 | { |
| 275 | input = "cd example:twoKeyList[number=4][name=abcd]"; |
| 276 | } |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 277 | } |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame] | 278 | REQUIRE_THROWS(parser.parseCommand(input, errorStream)); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 279 | } |
| 280 | } |