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