blob: 2ae0b7cee0f27d4eb167683483389203704ab24c [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 {
48 expectedPath = schemaPath_{};
49 }
50
51 SECTION("arg: example:a")
52 {
53 lsArg = dataPath_{Scope::Relative, {{module_{"example"}, container_{"a"}}}};
54 expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
55 }
56
57 SECTION("arg: example:list")
58 {
59 lsArg = dataPath_{Scope::Relative, {{module_{"example"}, list_{"list"}}}};
60 expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
61 }
62
63 SECTION("arg: /example:a")
64 {
65 lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
66 expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
67 }
68
69 SECTION("arg: /example:list")
70 {
71 lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
72 expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
73 }
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 {
88 expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
89 }
90
91 SECTION("arg: example:a2")
92 {
93 lsArg = dataPath_{Scope::Relative, {{container_{"a2"}}}};
94 expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}, {container_{"a2"}}}};
95 }
96
97 SECTION("arg: example:listInCont")
98 {
99 lsArg = dataPath_{Scope::Relative, {{list_{"listInCont"}}}};
100 expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}, {list_{"listInCont"}}}};
101 }
102
103 SECTION("arg: /example:a")
104 {
105 lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
106 expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
107 }
108
109 SECTION("arg: /example:list")
110 {
111 lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
112 expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
113 }
114 }
115 SECTION("cwd: /example:list")
116 {
117 parser.changeNode({Scope::Relative, {{module_{"example"}, list_{"list"}}}});
118
119 SECTION("arg: <none>")
120 {
121 expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
122 }
123
124 SECTION("arg: example:contInList")
125 {
126 lsArg = dataPath_{Scope::Relative, {{container_{"contInList"}}}};
127 expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}, {container_{"contInList"}}}};
128 }
129
130 SECTION("arg: /example:a")
131 {
132 lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
133 expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, container_{"a"}}}};
134 }
135
136 SECTION("arg: /example:list")
137 {
138 lsArg = dataPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
139 expectedPath = schemaPath_{Scope::Absolute, {{module_{"example"}, list_{"list"}}}};
140 }
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}