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 | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 10 | #include "ast.hpp" |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 11 | #include "parser.hpp" |
| 12 | #include "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 | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 16 | 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át | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 27 | std::string input; |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame] | 28 | std::ostringstream errorStream; |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 29 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 30 | SECTION("valid input") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 31 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 32 | cd_ expected; |
| 33 | |
| 34 | SECTION("container") |
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 | 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át | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 63 | SECTION("list elements") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 64 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 65 | SECTION("list[number=1]") |
| 66 | { |
| 67 | input = "cd list[number=1]"; |
| 68 | auto keys = std::map<std::string, std::string>{ |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 69 | {"number", "1"}}; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 70 | 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át | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 77 | {"number", "1"}}; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 78 | 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át | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 86 | {"number", "4"}, |
| 87 | {"name", "abcd"}}; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 88 | expected.m_path.m_nodes.push_back(listElement_("twoKeyList", keys)); |
| 89 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 90 | } |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 91 | |
| 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át | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 101 | 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át | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame^] | 128 | command_ command = parser.parseCommand(input, errorStream); |
| 129 | REQUIRE(command.type() == typeid(cd_)); |
| 130 | REQUIRE(boost::get<cd_>(command) == expected); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 131 | } |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 132 | SECTION("invalid input") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 133 | { |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 134 | 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át | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 145 | input = "cd a garbage"; |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 146 | } |
| 147 | SECTION("cd a/a2 garbage") |
| 148 | { |
| 149 | input = "cd a/a2 garbage"; |
| 150 | } |
| 151 | } |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 152 | SECTION("invalid identifiers") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 153 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 154 | SECTION("nonexistent") |
| 155 | { |
| 156 | input = "cd nonexistent"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 157 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 158 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 159 | SECTION("nonexistent/lol") |
| 160 | { |
| 161 | input = "cd nonexistent/lol"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 162 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 163 | } |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 164 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 165 | SECTION("invalid list key identifiers") |
| 166 | { |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 167 | SECTION("list") |
| 168 | { |
| 169 | input = "cd list"; |
| 170 | } |
| 171 | |
| 172 | SECTION("list[]") |
| 173 | { |
| 174 | input = "cd list[]"; |
| 175 | } |
| 176 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 177 | SECTION("twoKeyList[invalidKey=4]") |
| 178 | { |
| 179 | input = "cd twoKeyList[invalidKey=4]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | SECTION("twoKeyList[number=4 number=5]") |
| 183 | { |
| 184 | input = "cd twoKeyList[number=4 number=5]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | SECTION("twoKeyList[number=4 name=lol number=7]") |
| 188 | { |
| 189 | input = "cd twoKeyList[number=4 name=lol number=7]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | SECTION("twoKeyList[number=4]") |
| 193 | { |
| 194 | input = "cd twoKeyList[number=4]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 195 | } |
| 196 | } |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame] | 197 | REQUIRE_THROWS(parser.parseCommand(input, errorStream)); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 198 | } |
| 199 | } |