Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +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 | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 9 | #include "trompeloeil_doctest.hpp" |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 10 | #include "datastoreaccess_mock.hpp" |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 11 | #include "leaf_data_helpers.hpp" |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 12 | #include "parser.hpp" |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 13 | #include "pretty_printers.hpp" |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 14 | #include "static_schema.hpp" |
| 15 | |
Václav Kubernát | 549b08f | 2020-05-14 22:19:36 +0200 | [diff] [blame] | 16 | TEST_CASE("set value completion") |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 17 | { |
| 18 | auto schema = std::make_shared<StaticSchema>(); |
| 19 | schema->addModule("mod"); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 20 | schema->addContainer("/", "mod:contA"); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 21 | schema->addLeaf("/", "mod:leafEnum", createEnum({"lala", "lol", "data", "coze"})); |
| 22 | schema->addLeaf("/mod:contA", "mod:leafInCont", createEnum({"abc", "def"})); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 23 | schema->addList("/", "mod:list", {"number"}); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 24 | schema->addLeaf("/mod:list", "mod:number", yang::Int32{}); |
| 25 | schema->addLeaf("/mod:list", "mod:leafInList", createEnum({"ano", "anoda", "ne", "katoda"})); |
Václav Kubernát | 1011356 | 2020-05-15 15:10:00 +0200 | [diff] [blame] | 26 | 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át | 549b08f | 2020-05-14 22:19:36 +0200 | [diff] [blame] | 31 | schema->addLeaf("/", "mod:foodIdentRef", yang::IdentityRef{schema->validIdentities("mod", "food")}); |
Václav Kubernát | 467c22c | 2020-12-03 12:04:04 +0100 | [diff] [blame^] | 32 | schema->addLeaf("/", "mod:flags", yang::Bits{{"parity", "zero", "carry", "sign"}}); |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 33 | 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); |
Václav Kubernát | 28cf336 | 2020-06-29 17:52:51 +0200 | [diff] [blame] | 42 | Parser parser(schema, WritableOps::No, dataQuery); |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 43 | std::string input; |
| 44 | std::ostringstream errorStream; |
| 45 | |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 46 | std::set<std::string> expectedCompletions; |
| 47 | int expectedContextLength; |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 48 | |
| 49 | SECTION("set mod:leafEnum ") |
| 50 | { |
| 51 | input = "set mod:leafEnum "; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 52 | expectedCompletions = {"lala", "lol", "data", "coze"}; |
| 53 | expectedContextLength = 0; |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | SECTION("set mod:leafEnum c") |
| 57 | { |
| 58 | input = "set mod:leafEnum c"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 59 | expectedCompletions = {"coze"}; |
| 60 | expectedContextLength = 1; |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | SECTION("set mod:leafEnum l") |
| 64 | { |
| 65 | input = "set mod:leafEnum l"; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 66 | expectedCompletions = {"lala", "lol"}; |
| 67 | expectedContextLength = 1; |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | |
| 71 | SECTION("set mod:contA/leafInCont ") |
| 72 | { |
| 73 | input = "set mod:contA/leafInCont "; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 74 | expectedCompletions = {"abc", "def"}; |
| 75 | expectedContextLength = 0; |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | SECTION("set mod:list[number=42]/leafInList ") |
| 79 | { |
| 80 | input = "set mod:list[number=42]/leafInList "; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 81 | expectedCompletions = {"ano", "anoda", "ne", "katoda"}; |
| 82 | expectedContextLength = 0; |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Václav Kubernát | 549b08f | 2020-05-14 22:19:36 +0200 | [diff] [blame] | 85 | 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át | 467c22c | 2020-12-03 12:04:04 +0100 | [diff] [blame^] | 92 | SECTION("set mod:flags ") |
| 93 | { |
| 94 | input = "set mod:flags "; |
| 95 | expectedCompletions = {"carry", "sign", "parity", "zero"}; |
| 96 | expectedContextLength = 0; |
| 97 | } |
| 98 | |
| 99 | SECTION("set mod:flags ze") |
| 100 | { |
| 101 | input = "set mod:flags ze"; |
| 102 | expectedCompletions = {"zero"}; |
| 103 | expectedContextLength = 2; |
| 104 | } |
| 105 | |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 106 | REQUIRE(parser.completeCommand(input, errorStream) == (Completions{expectedCompletions, expectedContextLength})); |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 107 | } |