blob: 5adb7ffdb0cc31a33b3a93518d7ade8b1a115ec2 [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át8028f942019-09-25 16:03:23 +020054 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át8028f942019-09-25 16:03:23 +020060 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áta395d332019-02-13 16:49:20 +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áta395d332019-02-13 16:49:20 +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át8028f942019-09-25 16:03:23 +020084 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át8028f942019-09-25 16:03:23 +020090 expected = {"example:ano", "example:anoda", "example:bota", "example:leafInt", "example:list", "example:ovoce", "example:ovocezelenina", "example:twoKeyList"};
91
Václav Kubernát329c6c32019-02-06 16:41:53 +010092 }
93
94 SECTION("ls /s")
95 {
96 input = "ls /s";
Václav Kubernáta395d332019-02-13 16:49:20 +010097 expected = {"second:amelie"};
Václav Kubernát329c6c32019-02-06 16:41:53 +010098 }
99
100 SECTION("ls /example:list[number=3]/")
101 {
102 input = "ls /example:list[number=3]/";
103 expected = {"example:contInList"};
104 }
105
106 SECTION("ls /example:list[number=3]/c")
107 {
108 input = "ls /example:list[number=3]/e";
Václav Kubernáta395d332019-02-13 16:49:20 +0100109 expected = {"example:contInList"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100110 }
111
112 SECTION("ls /example:list[number=3]/a")
113 {
114 input = "ls /example:list[number=3]/a";
115 expected = {};
116 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100117 }
118
Václav Kubernát329c6c32019-02-06 16:41:53 +0100119 SECTION("list keys completion")
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100120 {
Václav Kubernát329c6c32019-02-06 16:41:53 +0100121 SECTION("cd example:lis")
122 {
123 input = "cd example:lis";
Václav Kubernáta395d332019-02-13 16:49:20 +0100124 expected = {"example:list"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100125 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100126
Václav Kubernát51853b22019-04-16 21:53:44 +0200127 SECTION("set example:list")
128 {
129 input = "set example:list";
Václav Kubernát4c325482019-04-11 17:51:55 +0200130 expected = {"example:list["};
131 }
132
133 SECTION("cd example:list")
134 {
135 input = "cd example:list";
136 expected = {"example:list["};
Václav Kubernát51853b22019-04-16 21:53:44 +0200137 }
138
Václav Kubernát329c6c32019-02-06 16:41:53 +0100139 SECTION("cd example:list[")
140 {
141 input = "cd example:list[";
142 expected = {"number="};
143 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100144
Václav Kubernát329c6c32019-02-06 16:41:53 +0100145 SECTION("cd example:list[numb")
146 {
147 input = "cd example:list[numb";
Václav Kubernáta395d332019-02-13 16:49:20 +0100148 expected = {"number="};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100149 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100150
Václav Kubernát329c6c32019-02-06 16:41:53 +0100151 SECTION("cd example:list[number")
152 {
153 input = "cd example:list[number";
Václav Kubernáta395d332019-02-13 16:49:20 +0100154 expected = {"number="};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100155 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100156
Václav Kubernát329c6c32019-02-06 16:41:53 +0100157 SECTION("cd example:list[number=12")
158 {
159 input = "cd example:list[number=12";
160 expected = {"]/"};
161 }
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=12]")
164 {
165 input = "cd example:list[number=12]";
Václav Kubernáta395d332019-02-13 16:49:20 +0100166 expected = {"]/"};
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:twoKeyList[")
170 {
171 input = "cd example:twoKeyList[";
172 expected = {"name=", "number="};
173 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100174
Václav Kubernát4c325482019-04-11 17:51:55 +0200175 SECTION("cd example:twoKeyList[name=\"AHOJ\"")
176 {
177 input = "cd example:twoKeyList[name=\"AHOJ\"";
178 expected = {"]["};
179 }
180
181 SECTION("cd example:twoKeyList[name=\"AHOJ\"]")
182 {
183 input = "cd example:twoKeyList[name=\"AHOJ\"]";
184 expected = {"]["};
185 }
186
Václav Kubernát329c6c32019-02-06 16:41:53 +0100187 SECTION("cd example:twoKeyList[name=\"AHOJ\"][")
188 {
189 input = "cd example:twoKeyList[name=\"AHOJ\"][";
190 expected = {"number="};
191 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100192
Václav Kubernát329c6c32019-02-06 16:41:53 +0100193 SECTION("cd example:twoKeyList[number=42][")
194 {
195 input = "cd example:twoKeyList[number=42][";
196 expected = {"name="};
197 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100198
Václav Kubernát329c6c32019-02-06 16:41:53 +0100199 SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123")
200 {
201 input = "cd example:twoKeyList[name=\"AHOJ\"][number=123";
202 expected = {"]/"};
203 }
204
205 SECTION("cd example:twoKeyList[name=\"AHOJ\"][number=123]")
206 {
207 input = "cd example:twoKeyList[name=\"AHOJ\"][number=123]";
Václav Kubernáta395d332019-02-13 16:49:20 +0100208 expected = {"]/"};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100209 }
Václav Kubernát4c325482019-04-11 17:51:55 +0200210
211 SECTION("cd example:ovoce")
212 {
213 input = "cd example:ovoce";
214 expected = {"example:ovoce", "example:ovocezelenina"};
215 }
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100216 }
217
Václav Kubernát8028f942019-09-25 16:03:23 +0200218 SECTION("clear completions when no longer inputting path")
219 {
220 input = "set example:leafInt ";
221 expected = {};
222 }
223
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100224 REQUIRE(parser.completeCommand(input, errorStream) == expected);
225}