blob: 2403f9d412731f942ce286096056c75a65fd1ed2 [file] [log] [blame]
Václav Kubernát4108e0d2018-10-29 13:32:22 +01001/*
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át26b56082020-02-03 18:28:56 +01009#include "trompeloeil_doctest.hpp"
Václav Kubernát43908fb2020-01-02 19:05:51 +010010#include "datastoreaccess_mock.hpp"
Václav Kubernát4108e0d2018-10-29 13:32:22 +010011#include "parser.hpp"
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010012#include "pretty_printers.hpp"
Václav Kubernát4108e0d2018-10-29 13:32:22 +010013#include "static_schema.hpp"
14
15TEST_CASE("path_completion")
16{
17 auto schema = std::make_shared<StaticSchema>();
18 schema->addModule("example");
19 schema->addModule("second");
Václav Kubernátefcac932020-01-10 15:26:32 +010020 schema->addContainer("/", "example:ano");
21 schema->addContainer("/", "example:anoda");
22 schema->addList("/example:ano", "example:listInCont", {"number"});
23 schema->addContainer("/", "second:amelie");
24 schema->addContainer("/", "example:bota");
25 schema->addContainer("/example:ano", "example:a2");
26 schema->addContainer("/example:bota", "example:b2");
27 schema->addContainer("/example:ano/example:a2", "example:a3");
28 schema->addContainer("/example:bota/example:b2", "example:b3");
29 schema->addList("/", "example:list", {"number"});
Václav Kubernát7707cae2020-01-16 12:04:53 +010030 schema->addLeaf("/example:list", "example:number", yang::LeafDataTypes::Int32);
Václav Kubernátefcac932020-01-10 15:26:32 +010031 schema->addContainer("/example:list", "example:contInList");
Václav Kubernát7707cae2020-01-16 12:04:53 +010032 schema->addList("/", "example:ovoce", {"name"});
33 schema->addLeaf("/example:ovoce", "example:name", yang::LeafDataTypes::String);
34 schema->addList("/", "example:ovocezelenina", {"name"});
35 schema->addLeaf("/example:ovocezelenina", "example:name", yang::LeafDataTypes::String);
Václav Kubernátefcac932020-01-10 15:26:32 +010036 schema->addList("/", "example:twoKeyList", {"number", "name"});
Václav Kubernát7707cae2020-01-16 12:04:53 +010037 schema->addLeaf("/example:twoKeyList", "example:name", yang::LeafDataTypes::String);
38 schema->addLeaf("/example:twoKeyList", "example:number", yang::LeafDataTypes::Int32);
Václav Kubernátefcac932020-01-10 15:26:32 +010039 schema->addLeaf("/", "example:leafInt", yang::LeafDataTypes::Int32);
Václav Kubernát43908fb2020-01-02 19:05:51 +010040 auto mockDatastore = std::make_shared<MockDatastoreAccess>();
41
42 // The parser will use DataQuery for key value completion, but I'm not testing that here, so I don't return anything.
43 ALLOW_CALL(*mockDatastore, listInstances("/example:list"))
44 .RETURN(std::vector<ListInstance>{});
45 ALLOW_CALL(*mockDatastore, listInstances("/example:twoKeyList"))
46 .RETURN(std::vector<ListInstance>{});
47
48 // DataQuery gets the schema from DatastoreAccess once
49 auto expectation = NAMED_REQUIRE_CALL(*mockDatastore, schema())
50 .RETURN(schema);
51 auto dataQuery = std::make_shared<DataQuery>(*mockDatastore);
52 expectation.reset();
53 Parser parser(schema, dataQuery);
Václav Kubernát4108e0d2018-10-29 13:32:22 +010054 std::string input;
55 std::ostringstream errorStream;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010056
57 std::set<std::string> expectedCompletions;
58 // GCC complains here with -Wmaybe-uninitialized if I don't assign
59 // something here. I suspect it's because of nested SECTIONs. -1 is an
60 // invalid value (as in, I'll never expect expectedContextLength to be -1),
61 // so let's go with that.
62 int expectedContextLength = -1;
Václav Kubernát4108e0d2018-10-29 13:32:22 +010063
Václav Kubernát329c6c32019-02-06 16:41:53 +010064 SECTION("node name completion")
Václav Kubernát4108e0d2018-10-29 13:32:22 +010065 {
Václav Kubernát329c6c32019-02-06 16:41:53 +010066 SECTION("ls ")
67 {
68 input = "ls ";
Václav Kubernátcb3af402020-02-12 16:49:17 +010069 expectedCompletions = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list", "example:ovoce", "example:ovocezelenina", "example:twoKeyList", "second:amelie/"};
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010070 expectedContextLength = 0;
Václav Kubernát329c6c32019-02-06 16:41:53 +010071 }
72
73 SECTION("ls e")
74 {
75 input = "ls e";
Václav Kubernátcb3af402020-02-12 16:49:17 +010076 expectedCompletions = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list", "example:ovoce", "example:ovocezelenina", "example:twoKeyList"};
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010077 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +010078 }
79
80 SECTION("ls example:ano")
81 {
82 input = "ls example:ano";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010083 expectedCompletions = {"example:ano/", "example:anoda/"};
84 expectedContextLength = 11;
Václav Kubernát329c6c32019-02-06 16:41:53 +010085 }
86
87 SECTION("ls example:ano/example:a")
88 {
89 input = "ls example:ano/example:a";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010090 expectedCompletions = {"example:a2/"};
91 expectedContextLength = 9;
Václav Kubernát329c6c32019-02-06 16:41:53 +010092 }
93
94 SECTION("ls x")
95 {
96 input = "ls x";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010097 expectedCompletions = {};
98 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +010099 }
100
101 SECTION("ls /")
102 {
103 input = "ls /";
Václav Kubernátcb3af402020-02-12 16:49:17 +0100104 expectedCompletions = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list", "example:ovoce", "example:ovocezelenina", "example:twoKeyList", "second:amelie/"};
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100105 expectedContextLength = 0;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100106 }
107
108 SECTION("ls /e")
109 {
110 input = "ls /e";
Václav Kubernátcb3af402020-02-12 16:49:17 +0100111 expectedCompletions = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list", "example:ovoce", "example:ovocezelenina", "example:twoKeyList"};
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100112 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100113 }
114
Václav Kubernáte69133a2019-11-01 19:01:34 +0100115 SECTION("ls example:bota")
116 {
117 input = "ls example:bota";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100118 expectedCompletions = {"example:bota/"};
119 expectedContextLength = 12;
Václav Kubernáte69133a2019-11-01 19:01:34 +0100120 }
121
122 SECTION("ls /example:bota")
123 {
124 input = "ls /example:bota";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100125 expectedCompletions = {"example:bota/"};
126 expectedContextLength = 12;
Václav Kubernáte69133a2019-11-01 19:01:34 +0100127 }
128
Václav Kubernát329c6c32019-02-06 16:41:53 +0100129 SECTION("ls /s")
130 {
131 input = "ls /s";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100132 expectedCompletions = {"second:amelie/"};
133 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100134 }
135
Václav Kubernát26be33a2020-02-13 13:30:14 +0100136 SECTION("ls /example:list/")
137 {
138 input = "ls /example:list/";
139 expectedCompletions = {"example:contInList/", "example:number "};
140 expectedContextLength = 0;
141 }
142
Václav Kubernát329c6c32019-02-06 16:41:53 +0100143 SECTION("ls /example:list[number=3]/")
144 {
145 input = "ls /example:list[number=3]/";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100146 expectedCompletions = {"example:contInList/", "example:number "};
147 expectedContextLength = 0;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100148 }
149
150 SECTION("ls /example:list[number=3]/c")
151 {
152 input = "ls /example:list[number=3]/e";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100153 expectedCompletions = {"example:contInList/", "example:number "};
154 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100155 }
156
157 SECTION("ls /example:list[number=3]/a")
158 {
159 input = "ls /example:list[number=3]/a";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100160 expectedCompletions = {};
161 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100162 }
Václav Kubernát26be33a2020-02-13 13:30:14 +0100163
164 SECTION("ls /example:list/example:contInList/")
165 {
166 input = "ls /example:list/example:contInList/";
167 expectedCompletions = {};
168 expectedContextLength = 0;
169 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100170 }
171
Václav Kubernát329c6c32019-02-06 16:41:53 +0100172 SECTION("list keys completion")
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100173 {
Václav Kubernát329c6c32019-02-06 16:41:53 +0100174 SECTION("cd example:lis")
175 {
176 input = "cd example:lis";
Václav Kubernátcb3af402020-02-12 16:49:17 +0100177 expectedCompletions = {"example:list"};
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100178 expectedContextLength = 11;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100179 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100180
Václav Kubernát51853b22019-04-16 21:53:44 +0200181 SECTION("set example:list")
182 {
183 input = "set example:list";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100184 expectedCompletions = {"example:list["};
185 expectedContextLength = 12;
Václav Kubernát4c325482019-04-11 17:51:55 +0200186 }
187
188 SECTION("cd example:list")
189 {
190 input = "cd example:list";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100191 expectedCompletions = {"example:list["};
192 expectedContextLength = 12;
Václav Kubernát51853b22019-04-16 21:53:44 +0200193 }
194
Václav Kubernát329c6c32019-02-06 16:41:53 +0100195 SECTION("cd example:list[")
196 {
197 input = "cd example:list[";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100198 expectedCompletions = {"number="};
199 expectedContextLength = 0;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100200 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100201
Václav Kubernát329c6c32019-02-06 16:41:53 +0100202 SECTION("cd example:list[numb")
203 {
204 input = "cd example:list[numb";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100205 expectedCompletions = {"number="};
206 expectedContextLength = 4;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100207 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100208
Václav Kubernát329c6c32019-02-06 16:41:53 +0100209 SECTION("cd example:list[number")
210 {
211 input = "cd example:list[number";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100212 expectedCompletions = {"number="};
213 expectedContextLength = 6;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100214 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100215
Václav Kubernát117f7b52020-01-23 16:10:55 +0100216 SECTION("cd example:list[number=")
217 {
218 input = "cd example:list[number=";
Václav Kubernát43908fb2020-01-02 19:05:51 +0100219 expectedCompletions = {};
220 expectedContextLength = 0;
Václav Kubernát117f7b52020-01-23 16:10:55 +0100221 }
222
Václav Kubernát329c6c32019-02-06 16:41:53 +0100223 SECTION("cd example:list[number=12")
224 {
225 input = "cd example:list[number=12";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100226 expectedCompletions = {"]/"};
227 expectedContextLength = 0;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100228 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100229
Václav Kubernát329c6c32019-02-06 16:41:53 +0100230 SECTION("cd example:list[number=12]")
231 {
232 input = "cd example:list[number=12]";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100233 expectedCompletions = {"]/"};
234 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100235 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100236
Václav Kubernát329c6c32019-02-06 16:41:53 +0100237 SECTION("cd example:twoKeyList[")
238 {
239 input = "cd example:twoKeyList[";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100240 expectedCompletions = {"name=", "number="};
241 expectedContextLength = 0;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100242 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100243
Václav Kubernát4c325482019-04-11 17:51:55 +0200244 SECTION("cd example:twoKeyList[name=\"AHOJ\"")
245 {
246 input = "cd example:twoKeyList[name=\"AHOJ\"";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100247 expectedCompletions = {"]["};
248 expectedContextLength = 0;
Václav Kubernát4c325482019-04-11 17:51:55 +0200249 }
250
251 SECTION("cd example:twoKeyList[name=\"AHOJ\"]")
252 {
253 input = "cd example:twoKeyList[name=\"AHOJ\"]";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100254 expectedCompletions = {"]["};
255 expectedContextLength = 1;
Václav Kubernát4c325482019-04-11 17:51:55 +0200256 }
257
Václav Kubernát329c6c32019-02-06 16:41:53 +0100258 SECTION("cd example:twoKeyList[name=\"AHOJ\"][")
259 {
260 input = "cd example:twoKeyList[name=\"AHOJ\"][";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100261 expectedCompletions = {"number="};
262 expectedContextLength = 0;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100263 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100264
Václav Kubernát329c6c32019-02-06 16:41:53 +0100265 SECTION("cd example:twoKeyList[number=42][")
266 {
267 input = "cd example:twoKeyList[number=42][";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100268 expectedCompletions = {"name="};
269 expectedContextLength = 0;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100270 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100271
Václav Kubernát329c6c32019-02-06 16:41:53 +0100272 SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123")
273 {
274 input = "cd example:twoKeyList[name=\"AHOJ\"][number=123";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100275 expectedCompletions = {"]/"};
276 expectedContextLength = 0;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100277 }
278
279 SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123]")
280 {
281 input = "cd example:twoKeyList[name=\"AHOJ\"][number=123]";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100282 expectedCompletions = {"]/"};
283 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100284 }
Václav Kubernát4c325482019-04-11 17:51:55 +0200285
286 SECTION("cd example:ovoce")
287 {
288 input = "cd example:ovoce";
Václav Kubernátcb3af402020-02-12 16:49:17 +0100289 expectedCompletions = {"example:ovoce", "example:ovocezelenina"};
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100290 expectedContextLength = 13;
Václav Kubernát4c325482019-04-11 17:51:55 +0200291 }
Václav Kubernátcb3af402020-02-12 16:49:17 +0100292
293 SECTION("cd example:ovoceze")
294 {
295 input = "cd example:ovoceze";
296 expectedCompletions = {"example:ovocezelenina"};
297 expectedContextLength = 15;
298 }
299
300 SECTION("cd example:ovocezelenina")
301 {
302 input = "cd example:ovocezelenina";
303 expectedCompletions = {"example:ovocezelenina["};
304 expectedContextLength = 21;
305 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100306 }
307
Václav Kubernát8028f942019-09-25 16:03:23 +0200308 SECTION("clear completions when no longer inputting path")
309 {
310 input = "set example:leafInt ";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100311 expectedCompletions = {};
312 expectedContextLength = 0;
Václav Kubernát8028f942019-09-25 16:03:23 +0200313 }
314
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100315 REQUIRE(parser.completeCommand(input, errorStream) == (Completions{expectedCompletions, expectedContextLength}));
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100316}