1 #include <gtest/gtest.h>
2
3 #include "hawktracer/client_utils/command_line_parser.hpp"
4
5 #include <vector>
6
7 using HawkTracer::ClientUtils::CommandLineParser;
8
parse(CommandLineParser & parser,std::vector<std::string> params)9 bool parse(CommandLineParser& parser, std::vector<std::string> params)
10 {
11 params.insert(params.begin(), "app");
12 char** c_params = new char*[params.size()];
13 for (size_t i = 0; i < params.size(); i++)
14 {
15 c_params[i] = const_cast<char*>(params[i].c_str());
16 }
17 bool ret = parser.parse(params.size(), c_params);
18 delete [] c_params;
19 return ret;
20 }
21
TEST(TestCommandLineParser,ParseShouldFailIfInvalidPrefixUsed)22 TEST(TestCommandLineParser, ParseShouldFailIfInvalidPrefixUsed)
23 {
24 // Arrange
25 CommandLineParser parser("--", "test");
26 parser.register_option("opt", CommandLineParser::OptionInfo(false, false, ""));
27
28 // Act & Assert
29 ASSERT_FALSE(parse(parser, {"-test"}));
30 }
31
TEST(TestCommandLineParser,ParseShouldFailIfOptionNotRegistered)32 TEST(TestCommandLineParser, ParseShouldFailIfOptionNotRegistered)
33 {
34 // Arrange
35 CommandLineParser parser("--", "test");
36 parser.register_option("opt", CommandLineParser::OptionInfo(false, false, ""));
37
38 // Act & Assert
39 ASSERT_FALSE(parse(parser, {"--opt2"}));
40 }
41
TEST(TestCommandLineParser,ParseShouldNotFailIfOptionIsFlagAndDoesNotHaveValue)42 TEST(TestCommandLineParser, ParseShouldNotFailIfOptionIsFlagAndDoesNotHaveValue)
43 {
44 // Arrange
45 CommandLineParser parser("--", "test");
46 parser.register_option("opt", CommandLineParser::OptionInfo(true, false, ""));
47 parser.register_option("other", CommandLineParser::OptionInfo(false, false, ""));
48
49 // Act & Assert
50 ASSERT_TRUE(parse(parser, {"--opt", "--other", "value"}));
51 }
52
TEST(TestCommandLineParser,ParseShouldFailIfOptionIsFlagAndHasValue)53 TEST(TestCommandLineParser, ParseShouldFailIfOptionIsFlagAndHasValue)
54 {
55 // Arrange
56 CommandLineParser parser("--", "test");
57 parser.register_option("opt", CommandLineParser::OptionInfo(true, false, ""));
58
59 // Act & Assert
60 ASSERT_FALSE(parse(parser, {"--opt", "value"}));
61 }
62
TEST(TestCommandLineParser,ParseShouldFailIfMandatoryOptionNotSpecified)63 TEST(TestCommandLineParser, ParseShouldFailIfMandatoryOptionNotSpecified)
64 {
65 // Arrange
66 CommandLineParser parser("--", "test");
67 parser.register_option("opt1", CommandLineParser::OptionInfo(false, false, ""));
68 parser.register_option("opt2", CommandLineParser::OptionInfo(false, true, ""));
69
70 // Act & Assert
71 ASSERT_FALSE(parse(parser, {"--opt1", "value"}));
72 }
73
TEST(TestCommandLineParser,ParseShouldNotFailIfOptionalOptionNotSpecified)74 TEST(TestCommandLineParser, ParseShouldNotFailIfOptionalOptionNotSpecified)
75 {
76 // Arrange
77 CommandLineParser parser("--", "test");
78 parser.register_option("opt1", CommandLineParser::OptionInfo(false, false, ""));
79 parser.register_option("opt2", CommandLineParser::OptionInfo(false, false, ""));
80
81 // Act & Assert
82 ASSERT_TRUE(parse(parser, {"--opt1", "value"}));
83 }
84
TEST(TestCommandLineParser,ParseShouldFailIfOptionMissesValue)85 TEST(TestCommandLineParser, ParseShouldFailIfOptionMissesValue)
86 {
87 // Arrange
88 CommandLineParser parser("--", "test");
89 parser.register_option("opt1", CommandLineParser::OptionInfo(false, false, ""));
90
91 // Act & Assert
92 ASSERT_FALSE(parse(parser, {"--opt1"}));
93 }
94
TEST(TestCommandLineParser,GetValueShouldReturnDefaultValueIfValueNotSpecified)95 TEST(TestCommandLineParser, GetValueShouldReturnDefaultValueIfValueNotSpecified)
96 {
97 // Arrange
98 CommandLineParser parser("--", "test");
99 parser.register_option("opt1", CommandLineParser::OptionInfo(false, false, ""));
100 ASSERT_TRUE(parse(parser, {}));
101
102 // Act & Assert
103 ASSERT_STREQ("default", parser.get_value("opt1", "default").c_str());
104 }
105
TEST(TestCommandLineParser,GetValueShouldReturnUserSpecifiedValueIfValue)106 TEST(TestCommandLineParser, GetValueShouldReturnUserSpecifiedValueIfValue)
107 {
108 // Arrange
109 CommandLineParser parser("--", "test");
110 parser.register_option("opt1", CommandLineParser::OptionInfo(false, false, ""));
111 ASSERT_TRUE(parse(parser, {"--opt1", "value"}));
112
113 // Act & Assert
114 ASSERT_STREQ("value", parser.get_value("opt1", "default").c_str());
115 }
116
TEST(TestCommandLineParser,HasValueShouldReturnTrueIfValueSpecified)117 TEST(TestCommandLineParser, HasValueShouldReturnTrueIfValueSpecified)
118 {
119 // Arrange
120 CommandLineParser parser("--", "test");
121 parser.register_option("opt1", CommandLineParser::OptionInfo(true, false, ""));
122 ASSERT_TRUE(parse(parser, {"--opt1"}));
123
124 // Act & Assert
125 ASSERT_TRUE(parser.has_value("opt1"));
126 }
127
TEST(TestCommandLineParser,HasValueShouldReturnFalseIfValueNotSpecified)128 TEST(TestCommandLineParser, HasValueShouldReturnFalseIfValueNotSpecified)
129 {
130 // Arrange
131 CommandLineParser parser("--", "test");
132 parser.register_option("opt1", CommandLineParser::OptionInfo(true, false, ""));
133 ASSERT_TRUE(parse(parser, {}));
134
135 // Act & Assert
136 ASSERT_FALSE(parser.has_value("opt1"));
137 }
138
TEST(TestCommandLineParser,RegisterMandatoryFlagShouldFail)139 TEST(TestCommandLineParser, RegisterMandatoryFlagShouldFail)
140 {
141 // Arrange
142 CommandLineParser parser("--", "test");
143
144 // Act
145 parser.register_option("opt1", CommandLineParser::OptionInfo(true, true, ""));
146
147 // Assert
148 ASSERT_FALSE(parse(parser, {"--opt1", "value"})); // should fail as "opt1" is not registered
149 }
150
TEST(TestCommandLineParser,PrintHelpShouldContainAllOptions)151 TEST(TestCommandLineParser, PrintHelpShouldContainAllOptions)
152 {
153 // Arrange
154 CommandLineParser parser("--", "test");
155 parser.register_option("opt1", CommandLineParser::OptionInfo(false, false, ""));
156 parser.register_option("opt2", CommandLineParser::OptionInfo(false, true, ""));
157
158 std::stringstream ss;
159
160 // Act
161 parser.print_help(ss);
162
163 // Assert
164 std::string help_str = ss.str();
165 ASSERT_NE(std::string::npos, help_str.find("--opt1"));
166 ASSERT_NE(std::string::npos, help_str.find("--opt2"));
167 }
168