blob: d2ad5f9c437ce391f205b65ae6c65223889551e9 [file] [log] [blame]
Václav Kubernátf5f64f02019-03-19 17:15:47 +01001/*
2 * Copyright (C) 2019 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Václav Kubernát <kubernat@cesnet.cz>
5 *
6*/
7
Václav Kubernát26b56082020-02-03 18:28:56 +01008#include "trompeloeil_doctest.hpp"
Václav Kubernátf5f64f02019-03-19 17:15:47 +01009#include "parser.hpp"
Václav Kubernát1bcee3b2020-05-28 22:19:59 +020010#include "pretty_printers.hpp"
Václav Kubernátf5f64f02019-03-19 17:15:47 +010011#include "static_schema.hpp"
12
13TEST_CASE("list manipulation")
14{
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020015 using namespace std::string_literals;
Václav Kubernátf5f64f02019-03-19 17:15:47 +010016 auto schema = std::make_shared<StaticSchema>();
17 schema->addModule("mod");
Václav Kubernát1bcee3b2020-05-28 22:19:59 +020018 schema->addModule("other");
Václav Kubernátefcac932020-01-10 15:26:32 +010019 schema->addList("/", "mod:list", {"number"});
Václav Kubernát3a99f002020-03-31 02:27:41 +020020 schema->addLeaf("/mod:list", "mod:number", yang::Int32{});
21 schema->addLeaf("/mod:list", "mod:leafInList", yang::String{});
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020022 schema->addLeafList("/", "mod:addresses", yang::String{});
Václav Kubernát1bcee3b2020-05-28 22:19:59 +020023 schema->addIdentity(std::nullopt, identityRef_{"other", "deptypes"});
24 schema->addIdentity(identityRef_{"other", "deptypes"}, identityRef_{"other", "engineering"});
25 schema->addList("/", "mod:company", {"department"});
26 schema->addLeaf("/mod:company", "mod:department", schema->validIdentities("other", "deptypes"));
27 schema->addList("/mod:company", "mod:inventory", {"id"});
28 schema->addLeaf("/mod:company/mod:inventory", "mod:id", yang::Int32{});
Václav Kubernátf5f64f02019-03-19 17:15:47 +010029 Parser parser(schema);
30 std::string input;
31 std::ostringstream errorStream;
32
33 SECTION("creating/deleting list instances")
34 {
35 dataPath_ expectedPath;
36 SECTION("mod:list[number=3]")
37 {
38 input = "mod:list[number=3]";
Václav Kubernát7707cae2020-01-16 12:04:53 +010039 auto keys = std::map<std::string, leaf_data_>{
40 {"number", int32_t{3}}};
Václav Kubernátf5f64f02019-03-19 17:15:47 +010041 expectedPath.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)});
42 }
43
Václav Kubernát1bcee3b2020-05-28 22:19:59 +020044 SECTION("mod:company[department=other:engineering]/inventory[id=1337]")
45 {
46 input = "mod:company[department=other:engineering]/inventory[id=1337]";
47 auto keys = std::map<std::string, leaf_data_>{
48 {"department", identityRef_{"other", "engineering"}}};
49 expectedPath.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("company", keys)});
50 keys = std::map<std::string, leaf_data_>{
51 {"id", int32_t{1337}}};
52 expectedPath.m_nodes.push_back(dataNode_{listElement_("inventory", keys)});
53 }
54
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020055 SECTION("create mod:addresses['0.0.0.0']")
56 {
57 input = "mod:addresses['0.0.0.0']";
58 expectedPath.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "0.0.0.0"s}});
59 }
60
61
Václav Kubernátf5f64f02019-03-19 17:15:47 +010062 command_ parsedCreate = parser.parseCommand("create " + input, errorStream);
63 command_ parsedDelete = parser.parseCommand("delete " + input, errorStream);
64 create_ expectedCreate;
65 expectedCreate.m_path = expectedPath;
66 delete_ expectedDelete;
67 expectedDelete.m_path = expectedPath;
68 REQUIRE(parsedCreate.type() == typeid(create_));
69 REQUIRE(parsedDelete.type() == typeid(delete_));
70 REQUIRE(boost::get<create_>(parsedCreate) == expectedCreate);
71 REQUIRE(boost::get<delete_>(parsedDelete) == expectedDelete);
72 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020073
74 SECTION("retrieving all leaflist instances")
75 {
76 dataPath_ expected;
77 input = "get mod:addresses";
78 expected.m_nodes.push_back(dataNode_{module_{"mod"}, leafList_{"addresses"}});
79
80 get_ expectedGet;
81 expectedGet.m_path = expected;
82 command_ commandGet = parser.parseCommand(input, errorStream);
83 REQUIRE(commandGet.type() == typeid(get_));
84 REQUIRE(boost::get<get_>(commandGet) == expectedGet);
85 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +010086}