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 | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 29 | 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át | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 39 | auto keys = std::map<std::string, leaf_data_>{ |
| 40 | {"number", int32_t{3}}}; |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 41 | expectedPath.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)}); |
| 42 | } |
| 43 | |
Václav Kubernát | 1bcee3b | 2020-05-28 22:19:59 +0200 | [diff] [blame] | 44 | 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át | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 55 | 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át | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 62 | 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át | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 73 | |
| 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át | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 86 | } |