blob: 77dae51b20caf50f4ac1d74863d88e2cc04f1b47 [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át8028f942019-09-25 16:03:23 +02009#include <experimental/iterator>
Jan Kundráta33cf082019-03-28 11:55:57 +010010#include "trompeloeil_doctest.h"
Václav Kubernát4108e0d2018-10-29 13:32:22 +010011#include "parser.hpp"
12#include "static_schema.hpp"
13
Václav Kubernát8028f942019-09-25 16:03:23 +020014namespace std {
15std::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át4108e0d2018-10-29 13:32:22 +010024TEST_CASE("path_completion")
25{
26 auto schema = std::make_shared<StaticSchema>();
27 schema->addModule("example");
28 schema->addModule("second");
Václav Kubernátefcac932020-01-10 15:26:32 +010029 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át7707cae2020-01-16 12:04:53 +010039 schema->addLeaf("/example:list", "example:number", yang::LeafDataTypes::Int32);
Václav Kubernátefcac932020-01-10 15:26:32 +010040 schema->addContainer("/example:list", "example:contInList");
Václav Kubernát7707cae2020-01-16 12:04:53 +010041 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átefcac932020-01-10 15:26:32 +010045 schema->addList("/", "example:twoKeyList", {"number", "name"});
Václav Kubernát7707cae2020-01-16 12:04:53 +010046 schema->addLeaf("/example:twoKeyList", "example:name", yang::LeafDataTypes::String);
47 schema->addLeaf("/example:twoKeyList", "example:number", yang::LeafDataTypes::Int32);
Václav Kubernátefcac932020-01-10 15:26:32 +010048 schema->addLeaf("/", "example:leafInt", yang::LeafDataTypes::Int32);
Václav Kubernát4108e0d2018-10-29 13:32:22 +010049 Parser parser(schema);
50 std::string input;
51 std::ostringstream errorStream;
52 std::set<std::string> expected;
53
Václav Kubernát329c6c32019-02-06 16:41:53 +010054 SECTION("node name completion")
Václav Kubernát4108e0d2018-10-29 13:32:22 +010055 {
Václav Kubernát329c6c32019-02-06 16:41:53 +010056 SECTION("ls ")
57 {
58 input = "ls ";
Václav Kubernáte69133a2019-11-01 19:01:34 +010059 expected = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList[", "second:amelie/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +010060 }
61
62 SECTION("ls e")
63 {
64 input = "ls e";
Václav Kubernáte69133a2019-11-01 19:01:34 +010065 expected = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList["};
Václav Kubernát329c6c32019-02-06 16:41:53 +010066 }
67
68 SECTION("ls example:ano")
69 {
70 input = "ls example:ano";
Václav Kubernáte69133a2019-11-01 19:01:34 +010071 expected = {"example:ano/", "example:anoda/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +010072 }
73
74 SECTION("ls example:ano/example:a")
75 {
76 input = "ls example:ano/example:a";
Václav Kubernáte69133a2019-11-01 19:01:34 +010077 expected = {"example:a2/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +010078 }
79
80 SECTION("ls x")
81 {
82 input = "ls x";
83 expected = {};
84 }
85
86 SECTION("ls /")
87 {
88 input = "ls /";
Václav Kubernáte69133a2019-11-01 19:01:34 +010089 expected = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList[", "second:amelie/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +010090 }
91
92 SECTION("ls /e")
93 {
94 input = "ls /e";
Václav Kubernáte69133a2019-11-01 19:01:34 +010095 expected = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList["};
Václav Kubernát329c6c32019-02-06 16:41:53 +010096 }
97
Václav Kubernáte69133a2019-11-01 19:01:34 +010098 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át329c6c32019-02-06 16:41:53 +0100110 SECTION("ls /s")
111 {
112 input = "ls /s";
Václav Kubernáte69133a2019-11-01 19:01:34 +0100113 expected = {"second:amelie/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100114 }
115
116 SECTION("ls /example:list[number=3]/")
117 {
118 input = "ls /example:list[number=3]/";
Václav Kubernát7707cae2020-01-16 12:04:53 +0100119 expected = {"example:contInList/", "example:number "};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100120 }
121
122 SECTION("ls /example:list[number=3]/c")
123 {
124 input = "ls /example:list[number=3]/e";
Václav Kubernát7707cae2020-01-16 12:04:53 +0100125 expected = {"example:contInList/", "example:number "};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100126 }
127
128 SECTION("ls /example:list[number=3]/a")
129 {
130 input = "ls /example:list[number=3]/a";
131 expected = {};
132 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100133 }
134
Václav Kubernát329c6c32019-02-06 16:41:53 +0100135 SECTION("list keys completion")
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100136 {
Václav Kubernát329c6c32019-02-06 16:41:53 +0100137 SECTION("cd example:lis")
138 {
139 input = "cd example:lis";
Václav Kubernáte69133a2019-11-01 19:01:34 +0100140 expected = {"example:list["};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100141 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100142
Václav Kubernát51853b22019-04-16 21:53:44 +0200143 SECTION("set example:list")
144 {
145 input = "set example:list";
Václav Kubernát4c325482019-04-11 17:51:55 +0200146 expected = {"example:list["};
147 }
148
149 SECTION("cd example:list")
150 {
151 input = "cd example:list";
152 expected = {"example:list["};
Václav Kubernát51853b22019-04-16 21:53:44 +0200153 }
154
Václav Kubernát329c6c32019-02-06 16:41:53 +0100155 SECTION("cd example:list[")
156 {
157 input = "cd example:list[";
158 expected = {"number="};
159 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100160
Václav Kubernát329c6c32019-02-06 16:41:53 +0100161 SECTION("cd example:list[numb")
162 {
163 input = "cd example:list[numb";
Václav Kubernáta395d332019-02-13 16:49:20 +0100164 expected = {"number="};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100165 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100166
Václav Kubernát329c6c32019-02-06 16:41:53 +0100167 SECTION("cd example:list[number")
168 {
169 input = "cd example:list[number";
Václav Kubernáta395d332019-02-13 16:49:20 +0100170 expected = {"number="};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100171 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100172
Václav Kubernát329c6c32019-02-06 16:41:53 +0100173 SECTION("cd example:list[number=12")
174 {
175 input = "cd example:list[number=12";
176 expected = {"]/"};
177 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100178
Václav Kubernát329c6c32019-02-06 16:41:53 +0100179 SECTION("cd example:list[number=12]")
180 {
181 input = "cd example:list[number=12]";
Václav Kubernáta395d332019-02-13 16:49:20 +0100182 expected = {"]/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100183 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100184
Václav Kubernát329c6c32019-02-06 16:41:53 +0100185 SECTION("cd example:twoKeyList[")
186 {
187 input = "cd example:twoKeyList[";
188 expected = {"name=", "number="};
189 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100190
Václav Kubernát4c325482019-04-11 17:51:55 +0200191 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át329c6c32019-02-06 16:41:53 +0100203 SECTION("cd example:twoKeyList[name=\"AHOJ\"][")
204 {
205 input = "cd example:twoKeyList[name=\"AHOJ\"][";
206 expected = {"number="};
207 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100208
Václav Kubernát329c6c32019-02-06 16:41:53 +0100209 SECTION("cd example:twoKeyList[number=42][")
210 {
211 input = "cd example:twoKeyList[number=42][";
212 expected = {"name="};
213 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100214
Václav Kubernát329c6c32019-02-06 16:41:53 +0100215 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áta395d332019-02-13 16:49:20 +0100224 expected = {"]/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100225 }
Václav Kubernát4c325482019-04-11 17:51:55 +0200226
227 SECTION("cd example:ovoce")
228 {
229 input = "cd example:ovoce";
Václav Kubernáte69133a2019-11-01 19:01:34 +0100230 expected = {"example:ovoce[", "example:ovocezelenina["};
Václav Kubernát4c325482019-04-11 17:51:55 +0200231 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100232 }
233
Václav Kubernát8028f942019-09-25 16:03:23 +0200234 SECTION("clear completions when no longer inputting path")
235 {
236 input = "set example:leafInt ";
237 expected = {};
238 }
239
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100240 REQUIRE(parser.completeCommand(input, errorStream) == expected);
241}