Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
| 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
| 4 | * |
| 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include "trompeloeil_catch.h" |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 10 | #include "ast_commands.hpp" |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 11 | #include "parser.hpp" |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 12 | #include "static_schema.hpp" |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 13 | |
| 14 | TEST_CASE("leaf editing") |
| 15 | { |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 16 | auto schema = std::make_shared<StaticSchema>(); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 17 | schema->addModule("mod"); |
| 18 | schema->addContainer("", "mod:contA"); |
| 19 | schema->addLeaf("", "mod:leafString", yang::LeafDataTypes::String); |
| 20 | schema->addLeaf("", "mod:leafDecimal", yang::LeafDataTypes::Decimal); |
| 21 | schema->addLeaf("", "mod:leafBool", yang::LeafDataTypes::Bool); |
| 22 | schema->addLeaf("", "mod:leafInt", yang::LeafDataTypes::Int); |
| 23 | schema->addLeaf("", "mod:leafUint", yang::LeafDataTypes::Uint); |
| 24 | schema->addLeafEnum("", "mod:leafEnum", {"lol", "data", "coze"}); |
| 25 | schema->addLeaf("mod:contA", "mod:leafInCont", yang::LeafDataTypes::String); |
| 26 | schema->addList("", "mod:list", {"number"}); |
| 27 | schema->addLeaf("mod:list", "mod:leafInList", yang::LeafDataTypes::String); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 28 | Parser parser(schema); |
| 29 | std::string input; |
| 30 | std::ostringstream errorStream; |
| 31 | |
| 32 | SECTION("valid input") |
| 33 | { |
| 34 | set_ expected; |
| 35 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 36 | SECTION("set leafString some_data") |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 37 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 38 | input = "set mod:leafString some_data"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 39 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 40 | expected.m_data = std::string("some_data"); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 41 | } |
| 42 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 43 | SECTION("set mod:contA/leafInCont more_data") |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 44 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 45 | input = "set mod:contA/leafInCont more_data"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 46 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")}); |
| 47 | expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 48 | expected.m_data = std::string("more_data"); |
| 49 | } |
| 50 | |
| 51 | SECTION("set list[number=1]/leafInList another_data") |
| 52 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 53 | input = "set mod:list[number=1]/leafInList another_data"; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 54 | auto keys = std::map<std::string, std::string>{ |
| 55 | {"number", "1"}}; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 56 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)}); |
| 57 | expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInList")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 58 | expected.m_data = std::string("another_data"); |
| 59 | } |
| 60 | |
| 61 | SECTION("data types") |
| 62 | { |
| 63 | SECTION("string") |
| 64 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 65 | input = "set mod:leafString somedata"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 66 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 67 | expected.m_data = std::string("somedata"); |
| 68 | } |
| 69 | |
| 70 | SECTION("int") |
| 71 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 72 | input = "set mod:leafInt 2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 73 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 74 | expected.m_data = 2; |
| 75 | } |
| 76 | |
| 77 | SECTION("decimal") |
| 78 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 79 | input = "set mod:leafDecimal 3.14159"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 80 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafDecimal")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 81 | expected.m_data = 3.14159; |
| 82 | } |
| 83 | |
| 84 | SECTION("enum") |
| 85 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 86 | input = "set mod:leafEnum coze"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 87 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafEnum")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 88 | expected.m_data = enum_("coze"); |
| 89 | } |
| 90 | |
| 91 | SECTION("bool") |
| 92 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 93 | input = "set mod:leafBool true"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 94 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafBool")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 95 | expected.m_data = true; |
| 96 | } |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | command_ command = parser.parseCommand(input, errorStream); |
| 100 | REQUIRE(command.type() == typeid(set_)); |
| 101 | REQUIRE(boost::get<set_>(command) == expected); |
| 102 | } |
| 103 | |
| 104 | SECTION("invalid input") |
| 105 | { |
| 106 | SECTION("missing space between a command and its arguments") |
| 107 | { |
| 108 | SECTION("setleaf some_data") |
| 109 | { |
| 110 | input = "setleaf some_data"; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | SECTION("missing space between arguments") |
| 115 | { |
| 116 | SECTION("set leaflol") |
| 117 | { |
| 118 | input = "set leaflol"; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | SECTION("non-leaf identifiers") |
| 123 | { |
| 124 | SECTION("set nonexistent blabla") |
| 125 | { |
| 126 | input = "set nonexistent blabla"; |
| 127 | } |
| 128 | |
| 129 | SECTION("set contA abde") |
| 130 | { |
| 131 | input = "set contA abde"; |
| 132 | } |
| 133 | } |
| 134 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 135 | SECTION("wrong types") |
| 136 | { |
| 137 | SECTION("set leafBool blabla") |
| 138 | { |
| 139 | input = "set leafBool blabla"; |
| 140 | } |
| 141 | SECTION("set leafUint blabla") |
| 142 | { |
| 143 | input = "set leafUint blabla"; |
| 144 | } |
| 145 | SECTION("set leafInt blabla") |
| 146 | { |
| 147 | input = "set leafInt blabla"; |
| 148 | } |
| 149 | SECTION("set leafEnum blabla") |
| 150 | { |
| 151 | input = "set leafEnum blabla"; |
| 152 | } |
| 153 | } |
| 154 | |
Václav Kubernát | b7a279d | 2018-10-04 15:27:50 +0200 | [diff] [blame] | 155 | REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException&); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 156 | } |
| 157 | } |