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 | |
Václav Kubernát | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 9 | #include "trompeloeil_doctest.hpp" |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 10 | #include "ast_commands.hpp" |
| 11 | #include "parser.hpp" |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 12 | #include "pretty_printers.hpp" |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 13 | #include "static_schema.hpp" |
| 14 | |
| 15 | TEST_CASE("ls") |
| 16 | { |
| 17 | auto schema = std::make_shared<StaticSchema>(); |
| 18 | schema->addModule("example"); |
| 19 | schema->addModule("second"); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 20 | schema->addContainer("/", "example:a"); |
| 21 | schema->addList("/example:a", "example:listInCont", {"number"}); |
Václav Kubernát | dd8ec15 | 2020-05-13 17:06:36 +0200 | [diff] [blame] | 22 | schema->addContainer("/example:a/example:listInCont", "example:contInList"); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 23 | schema->addContainer("/", "second:a"); |
| 24 | schema->addContainer("/", "example:b"); |
| 25 | schema->addContainer("/example:a", "example:a2"); |
| 26 | schema->addContainer("/example:b", "example:b2"); |
| 27 | schema->addContainer("/example:a/example:a2", "example:a3"); |
| 28 | schema->addContainer("/example:b/example:b2", "example:b3"); |
| 29 | schema->addList("/", "example:list", {"number"}); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 30 | schema->addLeaf("/example:list", "example:number", yang::Int32{}); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 31 | schema->addContainer("/example:list", "example:contInList"); |
| 32 | schema->addList("/", "example:twoKeyList", {"number", "name"}); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 33 | schema->addLeaf("/example:twoKeyList", "example:number", yang::Int32{}); |
| 34 | schema->addLeaf("/example:twoKeyList", "example:name", yang::String{}); |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 35 | Parser parser(schema); |
| 36 | std::string input; |
| 37 | std::ostringstream errorStream; |
| 38 | |
| 39 | SECTION("valid input") |
| 40 | { |
| 41 | ls_ expected; |
| 42 | |
| 43 | SECTION("no arguments") |
| 44 | { |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 45 | SECTION("ls") |
| 46 | input = "ls"; |
| 47 | |
| 48 | SECTION("ls --recursive") |
| 49 | { |
| 50 | input = "ls --recursive"; |
| 51 | expected.m_options.push_back(LsOption::Recursive); |
| 52 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | SECTION("with path argument") |
| 56 | { |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 57 | SECTION("ls example:a") |
| 58 | { |
| 59 | input = "ls example:a"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 60 | 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] | 61 | } |
| 62 | |
| 63 | SECTION("ls /example:a") |
| 64 | { |
| 65 | SECTION("cwd: /") {} |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 66 | 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] | 67 | |
| 68 | input = "ls /example:a"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 69 | 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] | 70 | } |
| 71 | |
| 72 | SECTION("ls /") |
| 73 | { |
| 74 | SECTION("cwd: /") {} |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 75 | 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] | 76 | input = "ls /"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 77 | expected.m_path = dataPath_{Scope::Absolute, {}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Václav Kubernát | aed4bc7 | 2020-06-08 01:09:25 +0200 | [diff] [blame] | 80 | SECTION("going up from a node with no children") |
| 81 | { |
| 82 | parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"second"}, container_{"a"})}}); |
| 83 | REQUIRE(schema->availableNodes(parser.currentPath(), Recursion::NonRecursive) == std::set<ModuleNodePair>{}); |
| 84 | input = "ls ../example:a"; |
| 85 | expected.m_path = dataPath_{Scope::Relative, { |
| 86 | dataNode_{nodeup_{}}, |
| 87 | dataNode_{module_{"example"}, container_{"a"}} |
| 88 | }}; |
| 89 | |
| 90 | } |
| 91 | |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 92 | SECTION("ls example:a/a2") |
| 93 | { |
| 94 | input = "ls example:a/a2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 95 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}), |
| 96 | dataNode_(container_{"a2"})}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | SECTION("ls a2") |
| 100 | { |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 101 | parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 102 | input = "ls a2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 103 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(container_{"a2"})}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | SECTION("ls /example:a/a2") |
| 107 | { |
| 108 | SECTION("cwd: /") {} |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 109 | 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] | 110 | input = "ls /example:a/a2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 111 | expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}), |
| 112 | dataNode_(container_{"a2"})}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | SECTION("ls example:a/example:a2") |
| 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::Relative, {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 | } |
| 121 | |
| 122 | SECTION("ls example:a2") |
| 123 | { |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 124 | parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 125 | input = "ls example:a2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 126 | 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] | 127 | } |
| 128 | |
| 129 | SECTION("ls /example:a/example:a2") |
| 130 | { |
| 131 | SECTION("cwd: /") {} |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 132 | 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] | 133 | |
| 134 | input = "ls /example:a/example:a2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 135 | expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, container_{"a"}), |
| 136 | dataNode_(module_{"example"}, container_{"a2"})}}; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 137 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 138 | |
| 139 | SECTION("ls --recursive /example:a") |
| 140 | { |
| 141 | SECTION("cwd: /") {} |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 142 | 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] | 143 | |
| 144 | input = "ls --recursive /example:a"; |
| 145 | expected.m_options.push_back(LsOption::Recursive); |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 146 | 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] | 147 | } |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 148 | |
| 149 | SECTION("ls example:list") |
| 150 | { |
| 151 | input = "ls example:list"; |
| 152 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, list_{"list"})}}; |
| 153 | } |
| 154 | |
Václav Kubernát | 211383d | 2020-01-06 18:07:50 +0100 | [diff] [blame] | 155 | SECTION("ls /example:list") |
| 156 | { |
| 157 | SECTION("cwd: /") {} |
| 158 | SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); } |
| 159 | input = "ls /example:list"; |
| 160 | expected.m_path = dataPath_{Scope::Absolute, {dataNode_(module_{"example"}, list_{"list"})}}; |
| 161 | } |
| 162 | |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 163 | SECTION("ls example:a/example:listInCont") |
| 164 | { |
| 165 | input = "ls example:a/example:listInCont"; |
| 166 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"}), |
| 167 | dataNode_(module_{"example"}, list_{"listInCont"})}}; |
| 168 | } |
| 169 | |
Václav Kubernát | dd8ec15 | 2020-05-13 17:06:36 +0200 | [diff] [blame] | 170 | SECTION("ls example:a/example:listInCont/example:contInList") |
| 171 | { |
| 172 | input = "ls example:a/example:listInCont/example:contInList"; |
| 173 | expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, container_{"a"}), |
| 174 | schemaNode_(module_{"example"}, list_{"listInCont"}), |
| 175 | schemaNode_(module_{"example"}, container_{"contInList"})}}; |
| 176 | } |
| 177 | |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 178 | SECTION("ls example:list[number=342]/contInList") |
| 179 | { |
| 180 | input = "ls example:list[number=342]/contInList"; |
Václav Kubernát | c15fe82 | 2020-06-04 11:28:39 +0200 | [diff] [blame] | 181 | auto keys = ListInstance { |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 182 | {"number", int32_t{342}}}; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 183 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}), |
| 184 | dataNode_(container_{"contInList"})}}; |
| 185 | } |
Václav Kubernát | ac035d6 | 2019-02-18 10:59:08 +0100 | [diff] [blame] | 186 | |
| 187 | SECTION("ls example:list/contInList") |
| 188 | { |
| 189 | input = "ls example:list/contInList"; |
| 190 | expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}), |
| 191 | schemaNode_(container_{"contInList"})}}; |
| 192 | } |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 193 | |
Václav Kubernát | 88c2a61 | 2020-05-06 02:20:03 +0200 | [diff] [blame] | 194 | SECTION("ls example:list/example:contInList") |
| 195 | { |
| 196 | input = "ls example:list/example:contInList"; |
| 197 | expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}), |
| 198 | schemaNode_(module_{"example"},container_{"contInList"})}}; |
| 199 | } |
| 200 | |
Václav Kubernát | 1cd8c9c | 2020-05-06 01:38:59 +0200 | [diff] [blame] | 201 | SECTION("ls example:list[number=343]/..") |
| 202 | { |
| 203 | input = "ls example:list[number=343]/.."; |
Václav Kubernát | c15fe82 | 2020-06-04 11:28:39 +0200 | [diff] [blame] | 204 | auto keys = ListInstance { |
Václav Kubernát | 1cd8c9c | 2020-05-06 01:38:59 +0200 | [diff] [blame] | 205 | {"number", int32_t{343}}}; |
| 206 | expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}), dataNode_{nodeup_{}}}}; |
| 207 | } |
| 208 | |
| 209 | SECTION("ls example:list/..") |
| 210 | { |
| 211 | input = "ls example:list/.."; |
| 212 | expected.m_path = schemaPath_{Scope::Relative, {schemaNode_(module_{"example"}, list_{"list"}), schemaNode_{nodeup_{}}}}; |
| 213 | } |
| 214 | |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 215 | SECTION("ls example:*") |
| 216 | { |
| 217 | input = "ls example:*"; |
| 218 | expected.m_path = module_{"example"}; |
| 219 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | command_ command = parser.parseCommand(input, errorStream); |
| 223 | REQUIRE(command.type() == typeid(ls_)); |
| 224 | REQUIRE(boost::get<ls_>(command) == expected); |
| 225 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 226 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 227 | SECTION("invalid input") |
| 228 | { |
| 229 | SECTION("invalid path") |
| 230 | { |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 231 | SECTION("ls example:nonexistent") |
| 232 | input = "ls example:nonexistent"; |
| 233 | |
| 234 | SECTION("ls /example:nonexistent") |
| 235 | input = "ls /example:nonexistent"; |
| 236 | |
Václav Kubernát | b962105 | 2019-03-18 18:27:49 +0100 | [diff] [blame] | 237 | SECTION("ls /exa") |
| 238 | { |
| 239 | SECTION("cwd: /") {} |
| 240 | SECTION("cwd: /example:a") { parser.changeNode(dataPath_{Scope::Relative, {dataNode_(module_{"example"}, container_{"a"})}}); } |
| 241 | input = "ls /exa"; |
| 242 | } |
| 243 | |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 244 | SECTION("ls /bad:nonexistent") |
| 245 | input = "ls /bad:nonexistent"; |
| 246 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 247 | SECTION("ls example:a/nonexistent") |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 248 | input = "ls example:a/nonexistent"; |
| 249 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 250 | SECTION("ls /example:a/nonexistent") |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 251 | input = "ls /example:a/nonexistent"; |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 254 | SECTION("whitespace before path") |
| 255 | { |
| 256 | SECTION("ls --recursive/") |
| 257 | input = "ls --recursive/"; |
| 258 | |
| 259 | SECTION("ls/") |
| 260 | input = "ls/"; |
| 261 | |
| 262 | SECTION("ls --recursive/example:a") |
| 263 | input = "ls --recursive/example:a"; |
| 264 | |
| 265 | SECTION("ls/example:a") |
| 266 | input = "ls/example:a"; |
| 267 | |
| 268 | SECTION("lssecond:a") |
| 269 | input = "lssecond:a"; |
| 270 | } |
| 271 | |
Jan Kundrát | c381e63 | 2019-03-14 13:39:11 +0100 | [diff] [blame] | 272 | REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException); |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 273 | } |
| 274 | } |