blob: c3ad64a04d31e804f58b435b7e1b8fa785967354 [file] [log] [blame]
Václav Kubernát989b5de2019-02-20 16:28:35 +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át26b56082020-02-03 18:28:56 +01009#include "trompeloeil_doctest.hpp"
Václav Kubernát43908fb2020-01-02 19:05:51 +010010#include "datastoreaccess_mock.hpp"
Václav Kubernát3a99f002020-03-31 02:27:41 +020011#include "leaf_data_helpers.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
Václav Kubernát549b08f2020-05-14 22:19:36 +020016TEST_CASE("set value completion")
Václav Kubernát989b5de2019-02-20 16:28:35 +010017{
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");
Václav Kubernát3a99f002020-03-31 02:27:41 +020021 schema->addLeaf("/", "mod:leafEnum", createEnum({"lala", "lol", "data", "coze"}));
22 schema->addLeaf("/mod:contA", "mod:leafInCont", createEnum({"abc", "def"}));
Václav Kubernátefcac932020-01-10 15:26:32 +010023 schema->addList("/", "mod:list", {"number"});
Václav Kubernát3a99f002020-03-31 02:27:41 +020024 schema->addLeaf("/mod:list", "mod:number", yang::Int32{});
25 schema->addLeaf("/mod:list", "mod:leafInList", createEnum({"ano", "anoda", "ne", "katoda"}));
Václav Kubernát10113562020-05-15 15:10:00 +020026 schema->addIdentity(std::nullopt, identityRef_{"mod", "food"});
27 schema->addIdentity(std::nullopt, identityRef_{"mod", "vehicle"});
28 schema->addIdentity(identityRef_{"mod", "food"}, identityRef_{"mod", "pizza"});
29 schema->addIdentity(identityRef_{"mod", "food"}, identityRef_{"mod", "spaghetti"});
30 schema->addIdentity(identityRef_{"mod", "pizza"}, identityRef_{"pizza-module", "hawaii"});
Václav Kubernát549b08f2020-05-14 22:19:36 +020031 schema->addLeaf("/", "mod:foodIdentRef", yang::IdentityRef{schema->validIdentities("mod", "food")});
Václav Kubernát43908fb2020-01-02 19:05:51 +010032 auto mockDatastore = std::make_shared<MockDatastoreAccess>();
33 // The parser will use DataQuery for key value completion, but I'm not testing that here, so I don't return anything.
34 ALLOW_CALL(*mockDatastore, listInstances("/mod:list"))
35 .RETURN(std::vector<ListInstance>{});
36
37 // DataQuery gets the schema from DatastoreAccess once
38 auto expectation = NAMED_REQUIRE_CALL(*mockDatastore, schema())
39 .RETURN(schema);
40 auto dataQuery = std::make_shared<DataQuery>(*mockDatastore);
41 Parser parser(schema, dataQuery);
Václav Kubernát989b5de2019-02-20 16:28:35 +010042 std::string input;
43 std::ostringstream errorStream;
44
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010045 std::set<std::string> expectedCompletions;
46 int expectedContextLength;
Václav Kubernát989b5de2019-02-20 16:28:35 +010047
48 SECTION("set mod:leafEnum ")
49 {
50 input = "set mod:leafEnum ";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010051 expectedCompletions = {"lala", "lol", "data", "coze"};
52 expectedContextLength = 0;
Václav Kubernát989b5de2019-02-20 16:28:35 +010053 }
54
55 SECTION("set mod:leafEnum c")
56 {
57 input = "set mod:leafEnum c";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010058 expectedCompletions = {"coze"};
59 expectedContextLength = 1;
Václav Kubernát989b5de2019-02-20 16:28:35 +010060 }
61
62 SECTION("set mod:leafEnum l")
63 {
64 input = "set mod:leafEnum l";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010065 expectedCompletions = {"lala", "lol"};
66 expectedContextLength = 1;
Václav Kubernát989b5de2019-02-20 16:28:35 +010067 }
68
69
70 SECTION("set mod:contA/leafInCont ")
71 {
72 input = "set mod:contA/leafInCont ";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010073 expectedCompletions = {"abc", "def"};
74 expectedContextLength = 0;
Václav Kubernát989b5de2019-02-20 16:28:35 +010075 }
76
77 SECTION("set mod:list[number=42]/leafInList ")
78 {
79 input = "set mod:list[number=42]/leafInList ";
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010080 expectedCompletions = {"ano", "anoda", "ne", "katoda"};
81 expectedContextLength = 0;
Václav Kubernát989b5de2019-02-20 16:28:35 +010082 }
83
Václav Kubernát549b08f2020-05-14 22:19:36 +020084 SECTION("set mod:foodIdentRef ")
85 {
86 input = "set mod:foodIdentRef ";
87 expectedCompletions = {"mod:food", "mod:pizza", "mod:spaghetti", "pizza-module:hawaii"};
88 expectedContextLength = 0;
89 }
90
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010091 REQUIRE(parser.completeCommand(input, errorStream) == (Completions{expectedCompletions, expectedContextLength}));
Václav Kubernát989b5de2019-02-20 16:28:35 +010092}