blob: e009c4117e795252bef2938e34aa825134002410 [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
Václav Kubernát26b56082020-02-03 18:28:56 +010010#include "trompeloeil_doctest.hpp"
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");
Václav Kubernátefcac932020-01-10 15:26:32 +010019 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"});
Václav Kubernát3a99f002020-03-31 02:27:41 +020025 schema->addLeaf("/mod:list", "mod:quote", yang::String{});
Václav Kubernátefcac932020-01-10 15:26:32 +010026 schema->addContainer("/mod:list", "mod:contInList", yang::ContainerTraits::Presence);
Václav Kubernátb61336d2018-05-28 17:35:03 +020027 Parser parser(schema);
28 std::string input;
29 std::ostringstream errorStream;
30
31 SECTION("valid input")
32 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +020033 dataPath_ expectedPath;
Václav Kubernátb61336d2018-05-28 17:35:03 +020034
Václav Kubernát744f57f2018-06-29 22:46:26 +020035 SECTION("mod:a")
Václav Kubernátb61336d2018-05-28 17:35:03 +020036 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020037 input = "mod:a";
38 expectedPath.m_nodes = {{module_{"mod"}, {container_("a")}}};
Václav Kubernátb61336d2018-05-28 17:35:03 +020039 }
40
Václav Kubernát744f57f2018-06-29 22:46:26 +020041 SECTION("mod:b/b2")
Václav Kubernátb61336d2018-05-28 17:35:03 +020042 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020043 input = "mod:b/b2";
44 expectedPath.m_nodes = {{{module_{"mod"}}, container_("b")}, {container_("b2")}};
Václav Kubernátb61336d2018-05-28 17:35:03 +020045 }
46
Václav Kubernát744f57f2018-06-29 22:46:26 +020047 SECTION("mod:a/a2/a3")
Václav Kubernátb61336d2018-05-28 17:35:03 +020048 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020049 input = "mod:a/a2/a3";
50 expectedPath.m_nodes = {{{module_{"mod"}}, container_("a")}, {container_("a2")}, {container_("a3")}};
Václav Kubernátb61336d2018-05-28 17:35:03 +020051 }
52
Václav Kubernát89728d82018-09-13 16:28:28 +020053 SECTION("mod:list[quote='lol']/contInList")
Václav Kubernátb61336d2018-05-28 17:35:03 +020054 {
Václav Kubernát89728d82018-09-13 16:28:28 +020055 input = "mod:list[quote='lol']/contInList";
Václav Kubernátc15fe822020-06-04 11:28:39 +020056 auto keys = ListInstance {
Václav Kubernát7707cae2020-01-16 12:04:53 +010057 {"quote", std::string{"lol"}}};
Václav Kubernát744f57f2018-06-29 22:46:26 +020058 expectedPath.m_nodes = {{{module_{"mod"}}, listElement_("list", keys)}, {container_("contInList")}};
Václav Kubernátb61336d2018-05-28 17:35:03 +020059 }
60
Jan Kundrát3081afa2018-09-14 16:53:43 +020061 SECTION("mod:list[quote='double\"quote']/contInList")
62 {
63 input = "mod:list[quote='double\"quote']/contInList";
Václav Kubernátc15fe822020-06-04 11:28:39 +020064 auto keys = ListInstance {
Václav Kubernát7707cae2020-01-16 12:04:53 +010065 {"quote", std::string{"double\"quote"}}};
Jan Kundrát3081afa2018-09-14 16:53:43 +020066 expectedPath.m_nodes = {{{module_{"mod"}}, listElement_("list", keys)}, {container_("contInList")}};
67 }
68
69 SECTION("mod:list[quote=\"single'quote\"]/contInList")
70 {
71 input = "mod:list[quote=\"single'quote\"]/contInList";
Václav Kubernátc15fe822020-06-04 11:28:39 +020072 auto keys = ListInstance {
Václav Kubernát7707cae2020-01-16 12:04:53 +010073 {"quote", std::string{"single'quote"}}};
Jan Kundrát3081afa2018-09-14 16:53:43 +020074 expectedPath.m_nodes = {{{module_{"mod"}}, listElement_("list", keys)}, {container_("contInList")}};
75 }
76
Václav Kubernátb61336d2018-05-28 17:35:03 +020077 create_ expectedCreate;
78 expectedCreate.m_path = expectedPath;
79 command_ commandCreate = parser.parseCommand("create " + input, errorStream);
80 REQUIRE(commandCreate.type() == typeid(create_));
81 create_ create = boost::get<create_>(commandCreate);
82 REQUIRE(create == expectedCreate);
83
Václav Kubernátefcac932020-01-10 15:26:32 +010084 REQUIRE(pathToDataString(create.m_path, Prefixes::WhenNeeded) == input);
Jan Kundrát2a8f4332018-09-14 17:05:31 +020085
Václav Kubernátb61336d2018-05-28 17:35:03 +020086 delete_ expectedDelete;
87 expectedDelete.m_path = expectedPath;
88 command_ commandDelete = parser.parseCommand("delete " + input, errorStream);
89 REQUIRE(commandDelete.type() == typeid(delete_));
90 delete_ delet = boost::get<delete_>(commandDelete);
91 REQUIRE(delet == expectedDelete);
92 }
93 SECTION("invalid input")
94 {
95 SECTION("c")
96 {
97 input = "c";
98 }
99
100 SECTION("a/a2")
101 {
102 input = "a/a2";
103 }
104
Václav Kubernát89728d82018-09-13 16:28:28 +0200105 SECTION("list[quote='lol']")
Václav Kubernátb61336d2018-05-28 17:35:03 +0200106 {
Václav Kubernát89728d82018-09-13 16:28:28 +0200107 input = "list[quote='lol']";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200108 }
109
Václav Kubernát0d42c512020-05-20 21:18:39 +0200110 SECTION("no path")
111 {
112 input = " ";
113 }
114
Jan Kundrátc381e632019-03-14 13:39:11 +0100115 REQUIRE_THROWS_AS(parser.parseCommand("create " + input, errorStream), InvalidCommandException);
116 REQUIRE_THROWS_AS(parser.parseCommand("delete " + input, errorStream), InvalidCommandException);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200117 }
118}