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