Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 1 | /* |
| 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át | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 9 | #include <experimental/iterator> |
Jan Kundrát | a33cf08 | 2019-03-28 11:55:57 +0100 | [diff] [blame] | 10 | #include "trompeloeil_doctest.h" |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 11 | #include "parser.hpp" |
| 12 | #include "static_schema.hpp" |
| 13 | |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 14 | namespace std { |
| 15 | std::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át | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 24 | TEST_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át | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 39 | schema->addList("", "example:ovoce", {"name"}); |
| 40 | schema->addList("", "example:ovocezelenina", {"name"}); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 41 | schema->addContainer("example:list", "example:contInList"); |
| 42 | schema->addList("", "example:twoKeyList", {"number", "name"}); |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 43 | schema->addLeaf("", "example:leafInt", yang::LeafDataTypes::Int32); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 44 | Parser parser(schema); |
| 45 | std::string input; |
| 46 | std::ostringstream errorStream; |
| 47 | std::set<std::string> expected; |
| 48 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 49 | SECTION("node name completion") |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 50 | { |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 51 | SECTION("ls ") |
| 52 | { |
| 53 | input = "ls "; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 54 | expected = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList[", "second:amelie/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | SECTION("ls e") |
| 58 | { |
| 59 | input = "ls e"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 60 | expected = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList["}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | SECTION("ls example:ano") |
| 64 | { |
| 65 | input = "ls example:ano"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 66 | expected = {"example:ano/", "example:anoda/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | SECTION("ls example:ano/example:a") |
| 70 | { |
| 71 | input = "ls example:ano/example:a"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 72 | expected = {"example:a2/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | SECTION("ls x") |
| 76 | { |
| 77 | input = "ls x"; |
| 78 | expected = {}; |
| 79 | } |
| 80 | |
| 81 | SECTION("ls /") |
| 82 | { |
| 83 | input = "ls /"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 84 | expected = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList[", "second:amelie/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | SECTION("ls /e") |
| 88 | { |
| 89 | input = "ls /e"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 90 | expected = {"example:ano/", "example:anoda/", "example:bota/", "example:leafInt ", "example:list[", "example:ovoce[", "example:ovocezelenina[", "example:twoKeyList["}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 93 | 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át | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 105 | SECTION("ls /s") |
| 106 | { |
| 107 | input = "ls /s"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 108 | expected = {"second:amelie/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | SECTION("ls /example:list[number=3]/") |
| 112 | { |
| 113 | input = "ls /example:list[number=3]/"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 114 | expected = {"example:contInList/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | SECTION("ls /example:list[number=3]/c") |
| 118 | { |
| 119 | input = "ls /example:list[number=3]/e"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 120 | expected = {"example:contInList/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | SECTION("ls /example:list[number=3]/a") |
| 124 | { |
| 125 | input = "ls /example:list[number=3]/a"; |
| 126 | expected = {}; |
| 127 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 130 | SECTION("list keys completion") |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 131 | { |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 132 | SECTION("cd example:lis") |
| 133 | { |
| 134 | input = "cd example:lis"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 135 | expected = {"example:list["}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 136 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 137 | |
Václav Kubernát | 51853b2 | 2019-04-16 21:53:44 +0200 | [diff] [blame] | 138 | SECTION("set example:list") |
| 139 | { |
| 140 | input = "set example:list"; |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 141 | expected = {"example:list["}; |
| 142 | } |
| 143 | |
| 144 | SECTION("cd example:list") |
| 145 | { |
| 146 | input = "cd example:list"; |
| 147 | expected = {"example:list["}; |
Václav Kubernát | 51853b2 | 2019-04-16 21:53:44 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 150 | SECTION("cd example:list[") |
| 151 | { |
| 152 | input = "cd example:list["; |
| 153 | expected = {"number="}; |
| 154 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 155 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 156 | SECTION("cd example:list[numb") |
| 157 | { |
| 158 | input = "cd example:list[numb"; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 159 | expected = {"number="}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 160 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 161 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 162 | SECTION("cd example:list[number") |
| 163 | { |
| 164 | input = "cd example:list[number"; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 165 | expected = {"number="}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 166 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 167 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 168 | SECTION("cd example:list[number=12") |
| 169 | { |
| 170 | input = "cd example:list[number=12"; |
| 171 | expected = {"]/"}; |
| 172 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 173 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 174 | SECTION("cd example:list[number=12]") |
| 175 | { |
| 176 | input = "cd example:list[number=12]"; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 177 | expected = {"]/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 178 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 179 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 180 | SECTION("cd example:twoKeyList[") |
| 181 | { |
| 182 | input = "cd example:twoKeyList["; |
| 183 | expected = {"name=", "number="}; |
| 184 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 185 | |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 186 | 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át | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 198 | SECTION("cd example:twoKeyList[name=\"AHOJ\"][") |
| 199 | { |
| 200 | input = "cd example:twoKeyList[name=\"AHOJ\"]["; |
| 201 | expected = {"number="}; |
| 202 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 203 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 204 | SECTION("cd example:twoKeyList[number=42][") |
| 205 | { |
| 206 | input = "cd example:twoKeyList[number=42]["; |
| 207 | expected = {"name="}; |
| 208 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 209 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 210 | 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át | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 219 | expected = {"]/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 220 | } |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 221 | |
| 222 | SECTION("cd example:ovoce") |
| 223 | { |
| 224 | input = "cd example:ovoce"; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 225 | expected = {"example:ovoce[", "example:ovocezelenina["}; |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 226 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 229 | SECTION("clear completions when no longer inputting path") |
| 230 | { |
| 231 | input = "set example:leafInt "; |
| 232 | expected = {}; |
| 233 | } |
| 234 | |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 235 | REQUIRE(parser.completeCommand(input, errorStream) == expected); |
| 236 | } |