Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 1 | |
| 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át | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 10 | #include "trompeloeil_doctest.hpp" |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 11 | #include "ast_commands.hpp" |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 12 | #include "parser.hpp" |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 13 | #include "static_schema.hpp" |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 14 | |
| 15 | TEST_CASE("presence containers") |
| 16 | { |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 17 | auto schema = std::make_shared<StaticSchema>(); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 18 | schema->addModule("mod"); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 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"}); |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 25 | schema->addLeaf("/mod:list", "mod:quote", yang::LeafDataTypes::String); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 26 | schema->addContainer("/mod:list", "mod:contInList", yang::ContainerTraits::Presence); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 27 | Parser parser(schema); |
| 28 | std::string input; |
| 29 | std::ostringstream errorStream; |
| 30 | |
| 31 | SECTION("valid input") |
| 32 | { |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 33 | dataPath_ expectedPath; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 34 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 35 | SECTION("mod:a") |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 36 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 37 | input = "mod:a"; |
| 38 | expectedPath.m_nodes = {{module_{"mod"}, {container_("a")}}}; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 41 | SECTION("mod:b/b2") |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 42 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 43 | input = "mod:b/b2"; |
| 44 | expectedPath.m_nodes = {{{module_{"mod"}}, container_("b")}, {container_("b2")}}; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 47 | SECTION("mod:a/a2/a3") |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 48 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 49 | input = "mod:a/a2/a3"; |
| 50 | expectedPath.m_nodes = {{{module_{"mod"}}, container_("a")}, {container_("a2")}, {container_("a3")}}; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 53 | SECTION("mod:list[quote='lol']/contInList") |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 54 | { |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 55 | input = "mod:list[quote='lol']/contInList"; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 56 | auto keys = std::map<std::string, leaf_data_>{ |
| 57 | {"quote", std::string{"lol"}}}; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 58 | expectedPath.m_nodes = {{{module_{"mod"}}, listElement_("list", keys)}, {container_("contInList")}}; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Jan Kundrát | 3081afa | 2018-09-14 16:53:43 +0200 | [diff] [blame] | 61 | SECTION("mod:list[quote='double\"quote']/contInList") |
| 62 | { |
| 63 | input = "mod:list[quote='double\"quote']/contInList"; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 64 | auto keys = std::map<std::string, leaf_data_>{ |
| 65 | {"quote", std::string{"double\"quote"}}}; |
Jan Kundrát | 3081afa | 2018-09-14 16:53:43 +0200 | [diff] [blame] | 66 | 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át | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 72 | auto keys = std::map<std::string, leaf_data_>{ |
| 73 | {"quote", std::string{"single'quote"}}}; |
Jan Kundrát | 3081afa | 2018-09-14 16:53:43 +0200 | [diff] [blame] | 74 | expectedPath.m_nodes = {{{module_{"mod"}}, listElement_("list", keys)}, {container_("contInList")}}; |
| 75 | } |
| 76 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 77 | 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át | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 84 | REQUIRE(pathToDataString(create.m_path, Prefixes::WhenNeeded) == input); |
Jan Kundrát | 2a8f433 | 2018-09-14 17:05:31 +0200 | [diff] [blame] | 85 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 86 | 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át | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 105 | SECTION("list[quote='lol']") |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 106 | { |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 107 | input = "list[quote='lol']"; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Jan Kundrát | c381e63 | 2019-03-14 13:39:11 +0100 | [diff] [blame] | 110 | REQUIRE_THROWS_AS(parser.parseCommand("create " + input, errorStream), InvalidCommandException); |
| 111 | REQUIRE_THROWS_AS(parser.parseCommand("delete " + input, errorStream), InvalidCommandException); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 112 | } |
| 113 | } |