blob: 115cfdd6f73479b0975db3d3b53ac3118549ede3 [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"});
39 schema->addList("/", "example:ovoce", {"name"});
40 schema->addList("/", "example:ovocezelenina", {"name"});
41 schema->addContainer("/example:list", "example:contInList");
42 schema->addList("/", "example:twoKeyList", {"number", "name"});
43 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át329c6c32019-02-06 16:41:53 +010091 }
92
Václav Kubernáte69133a2019-11-01 19:01:34 +010093 SECTION("ls example:bota")
94 {
95 input = "ls example:bota";
96 expected = {"example:bota/"};
97 }
98
99 SECTION("ls /example:bota")
100 {
101 input = "ls /example:bota";
102 expected = {"example:bota/"};
103 }
104
Václav Kubernát329c6c32019-02-06 16:41:53 +0100105 SECTION("ls /s")
106 {
107 input = "ls /s";
Václav Kubernáte69133a2019-11-01 19:01:34 +0100108 expected = {"second:amelie/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100109 }
110
111 SECTION("ls /example:list[number=3]/")
112 {
113 input = "ls /example:list[number=3]/";
Václav Kubernáte69133a2019-11-01 19:01:34 +0100114 expected = {"example:contInList/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100115 }
116
117 SECTION("ls /example:list[number=3]/c")
118 {
119 input = "ls /example:list[number=3]/e";
Václav Kubernáte69133a2019-11-01 19:01:34 +0100120 expected = {"example:contInList/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100121 }
122
123 SECTION("ls /example:list[number=3]/a")
124 {
125 input = "ls /example:list[number=3]/a";
126 expected = {};
127 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100128 }
129
Václav Kubernát329c6c32019-02-06 16:41:53 +0100130 SECTION("list keys completion")
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100131 {
Václav Kubernát329c6c32019-02-06 16:41:53 +0100132 SECTION("cd example:lis")
133 {
134 input = "cd example:lis";
Václav Kubernáte69133a2019-11-01 19:01:34 +0100135 expected = {"example:list["};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100136 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100137
Václav Kubernát51853b22019-04-16 21:53:44 +0200138 SECTION("set example:list")
139 {
140 input = "set example:list";
Václav Kubernát4c325482019-04-11 17:51:55 +0200141 expected = {"example:list["};
142 }
143
144 SECTION("cd example:list")
145 {
146 input = "cd example:list";
147 expected = {"example:list["};
Václav Kubernát51853b22019-04-16 21:53:44 +0200148 }
149
Václav Kubernát329c6c32019-02-06 16:41:53 +0100150 SECTION("cd example:list[")
151 {
152 input = "cd example:list[";
153 expected = {"number="};
154 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100155
Václav Kubernát329c6c32019-02-06 16:41:53 +0100156 SECTION("cd example:list[numb")
157 {
158 input = "cd example:list[numb";
Václav Kubernáta395d332019-02-13 16:49:20 +0100159 expected = {"number="};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100160 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100161
Václav Kubernát329c6c32019-02-06 16:41:53 +0100162 SECTION("cd example:list[number")
163 {
164 input = "cd example:list[number";
Václav Kubernáta395d332019-02-13 16:49:20 +0100165 expected = {"number="};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100166 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100167
Václav Kubernát329c6c32019-02-06 16:41:53 +0100168 SECTION("cd example:list[number=12")
169 {
170 input = "cd example:list[number=12";
171 expected = {"]/"};
172 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100173
Václav Kubernát329c6c32019-02-06 16:41:53 +0100174 SECTION("cd example:list[number=12]")
175 {
176 input = "cd example:list[number=12]";
Václav Kubernáta395d332019-02-13 16:49:20 +0100177 expected = {"]/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100178 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100179
Václav Kubernát329c6c32019-02-06 16:41:53 +0100180 SECTION("cd example:twoKeyList[")
181 {
182 input = "cd example:twoKeyList[";
183 expected = {"name=", "number="};
184 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100185
Václav Kubernát4c325482019-04-11 17:51:55 +0200186 SECTION("cd example:twoKeyList[name=\"AHOJ\"")
187 {
188 input = "cd example:twoKeyList[name=\"AHOJ\"";
189 expected = {"]["};
190 }
191
192 SECTION("cd example:twoKeyList[name=\"AHOJ\"]")
193 {
194 input = "cd example:twoKeyList[name=\"AHOJ\"]";
195 expected = {"]["};
196 }
197
Václav Kubernát329c6c32019-02-06 16:41:53 +0100198 SECTION("cd example:twoKeyList[name=\"AHOJ\"][")
199 {
200 input = "cd example:twoKeyList[name=\"AHOJ\"][";
201 expected = {"number="};
202 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100203
Václav Kubernát329c6c32019-02-06 16:41:53 +0100204 SECTION("cd example:twoKeyList[number=42][")
205 {
206 input = "cd example:twoKeyList[number=42][";
207 expected = {"name="};
208 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100209
Václav Kubernát329c6c32019-02-06 16:41:53 +0100210 SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123")
211 {
212 input = "cd example:twoKeyList[name=\"AHOJ\"][number=123";
213 expected = {"]/"};
214 }
215
216 SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123]")
217 {
218 input = "cd example:twoKeyList[name=\"AHOJ\"][number=123]";
Václav Kubernáta395d332019-02-13 16:49:20 +0100219 expected = {"]/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100220 }
Václav Kubernát4c325482019-04-11 17:51:55 +0200221
222 SECTION("cd example:ovoce")
223 {
224 input = "cd example:ovoce";
Václav Kubernáte69133a2019-11-01 19:01:34 +0100225 expected = {"example:ovoce[", "example:ovocezelenina["};
Václav Kubernát4c325482019-04-11 17:51:55 +0200226 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100227 }
228
Václav Kubernát8028f942019-09-25 16:03:23 +0200229 SECTION("clear completions when no longer inputting path")
230 {
231 input = "set example:leafInt ";
232 expected = {};
233 }
234
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100235 REQUIRE(parser.completeCommand(input, errorStream) == expected);
236}