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