1 // Copyright 2011 The Kyua Authors. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above copyright 11 // notice, this list of conditions and the following disclaimer in the 12 // documentation and/or other materials provided with the distribution. 13 // * Neither the name of Google Inc. nor the names of its contributors 14 // may be used to endorse or promote products derived from this software 15 // without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 #include "utils/cmdline/commands_map.ipp" 30 31 #include <atf-c++.hpp> 32 33 #include "utils/cmdline/base_command.hpp" 34 #include "utils/defs.hpp" 35 #include "utils/sanity.hpp" 36 37 namespace cmdline = utils::cmdline; 38 39 40 namespace { 41 42 43 /// Fake command to validate the behavior of commands_map. 44 /// 45 /// Note that this command does not do anything. It is only intended to provide 46 /// a specific class that can be inserted into commands_map instances and check 47 /// that it can be located properly. 48 class mock_cmd : public cmdline::base_command_no_data { 49 public: 50 /// Constructor for the mock command. 51 /// 52 /// \param mock_name The name of the command. All other settings are set to 53 /// irrelevant values. 54 mock_cmd(const char* mock_name) : 55 cmdline::base_command_no_data(mock_name, "", 0, 0, 56 "Command for testing.") 57 { 58 } 59 60 /// Runs the mock command. 61 /// 62 /// \return Nothing because this function is never called. 63 int 64 run(cmdline::ui* /* ui */, 65 const cmdline::parsed_cmdline& /* cmdline */) 66 { 67 UNREACHABLE; 68 } 69 }; 70 71 72 } // anonymous namespace 73 74 75 ATF_TEST_CASE_WITHOUT_HEAD(empty); 76 ATF_TEST_CASE_BODY(empty) 77 { 78 cmdline::commands_map< cmdline::base_command_no_data > commands; 79 ATF_REQUIRE(commands.empty()); 80 ATF_REQUIRE(commands.begin() == commands.end()); 81 } 82 83 84 ATF_TEST_CASE_WITHOUT_HEAD(some); 85 ATF_TEST_CASE_BODY(some) 86 { 87 cmdline::commands_map< cmdline::base_command_no_data > commands; 88 cmdline::base_command_no_data* cmd1 = new mock_cmd("cmd1"); 89 commands.insert(cmd1); 90 cmdline::base_command_no_data* cmd2 = new mock_cmd("cmd2"); 91 commands.insert(cmd2, "foo"); 92 93 ATF_REQUIRE(!commands.empty()); 94 95 cmdline::commands_map< cmdline::base_command_no_data >::const_iterator 96 iter = commands.begin(); 97 ATF_REQUIRE_EQ("", (*iter).first); 98 ATF_REQUIRE_EQ(1, (*iter).second.size()); 99 ATF_REQUIRE_EQ("cmd1", *(*iter).second.begin()); 100 101 ++iter; 102 ATF_REQUIRE_EQ("foo", (*iter).first); 103 ATF_REQUIRE_EQ(1, (*iter).second.size()); 104 ATF_REQUIRE_EQ("cmd2", *(*iter).second.begin()); 105 106 ATF_REQUIRE(++iter == commands.end()); 107 } 108 109 110 ATF_TEST_CASE_WITHOUT_HEAD(find__match); 111 ATF_TEST_CASE_BODY(find__match) 112 { 113 cmdline::commands_map< cmdline::base_command_no_data > commands; 114 cmdline::base_command_no_data* cmd1 = new mock_cmd("cmd1"); 115 commands.insert(cmd1); 116 cmdline::base_command_no_data* cmd2 = new mock_cmd("cmd2"); 117 commands.insert(cmd2); 118 119 ATF_REQUIRE(cmd1 == commands.find("cmd1")); 120 ATF_REQUIRE(cmd2 == commands.find("cmd2")); 121 } 122 123 124 ATF_TEST_CASE_WITHOUT_HEAD(find__nomatch); 125 ATF_TEST_CASE_BODY(find__nomatch) 126 { 127 cmdline::commands_map< cmdline::base_command_no_data > commands; 128 commands.insert(new mock_cmd("cmd1")); 129 130 ATF_REQUIRE(NULL == commands.find("cmd2")); 131 } 132 133 134 ATF_INIT_TEST_CASES(tcs) 135 { 136 ATF_ADD_TEST_CASE(tcs, empty); 137 ATF_ADD_TEST_CASE(tcs, some); 138 ATF_ADD_TEST_CASE(tcs, find__match); 139 ATF_ADD_TEST_CASE(tcs, find__nomatch); 140 } 141