Václav Kubernát | d8408e0 | 2020-12-02 05:13:27 +0100 | [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 | |
| 10 | #include "trompeloeil_doctest.hpp" |
| 11 | #include "parser.hpp" |
| 12 | #include "pretty_printers.hpp" |
| 13 | #include "static_schema.hpp" |
| 14 | |
| 15 | TEST_CASE("rpc") |
| 16 | { |
| 17 | auto schema = std::make_shared<StaticSchema>(); |
| 18 | schema->addModule("example"); |
| 19 | schema->addRpc("/", "example:fire"); |
| 20 | schema->addRpc("/", "example:shutdown"); |
| 21 | schema->addLeaf("/example:shutdown/input", "example:interface", yang::String{}); |
| 22 | schema->addList("/", "example:port", {"name"}); |
| 23 | schema->addLeaf("/example:port", "example:name", yang::String{}); |
| 24 | schema->addAction("/example:port", "example:shutdown"); |
| 25 | Parser parser(schema); |
| 26 | std::string input; |
| 27 | std::ostringstream errorStream; |
| 28 | SECTION("with prepare") |
| 29 | { |
| 30 | prepare_ prepExpected; |
| 31 | prepExpected.m_path.m_scope = Scope::Relative; |
| 32 | SECTION("rpc") |
| 33 | { |
| 34 | input = "prepare example:fire"; |
| 35 | prepExpected.m_path.m_nodes.emplace_back(module_{"example"}, rpcNode_{"fire"}); |
| 36 | } |
| 37 | |
| 38 | SECTION("action") |
| 39 | { |
| 40 | input = "prepare example:port[name='eth0']/shutdown"; |
| 41 | prepExpected.m_path.m_nodes.emplace_back(module_{"example"}, listElement_{"port", {{"name", std::string{"eth0"}}}}); |
| 42 | prepExpected.m_path.m_nodes.emplace_back(actionNode_{"shutdown"}); |
| 43 | } |
| 44 | |
| 45 | command_ command = parser.parseCommand(input, errorStream); |
| 46 | auto lol = boost::get<prepare_>(command); |
| 47 | REQUIRE(boost::get<prepare_>(command) == prepExpected); |
| 48 | } |
| 49 | |
| 50 | SECTION("direct exec") |
| 51 | { |
| 52 | exec_ execExpected; |
| 53 | execExpected.m_path = dataPath_{}; |
| 54 | execExpected.m_path->m_scope = Scope::Relative; |
| 55 | SECTION("the rpc has no arguments") |
| 56 | { |
| 57 | input = "exec example:fire"; |
| 58 | execExpected.m_path->m_nodes.emplace_back(module_{"example"}, rpcNode_{"fire"}); |
| 59 | command_ command = parser.parseCommand(input, errorStream); |
| 60 | auto lol = boost::get<exec_>(command); |
| 61 | REQUIRE(boost::get<exec_>(command) == execExpected); |
| 62 | } |
| 63 | |
| 64 | SECTION("the rpc has arguments") |
| 65 | { |
| 66 | input = "exec example:shutdown"; |
| 67 | REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException); |
| 68 | } |
Václav Kubernát | aff1f49 | 2021-02-17 10:56:28 +0100 | [diff] [blame] | 69 | |
| 70 | SECTION("require space between arguments") |
| 71 | { |
| 72 | input = "execexample:fire"; |
| 73 | REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | SECTION("exec with no arguments") |
| 78 | { |
| 79 | exec_ execExpected; |
| 80 | |
| 81 | SECTION("without space") |
| 82 | { |
| 83 | input = "exec"; |
| 84 | } |
| 85 | |
| 86 | SECTION("with space") |
| 87 | { |
| 88 | input = "exec "; |
| 89 | } |
| 90 | command_ command = parser.parseCommand(input, errorStream); |
| 91 | auto lol = boost::get<exec_>(command); |
| 92 | REQUIRE(boost::get<exec_>(command) == execExpected); |
Václav Kubernát | d8408e0 | 2020-12-02 05:13:27 +0100 | [diff] [blame] | 93 | } |
| 94 | } |