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 | |
Jan Kundrát | a33cf08 | 2019-03-28 11:55:57 +0100 | [diff] [blame] | 9 | #include "trompeloeil_doctest.h" |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 10 | #include "parser.hpp" |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 11 | #include "pretty_printers.hpp" |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 12 | #include "static_schema.hpp" |
| 13 | |
| 14 | TEST_CASE("path_completion") |
| 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:ano"); |
| 20 | schema->addContainer("/", "example:anoda"); |
| 21 | schema->addList("/example:ano", "example:listInCont", {"number"}); |
| 22 | schema->addContainer("/", "second:amelie"); |
| 23 | schema->addContainer("/", "example:bota"); |
| 24 | schema->addContainer("/example:ano", "example:a2"); |
| 25 | schema->addContainer("/example:bota", "example:b2"); |
| 26 | schema->addContainer("/example:ano/example:a2", "example:a3"); |
| 27 | schema->addContainer("/example:bota/example:b2", "example:b3"); |
| 28 | schema->addList("/", "example:list", {"number"}); |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 29 | schema->addLeaf("/example:list", "example:number", yang::LeafDataTypes::Int32); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 30 | schema->addContainer("/example:list", "example:contInList"); |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 31 | schema->addList("/", "example:ovoce", {"name"}); |
| 32 | schema->addLeaf("/example:ovoce", "example:name", yang::LeafDataTypes::String); |
| 33 | schema->addList("/", "example:ovocezelenina", {"name"}); |
| 34 | schema->addLeaf("/example:ovocezelenina", "example:name", yang::LeafDataTypes::String); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 35 | schema->addList("/", "example:twoKeyList", {"number", "name"}); |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 36 | schema->addLeaf("/example:twoKeyList", "example:name", yang::LeafDataTypes::String); |
| 37 | schema->addLeaf("/example:twoKeyList", "example:number", yang::LeafDataTypes::Int32); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 38 | schema->addLeaf("/", "example:leafInt", yang::LeafDataTypes::Int32); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 39 | Parser parser(schema); |
| 40 | std::string input; |
| 41 | std::ostringstream errorStream; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 42 | |
| 43 | std::set<std::string> expectedCompletions; |
| 44 | // GCC complains here with -Wmaybe-uninitialized if I don't assign |
| 45 | // something here. I suspect it's because of nested SECTIONs. -1 is an |
| 46 | // invalid value (as in, I'll never expect expectedContextLength to be -1), |
| 47 | // so let's go with that. |
| 48 | int expectedContextLength = -1; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 49 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 50 | SECTION("node name completion") |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 51 | { |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 52 | SECTION("ls ") |
| 53 | { |
| 54 | input = "ls "; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 55 | expectedCompletions = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList[", "second:amelie/"}; |
| 56 | expectedContextLength = 0; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | SECTION("ls e") |
| 60 | { |
| 61 | input = "ls e"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 62 | expectedCompletions = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList["}; |
| 63 | expectedContextLength = 1; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | SECTION("ls example:ano") |
| 67 | { |
| 68 | input = "ls example:ano"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 69 | expectedCompletions = {"example:ano/", "example:anoda/"}; |
| 70 | expectedContextLength = 11; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | SECTION("ls example:ano/example:a") |
| 74 | { |
| 75 | input = "ls example:ano/example:a"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 76 | expectedCompletions = {"example:a2/"}; |
| 77 | expectedContextLength = 9; |
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"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 83 | expectedCompletions = {}; |
| 84 | expectedContextLength = 1; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | SECTION("ls /") |
| 88 | { |
| 89 | input = "ls /"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 90 | expectedCompletions = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList[", "second:amelie/"}; |
| 91 | expectedContextLength = 0; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | SECTION("ls /e") |
| 95 | { |
| 96 | input = "ls /e"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 97 | expectedCompletions = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList["}; |
| 98 | expectedContextLength = 1; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 101 | SECTION("ls example:bota") |
| 102 | { |
| 103 | input = "ls example:bota"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 104 | expectedCompletions = {"example:bota/"}; |
| 105 | expectedContextLength = 12; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | SECTION("ls /example:bota") |
| 109 | { |
| 110 | input = "ls /example:bota"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 111 | expectedCompletions = {"example:bota/"}; |
| 112 | expectedContextLength = 12; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 115 | SECTION("ls /s") |
| 116 | { |
| 117 | input = "ls /s"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 118 | expectedCompletions = {"second:amelie/"}; |
| 119 | expectedContextLength = 1; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | SECTION("ls /example:list[number=3]/") |
| 123 | { |
| 124 | input = "ls /example:list[number=3]/"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 125 | expectedCompletions = {"example:contInList/", "example:number "}; |
| 126 | expectedContextLength = 0; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | SECTION("ls /example:list[number=3]/c") |
| 130 | { |
| 131 | input = "ls /example:list[number=3]/e"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 132 | expectedCompletions = {"example:contInList/", "example:number "}; |
| 133 | expectedContextLength = 1; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | SECTION("ls /example:list[number=3]/a") |
| 137 | { |
| 138 | input = "ls /example:list[number=3]/a"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 139 | expectedCompletions = {}; |
| 140 | expectedContextLength = 1; |
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 | } |
| 143 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 144 | SECTION("list keys completion") |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 145 | { |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 146 | SECTION("cd example:lis") |
| 147 | { |
| 148 | input = "cd example:lis"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 149 | expectedCompletions = {"example:list["}; |
| 150 | expectedContextLength = 11; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 151 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 152 | |
Václav Kubernát | 51853b2 | 2019-04-16 21:53:44 +0200 | [diff] [blame] | 153 | SECTION("set example:list") |
| 154 | { |
| 155 | input = "set example:list"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 156 | expectedCompletions = {"example:list["}; |
| 157 | expectedContextLength = 12; |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | SECTION("cd example:list") |
| 161 | { |
| 162 | input = "cd example:list"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 163 | expectedCompletions = {"example:list["}; |
| 164 | expectedContextLength = 12; |
Václav Kubernát | 51853b2 | 2019-04-16 21:53:44 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 167 | SECTION("cd example:list[") |
| 168 | { |
| 169 | input = "cd example:list["; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 170 | expectedCompletions = {"number="}; |
| 171 | expectedContextLength = 0; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 172 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 173 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 174 | SECTION("cd example:list[numb") |
| 175 | { |
| 176 | input = "cd example:list[numb"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 177 | expectedCompletions = {"number="}; |
| 178 | expectedContextLength = 4; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 179 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 180 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 181 | SECTION("cd example:list[number") |
| 182 | { |
| 183 | input = "cd example:list[number"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 184 | expectedCompletions = {"number="}; |
| 185 | expectedContextLength = 6; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 186 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 187 | |
Václav Kubernát | 117f7b5 | 2020-01-23 16:10:55 +0100 | [diff] [blame] | 188 | SECTION("cd example:list[number=") |
| 189 | { |
| 190 | input = "cd example:list[number="; |
| 191 | expectedCompletions = {"number="}; |
| 192 | expectedContextLength = 7; |
| 193 | } |
| 194 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 195 | SECTION("cd example:list[number=12") |
| 196 | { |
| 197 | input = "cd example:list[number=12"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 198 | expectedCompletions = {"]/"}; |
| 199 | expectedContextLength = 0; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 200 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 201 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 202 | SECTION("cd example:list[number=12]") |
| 203 | { |
| 204 | input = "cd example:list[number=12]"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 205 | expectedCompletions = {"]/"}; |
| 206 | expectedContextLength = 1; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 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[") |
| 210 | { |
| 211 | input = "cd example:twoKeyList["; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 212 | expectedCompletions = {"name=", "number="}; |
| 213 | expectedContextLength = 0; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 214 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 215 | |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 216 | SECTION("cd example:twoKeyList[name=\"AHOJ\"") |
| 217 | { |
| 218 | input = "cd example:twoKeyList[name=\"AHOJ\""; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 219 | expectedCompletions = {"]["}; |
| 220 | expectedContextLength = 0; |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | SECTION("cd example:twoKeyList[name=\"AHOJ\"]") |
| 224 | { |
| 225 | input = "cd example:twoKeyList[name=\"AHOJ\"]"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 226 | expectedCompletions = {"]["}; |
| 227 | expectedContextLength = 1; |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 230 | SECTION("cd example:twoKeyList[name=\"AHOJ\"][") |
| 231 | { |
| 232 | input = "cd example:twoKeyList[name=\"AHOJ\"]["; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 233 | expectedCompletions = {"number="}; |
| 234 | expectedContextLength = 0; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 235 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 236 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 237 | SECTION("cd example:twoKeyList[number=42][") |
| 238 | { |
| 239 | input = "cd example:twoKeyList[number=42]["; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 240 | expectedCompletions = {"name="}; |
| 241 | expectedContextLength = 0; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 242 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 243 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 244 | SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123") |
| 245 | { |
| 246 | input = "cd example:twoKeyList[name=\"AHOJ\"][number=123"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 247 | expectedCompletions = {"]/"}; |
| 248 | expectedContextLength = 0; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123]") |
| 252 | { |
| 253 | input = "cd example:twoKeyList[name=\"AHOJ\"][number=123]"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 254 | expectedCompletions = {"]/"}; |
| 255 | expectedContextLength = 1; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 256 | } |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 257 | |
| 258 | SECTION("cd example:ovoce") |
| 259 | { |
| 260 | input = "cd example:ovoce"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 261 | expectedCompletions = {"example:ovoce[", "example:ovocezelenina["}; |
| 262 | expectedContextLength = 13; |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 263 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 266 | SECTION("clear completions when no longer inputting path") |
| 267 | { |
| 268 | input = "set example:leafInt "; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 269 | expectedCompletions = {}; |
| 270 | expectedContextLength = 0; |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 273 | REQUIRE(parser.completeCommand(input, errorStream) == (Completions{expectedCompletions, expectedContextLength})); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 274 | } |