Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [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 | |
Jan Kundrát | a33cf08 | 2019-03-28 11:55:57 +0100 | [diff] [blame] | 9 | #include "trompeloeil_doctest.h" |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 10 | #include "ast_commands.hpp" |
| 11 | #include "parser.hpp" |
| 12 | #include "static_schema.hpp" |
| 13 | |
| 14 | TEST_CASE("ls") |
| 15 | { |
| 16 | auto schema = std::make_shared<StaticSchema>(); |
| 17 | schema->addModule("example"); |
| 18 | schema->addModule("second"); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame^] | 19 | schema->addContainer("/", "example:a"); |
| 20 | schema->addList("/example:a", "example:listInCont", {"number"}); |
| 21 | schema->addContainer("/", "second:a"); |
| 22 | schema->addContainer("/", "example:b"); |
| 23 | schema->addContainer("/example:a", "example:a2"); |
| 24 | schema->addContainer("/example:b", "example:b2"); |
| 25 | schema->addContainer("/example:a/example:a2", "example:a3"); |
| 26 | schema->addContainer("/example:b/example:b2", "example:b3"); |
| 27 | schema->addList("/", "example:list", {"number"}); |
| 28 | schema->addContainer("/example:list", "example:contInList"); |
| 29 | schema->addList("/", "example:twoKeyList", {"number", "name"}); |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 30 | Parser parser(schema); |
| 31 | std::string input; |
| 32 | std::ostringstream errorStream; |
| 33 | |
| 34 | SECTION("valid input") |
| 35 | { |
| 36 | ls_ expected; |
| 37 | |
| 38 | SECTION("no arguments") |
| 39 | { |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 40 | SECTION("ls") |
| 41 | input = "ls"; |
| 42 | |
| 43 | SECTION("ls --recursive") |
| 44 | { |
| 45 | input = "ls --recursive"; |
| 46 | expected.m_options.push_back(LsOption::Recursive); |
| 47 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | SECTION("with path argument") |
| 51 | { |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 52 | SECTION("ls example:a") |
| 53 | { |
| 54 | input = "ls example:a"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 55 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | SECTION("ls /example:a") |
| 59 | { |
| 60 | SECTION("cwd: /") {} |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 61 | SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); } |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 62 | |
| 63 | input = "ls /example:a"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 64 | expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"})}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | SECTION("ls /") |
| 68 | { |
| 69 | SECTION("cwd: /") {} |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 70 | SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); } |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 71 | input = "ls /"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 72 | expected.m_path = dataPath_{Scope::Absolute, {}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | SECTION("ls example:a/a2") |
| 76 | { |
| 77 | input = "ls example:a/a2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 78 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}), |
| 79 | dataNode_(container_{"a2"})}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | SECTION("ls a2") |
| 83 | { |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 84 | parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 85 | input = "ls a2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 86 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(container_{"a2"})}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | SECTION("ls /example:a/a2") |
| 90 | { |
| 91 | SECTION("cwd: /") {} |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 92 | SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); } |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 93 | input = "ls /example:a/a2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 94 | expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}), |
| 95 | dataNode_(container_{"a2"})}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | SECTION("ls example:a/example:a2") |
| 99 | { |
| 100 | input = "ls example:a/example:a2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 101 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}), |
| 102 | dataNode_(module_{"example"}, container_{"a2"})}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | SECTION("ls example:a2") |
| 106 | { |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 107 | parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 108 | input = "ls example:a2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 109 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a2"})}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | SECTION("ls /example:a/example:a2") |
| 113 | { |
| 114 | SECTION("cwd: /") {} |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 115 | SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); } |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 116 | |
| 117 | input = "ls /example:a/example:a2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 118 | expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}), |
| 119 | dataNode_(module_{"example"}, container_{"a2"})}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 120 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 121 | |
| 122 | SECTION("ls --recursive /example:a") |
| 123 | { |
| 124 | SECTION("cwd: /") {} |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 125 | SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 126 | |
| 127 | input = "ls --recursive /example:a"; |
| 128 | expected.m_options.push_back(LsOption::Recursive); |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 129 | expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"})}}; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 130 | } |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 131 | |
| 132 | SECTION("ls example:list") |
| 133 | { |
| 134 | input = "ls example:list"; |
| 135 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, list_{"list"})}}; |
| 136 | } |
| 137 | |
Václav Kubernát | 211383d | 2020-01-06 18:07:50 +0100 | [diff] [blame] | 138 | SECTION("ls /example:list") |
| 139 | { |
| 140 | SECTION("cwd: /") {} |
| 141 | SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); } |
| 142 | input = "ls /example:list"; |
| 143 | expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, list_{"list"})}}; |
| 144 | } |
| 145 | |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 146 | SECTION("ls example:a/example:listInCont") |
| 147 | { |
| 148 | input = "ls example:a/example:listInCont"; |
| 149 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}), |
| 150 | dataNode_(module_{"example"}, list_{"listInCont"})}}; |
| 151 | } |
| 152 | |
| 153 | SECTION("ls example:list[number=342]/contInList") |
| 154 | { |
| 155 | input = "ls example:list[number=342]/contInList"; |
| 156 | auto keys = std::map<std::string, std::string>{ |
| 157 | {"number", "342"}}; |
| 158 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}), |
| 159 | dataNode_(container_{"contInList"})}}; |
| 160 | } |
Václav Kubernát | ac035d6 | 2019-02-18 10:59:08 +0100 | [diff] [blame] | 161 | |
| 162 | SECTION("ls example:list/contInList") |
| 163 | { |
| 164 | input = "ls example:list/contInList"; |
| 165 | expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}), |
| 166 | schemaNode_(container_{"contInList"})}}; |
| 167 | } |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 168 | |
| 169 | SECTION("ls example:*") |
| 170 | { |
| 171 | input = "ls example:*"; |
| 172 | expected.m_path = module_{"example"}; |
| 173 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | command_ command = parser.parseCommand(input, errorStream); |
| 177 | REQUIRE(command.type() == typeid(ls_)); |
| 178 | REQUIRE(boost::get<ls_>(command) == expected); |
| 179 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 180 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 181 | SECTION("invalid input") |
| 182 | { |
| 183 | SECTION("invalid path") |
| 184 | { |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 185 | SECTION("ls example:nonexistent") |
| 186 | input = "ls example:nonexistent"; |
| 187 | |
| 188 | SECTION("ls /example:nonexistent") |
| 189 | input = "ls /example:nonexistent"; |
| 190 | |
Václav Kubernát | b962105 | 2019-03-18 18:27:49 +0100 | [diff] [blame] | 191 | SECTION("ls /exa") |
| 192 | { |
| 193 | SECTION("cwd: /") {} |
| 194 | SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); } |
| 195 | input = "ls /exa"; |
| 196 | } |
| 197 | |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 198 | SECTION("ls /bad:nonexistent") |
| 199 | input = "ls /bad:nonexistent"; |
| 200 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 201 | SECTION("ls example:a/nonexistent") |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 202 | input = "ls example:a/nonexistent"; |
| 203 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 204 | SECTION("ls /example:a/nonexistent") |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 205 | input = "ls /example:a/nonexistent"; |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 208 | SECTION("whitespace before path") |
| 209 | { |
| 210 | SECTION("ls --recursive/") |
| 211 | input = "ls --recursive/"; |
| 212 | |
| 213 | SECTION("ls/") |
| 214 | input = "ls/"; |
| 215 | |
| 216 | SECTION("ls --recursive/example:a") |
| 217 | input = "ls --recursive/example:a"; |
| 218 | |
| 219 | SECTION("ls/example:a") |
| 220 | input = "ls/example:a"; |
| 221 | |
| 222 | SECTION("lssecond:a") |
| 223 | input = "lssecond:a"; |
| 224 | } |
| 225 | |
Jan Kundrát | c381e63 | 2019-03-14 13:39:11 +0100 | [diff] [blame] | 226 | REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException); |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 227 | } |
| 228 | } |