32 class CommandlineflagsTest :
public ::testing::Test {
35 std::locale::global(std::locale(
""));
38 void TestParser(
int argc,
const char** const_argv) {
39 TestParser(
"", argc, const_argv);
41 void TestParser(
const char* usage,
int argc,
const char** const_argv) {
43 char** argv = const_cast<char**>(const_argv);
48 TEST_F(CommandlineflagsTest, RemoveFlags) {
49 const char* const_argv[] = {
"Progname",
"--foo_int",
"3",
"file1.h",
52 char** argv = const_cast<char**>(const_argv);
57 EXPECT_STREQ(
"Progname", argv[0]);
58 EXPECT_STREQ(
"file1.h", argv[1]);
59 EXPECT_STREQ(
"file2.h", argv[2]);
62 #if 0 // TODO: this test needs an update (it currently fails).
63 TEST_F(CommandlineflagsTest, PrintUsageAndExit) {
64 const char* argv[] = {
"Progname",
"--help" };
65 EXPECT_EXIT(TestParser(
"Progname [flags]",
ARRAYSIZE(argv), argv),
66 ::testing::ExitedWithCode(0),
67 "USAGE: Progname \\[flags\\]");
71 TEST_F(CommandlineflagsTest, ExitsWithErrorOnInvalidFlag) {
72 const char* argv[] = {
"",
"--test_nonexistent_flag"};
73 EXPECT_EXIT(TestParser(
ARRAYSIZE(argv), argv), ::testing::ExitedWithCode(1),
74 "ERROR: Non-existent flag");
77 TEST_F(CommandlineflagsTest, ParseIntegerFlags) {
78 const char* argv[] = {
"",
"--foo_int=3",
"--bar_int",
"-4"};
80 EXPECT_EQ(3, FLAGS_foo_int);
81 EXPECT_EQ(-4, FLAGS_bar_int);
83 const char* arg_no_value[] = {
"",
"--bar_int"};
84 EXPECT_EXIT(TestParser(
ARRAYSIZE(arg_no_value), arg_no_value),
85 ::testing::ExitedWithCode(1),
"ERROR");
87 const char* arg_invalid_value[] = {
"",
"--bar_int",
"--foo_int=3"};
88 EXPECT_EXIT(TestParser(
ARRAYSIZE(arg_invalid_value), arg_invalid_value),
89 ::testing::ExitedWithCode(1),
"ERROR");
91 const char* arg_bad_format[] = {
"",
"--bar_int="};
92 EXPECT_EXIT(TestParser(
ARRAYSIZE(arg_bad_format), arg_bad_format),
93 ::testing::ExitedWithCode(1),
"ERROR");
96 TEST_F(CommandlineflagsTest, ParseDoubleFlags) {
97 const char* argv[] = {
"",
"--foo_double=3.14",
"--bar_double",
"1.2"};
100 EXPECT_EQ(3.14, FLAGS_foo_double);
101 EXPECT_EQ(1.2, FLAGS_bar_double);
103 const char* arg_no_value[] = {
"",
"--bar_double"};
104 EXPECT_EXIT(TestParser(2, arg_no_value), ::testing::ExitedWithCode(1),
107 const char* arg_bad_format[] = {
"",
"--bar_double="};
108 EXPECT_EXIT(TestParser(2, arg_bad_format), ::testing::ExitedWithCode(1),
112 TEST_F(CommandlineflagsTest, ParseStringFlags) {
113 const char* argv[] = {
"",
"--foo_string=abc",
"--bar_string",
"def"};
116 EXPECT_STREQ(
"abc", FLAGS_foo_string.c_str());
117 EXPECT_STREQ(
"def", FLAGS_bar_string.c_str());
119 const char* arg_no_value[] = {
"",
"--bar_string"};
120 EXPECT_EXIT(TestParser(2, arg_no_value), ::testing::ExitedWithCode(1),
123 FLAGS_bar_string.set_value(
"bar");
124 const char* arg_empty_string[] = {
"",
"--bar_string="};
125 TestParser(2, arg_empty_string);
126 EXPECT_STREQ(
"", FLAGS_bar_string.c_str());
129 TEST_F(CommandlineflagsTest, ParseBoolFlags) {
130 const char* argv[] = {
"",
"--foo_bool=true",
"--bar_bool=1"};
131 FLAGS_foo_bool.set_value(
false);
132 FLAGS_bar_bool.set_value(
false);
135 EXPECT_TRUE(FLAGS_foo_bool);
136 EXPECT_TRUE(FLAGS_bar_bool);
138 const char* inv_argv[] = {
"",
"--foo_bool=false",
"--bar_bool=0"};
139 FLAGS_foo_bool.set_value(
true);
140 FLAGS_bar_bool.set_value(
true);
141 TestParser(3, inv_argv);
143 EXPECT_FALSE(FLAGS_foo_bool);
144 EXPECT_FALSE(FLAGS_bar_bool);
146 const char* arg_implied_true[] = {
"",
"--bar_bool"};
147 FLAGS_bar_bool.set_value(
false);
148 TestParser(2, arg_implied_true);
149 EXPECT_TRUE(FLAGS_bar_bool);
151 const char* arg_missing_val[] = {
"",
"--bar_bool="};
152 EXPECT_EXIT(TestParser(2, arg_missing_val), ::testing::ExitedWithCode(1),
156 TEST_F(CommandlineflagsTest, ParseOldFlags) {
157 EXPECT_STREQ(
"", FLAGS_q.c_str());
158 const char* argv[] = {
"",
"-q",
"text"};
160 EXPECT_STREQ(
"text", FLAGS_q.c_str());