blob: 5ae7debf26662410ec1a4fc1b65d09f9c1b802de [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át3a99f002020-03-31 02:27:41 +020012#include "leaf_data_helpers.hpp"
Václav Kubernát989b5de2019-02-20 16:28:35 +010013#include "parser.hpp"
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010014#include "pretty_printers.hpp"
Václav Kubernát989b5de2019-02-20 16:28:35 +010015#include "static_schema.hpp"
16
Václav Kubernát549b08f2020-05-14 22:19:36 +020017TEST_CASE("set value completion")
Václav Kubernát989b5de2019-02-20 16:28:35 +010018{
19 auto schema = std::make_shared<StaticSchema>();
20 schema->addModule("mod");
Václav Kubernátefcac932020-01-10 15:26:32 +010021 schema->addContainer("/", "mod:contA");
Václav Kubernát3a99f002020-03-31 02:27:41 +020022 schema->addLeaf("/", "mod:leafEnum", createEnum({"lala", "lol", "data", "coze"}));
23 schema->addLeaf("/mod:contA", "mod:leafInCont", createEnum({"abc", "def"}));
Václav Kubernátefcac932020-01-10 15:26:32 +010024 schema->addList("/", "mod:list", {"number"});
Václav Kubernát3a99f002020-03-31 02:27:41 +020025 schema->addLeaf("/mod:list", "mod:number", yang::Int32{});
26 schema->addLeaf("/mod:list", "mod:leafInList", createEnum({"ano", "anoda", "ne", "katoda"}));
Václav Kubernát549b08f2020-05-14 22:19:36 +020027 schema->addIdentity(std::nullopt, ModuleValuePair{"mod", "food"});
28 schema->addIdentity(std::nullopt, ModuleValuePair{"mod", "vehicle"});
29 schema->addIdentity(ModuleValuePair{"mod", "food"}, ModuleValuePair{"mod", "pizza"});
30 schema->addIdentity(ModuleValuePair{"mod", "food"}, ModuleValuePair{"mod", "spaghetti"});
31 schema->addIdentity(ModuleValuePair{"mod", "pizza"}, ModuleValuePair{"pizza-module", "hawaii"});
32 schema->addLeaf("/", "mod:foodIdentRef", yang::IdentityRef{schema->validIdentities("mod", "food")});
Václav Kubernát43908fb2020-01-02 19:05:51 +010033 auto mockDatastore = std::make_shared<MockDatastoreAccess>();
34 // The parser will use DataQuery for key value completion, but I'm not testing that here, so I don't return anything.
35 ALLOW_CALL(*mockDatastore, listInstances("/mod:list"))
36 .RETURN(std::vector<ListInstance>{});
37
38 // DataQuery gets the schema from DatastoreAccess once
39 auto expectation = NAMED_REQUIRE_CALL(*mockDatastore, schema())
40 .RETURN(schema);
41 auto dataQuery = std::make_shared<DataQuery>(*mockDatastore);
42 Parser parser(schema, dataQuery);
Václav Kubernát989b5de2019-02-20 16:28:35 +010043 std::string input;
44 std::ostringstream errorStream;
45
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010046 std::set<std::string> expectedCompletions;
47 int expectedContextLength;
Václav Kubernát989b5de2019-02-20 16:28:35 +010048
49 SECTION("set mod:leafEnum ")
50 {
51 input = "set mod:leafEnum ";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010052 expectedCompletions = {"lala", "lol", "data", "coze"};
53 expectedContextLength = 0;
Václav Kubernát989b5de2019-02-20 16:28:35 +010054 }
55
56 SECTION("set mod:leafEnum c")
57 {
58 input = "set mod:leafEnum c";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010059 expectedCompletions = {"coze"};
60 expectedContextLength = 1;
Václav Kubernát989b5de2019-02-20 16:28:35 +010061 }
62
63 SECTION("set mod:leafEnum l")
64 {
65 input = "set mod:leafEnum l";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010066 expectedCompletions = {"lala", "lol"};
67 expectedContextLength = 1;
Václav Kubernát989b5de2019-02-20 16:28:35 +010068 }
69
70
71 SECTION("set mod:contA/leafInCont ")
72 {
73 input = "set mod:contA/leafInCont ";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010074 expectedCompletions = {"abc", "def"};
75 expectedContextLength = 0;
Václav Kubernát989b5de2019-02-20 16:28:35 +010076 }
77
78 SECTION("set mod:list[number=42]/leafInList ")
79 {
80 input = "set mod:list[number=42]/leafInList ";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010081 expectedCompletions = {"ano", "anoda", "ne", "katoda"};
82 expectedContextLength = 0;
Václav Kubernát989b5de2019-02-20 16:28:35 +010083 }
84
Václav Kubernát549b08f2020-05-14 22:19:36 +020085 SECTION("set mod:foodIdentRef ")
86 {
87 input = "set mod:foodIdentRef ";
88 expectedCompletions = {"mod:food", "mod:pizza", "mod:spaghetti", "pizza-module:hawaii"};
89 expectedContextLength = 0;
90 }
91
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010092 REQUIRE(parser.completeCommand(input, errorStream) == (Completions{expectedCompletions, expectedContextLength}));
Václav Kubernát989b5de2019-02-20 16:28:35 +010093}