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; |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 43 | ExecuteCommand dummyExecute = [] (ExecuteContext&) { return 0; }; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 44 | |
| 45 | CommandDefinition defHelp("help", ""); |
| 46 | defHelp |
| 47 | .addArg("noun", ArgValueType::STRING, Required::NO, Positional::YES) |
| 48 | .addArg("verb", ArgValueType::STRING, Required::NO, Positional::YES); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 49 | parser.addCommand(defHelp, dummyExecute, AVAILABLE_IN_ONE_SHOT); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 50 | |
| 51 | CommandDefinition defStatusShow("status", "show"); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 52 | parser.addCommand(defStatusShow, dummyExecute); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 53 | parser.addAlias("status", "show", "list"); |
| 54 | BOOST_CHECK_THROW(parser.addAlias("status", "show2", "list"), std::out_of_range); |
| 55 | |
| 56 | CommandDefinition defRouteList("route", "list"); |
| 57 | defRouteList |
| 58 | .addArg("nexthop", ArgValueType::FACE_ID_OR_URI, Required::NO, Positional::YES); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 59 | parser.addCommand(defRouteList, dummyExecute); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 60 | |
| 61 | CommandDefinition defRouteAdd("route", "add"); |
| 62 | defRouteAdd |
| 63 | .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES) |
| 64 | .addArg("nexthop", ArgValueType::FACE_ID_OR_URI, Required::YES, Positional::YES); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 65 | parser.addCommand(defRouteAdd, dummyExecute); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 66 | parser.addAlias("route", "add", "add2"); |
| 67 | |
| 68 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 69 | std::string noun, verb; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 70 | CommandArguments ca; |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 71 | ExecuteCommand execute; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 72 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 73 | std::tie(noun, verb, ca, execute) = parser.parse( |
| 74 | std::vector<std::string>{"help"}, ParseMode::ONE_SHOT); |
| 75 | BOOST_CHECK_EQUAL(noun, "help"); |
| 76 | BOOST_CHECK_EQUAL(verb, ""); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 77 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 78 | std::tie(noun, verb, ca, execute) = parser.parse( |
| 79 | std::vector<std::string>{"status"}, ParseMode::ONE_SHOT); |
| 80 | BOOST_CHECK_EQUAL(noun, "status"); |
| 81 | BOOST_CHECK_EQUAL(verb, "show"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 82 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 83 | std::tie(noun, verb, ca, execute) = parser.parse( |
| 84 | std::vector<std::string>{"route", "add", "/n", "300"}, ParseMode::ONE_SHOT); |
| 85 | BOOST_CHECK_EQUAL(noun, "route"); |
| 86 | BOOST_CHECK_EQUAL(verb, "add"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL(boost::any_cast<Name>(ca.at("prefix")), "/n"); |
| 88 | BOOST_CHECK_EQUAL(boost::any_cast<uint64_t>(ca.at("nexthop")), 300); |
| 89 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 90 | std::tie(noun, verb, ca, execute) = parser.parse( |
| 91 | std::vector<std::string>{"route", "add2", "/n", "300"}, ParseMode::ONE_SHOT); |
| 92 | BOOST_CHECK_EQUAL(noun, "route"); |
| 93 | BOOST_CHECK_EQUAL(verb, "add"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 94 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 95 | std::tie(noun, verb, ca, execute) = parser.parse( |
| 96 | std::vector<std::string>{"route", "list", "400"}, ParseMode::ONE_SHOT); |
| 97 | BOOST_CHECK_EQUAL(noun, "route"); |
| 98 | BOOST_CHECK_EQUAL(verb, "list"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(boost::any_cast<uint64_t>(ca.at("nexthop")), 400); |
| 100 | |
| 101 | BOOST_CHECK_THROW(parser.parse(std::vector<std::string>{}, ParseMode::ONE_SHOT), |
| 102 | CommandParser::Error); |
| 103 | BOOST_CHECK_THROW(parser.parse(std::vector<std::string>{"cant-help"}, ParseMode::ONE_SHOT), |
| 104 | CommandParser::Error); |
| 105 | BOOST_CHECK_THROW(parser.parse(std::vector<std::string>{"status", "hide"}, ParseMode::ONE_SHOT), |
| 106 | CommandParser::Error); |
| 107 | BOOST_CHECK_THROW(parser.parse(std::vector<std::string>{"route", "400"}, ParseMode::ONE_SHOT), |
| 108 | CommandParser::Error); |
| 109 | BOOST_CHECK_THROW(parser.parse(std::vector<std::string>{"route", "add"}, ParseMode::ONE_SHOT), |
| 110 | CommandDefinition::Error); |
| 111 | } |
| 112 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 113 | BOOST_AUTO_TEST_SUITE_END() // TestCommandParser |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 114 | BOOST_AUTO_TEST_SUITE_END() // Nfdc |
| 115 | |
| 116 | } // namespace tests |
| 117 | } // namespace nfdc |
| 118 | } // namespace tools |
| 119 | } // namespace nfd |