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" |
| 10 | #include "CParser.hpp" |
| 11 | #include "CTree.hpp" |
| 12 | #include "ast.hpp" |
| 13 | |
| 14 | TEST_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át | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 23 | tree.addList("", "list", {"number"}); |
| 24 | tree.addContainer("list", "contInList"); |
| 25 | tree.addList("", "twoKeyList", {"number", "name"}); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 26 | CParser parser(tree); |
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 | } |
| 61 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 64 | SECTION("list elements") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 65 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 66 | 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át | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 93 | } |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 94 | |
| 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át | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame] | 104 | cd_ command = parser.parseCommand(input, errorStream); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 105 | REQUIRE(command == expected); |
| 106 | } |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 107 | SECTION("invalid input") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 108 | { |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 109 | 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át | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 127 | SECTION("invalid identifiers") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 128 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 129 | SECTION("nonexistent") |
| 130 | { |
| 131 | input = "cd nonexistent"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 132 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 133 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 134 | SECTION("nonexistent/lol") |
| 135 | { |
| 136 | input = "cd nonexistent/lol"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 137 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 138 | } |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 139 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 140 | SECTION("invalid list key identifiers") |
| 141 | { |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 142 | SECTION("list") |
| 143 | { |
| 144 | input = "cd list"; |
| 145 | } |
| 146 | |
| 147 | SECTION("list[]") |
| 148 | { |
| 149 | input = "cd list[]"; |
| 150 | } |
| 151 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 152 | SECTION("twoKeyList[invalidKey=4]") |
| 153 | { |
| 154 | input = "cd twoKeyList[invalidKey=4]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | SECTION("twoKeyList[number=4 number=5]") |
| 158 | { |
| 159 | input = "cd twoKeyList[number=4 number=5]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | SECTION("twoKeyList[number=4 name=lol number=7]") |
| 163 | { |
| 164 | input = "cd twoKeyList[number=4 name=lol number=7]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | SECTION("twoKeyList[number=4]") |
| 168 | { |
| 169 | input = "cd twoKeyList[number=4]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 170 | } |
| 171 | } |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame] | 172 | REQUIRE_THROWS(parser.parseCommand(input, errorStream)); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 173 | } |
| 174 | } |