blob: 939243fc2c1bdddeda43d79e9e95a0960894c961 [file] [log] [blame]
Václav Kubernát989b5de2019-02-20 16:28:35 +01001
2/*
3 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
4 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
5 *
6 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
7 *
8 */
9
Václav Kubernát26b56082020-02-03 18:28:56 +010010#include "trompeloeil_doctest.hpp"
Václav Kubernát43908fb2020-01-02 19:05:51 +010011#include "datastoreaccess_mock.hpp"
Václav Kubernát989b5de2019-02-20 16:28:35 +010012#include "parser.hpp"
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010013#include "pretty_printers.hpp"
Václav Kubernát989b5de2019-02-20 16:28:35 +010014#include "static_schema.hpp"
15
16TEST_CASE("enum completion")
17{
18 auto schema = std::make_shared<StaticSchema>();
19 schema->addModule("mod");
Václav Kubernátefcac932020-01-10 15:26:32 +010020 schema->addContainer("/", "mod:contA");
21 schema->addLeafEnum("/", "mod:leafEnum", {"lala", "lol", "data", "coze"});
22 schema->addLeafEnum("/mod:contA", "mod:leafInCont", {"abc", "def"});
23 schema->addList("/", "mod:list", {"number"});
Václav Kubernát7707cae2020-01-16 12:04:53 +010024 schema->addLeaf("/mod:list", "mod:number", yang::LeafDataTypes::Int32);
Václav Kubernátefcac932020-01-10 15:26:32 +010025 schema->addLeafEnum("/mod:list", "mod:leafInList", {"ano", "anoda", "ne", "katoda"});
Václav Kubernát43908fb2020-01-02 19:05:51 +010026 auto mockDatastore = std::make_shared<MockDatastoreAccess>();
27 // The parser will use DataQuery for key value completion, but I'm not testing that here, so I don't return anything.
28 ALLOW_CALL(*mockDatastore, listInstances("/mod:list"))
29 .RETURN(std::vector<ListInstance>{});
30
31 // DataQuery gets the schema from DatastoreAccess once
32 auto expectation = NAMED_REQUIRE_CALL(*mockDatastore, schema())
33 .RETURN(schema);
34 auto dataQuery = std::make_shared<DataQuery>(*mockDatastore);
35 Parser parser(schema, dataQuery);
Václav Kubernát989b5de2019-02-20 16:28:35 +010036 std::string input;
37 std::ostringstream errorStream;
38
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010039 std::set<std::string> expectedCompletions;
40 int expectedContextLength;
Václav Kubernát989b5de2019-02-20 16:28:35 +010041
42 SECTION("set mod:leafEnum ")
43 {
44 input = "set mod:leafEnum ";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010045 expectedCompletions = {"lala", "lol", "data", "coze"};
46 expectedContextLength = 0;
Václav Kubernát989b5de2019-02-20 16:28:35 +010047 }
48
49 SECTION("set mod:leafEnum c")
50 {
51 input = "set mod:leafEnum c";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010052 expectedCompletions = {"coze"};
53 expectedContextLength = 1;
Václav Kubernát989b5de2019-02-20 16:28:35 +010054 }
55
56 SECTION("set mod:leafEnum l")
57 {
58 input = "set mod:leafEnum l";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010059 expectedCompletions = {"lala", "lol"};
60 expectedContextLength = 1;
Václav Kubernát989b5de2019-02-20 16:28:35 +010061 }
62
63
64 SECTION("set mod:contA/leafInCont ")
65 {
66 input = "set mod:contA/leafInCont ";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010067 expectedCompletions = {"abc", "def"};
68 expectedContextLength = 0;
Václav Kubernát989b5de2019-02-20 16:28:35 +010069 }
70
71 SECTION("set mod:list[number=42]/leafInList ")
72 {
73 input = "set mod:list[number=42]/leafInList ";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010074 expectedCompletions = {"ano", "anoda", "ne", "katoda"};
75 expectedContextLength = 0;
Václav Kubernát989b5de2019-02-20 16:28:35 +010076 }
77
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010078 REQUIRE(parser.completeCommand(input, errorStream) == (Completions{expectedCompletions, expectedContextLength}));
Václav Kubernát989b5de2019-02-20 16:28:35 +010079}