Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "nfdc/command-parser.hpp" |
| 27 | |
| 28 | #include "tests/test-common.hpp" |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace tools { |
| 32 | namespace nfdc { |
| 33 | namespace tests { |
| 34 | |
| 35 | using namespace nfd::tests; |
| 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(Nfdc) |
| 38 | BOOST_FIXTURE_TEST_SUITE(TestCommandParser, BaseFixture) |
| 39 | |
| 40 | BOOST_AUTO_TEST_CASE(Basic) |
| 41 | { |
| 42 | CommandParser parser; |
| 43 | |
| 44 | std::string lastName; |
| 45 | auto makeExecute = [&] (const std::string& name) { |
| 46 | return [&, name] (const CommandArguments& args) { |
| 47 | lastName = name; |
| 48 | }; |
| 49 | }; |
| 50 | |
| 51 | CommandDefinition defHelp("help", ""); |
| 52 | defHelp |
| 53 | .addArg("noun", ArgValueType::STRING, Required::NO, Positional::YES) |
| 54 | .addArg("verb", ArgValueType::STRING, Required::NO, Positional::YES); |
| 55 | parser.addCommand(defHelp, makeExecute("help"), AVAILABLE_IN_ONE_SHOT); |
| 56 | |
| 57 | CommandDefinition defStatusShow("status", "show"); |
| 58 | parser.addCommand(defStatusShow, makeExecute("status show")); |
| 59 | parser.addAlias("status", "show", "list"); |
| 60 | BOOST_CHECK_THROW(parser.addAlias("status", "show2", "list"), std::out_of_range); |
| 61 | |
| 62 | CommandDefinition defRouteList("route", "list"); |
| 63 | defRouteList |
| 64 | .addArg("nexthop", ArgValueType::FACE_ID_OR_URI, Required::NO, Positional::YES); |
| 65 | parser.addCommand(defRouteList, makeExecute("route list")); |
| 66 | |
| 67 | CommandDefinition defRouteAdd("route", "add"); |
| 68 | defRouteAdd |
| 69 | .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES) |
| 70 | .addArg("nexthop", ArgValueType::FACE_ID_OR_URI, Required::YES, Positional::YES); |
| 71 | parser.addCommand(defRouteAdd, makeExecute("route add")); |
| 72 | parser.addAlias("route", "add", "add2"); |
| 73 | |
| 74 | |
| 75 | CommandParser::Execute* execute = nullptr; |
| 76 | CommandArguments ca; |
| 77 | |
| 78 | std::tie(execute, ca) = parser.parse(std::vector<std::string>{"help"}, |
| 79 | ParseMode::ONE_SHOT); |
| 80 | BOOST_REQUIRE(execute != nullptr); |
| 81 | (*execute)(ca); |
| 82 | BOOST_CHECK_EQUAL(lastName, "help"); |
| 83 | |
| 84 | std::tie(execute, ca) = parser.parse(std::vector<std::string>{"status"}, |
| 85 | ParseMode::ONE_SHOT); |
| 86 | BOOST_REQUIRE(execute != nullptr); |
| 87 | (*execute)(ca); |
| 88 | BOOST_CHECK_EQUAL(lastName, "status show"); |
| 89 | |
| 90 | std::tie(execute, ca) = parser.parse(std::vector<std::string>{"route", "add", "/n", "300"}, |
| 91 | ParseMode::ONE_SHOT); |
| 92 | BOOST_REQUIRE(execute != nullptr); |
| 93 | (*execute)(ca); |
| 94 | BOOST_CHECK_EQUAL(lastName, "route add"); |
| 95 | BOOST_CHECK_EQUAL(boost::any_cast<Name>(ca.at("prefix")), "/n"); |
| 96 | BOOST_CHECK_EQUAL(boost::any_cast<uint64_t>(ca.at("nexthop")), 300); |
| 97 | |
| 98 | std::tie(execute, ca) = parser.parse(std::vector<std::string>{"route", "add2", "/n", "300"}, |
| 99 | ParseMode::ONE_SHOT); |
| 100 | BOOST_REQUIRE(execute != nullptr); |
| 101 | (*execute)(ca); |
| 102 | BOOST_CHECK_EQUAL(lastName, "route add"); |
| 103 | |
| 104 | std::tie(execute, ca) = parser.parse(std::vector<std::string>{"route", "list", "400"}, |
| 105 | ParseMode::ONE_SHOT); |
| 106 | BOOST_REQUIRE(execute != nullptr); |
| 107 | (*execute)(ca); |
| 108 | BOOST_CHECK_EQUAL(lastName, "route list"); |
| 109 | BOOST_CHECK_EQUAL(boost::any_cast<uint64_t>(ca.at("nexthop")), 400); |
| 110 | |
| 111 | BOOST_CHECK_THROW(parser.parse(std::vector<std::string>{}, ParseMode::ONE_SHOT), |
| 112 | CommandParser::Error); |
| 113 | BOOST_CHECK_THROW(parser.parse(std::vector<std::string>{"cant-help"}, ParseMode::ONE_SHOT), |
| 114 | CommandParser::Error); |
| 115 | BOOST_CHECK_THROW(parser.parse(std::vector<std::string>{"status", "hide"}, ParseMode::ONE_SHOT), |
| 116 | CommandParser::Error); |
| 117 | BOOST_CHECK_THROW(parser.parse(std::vector<std::string>{"route", "400"}, ParseMode::ONE_SHOT), |
| 118 | CommandParser::Error); |
| 119 | BOOST_CHECK_THROW(parser.parse(std::vector<std::string>{"route", "add"}, ParseMode::ONE_SHOT), |
| 120 | CommandDefinition::Error); |
| 121 | } |
| 122 | |
| 123 | BOOST_AUTO_TEST_SUITE_END() // TestCommandDefinition |
| 124 | BOOST_AUTO_TEST_SUITE_END() // Nfdc |
| 125 | |
| 126 | } // namespace tests |
| 127 | } // namespace nfdc |
| 128 | } // namespace tools |
| 129 | } // namespace nfd |