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