blob: 9ae2d97260ef3d8bb2b1cbeded4427fde6843fac [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"
12#include "schema.hpp"
13
14TEST_CASE("leaf editing")
15{
Václav Kubernáta10d8b62018-06-13 17:42:46 +020016 auto schema = std::make_shared<Schema>();
17 schema->addContainer("", "contA");
18 schema->addLeaf("", "leafString", yang::LeafDataTypes::String);
19 schema->addLeaf("", "leafDecimal", yang::LeafDataTypes::Decimal);
20 schema->addLeaf("", "leafBool", yang::LeafDataTypes::Bool);
21 schema->addLeaf("", "leafInt", yang::LeafDataTypes::Int);
22 schema->addLeaf("", "leafUint", yang::LeafDataTypes::Uint);
23 schema->addLeafEnum("", "leafEnum", {"lol", "data", "coze"});
24 schema->addLeaf("contA", "leafInCont", yang::LeafDataTypes::String);
25 schema->addList("", "list", {"number"});
26 schema->addLeaf("list", "leafInList", yang::LeafDataTypes::String);
Václav Kubernát07204242018-06-04 18:12:09 +020027 Parser parser(schema);
28 std::string input;
29 std::ostringstream errorStream;
30
31 SECTION("valid input")
32 {
33 set_ expected;
34
Václav Kubernátebca2552018-06-08 19:06:02 +020035 SECTION("set leafString some_data")
Václav Kubernát07204242018-06-04 18:12:09 +020036 {
Václav Kubernátebca2552018-06-08 19:06:02 +020037 input = "set leafString some_data";
38 expected.m_path.m_nodes.push_back(leaf_("leafString"));
39 expected.m_data = std::string("some_data");
Václav Kubernát07204242018-06-04 18:12:09 +020040 }
41
42 SECTION("set contA/leafInCont more_data")
43 {
44 input = "set contA/leafInCont more_data";
45 expected.m_path.m_nodes.push_back(container_("contA"));
46 expected.m_path.m_nodes.push_back(leaf_("leafInCont"));
Václav Kubernátebca2552018-06-08 19:06:02 +020047 expected.m_data = std::string("more_data");
48 }
49
50 SECTION("set list[number=1]/leafInList another_data")
51 {
52 input = "set list[number=1]/leafInList another_data";
53 auto keys = std::map<std::string, std::string>{
54 {"number", "1"}};
55 expected.m_path.m_nodes.push_back(listElement_("list", keys));
56 expected.m_path.m_nodes.push_back(leaf_("leafInList"));
57 expected.m_data = std::string("another_data");
58 }
59
60 SECTION("data types")
61 {
62 SECTION("string")
63 {
64 input = "set leafString somedata";
65 expected.m_path.m_nodes.push_back(leaf_("leafString"));
66 expected.m_data = std::string("somedata");
67 }
68
69 SECTION("int")
70 {
71 input = "set leafInt 2";
72 expected.m_path.m_nodes.push_back(leaf_("leafInt"));
73 expected.m_data = 2;
74 }
75
76 SECTION("decimal")
77 {
78 input = "set leafDecimal 3.14159";
79 expected.m_path.m_nodes.push_back(leaf_("leafDecimal"));
80 expected.m_data = 3.14159;
81 }
82
83 SECTION("enum")
84 {
85 input = "set leafEnum coze";
86 expected.m_path.m_nodes.push_back(leaf_("leafEnum"));
87 expected.m_data = enum_("coze");
88 }
89
90 SECTION("bool")
91 {
92 input = "set leafBool true";
93 expected.m_path.m_nodes.push_back(leaf_("leafBool"));
94 expected.m_data = true;
95 }
Václav Kubernát07204242018-06-04 18:12:09 +020096 }
97
98 command_ command = parser.parseCommand(input, errorStream);
99 REQUIRE(command.type() == typeid(set_));
100 REQUIRE(boost::get<set_>(command) == expected);
101 }
102
103 SECTION("invalid input")
104 {
105 SECTION("missing space between a command and its arguments")
106 {
107 SECTION("setleaf some_data")
108 {
109 input = "setleaf some_data";
110 }
111 }
112
113 SECTION("missing space between arguments")
114 {
115 SECTION("set leaflol")
116 {
117 input = "set leaflol";
118 }
119 }
120
121 SECTION("non-leaf identifiers")
122 {
123 SECTION("set nonexistent blabla")
124 {
125 input = "set nonexistent blabla";
126 }
127
128 SECTION("set contA abde")
129 {
130 input = "set contA abde";
131 }
132 }
133
Václav Kubernátebca2552018-06-08 19:06:02 +0200134 SECTION("wrong types")
135 {
136 SECTION("set leafBool blabla")
137 {
138 input = "set leafBool blabla";
139 }
140 SECTION("set leafUint blabla")
141 {
142 input = "set leafUint blabla";
143 }
144 SECTION("set leafInt blabla")
145 {
146 input = "set leafInt blabla";
147 }
148 SECTION("set leafEnum blabla")
149 {
150 input = "set leafEnum blabla";
151 }
152 }
153
Václav Kubernát07204242018-06-04 18:12:09 +0200154 REQUIRE_THROWS(parser.parseCommand(input, errorStream));
155 }
156}