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 | 8028f94 | 2019-09-25 16:03:23 +0200 | [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 | 8028f94 | 2019-09-25 16:03:23 +0200 | [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 | a395d33 | 2019-02-13 16:49:20 +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 | a395d33 | 2019-02-13 16:49:20 +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 | 8028f94 | 2019-09-25 16:03:23 +0200 | [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 | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame^] | 90 | expected = {"example:ano", "example:anoda", "example:bota", "example:leafInt", "example:list", "example:ovoce", "example:ovocezelenina", "example:twoKeyList"}; |
| 91 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | SECTION("ls /s") |
| 95 | { |
| 96 | input = "ls /s"; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 97 | expected = {"second:amelie"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 98 | } |
| 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át | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 109 | expected = {"example:contInList"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | SECTION("ls /example:list[number=3]/a") |
| 113 | { |
| 114 | input = "ls /example:list[number=3]/a"; |
| 115 | expected = {}; |
| 116 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 119 | SECTION("list keys completion") |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 120 | { |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 121 | SECTION("cd example:lis") |
| 122 | { |
| 123 | input = "cd example:lis"; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 124 | expected = {"example:list"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 125 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 126 | |
Václav Kubernát | 51853b2 | 2019-04-16 21:53:44 +0200 | [diff] [blame] | 127 | SECTION("set example:list") |
| 128 | { |
| 129 | input = "set example:list"; |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 130 | expected = {"example:list["}; |
| 131 | } |
| 132 | |
| 133 | SECTION("cd example:list") |
| 134 | { |
| 135 | input = "cd example:list"; |
| 136 | expected = {"example:list["}; |
Václav Kubernát | 51853b2 | 2019-04-16 21:53:44 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 139 | SECTION("cd example:list[") |
| 140 | { |
| 141 | input = "cd example:list["; |
| 142 | expected = {"number="}; |
| 143 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 144 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 145 | SECTION("cd example:list[numb") |
| 146 | { |
| 147 | input = "cd example:list[numb"; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 148 | expected = {"number="}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 149 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 150 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 151 | SECTION("cd example:list[number") |
| 152 | { |
| 153 | input = "cd example:list[number"; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 154 | expected = {"number="}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 155 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 156 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 157 | SECTION("cd example:list[number=12") |
| 158 | { |
| 159 | input = "cd example:list[number=12"; |
| 160 | expected = {"]/"}; |
| 161 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 162 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 163 | SECTION("cd example:list[number=12]") |
| 164 | { |
| 165 | input = "cd example:list[number=12]"; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 166 | expected = {"]/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 167 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 168 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 169 | SECTION("cd example:twoKeyList[") |
| 170 | { |
| 171 | input = "cd example:twoKeyList["; |
| 172 | expected = {"name=", "number="}; |
| 173 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 174 | |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 175 | 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át | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 187 | SECTION("cd example:twoKeyList[name=\"AHOJ\"][") |
| 188 | { |
| 189 | input = "cd example:twoKeyList[name=\"AHOJ\"]["; |
| 190 | expected = {"number="}; |
| 191 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 192 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 193 | SECTION("cd example:twoKeyList[number=42][") |
| 194 | { |
| 195 | input = "cd example:twoKeyList[number=42]["; |
| 196 | expected = {"name="}; |
| 197 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 198 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 199 | 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át | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 208 | expected = {"]/"}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 209 | } |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 210 | |
| 211 | SECTION("cd example:ovoce") |
| 212 | { |
| 213 | input = "cd example:ovoce"; |
| 214 | expected = {"example:ovoce", "example:ovocezelenina"}; |
| 215 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame^] | 218 | SECTION("clear completions when no longer inputting path") |
| 219 | { |
| 220 | input = "set example:leafInt "; |
| 221 | expected = {}; |
| 222 | } |
| 223 | |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 224 | REQUIRE(parser.completeCommand(input, errorStream) == expected); |
| 225 | } |