Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +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 | |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 9 | #include <experimental/iterator> |
Jan Kundrát | a33cf08 | 2019-03-28 11:55:57 +0100 | [diff] [blame] | 10 | #include "trompeloeil_doctest.h" |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 11 | #include "parser.hpp" |
| 12 | #include "static_schema.hpp" |
| 13 | |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 14 | namespace std { |
| 15 | std::ostream& operator<<(std::ostream& s, const std::set<std::string> set) |
| 16 | { |
| 17 | s << std::endl << "{"; |
| 18 | std::copy(set.begin(), set.end(), std::experimental::make_ostream_joiner(s, ", ")); |
| 19 | s << "}" << std::endl; |
| 20 | return s; |
| 21 | } |
| 22 | } |
| 23 | |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 24 | TEST_CASE("path_completion") |
| 25 | { |
| 26 | auto schema = std::make_shared<StaticSchema>(); |
| 27 | schema->addModule("example"); |
| 28 | schema->addModule("second"); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 29 | schema->addContainer("/", "example:ano"); |
| 30 | schema->addContainer("/", "example:anoda"); |
| 31 | schema->addList("/example:ano", "example:listInCont", {"number"}); |
| 32 | schema->addContainer("/", "second:amelie"); |
| 33 | schema->addContainer("/", "example:bota"); |
| 34 | schema->addContainer("/example:ano", "example:a2"); |
| 35 | schema->addContainer("/example:bota", "example:b2"); |
| 36 | schema->addContainer("/example:ano/example:a2", "example:a3"); |
| 37 | schema->addContainer("/example:bota/example:b2", "example:b3"); |
| 38 | schema->addList("/", "example:list", {"number"}); |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 39 | schema->addLeaf("/example:list", "example:number", yang::LeafDataTypes::Int32); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 40 | schema->addContainer("/example:list", "example:contInList"); |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 41 | schema->addList("/", "example:ovoce", {"name"}); |
| 42 | schema->addLeaf("/example:ovoce", "example:name", yang::LeafDataTypes::String); |
| 43 | schema->addList("/", "example:ovocezelenina", {"name"}); |
| 44 | schema->addLeaf("/example:ovocezelenina", "example:name", yang::LeafDataTypes::String); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 45 | schema->addList("/", "example:twoKeyList", {"number", "name"}); |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 46 | schema->addLeaf("/example:twoKeyList", "example:name", yang::LeafDataTypes::String); |
| 47 | schema->addLeaf("/example:twoKeyList", "example:number", yang::LeafDataTypes::Int32); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 48 | schema->addLeaf("/", "example:leafInt", yang::LeafDataTypes::Int32); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 49 | Parser parser(schema); |
| 50 | std::string input; |
| 51 | std::ostringstream errorStream; |
| 52 | std::set<std::string> expected; |
| 53 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 54 | SECTION("node name completion") |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 55 | { |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 56 | SECTION("ls ") |
| 57 | { |
| 58 | input = "ls "; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 59 | expected = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList[", "second:amelie/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | SECTION("ls e") |
| 63 | { |
| 64 | input = "ls e"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 65 | expected = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList["}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | SECTION("ls example:ano") |
| 69 | { |
| 70 | input = "ls example:ano"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 71 | expected = {"example:ano/", "example:anoda/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | SECTION("ls example:ano/example:a") |
| 75 | { |
| 76 | input = "ls example:ano/example:a"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 77 | expected = {"example:a2/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | SECTION("ls x") |
| 81 | { |
| 82 | input = "ls x"; |
| 83 | expected = {}; |
| 84 | } |
| 85 | |
| 86 | SECTION("ls /") |
| 87 | { |
| 88 | input = "ls /"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 89 | expected = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList[", "second:amelie/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | SECTION("ls /e") |
| 93 | { |
| 94 | input = "ls /e"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 95 | expected = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList["}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 98 | SECTION("ls example:bota") |
| 99 | { |
| 100 | input = "ls example:bota"; |
| 101 | expected = {"example:bota/"}; |
| 102 | } |
| 103 | |
| 104 | SECTION("ls /example:bota") |
| 105 | { |
| 106 | input = "ls /example:bota"; |
| 107 | expected = {"example:bota/"}; |
| 108 | } |
| 109 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 110 | SECTION("ls /s") |
| 111 | { |
| 112 | input = "ls /s"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 113 | expected = {"second:amelie/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | SECTION("ls /example:list[number=3]/") |
| 117 | { |
| 118 | input = "ls /example:list[number=3]/"; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 119 | expected = {"example:contInList/", "example:number "}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | SECTION("ls /example:list[number=3]/c") |
| 123 | { |
| 124 | input = "ls /example:list[number=3]/e"; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 125 | expected = {"example:contInList/", "example:number "}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | SECTION("ls /example:list[number=3]/a") |
| 129 | { |
| 130 | input = "ls /example:list[number=3]/a"; |
| 131 | expected = {}; |
| 132 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 135 | SECTION("list keys completion") |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 136 | { |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 137 | SECTION("cd example:lis") |
| 138 | { |
| 139 | input = "cd example:lis"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 140 | expected = {"example:list["}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 141 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 142 | |
Václav Kubernát | 51853b2 | 2019-04-16 21:53:44 +0200 | [diff] [blame] | 143 | SECTION("set example:list") |
| 144 | { |
| 145 | input = "set example:list"; |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 146 | expected = {"example:list["}; |
| 147 | } |
| 148 | |
| 149 | SECTION("cd example:list") |
| 150 | { |
| 151 | input = "cd example:list"; |
| 152 | expected = {"example:list["}; |
Václav Kubernát | 51853b2 | 2019-04-16 21:53:44 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 155 | SECTION("cd example:list[") |
| 156 | { |
| 157 | input = "cd example:list["; |
| 158 | expected = {"number="}; |
| 159 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 160 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 161 | SECTION("cd example:list[numb") |
| 162 | { |
| 163 | input = "cd example:list[numb"; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 164 | expected = {"number="}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 165 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 166 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 167 | SECTION("cd example:list[number") |
| 168 | { |
| 169 | input = "cd example:list[number"; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 170 | expected = {"number="}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 171 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 172 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 173 | SECTION("cd example:list[number=12") |
| 174 | { |
| 175 | input = "cd example:list[number=12"; |
| 176 | expected = {"]/"}; |
| 177 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 178 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 179 | SECTION("cd example:list[number=12]") |
| 180 | { |
| 181 | input = "cd example:list[number=12]"; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 182 | expected = {"]/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 183 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 184 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 185 | SECTION("cd example:twoKeyList[") |
| 186 | { |
| 187 | input = "cd example:twoKeyList["; |
| 188 | expected = {"name=", "number="}; |
| 189 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 190 | |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 191 | SECTION("cd example:twoKeyList[name=\"AHOJ\"") |
| 192 | { |
| 193 | input = "cd example:twoKeyList[name=\"AHOJ\""; |
| 194 | expected = {"]["}; |
| 195 | } |
| 196 | |
| 197 | SECTION("cd example:twoKeyList[name=\"AHOJ\"]") |
| 198 | { |
| 199 | input = "cd example:twoKeyList[name=\"AHOJ\"]"; |
| 200 | expected = {"]["}; |
| 201 | } |
| 202 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 203 | SECTION("cd example:twoKeyList[name=\"AHOJ\"][") |
| 204 | { |
| 205 | input = "cd example:twoKeyList[name=\"AHOJ\"]["; |
| 206 | expected = {"number="}; |
| 207 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 208 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 209 | SECTION("cd example:twoKeyList[number=42][") |
| 210 | { |
| 211 | input = "cd example:twoKeyList[number=42]["; |
| 212 | expected = {"name="}; |
| 213 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 214 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 215 | SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123") |
| 216 | { |
| 217 | input = "cd example:twoKeyList[name=\"AHOJ\"][number=123"; |
| 218 | expected = {"]/"}; |
| 219 | } |
| 220 | |
| 221 | SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123]") |
| 222 | { |
| 223 | input = "cd example:twoKeyList[name=\"AHOJ\"][number=123]"; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 224 | expected = {"]/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 225 | } |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 226 | |
| 227 | SECTION("cd example:ovoce") |
| 228 | { |
| 229 | input = "cd example:ovoce"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 230 | expected = {"example:ovoce[", "example:ovocezelenina["}; |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 231 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 232 | } |
| 233 | |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 234 | SECTION("clear completions when no longer inputting path") |
| 235 | { |
| 236 | input = "set example:leafInt "; |
| 237 | expected = {}; |
| 238 | } |
| 239 | |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 240 | REQUIRE(parser.completeCommand(input, errorStream) == expected); |
| 241 | } |