| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 1 | #include "argument_test.hpp" | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 2 |  | 
|  | 3 | #include "argument.hpp" | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 4 |  | 
|  | 5 | #include <string> | 
|  | 6 | #include <vector> | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 7 |  | 
|  | 8 | static const std::string expected_path1 = "/arg1-test-path"; | 
|  | 9 | static const std::string expected_target1 = "t1.target"; | 
| William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 10 | static const std::string expected_target2 = "t2.target"; | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 11 |  | 
|  | 12 | // Allow for a single unrecognized option then the Usage printout | 
|  | 13 | static const std::string invalid_arg_regex = | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 14 | "^[^\n]*unrecognized option[^\n]*\nUsage: "; | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 15 |  | 
|  | 16 | static const std::string clean_usage_regex = "^Usage: "; | 
|  | 17 |  | 
|  | 18 | namespace phosphor | 
|  | 19 | { | 
|  | 20 | namespace watchdog | 
|  | 21 | { | 
|  | 22 |  | 
|  | 23 | void ArgumentTest::SetUp() | 
|  | 24 | { | 
|  | 25 | arg0 = "argument_test"; | 
|  | 26 | } | 
|  | 27 |  | 
|  | 28 | /** @brief ArgumentParser should return no values if given no options */ | 
|  | 29 | TEST_F(ArgumentTest, NoOptions) | 
|  | 30 | { | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 31 | char* const args[] = {&arg0[0], nullptr}; | 
|  | 32 | ArgumentParser ap(sizeof(args) / sizeof(char*) - 1, args); | 
| William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 33 | EXPECT_EQ(std::vector<std::string>({}), ap["path"]); | 
|  | 34 | EXPECT_EQ(std::vector<std::string>({}), ap["continue"]); | 
|  | 35 | EXPECT_EQ(std::vector<std::string>({}), ap["arbitrary_unknown"]); | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 36 | } | 
|  | 37 |  | 
|  | 38 | /** @brief ArgumentParser should return true for an existing no-arg option | 
|  | 39 | *         Make sure we don't parse arguments if an option takes none | 
|  | 40 | *         Also make sure we don't populate options not used. | 
|  | 41 | */ | 
|  | 42 | TEST_F(ArgumentTest, LongOptionNoArg) | 
|  | 43 | { | 
|  | 44 | std::string arg_continue = "--continue"; | 
|  | 45 | std::string arg_extra = "not-a-bool"; | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 46 | char* const args[] = {&arg0[0], &arg_continue[0], &arg_extra[0], nullptr}; | 
|  | 47 | ArgumentParser ap(sizeof(args) / sizeof(char*) - 1, args); | 
| William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 48 | EXPECT_EQ(std::vector<std::string>({}), ap["path"]); | 
|  | 49 | EXPECT_EQ(std::vector<std::string>({ArgumentParser::trueString}), | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 50 | ap["continue"]); | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
|  | 53 | /** @brief ArgumentParser should return a string for long options that | 
|  | 54 | *         take an arg | 
|  | 55 | */ | 
|  | 56 | TEST_F(ArgumentTest, LongOptionRequiredArg) | 
|  | 57 | { | 
|  | 58 | std::string arg_path = "--path"; | 
|  | 59 | std::string arg_path_val = expected_path1; | 
|  | 60 | std::string arg_extra = "/unused-path"; | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 61 | char* const args[] = {&arg0[0], &arg_path[0], &arg_path_val[0], | 
|  | 62 | &arg_extra[0], nullptr}; | 
|  | 63 | ArgumentParser ap(sizeof(args) / sizeof(char*) - 1, args); | 
| William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 64 | EXPECT_EQ(std::vector<std::string>({expected_path1}), ap["path"]); | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 65 | } | 
|  | 66 |  | 
|  | 67 | /** @brief ArgumentParser should return a string for long options that | 
|  | 68 | *         accept an argument when passed an argument inline | 
|  | 69 | */ | 
|  | 70 | TEST_F(ArgumentTest, LongOptionInlineArg) | 
|  | 71 | { | 
|  | 72 | std::string arg_path = "--path=" + expected_path1; | 
|  | 73 | std::string arg_extra = "/unused-path"; | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 74 | char* const args[] = {&arg0[0], &arg_path[0], &arg_extra[0], nullptr}; | 
|  | 75 | ArgumentParser ap(sizeof(args) / sizeof(char*) - 1, args); | 
| William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 76 | EXPECT_EQ(std::vector<std::string>({expected_path1}), ap["path"]); | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | /** @brief ArgumentParser should return a string for short options that | 
|  | 80 | *         accept an argument. | 
|  | 81 | */ | 
|  | 82 | TEST_F(ArgumentTest, ShortOptionRequiredArg) | 
|  | 83 | { | 
|  | 84 | std::string arg_path = "-p"; | 
|  | 85 | std::string arg_path_val = expected_path1; | 
|  | 86 | std::string arg_extra = "/unused-path"; | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 87 | char* const args[] = {&arg0[0], &arg_path[0], &arg_path_val[0], | 
|  | 88 | &arg_extra[0], nullptr}; | 
|  | 89 | ArgumentParser ap(sizeof(args) / sizeof(char*) - 1, args); | 
| William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 90 | EXPECT_EQ(std::vector<std::string>({expected_path1}), ap["path"]); | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
|  | 93 | /** @brief ArgumentParser should be able to handle parsing multiple options | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 94 | *         Make sure this works for no-arg and required-arg type options | 
|  | 95 | *         Make sure this works between short and long options | 
|  | 96 | */ | 
|  | 97 | TEST_F(ArgumentTest, MultiOptionOverride) | 
|  | 98 | { | 
|  | 99 | std::string arg_continue_short = "-c"; | 
|  | 100 | std::string arg_path = "--path=" + expected_path1; | 
|  | 101 | std::string arg_continue_long = "--continue"; | 
| William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 102 | std::string arg_target = "--target=" + expected_target2; | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 103 | std::string arg_target_short = "-t"; | 
|  | 104 | std::string arg_target_val = expected_target1; | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 105 | char* const args[] = {&arg0[0],           &arg_continue_short[0], | 
|  | 106 | &arg_path[0],       &arg_continue_long[0], | 
|  | 107 | &arg_target[0],     &arg_target_short[0], | 
|  | 108 | &arg_target_val[0], nullptr}; | 
|  | 109 | ArgumentParser ap(sizeof(args) / sizeof(char*) - 1, args); | 
| William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 110 | EXPECT_EQ(std::vector<std::string>({expected_path1}), ap["path"]); | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 111 | EXPECT_EQ(std::vector<std::string>( | 
|  | 112 | {ArgumentParser::trueString, ArgumentParser::trueString}), | 
|  | 113 | ap["continue"]); | 
| William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 114 | EXPECT_EQ(std::vector<std::string>({expected_target2, expected_target1}), | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 115 | ap["target"]); | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
|  | 118 | /** @brief ArgumentParser should print usage information when given a help | 
|  | 119 | *         argument anywhere in the arguments array | 
|  | 120 | */ | 
|  | 121 | TEST_F(ArgumentTest, ShortOptionHelp) | 
|  | 122 | { | 
|  | 123 | std::string arg_extra = "extra"; | 
|  | 124 | std::string arg_help = "-h"; | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 125 | char* const args[] = {&arg0[0], &arg_extra[0], &arg_help[0], nullptr}; | 
|  | 126 | EXPECT_EXIT(ArgumentParser(sizeof(args) / sizeof(char*) - 1, args), | 
|  | 127 | ::testing::ExitedWithCode(255), clean_usage_regex); | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
|  | 130 | /** @brief ArgumentParser should print usage information when given a help | 
|  | 131 | *         argument anywhere in the arguments array | 
|  | 132 | */ | 
|  | 133 | TEST_F(ArgumentTest, LongOptionHelp) | 
|  | 134 | { | 
|  | 135 | std::string arg_help = "--help"; | 
|  | 136 | std::string arg_extra = "extra"; | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 137 | char* const args[] = {&arg0[0], &arg_help[0], &arg_extra[0], nullptr}; | 
|  | 138 | EXPECT_EXIT(ArgumentParser(sizeof(args) / sizeof(char*) - 1, args), | 
|  | 139 | ::testing::ExitedWithCode(255), clean_usage_regex); | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 140 | } | 
|  | 141 |  | 
|  | 142 | /** @brief ArgumentParser should print an invalid argument error and | 
|  | 143 | *         usage information when given an invalid argument anywhere | 
|  | 144 | *         in the arguments array | 
|  | 145 | */ | 
|  | 146 | TEST_F(ArgumentTest, InvalidOptionHelp) | 
|  | 147 | { | 
|  | 148 | std::string arg_continue = "--continue"; | 
|  | 149 | std::string arg_bad = "--bad_arg"; | 
|  | 150 | std::string arg_target = "--target=/unused-path"; | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 151 | char* const args[] = {&arg0[0], &arg_continue[0], &arg_bad[0], | 
|  | 152 | &arg_target[0], nullptr}; | 
|  | 153 | EXPECT_EXIT(ArgumentParser(sizeof(args) / sizeof(char*) - 1, args), | 
|  | 154 | ::testing::ExitedWithCode(255), invalid_arg_regex); | 
| William A. Kennington III | e335ed9 | 2018-01-23 22:13:44 -0800 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
| Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 157 | } // namespace watchdog | 
|  | 158 | } // namespace phosphor |