blob: f18af1c0a7084143736ee3054375907837f6696a [file] [log] [blame]
Václav Kubernát07204242018-06-04 18:12:09 +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 "trompeloeil_catch.h"
Václav Kubernát24df80e2018-06-06 15:18:03 +020010#include "ast_commands.hpp"
Václav Kubernát07204242018-06-04 18:12:09 +020011#include "parser.hpp"
Václav Kubernátbddbb172018-06-13 16:27:39 +020012#include "static_schema.hpp"
Václav Kubernát07204242018-06-04 18:12:09 +020013
14TEST_CASE("leaf editing")
15{
Václav Kubernátbddbb172018-06-13 16:27:39 +020016 auto schema = std::make_shared<StaticSchema>();
Václav Kubernát744f57f2018-06-29 22:46:26 +020017 schema->addModule("mod");
18 schema->addContainer("", "mod:contA");
19 schema->addLeaf("", "mod:leafString", yang::LeafDataTypes::String);
20 schema->addLeaf("", "mod:leafDecimal", yang::LeafDataTypes::Decimal);
21 schema->addLeaf("", "mod:leafBool", yang::LeafDataTypes::Bool);
22 schema->addLeaf("", "mod:leafInt", yang::LeafDataTypes::Int);
23 schema->addLeaf("", "mod:leafUint", yang::LeafDataTypes::Uint);
Václav Kubernátab538992019-03-06 15:30:50 +010024 schema->addLeaf("", "mod:leafBinary", yang::LeafDataTypes::Binary);
Václav Kubernát744f57f2018-06-29 22:46:26 +020025 schema->addLeafEnum("", "mod:leafEnum", {"lol", "data", "coze"});
26 schema->addLeaf("mod:contA", "mod:leafInCont", yang::LeafDataTypes::String);
27 schema->addList("", "mod:list", {"number"});
28 schema->addLeaf("mod:list", "mod:leafInList", yang::LeafDataTypes::String);
Václav Kubernát07204242018-06-04 18:12:09 +020029 Parser parser(schema);
30 std::string input;
31 std::ostringstream errorStream;
32
33 SECTION("valid input")
34 {
35 set_ expected;
36
Václav Kubernátab538992019-03-06 15:30:50 +010037 SECTION("set mod:leafString some_data")
Václav Kubernát07204242018-06-04 18:12:09 +020038 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020039 input = "set mod:leafString some_data";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020040 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")});
Václav Kubernátebca2552018-06-08 19:06:02 +020041 expected.m_data = std::string("some_data");
Václav Kubernát07204242018-06-04 18:12:09 +020042 }
43
Václav Kubernát744f57f2018-06-29 22:46:26 +020044 SECTION("set mod:contA/leafInCont more_data")
Václav Kubernát07204242018-06-04 18:12:09 +020045 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020046 input = "set mod:contA/leafInCont more_data";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020047 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")});
48 expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")});
Václav Kubernátebca2552018-06-08 19:06:02 +020049 expected.m_data = std::string("more_data");
50 }
51
Václav Kubernátab538992019-03-06 15:30:50 +010052 SECTION("set mod:list[number=1]/leafInList another_data")
Václav Kubernátebca2552018-06-08 19:06:02 +020053 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020054 input = "set mod:list[number=1]/leafInList another_data";
Václav Kubernátebca2552018-06-08 19:06:02 +020055 auto keys = std::map<std::string, std::string>{
56 {"number", "1"}};
Václav Kubernát2eaceb82018-10-08 19:56:30 +020057 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)});
58 expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInList")});
Václav Kubernátebca2552018-06-08 19:06:02 +020059 expected.m_data = std::string("another_data");
60 }
61
62 SECTION("data types")
63 {
64 SECTION("string")
65 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020066 input = "set mod:leafString somedata";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020067 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")});
Václav Kubernátebca2552018-06-08 19:06:02 +020068 expected.m_data = std::string("somedata");
69 }
70
71 SECTION("int")
72 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020073 input = "set mod:leafInt 2";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020074 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt")});
Václav Kubernátebca2552018-06-08 19:06:02 +020075 expected.m_data = 2;
76 }
77
78 SECTION("decimal")
79 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020080 input = "set mod:leafDecimal 3.14159";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020081 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafDecimal")});
Václav Kubernátebca2552018-06-08 19:06:02 +020082 expected.m_data = 3.14159;
83 }
84
85 SECTION("enum")
86 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020087 input = "set mod:leafEnum coze";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020088 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafEnum")});
Václav Kubernátebca2552018-06-08 19:06:02 +020089 expected.m_data = enum_("coze");
90 }
91
92 SECTION("bool")
93 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020094 input = "set mod:leafBool true";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020095 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafBool")});
Václav Kubernátebca2552018-06-08 19:06:02 +020096 expected.m_data = true;
97 }
Václav Kubernátab538992019-03-06 15:30:50 +010098
99 SECTION("binary")
100 {
101 SECTION("zero ending '='")
102 {
103 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++";
104 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++"};
105 }
106
107 SECTION("one ending '='")
108 {
109 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++=";
110 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++="};
111 }
112
113 SECTION("two ending '='")
114 {
115 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++==";
116 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++=="};
117 }
118 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafBinary")});
119 }
Václav Kubernát07204242018-06-04 18:12:09 +0200120 }
121
122 command_ command = parser.parseCommand(input, errorStream);
123 REQUIRE(command.type() == typeid(set_));
124 REQUIRE(boost::get<set_>(command) == expected);
125 }
126
127 SECTION("invalid input")
128 {
129 SECTION("missing space between a command and its arguments")
130 {
131 SECTION("setleaf some_data")
132 {
133 input = "setleaf some_data";
134 }
135 }
136
137 SECTION("missing space between arguments")
138 {
139 SECTION("set leaflol")
140 {
141 input = "set leaflol";
142 }
143 }
144
145 SECTION("non-leaf identifiers")
146 {
147 SECTION("set nonexistent blabla")
148 {
149 input = "set nonexistent blabla";
150 }
151
152 SECTION("set contA abde")
153 {
154 input = "set contA abde";
155 }
156 }
157
Václav Kubernátebca2552018-06-08 19:06:02 +0200158 SECTION("wrong types")
159 {
160 SECTION("set leafBool blabla")
161 {
162 input = "set leafBool blabla";
163 }
164 SECTION("set leafUint blabla")
165 {
166 input = "set leafUint blabla";
167 }
168 SECTION("set leafInt blabla")
169 {
170 input = "set leafInt blabla";
171 }
172 SECTION("set leafEnum blabla")
173 {
174 input = "set leafEnum blabla";
175 }
176 }
177
Václav Kubernátab538992019-03-06 15:30:50 +0100178 SECTION("wrong base64 strings")
179 {
180 SECTION("invalid character")
181 input = "set leafBinary dbahj-";
182 SECTION("equal sign in the middle")
183 input = "set leafBinary db=ahj";
184 }
185
Jan Kundrátc381e632019-03-14 13:39:11 +0100186 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát07204242018-06-04 18:12:09 +0200187 }
188}