blob: 5a43ce9dd5b96e0a0b20397a3ff8459343d1ede5 [file] [log] [blame]
Václav Kubernátb61336d2018-05-28 17:35:03 +02001
2/*
3 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
4 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
5 *
6 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
7 *
8*/
9
10#include "trompeloeil_catch.h"
Václav Kubernát24df80e2018-06-06 15:18:03 +020011#include "ast_commands.hpp"
Václav Kubernátb61336d2018-05-28 17:35:03 +020012#include "parser.hpp"
Václav Kubernátbddbb172018-06-13 16:27:39 +020013#include "static_schema.hpp"
Václav Kubernátb61336d2018-05-28 17:35:03 +020014
15TEST_CASE("presence containers")
16{
Václav Kubernátbddbb172018-06-13 16:27:39 +020017 auto schema = std::make_shared<StaticSchema>();
Václav Kubernát744f57f2018-06-29 22:46:26 +020018 schema->addModule("mod");
19 schema->addContainer("", "mod:a", yang::ContainerTraits::Presence);
20 schema->addContainer("", "mod:b");
21 schema->addContainer("mod:a", "mod:a2");
22 schema->addContainer("mod:a/mod:a2", "mod:a3", yang::ContainerTraits::Presence);
23 schema->addContainer("mod:b", "mod:b2", yang::ContainerTraits::Presence);
24 schema->addList("", "mod:list", {"quote"});
25 schema->addContainer("mod:list", "mod:contInList", yang::ContainerTraits::Presence);
Václav Kubernátb61336d2018-05-28 17:35:03 +020026 Parser parser(schema);
27 std::string input;
28 std::ostringstream errorStream;
29
30 SECTION("valid input")
31 {
32 path_ expectedPath;
33
Václav Kubernát744f57f2018-06-29 22:46:26 +020034 SECTION("mod:a")
Václav Kubernátb61336d2018-05-28 17:35:03 +020035 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020036 input = "mod:a";
37 expectedPath.m_nodes = {{module_{"mod"}, {container_("a")}}};
Václav Kubernátb61336d2018-05-28 17:35:03 +020038 }
39
Václav Kubernát744f57f2018-06-29 22:46:26 +020040 SECTION("mod:b/b2")
Václav Kubernátb61336d2018-05-28 17:35:03 +020041 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020042 input = "mod:b/b2";
43 expectedPath.m_nodes = {{{module_{"mod"}}, container_("b")}, {container_("b2")}};
Václav Kubernátb61336d2018-05-28 17:35:03 +020044 }
45
Václav Kubernát744f57f2018-06-29 22:46:26 +020046 SECTION("mod:a/a2/a3")
Václav Kubernátb61336d2018-05-28 17:35:03 +020047 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020048 input = "mod:a/a2/a3";
49 expectedPath.m_nodes = {{{module_{"mod"}}, container_("a")}, {container_("a2")}, {container_("a3")}};
Václav Kubernátb61336d2018-05-28 17:35:03 +020050 }
51
Václav Kubernát89728d82018-09-13 16:28:28 +020052 SECTION("mod:list[quote='lol']/contInList")
Václav Kubernátb61336d2018-05-28 17:35:03 +020053 {
Václav Kubernát89728d82018-09-13 16:28:28 +020054 input = "mod:list[quote='lol']/contInList";
Václav Kubernátb61336d2018-05-28 17:35:03 +020055 auto keys = std::map<std::string, std::string>{
56 {"quote", "lol"}};
Václav Kubernát744f57f2018-06-29 22:46:26 +020057 expectedPath.m_nodes = {{{module_{"mod"}}, listElement_("list", keys)}, {container_("contInList")}};
Václav Kubernátb61336d2018-05-28 17:35:03 +020058 }
59
Jan Kundrát3081afa2018-09-14 16:53:43 +020060 SECTION("mod:list[quote='double\"quote']/contInList")
61 {
62 input = "mod:list[quote='double\"quote']/contInList";
63 auto keys = std::map<std::string, std::string>{
64 {"quote", "double\"quote"}};
65 expectedPath.m_nodes = {{{module_{"mod"}}, listElement_("list", keys)}, {container_("contInList")}};
66 }
67
68 SECTION("mod:list[quote=\"single'quote\"]/contInList")
69 {
70 input = "mod:list[quote=\"single'quote\"]/contInList";
71 auto keys = std::map<std::string, std::string>{
72 {"quote", "single'quote"}};
73 expectedPath.m_nodes = {{{module_{"mod"}}, listElement_("list", keys)}, {container_("contInList")}};
74 }
75
Václav Kubernátb61336d2018-05-28 17:35:03 +020076 create_ expectedCreate;
77 expectedCreate.m_path = expectedPath;
78 command_ commandCreate = parser.parseCommand("create " + input, errorStream);
79 REQUIRE(commandCreate.type() == typeid(create_));
80 create_ create = boost::get<create_>(commandCreate);
81 REQUIRE(create == expectedCreate);
82
Jan Kundrát2a8f4332018-09-14 17:05:31 +020083 REQUIRE(pathToDataString(create.m_path) == input);
84
Václav Kubernátb61336d2018-05-28 17:35:03 +020085 delete_ expectedDelete;
86 expectedDelete.m_path = expectedPath;
87 command_ commandDelete = parser.parseCommand("delete " + input, errorStream);
88 REQUIRE(commandDelete.type() == typeid(delete_));
89 delete_ delet = boost::get<delete_>(commandDelete);
90 REQUIRE(delet == expectedDelete);
91 }
92 SECTION("invalid input")
93 {
94 SECTION("c")
95 {
96 input = "c";
97 }
98
99 SECTION("a/a2")
100 {
101 input = "a/a2";
102 }
103
Václav Kubernát89728d82018-09-13 16:28:28 +0200104 SECTION("list[quote='lol']")
Václav Kubernátb61336d2018-05-28 17:35:03 +0200105 {
Václav Kubernát89728d82018-09-13 16:28:28 +0200106 input = "list[quote='lol']";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200107 }
108
Václav Kubernátb7a279d2018-10-04 15:27:50 +0200109 REQUIRE_THROWS_AS(parser.parseCommand("create " + input, errorStream), InvalidCommandException&);
110 REQUIRE_THROWS_AS(parser.parseCommand("delete " + input, errorStream), InvalidCommandException&);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200111 }
112}