blob: d8f21dbf3c2aed072327601b0e94bef2415fa8fc [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);
24 schema->addLeafEnum("", "mod:leafEnum", {"lol", "data", "coze"});
25 schema->addLeaf("mod:contA", "mod:leafInCont", yang::LeafDataTypes::String);
26 schema->addList("", "mod:list", {"number"});
27 schema->addLeaf("mod:list", "mod:leafInList", yang::LeafDataTypes::String);
Václav Kubernát07204242018-06-04 18:12:09 +020028 Parser parser(schema);
29 std::string input;
30 std::ostringstream errorStream;
31
32 SECTION("valid input")
33 {
34 set_ expected;
35
Václav Kubernátebca2552018-06-08 19:06:02 +020036 SECTION("set leafString some_data")
Václav Kubernát07204242018-06-04 18:12:09 +020037 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020038 input = "set mod:leafString some_data";
39 expected.m_path.m_nodes.push_back(node_{module_{"mod"}, leaf_("leafString")});
Václav Kubernátebca2552018-06-08 19:06:02 +020040 expected.m_data = std::string("some_data");
Václav Kubernát07204242018-06-04 18:12:09 +020041 }
42
Václav Kubernát744f57f2018-06-29 22:46:26 +020043 SECTION("set mod:contA/leafInCont more_data")
Václav Kubernát07204242018-06-04 18:12:09 +020044 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020045 input = "set mod:contA/leafInCont more_data";
46 expected.m_path.m_nodes.push_back(node_{module_{"mod"}, container_("contA")});
47 expected.m_path.m_nodes.push_back(node_{leaf_("leafInCont")});
Václav Kubernátebca2552018-06-08 19:06:02 +020048 expected.m_data = std::string("more_data");
49 }
50
51 SECTION("set list[number=1]/leafInList another_data")
52 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020053 input = "set mod:list[number=1]/leafInList another_data";
Václav Kubernátebca2552018-06-08 19:06:02 +020054 auto keys = std::map<std::string, std::string>{
55 {"number", "1"}};
Václav Kubernát744f57f2018-06-29 22:46:26 +020056 expected.m_path.m_nodes.push_back(node_{module_{"mod"}, listElement_("list", keys)});
57 expected.m_path.m_nodes.push_back(node_{leaf_("leafInList")});
Václav Kubernátebca2552018-06-08 19:06:02 +020058 expected.m_data = std::string("another_data");
59 }
60
61 SECTION("data types")
62 {
63 SECTION("string")
64 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020065 input = "set mod:leafString somedata";
66 expected.m_path.m_nodes.push_back(node_{module_{"mod"}, leaf_("leafString")});
Václav Kubernátebca2552018-06-08 19:06:02 +020067 expected.m_data = std::string("somedata");
68 }
69
70 SECTION("int")
71 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020072 input = "set mod:leafInt 2";
73 expected.m_path.m_nodes.push_back(node_{module_{"mod"}, leaf_("leafInt")});
Václav Kubernátebca2552018-06-08 19:06:02 +020074 expected.m_data = 2;
75 }
76
77 SECTION("decimal")
78 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020079 input = "set mod:leafDecimal 3.14159";
80 expected.m_path.m_nodes.push_back(node_{module_{"mod"}, leaf_("leafDecimal")});
Václav Kubernátebca2552018-06-08 19:06:02 +020081 expected.m_data = 3.14159;
82 }
83
84 SECTION("enum")
85 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020086 input = "set mod:leafEnum coze";
87 expected.m_path.m_nodes.push_back(node_{module_{"mod"}, leaf_("leafEnum")});
Václav Kubernátebca2552018-06-08 19:06:02 +020088 expected.m_data = enum_("coze");
89 }
90
91 SECTION("bool")
92 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020093 input = "set mod:leafBool true";
94 expected.m_path.m_nodes.push_back(node_{module_{"mod"}, leaf_("leafBool")});
Václav Kubernátebca2552018-06-08 19:06:02 +020095 expected.m_data = true;
96 }
Václav Kubernát07204242018-06-04 18:12:09 +020097 }
98
99 command_ command = parser.parseCommand(input, errorStream);
100 REQUIRE(command.type() == typeid(set_));
101 REQUIRE(boost::get<set_>(command) == expected);
102 }
103
104 SECTION("invalid input")
105 {
106 SECTION("missing space between a command and its arguments")
107 {
108 SECTION("setleaf some_data")
109 {
110 input = "setleaf some_data";
111 }
112 }
113
114 SECTION("missing space between arguments")
115 {
116 SECTION("set leaflol")
117 {
118 input = "set leaflol";
119 }
120 }
121
122 SECTION("non-leaf identifiers")
123 {
124 SECTION("set nonexistent blabla")
125 {
126 input = "set nonexistent blabla";
127 }
128
129 SECTION("set contA abde")
130 {
131 input = "set contA abde";
132 }
133 }
134
Václav Kubernátebca2552018-06-08 19:06:02 +0200135 SECTION("wrong types")
136 {
137 SECTION("set leafBool blabla")
138 {
139 input = "set leafBool blabla";
140 }
141 SECTION("set leafUint blabla")
142 {
143 input = "set leafUint blabla";
144 }
145 SECTION("set leafInt blabla")
146 {
147 input = "set leafInt blabla";
148 }
149 SECTION("set leafEnum blabla")
150 {
151 input = "set leafEnum blabla";
152 }
153 }
154
Václav Kubernát07204242018-06-04 18:12:09 +0200155 REQUIRE_THROWS(parser.parseCommand(input, errorStream));
156 }
157}