blob: b3d927bb4a3dc651e4a23109b268c8909e6df27b [file] [log] [blame]
Václav Kubernát82086872020-04-29 01:09:50 +02001/*
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
18class MockSchema : public trompeloeil::mock_interface<Schema> {
19public:
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át912b9492020-05-29 02:03:40 +020029 IMPLEMENT_CONST_MOCK2(listHasKey);
Václav Kubernát82086872020-04-29 01:09:50 +020030 IMPLEMENT_CONST_MOCK1(leafIsKey);
Václav Kubernát912b9492020-05-29 02:03:40 +020031 IMPLEMENT_CONST_MOCK1(listKeys);
Václav Kubernát82086872020-04-29 01:09:50 +020032 MAKE_CONST_MOCK1(nodeType, yang::NodeTypes(const std::string&), override);
33 MAKE_CONST_MOCK2(nodeType, yang::NodeTypes(const schemaPath_&, const ModuleNodePair&), override);
34 IMPLEMENT_CONST_MOCK1(status);
35};
36
37TEST_CASE("ls interpreter")
38{
39 auto schema = std::make_shared<MockSchema>();
40 Parser parser(schema);
41
42 boost::variant<dataPath_, schemaPath_, module_> expectedPath;
43 boost::optional<boost::variant<dataPath_, schemaPath_, module_>> lsArg{boost::none};
44 SECTION("cwd: /")
45 {
46 SECTION("arg: <none>")
47 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020048 expectedPath = dataPath_{};
Václav Kubernát82086872020-04-29 01:09:50 +020049 }
50
51 SECTION("arg: example:a")
52 {
53 lsArg = dataPath_{Scope::Relative, {{module_{"example"}, container_{"a"}}}};
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020054 expectedPath = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
Václav Kubernát82086872020-04-29 01:09:50 +020055 }
56
57 SECTION("arg: example:list")
58 {
59 lsArg = dataPath_{Scope::Relative, {{module_{"example"}, list_{"list"}}}};
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020060 expectedPath = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
Václav Kubernát82086872020-04-29 01:09:50 +020061 }
62
63 SECTION("arg: /example:a")
64 {
65 lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020066 expectedPath = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
Václav Kubernát82086872020-04-29 01:09:50 +020067 }
68
69 SECTION("arg: /example:list")
70 {
71 lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020072 expectedPath = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
Václav Kubernát82086872020-04-29 01:09:50 +020073 }
74
75 SECTION("arg example:*")
76 {
77 lsArg = module_{"example"};
78 expectedPath = module_{"example"};
79 }
80 }
81
82 SECTION("cwd: /example:a")
83 {
84 parser.changeNode({Scope::Relative, {{module_{"example"}, container_{"a"}}}});
85
86 SECTION("arg: <none>")
87 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020088 expectedPath = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
Václav Kubernát82086872020-04-29 01:09:50 +020089 }
90
91 SECTION("arg: example:a2")
92 {
93 lsArg = dataPath_{Scope::Relative, {{container_{"a2"}}}};
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020094 expectedPath = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}, {container_{"a2"}}}};
Václav Kubernát82086872020-04-29 01:09:50 +020095 }
96
97 SECTION("arg: example:listInCont")
98 {
99 lsArg = dataPath_{Scope::Relative, {{list_{"listInCont"}}}};
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200100 expectedPath = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}, {list_{"listInCont"}}}};
Václav Kubernát82086872020-04-29 01:09:50 +0200101 }
102
103 SECTION("arg: /example:a")
104 {
105 lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200106 expectedPath = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
Václav Kubernát82086872020-04-29 01:09:50 +0200107 }
108
109 SECTION("arg: /example:list")
110 {
111 lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200112 expectedPath = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
Václav Kubernát82086872020-04-29 01:09:50 +0200113 }
114 }
115 SECTION("cwd: /example:list")
116 {
117 parser.changeNode({Scope::Relative, {{module_{"example"}, list_{"list"}}}});
118
119 SECTION("arg: <none>")
120 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200121 expectedPath = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
Václav Kubernát82086872020-04-29 01:09:50 +0200122 }
123
124 SECTION("arg: example:contInList")
125 {
126 lsArg = dataPath_{Scope::Relative, {{container_{"contInList"}}}};
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200127 expectedPath = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}, {container_{"contInList"}}}};
Václav Kubernát82086872020-04-29 01:09:50 +0200128 }
129
130 SECTION("arg: /example:a")
131 {
132 lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200133 expectedPath = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
Václav Kubernát82086872020-04-29 01:09:50 +0200134 }
135
136 SECTION("arg: /example:list")
137 {
138 lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200139 expectedPath = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
Václav Kubernát82086872020-04-29 01:09:50 +0200140 }
141
142 SECTION("arg example:*")
143 {
144 lsArg = module_{"example"};
145 expectedPath = module_{"example"};
146 }
147 }
148 MockDatastoreAccess datastore;
149 REQUIRE_CALL(datastore, schema()).RETURN(schema);
150 ls_ ls;
151 ls.m_path = lsArg;
Václav Kubernát95b08872020-04-28 01:04:17 +0200152 REQUIRE_CALL(*schema, availableNodes(expectedPath, Recursion::NonRecursive)).RETURN(std::set<ModuleNodePair>{});
Václav Kubernát82086872020-04-29 01:09:50 +0200153 Interpreter(parser, datastore)(ls);
154}