Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 1 | /* |
| 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át | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 8 | #include "trompeloeil_doctest.hpp" |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 9 | #include "parser.hpp" |
Václav Kubernát | 1bcee3b | 2020-05-28 22:19:59 +0200 | [diff] [blame] | 10 | #include "pretty_printers.hpp" |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 11 | #include "static_schema.hpp" |
| 12 | |
| 13 | TEST_CASE("list manipulation") |
| 14 | { |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 15 | using namespace std::string_literals; |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 16 | auto schema = std::make_shared<StaticSchema>(); |
| 17 | schema->addModule("mod"); |
Václav Kubernát | 1bcee3b | 2020-05-28 22:19:59 +0200 | [diff] [blame] | 18 | schema->addModule("other"); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 19 | schema->addList("/", "mod:list", {"number"}); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 20 | schema->addLeaf("/mod:list", "mod:number", yang::Int32{}); |
| 21 | schema->addLeaf("/mod:list", "mod:leafInList", yang::String{}); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 22 | schema->addLeafList("/", "mod:addresses", yang::String{}); |
Václav Kubernát | 1bcee3b | 2020-05-28 22:19:59 +0200 | [diff] [blame] | 23 | 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át | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 29 | schema->addContainer("/", "mod:cont"); |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 30 | Parser parser(schema); |
| 31 | std::string input; |
| 32 | std::ostringstream errorStream; |
| 33 | |
| 34 | SECTION("creating/deleting list instances") |
| 35 | { |
| 36 | dataPath_ expectedPath; |
| 37 | SECTION("mod:list[number=3]") |
| 38 | { |
| 39 | input = "mod:list[number=3]"; |
Václav Kubernát | c15fe82 | 2020-06-04 11:28:39 +0200 | [diff] [blame] | 40 | auto keys = ListInstance { |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 41 | {"number", int32_t{3}}}; |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 42 | expectedPath.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)}); |
| 43 | } |
| 44 | |
Václav Kubernát | 1bcee3b | 2020-05-28 22:19:59 +0200 | [diff] [blame] | 45 | SECTION("mod:company[department=other:engineering]/inventory[id=1337]") |
| 46 | { |
| 47 | input = "mod:company[department=other:engineering]/inventory[id=1337]"; |
Václav Kubernát | c15fe82 | 2020-06-04 11:28:39 +0200 | [diff] [blame] | 48 | auto keys = ListInstance { |
Václav Kubernát | 1bcee3b | 2020-05-28 22:19:59 +0200 | [diff] [blame] | 49 | {"department", identityRef_{"other", "engineering"}}}; |
| 50 | expectedPath.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("company", keys)}); |
Václav Kubernát | c15fe82 | 2020-06-04 11:28:39 +0200 | [diff] [blame] | 51 | keys = ListInstance { |
Václav Kubernát | 1bcee3b | 2020-05-28 22:19:59 +0200 | [diff] [blame] | 52 | {"id", int32_t{1337}}}; |
| 53 | expectedPath.m_nodes.push_back(dataNode_{listElement_("inventory", keys)}); |
| 54 | } |
| 55 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 56 | SECTION("create mod:addresses['0.0.0.0']") |
| 57 | { |
| 58 | input = "mod:addresses['0.0.0.0']"; |
| 59 | expectedPath.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "0.0.0.0"s}}); |
| 60 | } |
| 61 | |
| 62 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 63 | command_ parsedCreate = parser.parseCommand("create " + input, errorStream); |
| 64 | command_ parsedDelete = parser.parseCommand("delete " + input, errorStream); |
| 65 | create_ expectedCreate; |
| 66 | expectedCreate.m_path = expectedPath; |
| 67 | delete_ expectedDelete; |
| 68 | expectedDelete.m_path = expectedPath; |
| 69 | REQUIRE(parsedCreate.type() == typeid(create_)); |
| 70 | REQUIRE(parsedDelete.type() == typeid(delete_)); |
| 71 | REQUIRE(boost::get<create_>(parsedCreate) == expectedCreate); |
| 72 | REQUIRE(boost::get<delete_>(parsedDelete) == expectedDelete); |
| 73 | } |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 74 | |
| 75 | SECTION("retrieving all leaflist instances") |
| 76 | { |
| 77 | dataPath_ expected; |
| 78 | input = "get mod:addresses"; |
| 79 | expected.m_nodes.push_back(dataNode_{module_{"mod"}, leafList_{"addresses"}}); |
| 80 | |
| 81 | get_ expectedGet; |
| 82 | expectedGet.m_path = expected; |
| 83 | command_ commandGet = parser.parseCommand(input, errorStream); |
| 84 | REQUIRE(commandGet.type() == typeid(get_)); |
| 85 | REQUIRE(boost::get<get_>(commandGet) == expectedGet); |
| 86 | } |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 87 | |
| 88 | SECTION("moving (leaf)list instances") |
| 89 | { |
| 90 | move_ expected; |
| 91 | SECTION("begin") |
| 92 | { |
| 93 | SECTION("cwd: /") |
| 94 | { |
| 95 | input = "move mod:addresses['1.2.3.4'] begin"; |
| 96 | expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s}}); |
| 97 | } |
| 98 | |
| 99 | SECTION("cwd: /mod:cont") |
| 100 | { |
| 101 | parser.changeNode(dataPath_{Scope::Absolute, {dataNode_{module_{"mod"}, container_{"cont"}}}}); |
| 102 | SECTION("relative") |
| 103 | { |
| 104 | input = "move ../mod:addresses['1.2.3.4'] begin"; |
| 105 | expected.m_source.m_nodes.push_back(dataNode_{nodeup_{}}); |
| 106 | expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s}}); |
| 107 | } |
| 108 | |
| 109 | SECTION("absolute") |
| 110 | { |
| 111 | input = "move /mod:addresses['1.2.3.4'] begin"; |
| 112 | expected.m_source.m_scope = Scope::Absolute; |
| 113 | expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s}}); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | expected.m_destination = yang::move::Absolute::Begin; |
| 118 | } |
| 119 | |
| 120 | SECTION("end") |
| 121 | { |
| 122 | input = "move mod:addresses['1.2.3.4'] end"; |
| 123 | expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s}}); |
| 124 | expected.m_destination = yang::move::Absolute::End; |
| 125 | } |
| 126 | |
| 127 | SECTION("after") |
| 128 | { |
| 129 | input = "move mod:addresses['1.2.3.4'] after '0.0.0.0'"; |
| 130 | expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s}}); |
| 131 | expected.m_destination = yang::move::Relative { |
| 132 | yang::move::Relative::Position::After, |
| 133 | {{".", "0.0.0.0"s}} |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | SECTION("before") |
| 138 | { |
| 139 | input = "move mod:addresses['1.2.3.4'] before '0.0.0.0'"; |
| 140 | expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s}}); |
| 141 | expected.m_destination = yang::move::Relative { |
| 142 | yang::move::Relative::Position::Before, |
| 143 | {{".", "0.0.0.0"s}} |
| 144 | }; |
| 145 | } |
| 146 | |
| 147 | SECTION("list instance with destination") |
| 148 | { |
| 149 | input = "move mod:list[number=12] before [number=15]"; |
| 150 | auto keys = std::map<std::string, leaf_data_>{ |
| 151 | {"number", int32_t{12}}}; |
| 152 | expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)}); |
| 153 | expected.m_destination = yang::move::Relative { |
| 154 | yang::move::Relative::Position::Before, |
| 155 | ListInstance{{"number", int32_t{15}}} |
| 156 | }; |
| 157 | } |
| 158 | |
| 159 | SECTION("list instance without destination") |
| 160 | { |
| 161 | input = "move mod:list[number=3] begin"; |
| 162 | auto keys = std::map<std::string, leaf_data_>{ |
| 163 | {"number", int32_t{3}}}; |
| 164 | expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)}); |
| 165 | expected.m_destination = yang::move::Absolute::Begin; |
| 166 | } |
| 167 | |
| 168 | command_ commandMove = parser.parseCommand(input, errorStream); |
| 169 | REQUIRE(commandMove.type() == typeid(move_)); |
| 170 | REQUIRE(boost::get<move_>(commandMove) == expected); |
| 171 | } |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 172 | } |