blob: 832110d55008d069e89a4ff3d7d35f094c0d9e9b [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
Jan Kundráta33cf082019-03-28 11:55:57 +01009#include "trompeloeil_doctest.h"
Václav Kubernát4108e0d2018-10-29 13:32:22 +010010#include "parser.hpp"
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010011#include "pretty_printers.hpp"
Václav Kubernát4108e0d2018-10-29 13:32:22 +010012#include "static_schema.hpp"
13
14TEST_CASE("path_completion")
15{
16 auto schema = std::make_shared<StaticSchema>();
17 schema->addModule("example");
18 schema->addModule("second");
Václav Kubernátefcac932020-01-10 15:26:32 +010019 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át7707cae2020-01-16 12:04:53 +010029 schema->addLeaf("/example:list", "example:number", yang::LeafDataTypes::Int32);
Václav Kubernátefcac932020-01-10 15:26:32 +010030 schema->addContainer("/example:list", "example:contInList");
Václav Kubernát7707cae2020-01-16 12:04:53 +010031 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átefcac932020-01-10 15:26:32 +010035 schema->addList("/", "example:twoKeyList", {"number", "name"});
Václav Kubernát7707cae2020-01-16 12:04:53 +010036 schema->addLeaf("/example:twoKeyList", "example:name", yang::LeafDataTypes::String);
37 schema->addLeaf("/example:twoKeyList", "example:number", yang::LeafDataTypes::Int32);
Václav Kubernátefcac932020-01-10 15:26:32 +010038 schema->addLeaf("/", "example:leafInt", yang::LeafDataTypes::Int32);
Václav Kubernát4108e0d2018-10-29 13:32:22 +010039 Parser parser(schema);
40 std::string input;
41 std::ostringstream errorStream;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010042
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át4108e0d2018-10-29 13:32:22 +010049
Václav Kubernát329c6c32019-02-06 16:41:53 +010050 SECTION("node name completion")
Václav Kubernát4108e0d2018-10-29 13:32:22 +010051 {
Václav Kubernát329c6c32019-02-06 16:41:53 +010052 SECTION("ls ")
53 {
54 input = "ls ";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010055 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át329c6c32019-02-06 16:41:53 +010057 }
58
59 SECTION("ls e")
60 {
61 input = "ls e";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010062 expectedCompletions = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList["};
63 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +010064 }
65
66 SECTION("ls example:ano")
67 {
68 input = "ls example:ano";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010069 expectedCompletions = {"example:ano/", "example:anoda/"};
70 expectedContextLength = 11;
Václav Kubernát329c6c32019-02-06 16:41:53 +010071 }
72
73 SECTION("ls example:ano/example:a")
74 {
75 input = "ls example:ano/example:a";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010076 expectedCompletions = {"example:a2/"};
77 expectedContextLength = 9;
Václav Kubernát329c6c32019-02-06 16:41:53 +010078 }
79
80 SECTION("ls x")
81 {
82 input = "ls x";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010083 expectedCompletions = {};
84 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +010085 }
86
87 SECTION("ls /")
88 {
89 input = "ls /";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010090 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át329c6c32019-02-06 16:41:53 +010092 }
93
94 SECTION("ls /e")
95 {
96 input = "ls /e";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010097 expectedCompletions = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList["};
98 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +010099 }
100
Václav Kubernáte69133a2019-11-01 19:01:34 +0100101 SECTION("ls example:bota")
102 {
103 input = "ls example:bota";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100104 expectedCompletions = {"example:bota/"};
105 expectedContextLength = 12;
Václav Kubernáte69133a2019-11-01 19:01:34 +0100106 }
107
108 SECTION("ls /example:bota")
109 {
110 input = "ls /example:bota";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100111 expectedCompletions = {"example:bota/"};
112 expectedContextLength = 12;
Václav Kubernáte69133a2019-11-01 19:01:34 +0100113 }
114
Václav Kubernát329c6c32019-02-06 16:41:53 +0100115 SECTION("ls /s")
116 {
117 input = "ls /s";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100118 expectedCompletions = {"second:amelie/"};
119 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100120 }
121
122 SECTION("ls /example:list[number=3]/")
123 {
124 input = "ls /example:list[number=3]/";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100125 expectedCompletions = {"example:contInList/", "example:number "};
126 expectedContextLength = 0;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100127 }
128
129 SECTION("ls /example:list[number=3]/c")
130 {
131 input = "ls /example:list[number=3]/e";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100132 expectedCompletions = {"example:contInList/", "example:number "};
133 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100134 }
135
136 SECTION("ls /example:list[number=3]/a")
137 {
138 input = "ls /example:list[number=3]/a";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100139 expectedCompletions = {};
140 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100141 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100142 }
143
Václav Kubernát329c6c32019-02-06 16:41:53 +0100144 SECTION("list keys completion")
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100145 {
Václav Kubernát329c6c32019-02-06 16:41:53 +0100146 SECTION("cd example:lis")
147 {
148 input = "cd example:lis";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100149 expectedCompletions = {"example:list["};
150 expectedContextLength = 11;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100151 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100152
Václav Kubernát51853b22019-04-16 21:53:44 +0200153 SECTION("set example:list")
154 {
155 input = "set example:list";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100156 expectedCompletions = {"example:list["};
157 expectedContextLength = 12;
Václav Kubernát4c325482019-04-11 17:51:55 +0200158 }
159
160 SECTION("cd example:list")
161 {
162 input = "cd example:list";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100163 expectedCompletions = {"example:list["};
164 expectedContextLength = 12;
Václav Kubernát51853b22019-04-16 21:53:44 +0200165 }
166
Václav Kubernát329c6c32019-02-06 16:41:53 +0100167 SECTION("cd example:list[")
168 {
169 input = "cd example:list[";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100170 expectedCompletions = {"number="};
171 expectedContextLength = 0;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100172 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100173
Václav Kubernát329c6c32019-02-06 16:41:53 +0100174 SECTION("cd example:list[numb")
175 {
176 input = "cd example:list[numb";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100177 expectedCompletions = {"number="};
178 expectedContextLength = 4;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100179 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100180
Václav Kubernát329c6c32019-02-06 16:41:53 +0100181 SECTION("cd example:list[number")
182 {
183 input = "cd example:list[number";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100184 expectedCompletions = {"number="};
185 expectedContextLength = 6;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100186 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100187
Václav Kubernát117f7b52020-01-23 16:10:55 +0100188 SECTION("cd example:list[number=")
189 {
190 input = "cd example:list[number=";
191 expectedCompletions = {"number="};
192 expectedContextLength = 7;
193 }
194
Václav Kubernát329c6c32019-02-06 16:41:53 +0100195 SECTION("cd example:list[number=12")
196 {
197 input = "cd example:list[number=12";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100198 expectedCompletions = {"]/"};
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[number=12]")
203 {
204 input = "cd example:list[number=12]";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100205 expectedCompletions = {"]/"};
206 expectedContextLength = 1;
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:twoKeyList[")
210 {
211 input = "cd example:twoKeyList[";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100212 expectedCompletions = {"name=", "number="};
213 expectedContextLength = 0;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100214 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100215
Václav Kubernát4c325482019-04-11 17:51:55 +0200216 SECTION("cd example:twoKeyList[name=\"AHOJ\"")
217 {
218 input = "cd example:twoKeyList[name=\"AHOJ\"";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100219 expectedCompletions = {"]["};
220 expectedContextLength = 0;
Václav Kubernát4c325482019-04-11 17:51:55 +0200221 }
222
223 SECTION("cd example:twoKeyList[name=\"AHOJ\"]")
224 {
225 input = "cd example:twoKeyList[name=\"AHOJ\"]";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100226 expectedCompletions = {"]["};
227 expectedContextLength = 1;
Václav Kubernát4c325482019-04-11 17:51:55 +0200228 }
229
Václav Kubernát329c6c32019-02-06 16:41:53 +0100230 SECTION("cd example:twoKeyList[name=\"AHOJ\"][")
231 {
232 input = "cd example:twoKeyList[name=\"AHOJ\"][";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100233 expectedCompletions = {"number="};
234 expectedContextLength = 0;
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[number=42][")
238 {
239 input = "cd example:twoKeyList[number=42][";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100240 expectedCompletions = {"name="};
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át329c6c32019-02-06 16:41:53 +0100244 SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123")
245 {
246 input = "cd example:twoKeyList[name=\"AHOJ\"][number=123";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100247 expectedCompletions = {"]/"};
248 expectedContextLength = 0;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100249 }
250
251 SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123]")
252 {
253 input = "cd example:twoKeyList[name=\"AHOJ\"][number=123]";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100254 expectedCompletions = {"]/"};
255 expectedContextLength = 1;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100256 }
Václav Kubernát4c325482019-04-11 17:51:55 +0200257
258 SECTION("cd example:ovoce")
259 {
260 input = "cd example:ovoce";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100261 expectedCompletions = {"example:ovoce[", "example:ovocezelenina["};
262 expectedContextLength = 13;
Václav Kubernát4c325482019-04-11 17:51:55 +0200263 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100264 }
265
Václav Kubernát8028f942019-09-25 16:03:23 +0200266 SECTION("clear completions when no longer inputting path")
267 {
268 input = "set example:leafInt ";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100269 expectedCompletions = {};
270 expectedContextLength = 0;
Václav Kubernát8028f942019-09-25 16:03:23 +0200271 }
272
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100273 REQUIRE(parser.completeCommand(input, errorStream) == (Completions{expectedCompletions, expectedContextLength}));
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100274}