blob: c68cca07ba2d63ae9359eab657f4165eba583285 [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");
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át4c325482019-04-11 17:51:55 +020039 schema->addList("", "example:ovoce", {"name"});
40 schema->addList("", "example:ovocezelenina", {"name"});
Václav Kubernát4108e0d2018-10-29 13:32:22 +010041 schema->addContainer("example:list", "example:contInList");
42 schema->addList("", "example:twoKeyList", {"number", "name"});
Václav Kubernát8028f942019-09-25 16:03:23 +020043 schema->addLeaf("", "example:leafInt", yang::LeafDataTypes::Int32);
Václav Kubernát4108e0d2018-10-29 13:32:22 +010044 Parser parser(schema);
45 std::string input;
46 std::ostringstream errorStream;
47 std::set<std::string> expected;
48
Václav Kubernát329c6c32019-02-06 16:41:53 +010049 SECTION("node name completion")
Václav Kubernát4108e0d2018-10-29 13:32:22 +010050 {
Václav Kubernát329c6c32019-02-06 16:41:53 +010051 SECTION("ls ")
52 {
53 input = "ls ";
Václav Kubernáte69133a2019-11-01 19:01:34 +010054 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 +010055 }
56
57 SECTION("ls e")
58 {
59 input = "ls e";
Václav Kubernáte69133a2019-11-01 19:01:34 +010060 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 +010061 }
62
63 SECTION("ls example:ano")
64 {
65 input = "ls example:ano";
Václav Kubernáte69133a2019-11-01 19:01:34 +010066 expected = {"example:ano/", "example:anoda/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +010067 }
68
69 SECTION("ls example:ano/example:a")
70 {
71 input = "ls example:ano/example:a";
Václav Kubernáte69133a2019-11-01 19:01:34 +010072 expected = {"example:a2/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +010073 }
74
75 SECTION("ls x")
76 {
77 input = "ls x";
78 expected = {};
79 }
80
81 SECTION("ls /")
82 {
83 input = "ls /";
Václav Kubernáte69133a2019-11-01 19:01:34 +010084 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 +010085 }
86
87 SECTION("ls /e")
88 {
89 input = "ls /e";
Václav Kubernáte69133a2019-11-01 19:01:34 +010090 expected = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList["};
Václav Kubernát8028f942019-09-25 16:03:23 +020091
Václav Kubernát329c6c32019-02-06 16:41:53 +010092 }
93
Václav Kubernáte69133a2019-11-01 19:01:34 +010094 SECTION("ls example:bota")
95 {
96 input = "ls example:bota";
97 expected = {"example:bota/"};
98 }
99
100 SECTION("ls /example:bota")
101 {
102 input = "ls /example:bota";
103 expected = {"example:bota/"};
104 }
105
Václav Kubernát329c6c32019-02-06 16:41:53 +0100106 SECTION("ls /s")
107 {
108 input = "ls /s";
Václav Kubernáte69133a2019-11-01 19:01:34 +0100109 expected = {"second:amelie/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100110 }
111
112 SECTION("ls /example:list[number=3]/")
113 {
114 input = "ls /example:list[number=3]/";
Václav Kubernáte69133a2019-11-01 19:01:34 +0100115 expected = {"example:contInList/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100116 }
117
118 SECTION("ls /example:list[number=3]/c")
119 {
120 input = "ls /example:list[number=3]/e";
Václav Kubernáte69133a2019-11-01 19:01:34 +0100121 expected = {"example:contInList/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100122 }
123
124 SECTION("ls /example:list[number=3]/a")
125 {
126 input = "ls /example:list[number=3]/a";
127 expected = {};
128 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100129 }
130
Václav Kubernát329c6c32019-02-06 16:41:53 +0100131 SECTION("list keys completion")
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100132 {
Václav Kubernát329c6c32019-02-06 16:41:53 +0100133 SECTION("cd example:lis")
134 {
135 input = "cd example:lis";
Václav Kubernáte69133a2019-11-01 19:01:34 +0100136 expected = {"example:list["};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100137 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100138
Václav Kubernát51853b22019-04-16 21:53:44 +0200139 SECTION("set example:list")
140 {
141 input = "set example:list";
Václav Kubernát4c325482019-04-11 17:51:55 +0200142 expected = {"example:list["};
143 }
144
145 SECTION("cd example:list")
146 {
147 input = "cd example:list";
148 expected = {"example:list["};
Václav Kubernát51853b22019-04-16 21:53:44 +0200149 }
150
Václav Kubernát329c6c32019-02-06 16:41:53 +0100151 SECTION("cd example:list[")
152 {
153 input = "cd example:list[";
154 expected = {"number="};
155 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100156
Václav Kubernát329c6c32019-02-06 16:41:53 +0100157 SECTION("cd example:list[numb")
158 {
159 input = "cd example:list[numb";
Václav Kubernáta395d332019-02-13 16:49:20 +0100160 expected = {"number="};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100161 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100162
Václav Kubernát329c6c32019-02-06 16:41:53 +0100163 SECTION("cd example:list[number")
164 {
165 input = "cd example:list[number";
Václav Kubernáta395d332019-02-13 16:49:20 +0100166 expected = {"number="};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100167 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100168
Václav Kubernát329c6c32019-02-06 16:41:53 +0100169 SECTION("cd example:list[number=12")
170 {
171 input = "cd example:list[number=12";
172 expected = {"]/"};
173 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100174
Václav Kubernát329c6c32019-02-06 16:41:53 +0100175 SECTION("cd example:list[number=12]")
176 {
177 input = "cd example:list[number=12]";
Václav Kubernáta395d332019-02-13 16:49:20 +0100178 expected = {"]/"};
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:twoKeyList[")
182 {
183 input = "cd example:twoKeyList[";
184 expected = {"name=", "number="};
185 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100186
Václav Kubernát4c325482019-04-11 17:51:55 +0200187 SECTION("cd example:twoKeyList[name=\"AHOJ\"")
188 {
189 input = "cd example:twoKeyList[name=\"AHOJ\"";
190 expected = {"]["};
191 }
192
193 SECTION("cd example:twoKeyList[name=\"AHOJ\"]")
194 {
195 input = "cd example:twoKeyList[name=\"AHOJ\"]";
196 expected = {"]["};
197 }
198
Václav Kubernát329c6c32019-02-06 16:41:53 +0100199 SECTION("cd example:twoKeyList[name=\"AHOJ\"][")
200 {
201 input = "cd example:twoKeyList[name=\"AHOJ\"][";
202 expected = {"number="};
203 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100204
Václav Kubernát329c6c32019-02-06 16:41:53 +0100205 SECTION("cd example:twoKeyList[number=42][")
206 {
207 input = "cd example:twoKeyList[number=42][";
208 expected = {"name="};
209 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100210
Václav Kubernát329c6c32019-02-06 16:41:53 +0100211 SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123")
212 {
213 input = "cd example:twoKeyList[name=\"AHOJ\"][number=123";
214 expected = {"]/"};
215 }
216
217 SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123]")
218 {
219 input = "cd example:twoKeyList[name=\"AHOJ\"][number=123]";
Václav Kubernáta395d332019-02-13 16:49:20 +0100220 expected = {"]/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100221 }
Václav Kubernát4c325482019-04-11 17:51:55 +0200222
223 SECTION("cd example:ovoce")
224 {
225 input = "cd example:ovoce";
Václav Kubernáte69133a2019-11-01 19:01:34 +0100226 expected = {"example:ovoce[", "example:ovocezelenina["};
Václav Kubernát4c325482019-04-11 17:51:55 +0200227 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100228 }
229
Václav Kubernát8028f942019-09-25 16:03:23 +0200230 SECTION("clear completions when no longer inputting path")
231 {
232 input = "set example:leafInt ";
233 expected = {};
234 }
235
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100236 REQUIRE(parser.completeCommand(input, errorStream) == expected);
237}