Václav Kubernát | 8208687 | 2020-04-29 01:09:50 +0200 | [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 | |
| 9 | #include <experimental/iterator> |
| 10 | #include "trompeloeil_doctest.hpp" |
| 11 | #include "ast_commands.hpp" |
| 12 | #include "interpreter.hpp" |
| 13 | #include "datastoreaccess_mock.hpp" |
| 14 | #include "parser.hpp" |
| 15 | #include "pretty_printers.hpp" |
| 16 | #include "static_schema.hpp" |
| 17 | |
| 18 | class MockSchema : public trompeloeil::mock_interface<Schema> { |
| 19 | public: |
| 20 | IMPLEMENT_CONST_MOCK1(defaultValue); |
| 21 | IMPLEMENT_CONST_MOCK1(description); |
| 22 | IMPLEMENT_CONST_MOCK2(availableNodes); |
| 23 | IMPLEMENT_CONST_MOCK1(isConfig); |
| 24 | MAKE_CONST_MOCK1(leafType, yang::TypeInfo(const std::string&), override); |
| 25 | MAKE_CONST_MOCK2(leafType, yang::TypeInfo(const schemaPath_&, const ModuleNodePair&), override); |
| 26 | IMPLEMENT_CONST_MOCK1(leafTypeName); |
| 27 | IMPLEMENT_CONST_MOCK1(isModule); |
| 28 | IMPLEMENT_CONST_MOCK1(leafrefPath); |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 29 | MAKE_CONST_MOCK3(listHasKey, bool(const schemaPath_& location, const ModuleNodePair& node, const std::string& key), override); |
| 30 | MAKE_CONST_MOCK2(listHasKey, bool(const schemaPath_& listPath, const std::string& key), override); |
Václav Kubernát | 8208687 | 2020-04-29 01:09:50 +0200 | [diff] [blame] | 31 | IMPLEMENT_CONST_MOCK1(leafIsKey); |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 32 | MAKE_CONST_MOCK2(listKeys, const std::set<std::string>(const schemaPath_& location, const ModuleNodePair& node), override); |
| 33 | MAKE_CONST_MOCK1(listKeys, const std::set<std::string>(const schemaPath_& listPath), override); |
Václav Kubernát | 8208687 | 2020-04-29 01:09:50 +0200 | [diff] [blame] | 34 | MAKE_CONST_MOCK1(nodeType, yang::NodeTypes(const std::string&), override); |
| 35 | MAKE_CONST_MOCK2(nodeType, yang::NodeTypes(const schemaPath_&, const ModuleNodePair&), override); |
| 36 | IMPLEMENT_CONST_MOCK1(status); |
| 37 | }; |
| 38 | |
| 39 | TEST_CASE("ls interpreter") |
| 40 | { |
| 41 | auto schema = std::make_shared<MockSchema>(); |
| 42 | Parser parser(schema); |
| 43 | |
| 44 | boost::variant<dataPath_, schemaPath_, module_> expectedPath; |
| 45 | boost::optional<boost::variant<dataPath_, schemaPath_, module_>> lsArg{boost::none}; |
| 46 | SECTION("cwd: /") |
| 47 | { |
| 48 | SECTION("arg: <none>") |
| 49 | { |
| 50 | expectedPath = schemaPath_{}; |
| 51 | } |
| 52 | |
| 53 | SECTION("arg: example:a") |
| 54 | { |
| 55 | lsArg = dataPath_{Scope::Relative, {{module_{"example"}, container_{"a"}}}}; |
| 56 | expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}}; |
| 57 | } |
| 58 | |
| 59 | SECTION("arg: example:list") |
| 60 | { |
| 61 | lsArg = dataPath_{Scope::Relative, {{module_{"example"}, list_{"list"}}}}; |
| 62 | expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}}; |
| 63 | } |
| 64 | |
| 65 | SECTION("arg: /example:a") |
| 66 | { |
| 67 | lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}}; |
| 68 | expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}}; |
| 69 | } |
| 70 | |
| 71 | SECTION("arg: /example:list") |
| 72 | { |
| 73 | lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}}; |
| 74 | expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}}; |
| 75 | } |
| 76 | |
| 77 | SECTION("arg example:*") |
| 78 | { |
| 79 | lsArg = module_{"example"}; |
| 80 | expectedPath = module_{"example"}; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | SECTION("cwd: /example:a") |
| 85 | { |
| 86 | parser.changeNode({Scope::Relative, {{module_{"example"}, container_{"a"}}}}); |
| 87 | |
| 88 | SECTION("arg: <none>") |
| 89 | { |
| 90 | expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}}; |
| 91 | } |
| 92 | |
| 93 | SECTION("arg: example:a2") |
| 94 | { |
| 95 | lsArg = dataPath_{Scope::Relative, {{container_{"a2"}}}}; |
| 96 | expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}, {container_{"a2"}}}}; |
| 97 | } |
| 98 | |
| 99 | SECTION("arg: example:listInCont") |
| 100 | { |
| 101 | lsArg = dataPath_{Scope::Relative, {{list_{"listInCont"}}}}; |
| 102 | expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}, {list_{"listInCont"}}}}; |
| 103 | } |
| 104 | |
| 105 | SECTION("arg: /example:a") |
| 106 | { |
| 107 | lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}}; |
| 108 | expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}}; |
| 109 | } |
| 110 | |
| 111 | SECTION("arg: /example:list") |
| 112 | { |
| 113 | lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}}; |
| 114 | expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}}; |
| 115 | } |
| 116 | } |
| 117 | SECTION("cwd: /example:list") |
| 118 | { |
| 119 | parser.changeNode({Scope::Relative, {{module_{"example"}, list_{"list"}}}}); |
| 120 | |
| 121 | SECTION("arg: <none>") |
| 122 | { |
| 123 | expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}}; |
| 124 | } |
| 125 | |
| 126 | SECTION("arg: example:contInList") |
| 127 | { |
| 128 | lsArg = dataPath_{Scope::Relative, {{container_{"contInList"}}}}; |
| 129 | expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}, {container_{"contInList"}}}}; |
| 130 | } |
| 131 | |
| 132 | SECTION("arg: /example:a") |
| 133 | { |
| 134 | lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}}; |
| 135 | expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}}; |
| 136 | } |
| 137 | |
| 138 | SECTION("arg: /example:list") |
| 139 | { |
| 140 | lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}}; |
| 141 | expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}}; |
| 142 | } |
| 143 | |
| 144 | SECTION("arg example:*") |
| 145 | { |
| 146 | lsArg = module_{"example"}; |
| 147 | expectedPath = module_{"example"}; |
| 148 | } |
| 149 | } |
| 150 | MockDatastoreAccess datastore; |
| 151 | REQUIRE_CALL(datastore, schema()).RETURN(schema); |
| 152 | ls_ ls; |
| 153 | ls.m_path = lsArg; |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 154 | REQUIRE_CALL(*schema, availableNodes(expectedPath, Recursion::NonRecursive)).RETURN(std::set<ModuleNodePair>{}); |
Václav Kubernát | 8208687 | 2020-04-29 01:09:50 +0200 | [diff] [blame] | 155 | Interpreter(parser, datastore)(ls); |
| 156 | } |