1
// Copyright (c) 2005, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
8
// * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above
11
// copyright notice, this list of conditions and the following disclaimer
12
// in the documentation and/or other materials provided with the
14
// * Neither the name of Google Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from
16
// this software without specific prior written permission.
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
// For now, this unit test does not cover all features of
35
#include <gflags/gflags.h>
40
#include <math.h> // for isinf() and isnan()
45
# include <unistd.h> // for unlink()
52
// I don't actually use this header file, but #include it under the
53
// old location to make sure that the include-header-forwarding
54
// works. But don't bother on windows; the windows port is so new
55
// it never had the old location-names.
57
#include <gflags/gflags_completions.h>
58
void (*unused_fn)() = &GFLAGS_NAMESPACE::HandleCommandLineCompletions;
63
using GFLAGS_NAMESPACE::int32;
64
using GFLAGS_NAMESPACE::FlagRegisterer;
65
using GFLAGS_NAMESPACE::StringFromEnv;
66
using GFLAGS_NAMESPACE::RegisterFlagValidator;
67
using GFLAGS_NAMESPACE::CommandLineFlagInfo;
68
using GFLAGS_NAMESPACE::GetAllFlags;
70
DEFINE_string(test_tmpdir, "", "Dir we use for temp files");
71
DEFINE_string(srcdir, StringFromEnv("SRCDIR", "."), "Source-dir root, needed to find gflags_unittest_flagfile");
73
DECLARE_string(tryfromenv); // in gflags.cc
75
DEFINE_bool(test_bool, false, "tests bool-ness");
76
DEFINE_int32(test_int32, -1, "");
77
DEFINE_int64(test_int64, -2, "");
78
DEFINE_uint32(test_uint32, 1, "");
79
DEFINE_uint64(test_uint64, 2, "");
80
DEFINE_double(test_double, -1.0, "");
81
DEFINE_string(test_string, "initial", "");
84
// The below ugliness gets some additional code coverage in the -helpxml
85
// and -helpmatch test cases having to do with string lengths and formatting
87
DEFINE_bool(test_bool_with_quite_quite_quite_quite_quite_quite_quite_quite_quite_quite_quite_quite_quite_quite_long_name,
89
"extremely_extremely_extremely_extremely_extremely_extremely_extremely_extremely_long_meaning");
91
DEFINE_string(test_str1, "initial", "");
92
DEFINE_string(test_str2, "initial", "");
93
DEFINE_string(test_str3, "initial", "");
95
// This is used to test setting tryfromenv manually
96
DEFINE_string(test_tryfromenv, "initial", "");
98
// Don't try this at home!
99
static int changeable_var = 12;
100
DEFINE_int32(changeable_var, ++changeable_var, "");
102
static int changeable_bool_var = 8008;
103
DEFINE_bool(changeable_bool_var, ++changeable_bool_var == 8009, "");
105
static int changeable_string_var = 0;
106
static string ChangeableString() {
107
char r[] = {static_cast<char>('0' + ++changeable_string_var), '\0'};
110
DEFINE_string(changeable_string_var, ChangeableString(), "");
112
// These are never used in this unittest, but can be used by
113
// gflags_unittest.sh when it needs to specify flags
114
// that are legal for gflags_unittest but don't need to
115
// be a particular value.
116
DEFINE_bool(unused_bool, true, "unused bool-ness");
117
DEFINE_int32(unused_int32, -1001, "");
118
DEFINE_int64(unused_int64, -2001, "");
119
DEFINE_uint32(unused_uint32, 1000, "");
120
DEFINE_uint64(unused_uint64, 2000, "");
121
DEFINE_double(unused_double, -1000.0, "");
122
DEFINE_string(unused_string, "unused", "");
124
// These flags are used by gflags_unittest.sh
125
DEFINE_bool(changed_bool1, false, "changed");
126
DEFINE_bool(changed_bool2, false, "changed");
127
DEFINE_bool(long_helpstring, false,
128
"This helpstring goes on forever and ever and ever and ever and "
129
"ever and ever and ever and ever and ever and ever and ever and "
130
"ever and ever and ever and ever and ever and ever and ever and "
131
"ever and ever and ever and ever and ever and ever and ever and "
132
"ever and ever and ever and ever and ever and ever and ever and "
133
"ever and ever and ever and ever and ever and ever and ever and "
134
"ever and ever and ever and ever and ever and ever and ever and "
135
"ever and ever and ever and ever and ever and ever and ever and "
136
"ever and ever and ever and ever and ever and ever and ever and "
137
"ever and ever and ever and ever and ever and ever and ever and "
138
"ever. This is the end of a long helpstring");
141
static bool AlwaysFail(const char* flag, bool value) { return value == false; }
142
DEFINE_bool(always_fail, false, "will fail to validate when you set it");
143
DEFINE_validator(always_fail, AlwaysFail);
145
// See the comment by GetAllFlags in gflags.h
146
static bool DeadlockIfCantLockInValidators(const char* flag, bool value) {
150
vector<CommandLineFlagInfo> dummy;
154
DEFINE_bool(deadlock_if_cant_lock,
156
"will deadlock if set to true and "
157
"if locking of registry in validators fails.");
158
DEFINE_validator(deadlock_if_cant_lock, DeadlockIfCantLockInValidators);
160
#define MAKEFLAG(x) DEFINE_int32(test_flag_num##x, x, "Test flag")
163
#define MAKEFLAG10(x) \
176
#define MAKEFLAG100(x) \
188
// Define a bunch of command-line flags. Each occurrence of the MAKEFLAG100
189
// macro defines 100 integer flags. This lets us test the effect of having
190
// many flags on startup time.
211
// This is a pseudo-flag -- we want to register a flag with a filename
212
// at the top level, but there is no way to do this except by faking
215
static const int32 FLAGS_nonotldflag1 = 12;
216
int32 FLAGS_tldflag1 = FLAGS_nonotldflag1;
217
int32 FLAGS_notldflag1 = FLAGS_nonotldflag1;
218
static FlagRegisterer o_tldflag1(
220
"should show up in --helpshort", "gflags_unittest.cc",
221
&FLAGS_tldflag1, &FLAGS_notldflag1);
223
using fLI::FLAGS_tldflag1;
226
static const int32 FLAGS_nonotldflag2 = 23;
227
int32 FLAGS_tldflag2 = FLAGS_nonotldflag2;
228
int32 FLAGS_notldflag2 = FLAGS_nonotldflag2;
229
static FlagRegisterer o_tldflag2(
231
"should show up in --helpshort", "gflags_unittest.",
232
&FLAGS_tldflag2, &FLAGS_notldflag2);
234
using fLI::FLAGS_tldflag2;
236
namespace GFLAGS_NAMESPACE {
241
static string TmpFile(const string& basename) {
243
return FLAGS_test_tmpdir + "\\" + basename;
245
return FLAGS_test_tmpdir + "/" + basename;
249
// Returns the definition of the --flagfile flag to be used in the tests.
250
// Must be called after ParseCommandLineFlags().
251
static const char* GetFlagFileFlag() {
253
static const string flagfile = FLAGS_srcdir + "\\gflags_unittest_flagfile";
255
static const string flagfile = FLAGS_srcdir + "/gflags_unittest_flagfile";
257
static const string flagfile_flag = string("--flagfile=") + flagfile;
258
return flagfile_flag.c_str();
262
// Defining a variable of type CompileAssertTypesEqual<T1, T2> will cause a
263
// compiler error iff T1 and T2 are different types.
264
template <typename T1, typename T2>
265
struct CompileAssertTypesEqual;
267
template <typename T>
268
struct CompileAssertTypesEqual<T, T> {
272
template <typename Expected, typename Actual>
273
void AssertIsType(Actual& x) {
274
CompileAssertTypesEqual<Expected, Actual>();
277
// Verify all the flags are the right type.
278
TEST(FlagTypes, FlagTypes) {
279
AssertIsType<bool>(FLAGS_test_bool);
280
AssertIsType<int32>(FLAGS_test_int32);
281
AssertIsType<int64>(FLAGS_test_int64);
282
AssertIsType<uint32>(FLAGS_test_uint32);
283
AssertIsType<uint64>(FLAGS_test_uint64);
284
AssertIsType<double>(FLAGS_test_double);
285
AssertIsType<string>(FLAGS_test_string);
288
#ifdef GTEST_HAS_DEATH_TEST
289
// Death tests for "help" options.
291
// The help system automatically calls gflags_exitfunc(1) when you specify any of
292
// the help-related flags ("-helpmatch", "-helpxml") so we can't test
295
// Tests that "-helpmatch" causes the process to die.
296
TEST(ReadFlagsFromStringDeathTest, HelpMatch) {
297
EXPECT_DEATH(ReadFlagsFromString("-helpmatch=base", GetArgv0(), true),
302
// Tests that "-helpxml" causes the process to die.
303
TEST(ReadFlagsFromStringDeathTest, HelpXml) {
304
EXPECT_DEATH(ReadFlagsFromString("-helpxml", GetArgv0(), true),
310
// A subroutine needed for testing reading flags from a string.
311
void TestFlagString(const string& flags,
312
const string& expected_string,
314
int32 expected_int32,
315
double expected_double) {
316
EXPECT_TRUE(ReadFlagsFromString(flags,
321
EXPECT_EQ(expected_string, FLAGS_test_string);
322
EXPECT_EQ(expected_bool, FLAGS_test_bool);
323
EXPECT_EQ(expected_int32, FLAGS_test_int32);
324
EXPECT_DOUBLE_EQ(expected_double, FLAGS_test_double);
328
// Tests reading flags from a string.
329
TEST(FlagFileTest, ReadFlagsFromString) {
332
"-test_string=continued\n"
333
"# some comments are in order\n"
341
"-test_double=0.0\n",
350
"# let's make sure it can update values\n"
351
"-test_string=initial\n"
354
"-test_double=123.0\n",
361
// Test that flags can use dashes instead of underscores.
364
"-test-string=initial\n"
365
"--test-bool=false\n"
367
"--test-double=123.0\n",
375
// Tests the filename part of the flagfile
376
TEST(FlagFileTest, FilenamesOurfileLast) {
377
FLAGS_test_string = "initial";
378
FLAGS_test_bool = false;
379
FLAGS_test_int32 = -1;
380
FLAGS_test_double = -1.0;
383
"-test_string=continued\n"
384
"# some comments are in order\n"
394
"-test_double=1000.0\n",
402
TEST(FlagFileTest, FilenamesOurfileFirst) {
403
FLAGS_test_string = "initial";
404
FLAGS_test_bool = false;
405
FLAGS_test_int32 = -1;
406
FLAGS_test_double = -1.0;
409
"-test_string=continued\n"
410
"# some comments are in order\n"
420
"-test_double=1000.0\n",
428
#if defined(HAVE_FNMATCH_H) || defined(HAVE_SHLWAPI_H) // otherwise glob isn't supported
429
TEST(FlagFileTest, FilenamesOurfileGlob) {
430
FLAGS_test_string = "initial";
431
FLAGS_test_bool = false;
432
FLAGS_test_int32 = -1;
433
FLAGS_test_double = -1.0;
436
"-test_string=continued\n"
437
"# some comments are in order\n"
447
"-test_double=1000.0\n",
455
TEST(FlagFileTest, FilenamesOurfileInBigList) {
456
FLAGS_test_string = "initial";
457
FLAGS_test_bool = false;
458
FLAGS_test_int32 = -1;
459
FLAGS_test_double = -1.0;
462
"-test_string=continued\n"
463
"# some comments are in order\n"
469
"*first* *flags* *third*\n"
473
"-test_double=1000.0\n",
480
#endif // defined(HAVE_FNMATCH_H) || defined(HAVE_SHLWAPI_H)
482
// Tests that a failed flag-from-string read keeps flags at default values
483
TEST(FlagFileTest, FailReadFlagsFromString) {
484
FLAGS_test_int32 = 119;
485
string flags("# let's make sure it can update values\n"
486
"-test_string=non_initial\n"
489
"-test_double=illegal\n");
491
EXPECT_FALSE(ReadFlagsFromString(flags,
496
EXPECT_EQ(119, FLAGS_test_int32);
497
EXPECT_EQ("initial", FLAGS_test_string);
500
// Tests that flags can be set to ordinary values.
501
TEST(SetFlagValueTest, OrdinaryValues) {
502
EXPECT_EQ("initial", FLAGS_test_str1);
504
SetCommandLineOptionWithMode("test_str1", "second", SET_FLAG_IF_DEFAULT);
505
EXPECT_EQ("second", FLAGS_test_str1); // set; was default
507
SetCommandLineOptionWithMode("test_str1", "third", SET_FLAG_IF_DEFAULT);
508
EXPECT_EQ("second", FLAGS_test_str1); // already set once
510
FLAGS_test_str1 = "initial";
511
SetCommandLineOptionWithMode("test_str1", "third", SET_FLAG_IF_DEFAULT);
512
EXPECT_EQ("initial", FLAGS_test_str1); // still already set before
514
SetCommandLineOptionWithMode("test_str1", "third", SET_FLAGS_VALUE);
515
EXPECT_EQ("third", FLAGS_test_str1); // changed value
517
SetCommandLineOptionWithMode("test_str1", "fourth", SET_FLAGS_DEFAULT);
518
EXPECT_EQ("third", FLAGS_test_str1);
519
// value not changed (already set before)
521
EXPECT_EQ("initial", FLAGS_test_str2);
523
SetCommandLineOptionWithMode("test_str2", "second", SET_FLAGS_DEFAULT);
524
EXPECT_EQ("second", FLAGS_test_str2); // changed (was default)
526
FLAGS_test_str2 = "extra";
527
EXPECT_EQ("extra", FLAGS_test_str2);
529
FLAGS_test_str2 = "second";
530
SetCommandLineOptionWithMode("test_str2", "third", SET_FLAGS_DEFAULT);
531
EXPECT_EQ("third", FLAGS_test_str2); // still changed (was equal to default)
533
SetCommandLineOptionWithMode("test_str2", "fourth", SET_FLAG_IF_DEFAULT);
534
EXPECT_EQ("fourth", FLAGS_test_str2); // changed (was default)
536
EXPECT_EQ("initial", FLAGS_test_str3);
538
SetCommandLineOptionWithMode("test_str3", "second", SET_FLAGS_DEFAULT);
539
EXPECT_EQ("second", FLAGS_test_str3); // changed
541
FLAGS_test_str3 = "third";
542
SetCommandLineOptionWithMode("test_str3", "fourth", SET_FLAGS_DEFAULT);
543
EXPECT_EQ("third", FLAGS_test_str3); // not changed (was set)
545
SetCommandLineOptionWithMode("test_str3", "fourth", SET_FLAG_IF_DEFAULT);
546
EXPECT_EQ("third", FLAGS_test_str3); // not changed (was set)
548
SetCommandLineOptionWithMode("test_str3", "fourth", SET_FLAGS_VALUE);
549
EXPECT_EQ("fourth", FLAGS_test_str3); // changed value
553
// Tests that flags can be set to exceptional values.
554
// Note: apparently MINGW doesn't parse inf and nan correctly:
555
// http://www.mail-archive.com/bug-gnulib@gnu.org/msg09573.html
556
// This url says FreeBSD also has a problem, but I didn't see that.
557
TEST(SetFlagValueTest, ExceptionalValues) {
558
#if defined(isinf) && !defined(__MINGW32__)
559
EXPECT_EQ("test_double set to inf\n",
560
SetCommandLineOption("test_double", "inf"));
561
EXPECT_INF(FLAGS_test_double);
563
EXPECT_EQ("test_double set to inf\n",
564
SetCommandLineOption("test_double", "INF"));
565
EXPECT_INF(FLAGS_test_double);
568
// set some bad values
570
SetCommandLineOption("test_double", "0.1xxx"));
572
SetCommandLineOption("test_double", " "));
574
SetCommandLineOption("test_double", ""));
575
#if defined(isinf) && !defined(__MINGW32__)
576
EXPECT_EQ("test_double set to -inf\n",
577
SetCommandLineOption("test_double", "-inf"));
578
EXPECT_INF(FLAGS_test_double);
579
EXPECT_GT(0, FLAGS_test_double);
582
#if defined(isnan) && !defined(__MINGW32__)
583
EXPECT_EQ("test_double set to nan\n",
584
SetCommandLineOption("test_double", "NaN"));
585
EXPECT_NAN(FLAGS_test_double);
589
// Tests that integer flags can be specified in many ways
590
TEST(SetFlagValueTest, DifferentRadices) {
591
EXPECT_EQ("test_int32 set to 12\n",
592
SetCommandLineOption("test_int32", "12"));
594
EXPECT_EQ("test_int32 set to 16\n",
595
SetCommandLineOption("test_int32", "0x10"));
597
EXPECT_EQ("test_int32 set to 34\n",
598
SetCommandLineOption("test_int32", "0X22"));
600
// Leading 0 is *not* octal; it's still decimal
601
EXPECT_EQ("test_int32 set to 10\n",
602
SetCommandLineOption("test_int32", "010"));
605
// Tests what happens when you try to set a flag to an illegal value
606
TEST(SetFlagValueTest, IllegalValues) {
607
FLAGS_test_bool = true;
608
FLAGS_test_int32 = 119;
609
FLAGS_test_int64 = 1191;
610
FLAGS_test_uint32 = 11911;
611
FLAGS_test_uint64 = 119111;
614
SetCommandLineOption("test_bool", "12"));
617
SetCommandLineOption("test_uint32", "-1970"));
620
SetCommandLineOption("test_int32", "7000000000000"));
623
SetCommandLineOption("test_uint64", "-1"));
626
SetCommandLineOption("test_int64", "not a number!"));
628
// Test the empty string with each type of input
629
EXPECT_EQ("", SetCommandLineOption("test_bool", ""));
630
EXPECT_EQ("", SetCommandLineOption("test_int32", ""));
631
EXPECT_EQ("", SetCommandLineOption("test_int64", ""));
632
EXPECT_EQ("", SetCommandLineOption("test_uint32", ""));
633
EXPECT_EQ("", SetCommandLineOption("test_uint64", ""));
634
EXPECT_EQ("", SetCommandLineOption("test_double", ""));
635
EXPECT_EQ("test_string set to \n", SetCommandLineOption("test_string", ""));
637
EXPECT_TRUE(FLAGS_test_bool);
638
EXPECT_EQ(119, FLAGS_test_int32);
639
EXPECT_EQ(1191, FLAGS_test_int64);
640
EXPECT_EQ(11911, FLAGS_test_uint32);
641
EXPECT_EQ(119111, FLAGS_test_uint64);
645
// Tests that we only evaluate macro args once
646
TEST(MacroArgs, EvaluateOnce) {
647
EXPECT_EQ(13, FLAGS_changeable_var);
648
// Make sure we don't ++ the value somehow, when evaluating the flag.
649
EXPECT_EQ(13, FLAGS_changeable_var);
650
// Make sure the macro only evaluated this var once.
651
EXPECT_EQ(13, changeable_var);
652
// Make sure the actual value and default value are the same
653
SetCommandLineOptionWithMode("changeable_var", "21", SET_FLAG_IF_DEFAULT);
654
EXPECT_EQ(21, FLAGS_changeable_var);
657
TEST(MacroArgs, EvaluateOnceBool) {
658
EXPECT_TRUE(FLAGS_changeable_bool_var);
659
EXPECT_TRUE(FLAGS_changeable_bool_var);
660
EXPECT_EQ(8009, changeable_bool_var);
661
SetCommandLineOptionWithMode("changeable_bool_var", "false",
662
SET_FLAG_IF_DEFAULT);
663
EXPECT_FALSE(FLAGS_changeable_bool_var);
666
TEST(MacroArgs, EvaluateOnceStrings) {
667
EXPECT_EQ("1", FLAGS_changeable_string_var);
668
EXPECT_EQ("1", FLAGS_changeable_string_var);
669
EXPECT_EQ(1, changeable_string_var);
670
SetCommandLineOptionWithMode("changeable_string_var", "different",
671
SET_FLAG_IF_DEFAULT);
672
EXPECT_EQ("different", FLAGS_changeable_string_var);
675
// Tests that the FooFromEnv does the right thing
676
TEST(FromEnvTest, LegalValues) {
677
setenv("BOOL_VAL1", "true", 1);
678
setenv("BOOL_VAL2", "false", 1);
679
setenv("BOOL_VAL3", "1", 1);
680
setenv("BOOL_VAL4", "F", 1);
681
EXPECT_TRUE(BoolFromEnv("BOOL_VAL1", false));
682
EXPECT_FALSE(BoolFromEnv("BOOL_VAL2", true));
683
EXPECT_TRUE(BoolFromEnv("BOOL_VAL3", false));
684
EXPECT_FALSE(BoolFromEnv("BOOL_VAL4", true));
685
EXPECT_TRUE(BoolFromEnv("BOOL_VAL_UNKNOWN", true));
686
EXPECT_FALSE(BoolFromEnv("BOOL_VAL_UNKNOWN", false));
688
setenv("INT_VAL1", "1", 1);
689
setenv("INT_VAL2", "-1", 1);
690
EXPECT_EQ(1, Int32FromEnv("INT_VAL1", 10));
691
EXPECT_EQ(-1, Int32FromEnv("INT_VAL2", 10));
692
EXPECT_EQ(10, Int32FromEnv("INT_VAL_UNKNOWN", 10));
694
setenv("INT_VAL3", "4294967295", 1);
695
EXPECT_EQ(1, Uint32FromEnv("INT_VAL1", 10));
696
EXPECT_EQ(4294967295L, Uint32FromEnv("INT_VAL3", 30));
697
EXPECT_EQ(10, Uint32FromEnv("INT_VAL_UNKNOWN", 10));
699
setenv("INT_VAL4", "1099511627776", 1);
700
EXPECT_EQ(1, Int64FromEnv("INT_VAL1", 20));
701
EXPECT_EQ(-1, Int64FromEnv("INT_VAL2", 20));
702
EXPECT_EQ(1099511627776LL, Int64FromEnv("INT_VAL4", 20));
703
EXPECT_EQ(20, Int64FromEnv("INT_VAL_UNKNOWN", 20));
705
EXPECT_EQ(1, Uint64FromEnv("INT_VAL1", 30));
706
EXPECT_EQ(1099511627776ULL, Uint64FromEnv("INT_VAL4", 30));
707
EXPECT_EQ(30, Uint64FromEnv("INT_VAL_UNKNOWN", 30));
709
// I pick values here that can be easily represented exactly in floating-point
710
setenv("DOUBLE_VAL1", "0.0", 1);
711
setenv("DOUBLE_VAL2", "1.0", 1);
712
setenv("DOUBLE_VAL3", "-1.0", 1);
713
EXPECT_EQ(0.0, DoubleFromEnv("DOUBLE_VAL1", 40.0));
714
EXPECT_EQ(1.0, DoubleFromEnv("DOUBLE_VAL2", 40.0));
715
EXPECT_EQ(-1.0, DoubleFromEnv("DOUBLE_VAL3", 40.0));
716
EXPECT_EQ(40.0, DoubleFromEnv("DOUBLE_VAL_UNKNOWN", 40.0));
718
setenv("STRING_VAL1", "", 1);
719
setenv("STRING_VAL2", "my happy string!", 1);
720
EXPECT_STREQ("", StringFromEnv("STRING_VAL1", "unknown"));
721
EXPECT_STREQ("my happy string!", StringFromEnv("STRING_VAL2", "unknown"));
722
EXPECT_STREQ("unknown", StringFromEnv("STRING_VAL_UNKNOWN", "unknown"));
725
#ifdef GTEST_HAS_DEATH_TEST
726
// Tests that the FooFromEnv dies on parse-error
727
TEST(FromEnvDeathTest, IllegalValues) {
728
setenv("BOOL_BAD1", "so true!", 1);
729
setenv("BOOL_BAD2", "", 1);
730
EXPECT_DEATH(BoolFromEnv("BOOL_BAD1", false), "error parsing env variable");
731
EXPECT_DEATH(BoolFromEnv("BOOL_BAD2", true), "error parsing env variable");
733
setenv("INT_BAD1", "one", 1);
734
setenv("INT_BAD2", "100000000000000000", 1);
735
setenv("INT_BAD3", "0xx10", 1);
736
setenv("INT_BAD4", "", 1);
737
EXPECT_DEATH(Int32FromEnv("INT_BAD1", 10), "error parsing env variable");
738
EXPECT_DEATH(Int32FromEnv("INT_BAD2", 10), "error parsing env variable");
739
EXPECT_DEATH(Int32FromEnv("INT_BAD3", 10), "error parsing env variable");
740
EXPECT_DEATH(Int32FromEnv("INT_BAD4", 10), "error parsing env variable");
742
EXPECT_DEATH(Uint32FromEnv("INT_BAD1", 10), "error parsing env variable");
743
EXPECT_DEATH(Uint32FromEnv("INT_BAD2", 10), "error parsing env variable");
744
EXPECT_DEATH(Uint32FromEnv("INT_BAD3", 10), "error parsing env variable");
745
EXPECT_DEATH(Uint32FromEnv("INT_BAD4", 10), "error parsing env variable");
747
setenv("BIGINT_BAD1", "18446744073709551616000", 1);
748
EXPECT_DEATH(Int64FromEnv("INT_BAD1", 20), "error parsing env variable");
749
EXPECT_DEATH(Int64FromEnv("INT_BAD3", 20), "error parsing env variable");
750
EXPECT_DEATH(Int64FromEnv("INT_BAD4", 20), "error parsing env variable");
751
EXPECT_DEATH(Int64FromEnv("BIGINT_BAD1", 200), "error parsing env variable");
753
setenv("BIGINT_BAD2", "-1", 1);
754
EXPECT_DEATH(Uint64FromEnv("INT_BAD1", 30), "error parsing env variable");
755
EXPECT_DEATH(Uint64FromEnv("INT_BAD3", 30), "error parsing env variable");
756
EXPECT_DEATH(Uint64FromEnv("INT_BAD4", 30), "error parsing env variable");
757
EXPECT_DEATH(Uint64FromEnv("BIGINT_BAD1", 30), "error parsing env variable");
758
// TODO(csilvers): uncomment this when we disallow negative numbers for uint64
760
EXPECT_DEATH(Uint64FromEnv("BIGINT_BAD2", 30), "error parsing env variable");
763
setenv("DOUBLE_BAD1", "0.0.0", 1);
764
setenv("DOUBLE_BAD2", "", 1);
765
EXPECT_DEATH(DoubleFromEnv("DOUBLE_BAD1", 40.0), "error parsing env variable");
766
EXPECT_DEATH(DoubleFromEnv("DOUBLE_BAD2", 40.0), "error parsing env variable");
771
// Tests that FlagSaver can save the states of string flags.
772
TEST(FlagSaverTest, CanSaveStringFlagStates) {
773
// 1. Initializes the flags.
775
// State of flag test_str1:
776
// default value - "initial"
777
// current value - "initial"
780
SetCommandLineOptionWithMode("test_str2", "second", SET_FLAGS_VALUE);
781
// State of flag test_str2:
782
// default value - "initial"
783
// current value - "second"
786
SetCommandLineOptionWithMode("test_str3", "second", SET_FLAGS_DEFAULT);
787
// State of flag test_str3:
788
// default value - "second"
789
// current value - "second"
792
// 2. Saves the flag states.
797
// 3. Modifies the flag states.
799
SetCommandLineOptionWithMode("test_str1", "second", SET_FLAGS_VALUE);
800
EXPECT_EQ("second", FLAGS_test_str1);
801
// State of flag test_str1:
802
// default value - "second"
803
// current value - "second"
806
SetCommandLineOptionWithMode("test_str2", "third", SET_FLAGS_DEFAULT);
807
EXPECT_EQ("second", FLAGS_test_str2);
808
// State of flag test_str2:
809
// default value - "third"
810
// current value - "second"
813
SetCommandLineOptionWithMode("test_str3", "third", SET_FLAGS_VALUE);
814
EXPECT_EQ("third", FLAGS_test_str3);
815
// State of flag test_str1:
816
// default value - "second"
817
// current value - "third"
820
// 4. Restores the flag states.
823
// 5. Verifies that the states were restored.
825
// Verifies that the value of test_str1 was restored.
826
EXPECT_EQ("initial", FLAGS_test_str1);
827
// Verifies that the "not set" attribute of test_str1 was restored to true.
828
SetCommandLineOptionWithMode("test_str1", "second", SET_FLAG_IF_DEFAULT);
829
EXPECT_EQ("second", FLAGS_test_str1);
831
// Verifies that the value of test_str2 was restored.
832
EXPECT_EQ("second", FLAGS_test_str2);
833
// Verifies that the "not set" attribute of test_str2 was restored to false.
834
SetCommandLineOptionWithMode("test_str2", "fourth", SET_FLAG_IF_DEFAULT);
835
EXPECT_EQ("second", FLAGS_test_str2);
837
// Verifies that the value of test_str3 was restored.
838
EXPECT_EQ("second", FLAGS_test_str3);
839
// Verifies that the "not set" attribute of test_str3 was restored to true.
840
SetCommandLineOptionWithMode("test_str3", "fourth", SET_FLAG_IF_DEFAULT);
841
EXPECT_EQ("fourth", FLAGS_test_str3);
845
// Tests that FlagSaver can save the values of various-typed flags.
846
TEST(FlagSaverTest, CanSaveVariousTypedFlagValues) {
847
// Initializes the flags.
848
FLAGS_test_bool = false;
849
FLAGS_test_int32 = -1;
850
FLAGS_test_uint32 = 2;
851
FLAGS_test_int64 = -3;
852
FLAGS_test_uint64 = 4;
853
FLAGS_test_double = 5.0;
854
FLAGS_test_string = "good";
856
// Saves the flag states.
860
// Modifies the flags.
861
FLAGS_test_bool = true;
862
FLAGS_test_int32 = -5;
863
FLAGS_test_uint32 = 6;
864
FLAGS_test_int64 = -7;
865
FLAGS_test_uint64 = 8;
866
FLAGS_test_double = 8.0;
867
FLAGS_test_string = "bad";
869
// Restores the flag states.
872
// Verifies the flag values were restored.
873
EXPECT_FALSE(FLAGS_test_bool);
874
EXPECT_EQ(-1, FLAGS_test_int32);
875
EXPECT_EQ(2, FLAGS_test_uint32);
876
EXPECT_EQ(-3, FLAGS_test_int64);
877
EXPECT_EQ(4, FLAGS_test_uint64);
878
EXPECT_DOUBLE_EQ(5.0, FLAGS_test_double);
879
EXPECT_EQ("good", FLAGS_test_string);
882
TEST(GetAllFlagsTest, BaseTest) {
883
vector<CommandLineFlagInfo> flags;
885
bool found_test_bool = false;
886
vector<CommandLineFlagInfo>::const_iterator i;
887
for (i = flags.begin(); i != flags.end(); ++i) {
888
if (i->name == "test_bool") {
889
found_test_bool = true;
890
EXPECT_EQ(i->type, "bool");
891
EXPECT_EQ(i->default_value, "false");
892
EXPECT_EQ(i->flag_ptr, &FLAGS_test_bool);
896
EXPECT_TRUE(found_test_bool);
899
TEST(ShowUsageWithFlagsTest, BaseTest) {
900
// TODO(csilvers): test this by allowing output other than to stdout.
901
// Not urgent since this functionality is tested via
902
// gflags_unittest.sh, though only through use of --help.
905
TEST(ShowUsageWithFlagsRestrictTest, BaseTest) {
906
// TODO(csilvers): test this by allowing output other than to stdout.
907
// Not urgent since this functionality is tested via
908
// gflags_unittest.sh, though only through use of --helpmatch.
911
// Note: all these argv-based tests depend on SetArgv being called
912
// before ParseCommandLineFlags() in main(), below.
913
TEST(GetArgvsTest, BaseTest) {
914
vector<string> argvs = GetArgvs();
915
EXPECT_EQ(4, argvs.size());
916
EXPECT_EQ("/test/argv/for/gflags_unittest", argvs[0]);
917
EXPECT_EQ("argv 2", argvs[1]);
918
EXPECT_EQ("3rd argv", argvs[2]);
919
EXPECT_EQ("argv #4", argvs[3]);
922
TEST(GetArgvTest, BaseTest) {
923
EXPECT_STREQ("/test/argv/for/gflags_unittest "
924
"argv 2 3rd argv argv #4", GetArgv());
927
TEST(GetArgv0Test, BaseTest) {
928
EXPECT_STREQ("/test/argv/for/gflags_unittest", GetArgv0());
931
TEST(GetArgvSumTest, BaseTest) {
932
// This number is just the sum of the ASCII values of all the chars
934
EXPECT_EQ(4904, GetArgvSum());
937
TEST(ProgramInvocationNameTest, BaseTest) {
938
EXPECT_STREQ("/test/argv/for/gflags_unittest",
939
ProgramInvocationName());
942
TEST(ProgramInvocationShortNameTest, BaseTest) {
943
EXPECT_STREQ("gflags_unittest", ProgramInvocationShortName());
946
TEST(ProgramUsageTest, BaseTest) { // Depends on 1st arg to ParseCommandLineFlags()
947
EXPECT_STREQ("/test/argv/for/gflags_unittest: "
948
"<useless flag> [...]\nDoes something useless.\n",
952
TEST(GetCommandLineOptionTest, NameExistsAndIsDefault) {
953
string value("will be changed");
954
bool r = GetCommandLineOption("test_bool", &value);
956
EXPECT_EQ("false", value);
958
r = GetCommandLineOption("test_int32", &value);
960
EXPECT_EQ("-1", value);
963
TEST(GetCommandLineOptionTest, NameExistsAndWasAssigned) {
964
FLAGS_test_int32 = 400;
965
string value("will be changed");
966
const bool r = GetCommandLineOption("test_int32", &value);
968
EXPECT_EQ("400", value);
971
TEST(GetCommandLineOptionTest, NameExistsAndWasSet) {
972
SetCommandLineOption("test_int32", "700");
973
string value("will be changed");
974
const bool r = GetCommandLineOption("test_int32", &value);
976
EXPECT_EQ("700", value);
979
TEST(GetCommandLineOptionTest, NameExistsAndWasNotSet) {
980
// This doesn't set the flag's value, but rather its default value.
981
// is_default is still true, but the 'default' value returned has changed!
982
SetCommandLineOptionWithMode("test_int32", "800", SET_FLAGS_DEFAULT);
983
string value("will be changed");
984
const bool r = GetCommandLineOption("test_int32", &value);
986
EXPECT_EQ("800", value);
987
EXPECT_TRUE(GetCommandLineFlagInfoOrDie("test_int32").is_default);
990
TEST(GetCommandLineOptionTest, NameExistsAndWasConditionallySet) {
991
SetCommandLineOptionWithMode("test_int32", "900", SET_FLAG_IF_DEFAULT);
992
string value("will be changed");
993
const bool r = GetCommandLineOption("test_int32", &value);
995
EXPECT_EQ("900", value);
998
TEST(GetCommandLineOptionTest, NameDoesNotExist) {
999
string value("will not be changed");
1000
const bool r = GetCommandLineOption("test_int3210", &value);
1002
EXPECT_EQ("will not be changed", value);
1005
TEST(GetCommandLineFlagInfoTest, FlagExists) {
1006
CommandLineFlagInfo info;
1007
bool r = GetCommandLineFlagInfo("test_int32", &info);
1009
EXPECT_EQ("test_int32", info.name);
1010
EXPECT_EQ("int32", info.type);
1011
EXPECT_EQ("", info.description);
1012
EXPECT_EQ("-1", info.current_value);
1013
EXPECT_EQ("-1", info.default_value);
1014
EXPECT_TRUE(info.is_default);
1015
EXPECT_FALSE(info.has_validator_fn);
1016
EXPECT_EQ(&FLAGS_test_int32, info.flag_ptr);
1018
FLAGS_test_bool = true;
1019
r = GetCommandLineFlagInfo("test_bool", &info);
1021
EXPECT_EQ("test_bool", info.name);
1022
EXPECT_EQ("bool", info.type);
1023
EXPECT_EQ("tests bool-ness", info.description);
1024
EXPECT_EQ("true", info.current_value);
1025
EXPECT_EQ("false", info.default_value);
1026
EXPECT_FALSE(info.is_default);
1027
EXPECT_FALSE(info.has_validator_fn);
1028
EXPECT_EQ(&FLAGS_test_bool, info.flag_ptr);
1030
FLAGS_test_bool = false;
1031
r = GetCommandLineFlagInfo("test_bool", &info);
1033
EXPECT_EQ("test_bool", info.name);
1034
EXPECT_EQ("bool", info.type);
1035
EXPECT_EQ("tests bool-ness", info.description);
1036
EXPECT_EQ("false", info.current_value);
1037
EXPECT_EQ("false", info.default_value);
1038
EXPECT_FALSE(info.is_default); // value is same, but flag *was* modified
1039
EXPECT_FALSE(info.has_validator_fn);
1040
EXPECT_EQ(&FLAGS_test_bool, info.flag_ptr);
1043
TEST(GetCommandLineFlagInfoTest, FlagDoesNotExist) {
1044
CommandLineFlagInfo info;
1045
// Set to some random values that GetCommandLineFlagInfo should not change
1048
info.current_value = "curr";
1049
info.default_value = "def";
1050
info.filename = "/";
1051
info.is_default = false;
1052
info.has_validator_fn = true;
1053
info.flag_ptr = NULL;
1054
bool r = GetCommandLineFlagInfo("test_int3210", &info);
1056
EXPECT_EQ("name", info.name);
1057
EXPECT_EQ("type", info.type);
1058
EXPECT_EQ("", info.description);
1059
EXPECT_EQ("curr", info.current_value);
1060
EXPECT_EQ("def", info.default_value);
1061
EXPECT_EQ("/", info.filename);
1062
EXPECT_FALSE(info.is_default);
1063
EXPECT_TRUE(info.has_validator_fn);
1064
EXPECT_EQ(NULL, info.flag_ptr);
1067
TEST(GetCommandLineFlagInfoOrDieTest, FlagExistsAndIsDefault) {
1068
CommandLineFlagInfo info;
1069
info = GetCommandLineFlagInfoOrDie("test_int32");
1070
EXPECT_EQ("test_int32", info.name);
1071
EXPECT_EQ("int32", info.type);
1072
EXPECT_EQ("", info.description);
1073
EXPECT_EQ("-1", info.current_value);
1074
EXPECT_EQ("-1", info.default_value);
1075
EXPECT_TRUE(info.is_default);
1076
EXPECT_EQ(&FLAGS_test_int32, info.flag_ptr);
1077
info = GetCommandLineFlagInfoOrDie("test_bool");
1078
EXPECT_EQ("test_bool", info.name);
1079
EXPECT_EQ("bool", info.type);
1080
EXPECT_EQ("tests bool-ness", info.description);
1081
EXPECT_EQ("false", info.current_value);
1082
EXPECT_EQ("false", info.default_value);
1083
EXPECT_TRUE(info.is_default);
1084
EXPECT_FALSE(info.has_validator_fn);
1085
EXPECT_EQ(&FLAGS_test_bool, info.flag_ptr);
1088
TEST(GetCommandLineFlagInfoOrDieTest, FlagExistsAndWasAssigned) {
1089
FLAGS_test_int32 = 400;
1090
CommandLineFlagInfo info;
1091
info = GetCommandLineFlagInfoOrDie("test_int32");
1092
EXPECT_EQ("test_int32", info.name);
1093
EXPECT_EQ("int32", info.type);
1094
EXPECT_EQ("", info.description);
1095
EXPECT_EQ("400", info.current_value);
1096
EXPECT_EQ("-1", info.default_value);
1097
EXPECT_FALSE(info.is_default);
1098
EXPECT_EQ(&FLAGS_test_int32, info.flag_ptr);
1099
FLAGS_test_bool = true;
1100
info = GetCommandLineFlagInfoOrDie("test_bool");
1101
EXPECT_EQ("test_bool", info.name);
1102
EXPECT_EQ("bool", info.type);
1103
EXPECT_EQ("tests bool-ness", info.description);
1104
EXPECT_EQ("true", info.current_value);
1105
EXPECT_EQ("false", info.default_value);
1106
EXPECT_FALSE(info.is_default);
1107
EXPECT_FALSE(info.has_validator_fn);
1108
EXPECT_EQ(&FLAGS_test_bool, info.flag_ptr);
1111
#ifdef GTEST_HAS_DEATH_TEST
1112
TEST(GetCommandLineFlagInfoOrDieDeathTest, FlagDoesNotExist) {
1113
EXPECT_DEATH(GetCommandLineFlagInfoOrDie("test_int3210"),
1114
".*: flag test_int3210 does not exist");
1119
// These are lightly tested because they're deprecated. Basically,
1120
// the tests are meant to cover how existing users use these functions,
1121
// but not necessarily how new users could use them.
1122
TEST(DeprecatedFunctionsTest, CommandlineFlagsIntoString) {
1123
string s = CommandlineFlagsIntoString();
1124
EXPECT_NE(string::npos, s.find("--test_bool="));
1127
TEST(DeprecatedFunctionsTest, AppendFlagsIntoFile) {
1128
FLAGS_test_int32 = 10; // just to make the test more interesting
1129
string filename(TmpFile("flagfile"));
1130
unlink(filename.c_str()); // just to be safe
1131
const bool r = AppendFlagsIntoFile(filename, "not the real argv0");
1135
EXPECT_EQ(0, SafeFOpen(&fp, filename.c_str(), "r"));
1136
EXPECT_TRUE(fp != NULL);
1138
EXPECT_TRUE(fgets(line, sizeof(line)-1, fp) != NULL); // get the first line
1139
// First line should be progname.
1140
EXPECT_STREQ("not the real argv0\n", line);
1142
bool found_bool = false, found_int32 = false;
1143
while (fgets(line, sizeof(line)-1, fp)) {
1144
line[sizeof(line)-1] = '\0'; // just to be safe
1145
if (strcmp(line, "--test_bool=false\n") == 0)
1147
if (strcmp(line, "--test_int32=10\n") == 0)
1150
EXPECT_TRUE(found_int32);
1151
EXPECT_TRUE(found_bool);
1155
TEST(DeprecatedFunctionsTest, ReadFromFlagsFile) {
1156
FLAGS_test_int32 = -10; // just to make the test more interesting
1157
string filename(TmpFile("flagfile2"));
1158
unlink(filename.c_str()); // just to be safe
1159
bool r = AppendFlagsIntoFile(filename, GetArgv0());
1162
FLAGS_test_int32 = -11;
1163
r = ReadFromFlagsFile(filename, GetArgv0(), true);
1165
EXPECT_EQ(-10, FLAGS_test_int32);
1166
} // unnamed namespace
1168
TEST(DeprecatedFunctionsTest, ReadFromFlagsFileFailure) {
1169
FLAGS_test_int32 = -20;
1170
string filename(TmpFile("flagfile3"));
1172
EXPECT_EQ(0, SafeFOpen(&fp, filename.c_str(), "w"));
1173
EXPECT_TRUE(fp != NULL);
1174
// Note the error in the bool assignment below...
1175
fprintf(fp, "%s\n--test_int32=-21\n--test_bool=not_a_bool!\n", GetArgv0());
1178
FLAGS_test_int32 = -22;
1179
const bool r = ReadFromFlagsFile(filename, GetArgv0(), false);
1181
EXPECT_EQ(-22, FLAGS_test_int32); // the -21 from the flagsfile didn't take
1184
TEST(FlagsSetBeforeInitTest, TryFromEnv) {
1185
EXPECT_EQ("pre-set", FLAGS_test_tryfromenv);
1188
// The following test case verifies that ParseCommandLineFlags() and
1189
// ParseCommandLineNonHelpFlags() uses the last definition of a flag
1190
// in case it's defined more than once.
1192
DEFINE_int32(test_flag, -1, "used for testing gflags.cc");
1194
// Parses and returns the --test_flag flag.
1195
// If with_help is true, calls ParseCommandLineFlags; otherwise calls
1196
// ParseCommandLineNonHelpFlags.
1197
int32 ParseTestFlag(bool with_help, int argc, const char** const_argv) {
1198
FlagSaver fs; // Restores the flags before returning.
1200
// Makes a copy of the input array s.t. it can be reused
1201
// (ParseCommandLineFlags() will alter the array).
1202
char** const argv_save = new char*[argc + 1];
1203
char** argv = argv_save;
1204
memcpy(argv, const_argv, sizeof(*argv)*(argc + 1));
1207
ParseCommandLineFlags(&argc, &argv, true);
1209
ParseCommandLineNonHelpFlags(&argc, &argv, true);
1213
return FLAGS_test_flag;
1216
TEST(ParseCommandLineFlagsUsesLastDefinitionTest,
1217
WhenFlagIsDefinedTwiceOnCommandLine) {
1218
const char* argv[] = {
1225
EXPECT_EQ(2, ParseTestFlag(true, arraysize(argv) - 1, argv));
1226
EXPECT_EQ(2, ParseTestFlag(false, arraysize(argv) - 1, argv));
1229
TEST(ParseCommandLineFlagsUsesLastDefinitionTest,
1230
WhenFlagIsDefinedTwiceInFlagFile) {
1231
const char* argv[] = {
1237
EXPECT_EQ(2, ParseTestFlag(true, arraysize(argv) - 1, argv));
1238
EXPECT_EQ(2, ParseTestFlag(false, arraysize(argv) - 1, argv));
1241
TEST(ParseCommandLineFlagsUsesLastDefinitionTest,
1242
WhenFlagIsDefinedInCommandLineAndThenFlagFile) {
1243
const char* argv[] = {
1250
EXPECT_EQ(2, ParseTestFlag(true, arraysize(argv) - 1, argv));
1251
EXPECT_EQ(2, ParseTestFlag(false, arraysize(argv) - 1, argv));
1254
TEST(ParseCommandLineFlagsUsesLastDefinitionTest,
1255
WhenFlagIsDefinedInFlagFileAndThenCommandLine) {
1256
const char* argv[] = {
1263
EXPECT_EQ(3, ParseTestFlag(true, arraysize(argv) - 1, argv));
1264
EXPECT_EQ(3, ParseTestFlag(false, arraysize(argv) - 1, argv));
1267
TEST(ParseCommandLineFlagsUsesLastDefinitionTest,
1268
WhenFlagIsDefinedInCommandLineAndFlagFileAndThenCommandLine) {
1269
const char* argv[] = {
1277
EXPECT_EQ(3, ParseTestFlag(true, arraysize(argv) - 1, argv));
1278
EXPECT_EQ(3, ParseTestFlag(false, arraysize(argv) - 1, argv));
1281
TEST(ParseCommandLineFlagsAndDashArgs, TwoDashArgFirst) {
1282
const char* argv[] = {
1289
EXPECT_EQ(-1, ParseTestFlag(true, arraysize(argv) - 1, argv));
1290
EXPECT_EQ(-1, ParseTestFlag(false, arraysize(argv) - 1, argv));
1293
TEST(ParseCommandLineFlagsAndDashArgs, TwoDashArgMiddle) {
1294
const char* argv[] = {
1302
EXPECT_EQ(7, ParseTestFlag(true, arraysize(argv) - 1, argv));
1303
EXPECT_EQ(7, ParseTestFlag(false, arraysize(argv) - 1, argv));
1306
TEST(ParseCommandLineFlagsAndDashArgs, OneDashArg) {
1307
const char* argv[] = {
1314
EXPECT_EQ(0, ParseTestFlag(true, arraysize(argv) - 1, argv));
1315
EXPECT_EQ(0, ParseTestFlag(false, arraysize(argv) - 1, argv));
1318
#ifdef GTEST_HAS_DEATH_TEST
1319
TEST(ParseCommandLineFlagsUnknownFlagDeathTest,
1320
FlagIsCompletelyUnknown) {
1321
const char* argv[] = {
1323
"--this_flag_does_not_exist",
1327
EXPECT_DEATH(ParseTestFlag(true, arraysize(argv) - 1, argv),
1328
"unknown command line flag.*");
1329
EXPECT_DEATH(ParseTestFlag(false, arraysize(argv) - 1, argv),
1330
"unknown command line flag.*");
1333
TEST(ParseCommandLineFlagsUnknownFlagDeathTest,
1334
BoolFlagIsCompletelyUnknown) {
1335
const char* argv[] = {
1337
"--nothis_flag_does_not_exist",
1341
EXPECT_DEATH(ParseTestFlag(true, arraysize(argv) - 1, argv),
1342
"unknown command line flag.*");
1343
EXPECT_DEATH(ParseTestFlag(false, arraysize(argv) - 1, argv),
1344
"unknown command line flag.*");
1347
TEST(ParseCommandLineFlagsUnknownFlagDeathTest,
1349
const char* argv[] = {
1355
EXPECT_DEATH(ParseTestFlag(true, arraysize(argv) - 1, argv),
1356
"boolean value .* specified for .* command line flag");
1357
EXPECT_DEATH(ParseTestFlag(false, arraysize(argv) - 1, argv),
1358
"boolean value .* specified for .* command line flag");
1362
TEST(ParseCommandLineFlagsWrongFields,
1363
DescriptionIsInvalid) {
1364
// These must not be automatic variables, since command line flags
1365
// aren't unregistered and gUnit uses FlagSaver to save and restore
1366
// command line flags' values. If these are on the stack, then when
1367
// later tests attempt to save and restore their values, the stack
1368
// addresses of these variables will be overwritten... Stack smash!
1369
static bool current_storage;
1370
static bool defvalue_storage;
1371
FlagRegisterer fr("flag_name", NULL, "filename",
1372
¤t_storage, &defvalue_storage);
1373
CommandLineFlagInfo fi;
1374
EXPECT_TRUE(GetCommandLineFlagInfo("flag_name", &fi));
1375
EXPECT_EQ("", fi.description);
1376
EXPECT_EQ(¤t_storage, fi.flag_ptr);
1379
static bool ValidateTestFlagIs5(const char* flagname, int32 flagval) {
1382
printf("%s isn't 5!\n", flagname);
1386
static bool ValidateTestFlagIs10(const char* flagname, int32 flagval) {
1387
return flagval == 10;
1391
TEST(FlagsValidator, ValidFlagViaArgv) {
1392
const char* argv[] = {
1397
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
1398
EXPECT_EQ(5, ParseTestFlag(true, arraysize(argv) - 1, argv));
1399
// Undo the flag validator setting
1400
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
1403
TEST(FlagsValidator, ValidFlagViaSetDefault) {
1404
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
1405
// SetCommandLineOptionWithMode returns the empty string on error.
1406
EXPECT_NE("", SetCommandLineOptionWithMode("test_flag", "5",
1407
SET_FLAG_IF_DEFAULT));
1408
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
1411
TEST(FlagsValidator, ValidFlagViaSetValue) {
1412
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
1413
FLAGS_test_flag = 100; // doesn't trigger the validator
1414
// SetCommandLineOptionWithMode returns the empty string on error.
1415
EXPECT_NE("", SetCommandLineOptionWithMode("test_flag", "5",
1417
EXPECT_NE("", SetCommandLineOptionWithMode("test_flag", "5",
1418
SET_FLAGS_DEFAULT));
1419
EXPECT_NE("", SetCommandLineOption("test_flag", "5"));
1420
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
1423
#ifdef GTEST_HAS_DEATH_TEST
1424
TEST(FlagsValidatorDeathTest, InvalidFlagViaArgv) {
1425
const char* argv[] = {
1430
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
1431
EXPECT_DEATH(ParseTestFlag(true, arraysize(argv) - 1, argv),
1432
"ERROR: failed validation of new value '50' for flag 'test_flag'");
1433
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
1437
TEST(FlagsValidator, InvalidFlagViaSetDefault) {
1438
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
1439
// SetCommandLineOptionWithMode returns the empty string on error.
1440
EXPECT_EQ("", SetCommandLineOptionWithMode("test_flag", "50",
1441
SET_FLAG_IF_DEFAULT));
1442
EXPECT_EQ(-1, FLAGS_test_flag); // the setting-to-50 should have failed
1443
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
1446
TEST(FlagsValidator, InvalidFlagViaSetValue) {
1447
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
1448
FLAGS_test_flag = 100; // doesn't trigger the validator
1449
// SetCommandLineOptionWithMode returns the empty string on error.
1450
EXPECT_EQ("", SetCommandLineOptionWithMode("test_flag", "50",
1452
EXPECT_EQ("", SetCommandLineOptionWithMode("test_flag", "50",
1453
SET_FLAGS_DEFAULT));
1454
EXPECT_EQ("", SetCommandLineOption("test_flag", "50"));
1455
EXPECT_EQ(100, FLAGS_test_flag); // the setting-to-50 should have failed
1456
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
1459
#ifdef GTEST_HAS_DEATH_TEST
1460
TEST(FlagsValidatorDeathTest, InvalidFlagNeverSet) {
1461
// If a flag keeps its default value, and that default value is
1462
// invalid, we should die at argv-parse time.
1463
const char* argv[] = {
1467
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
1468
EXPECT_DEATH(ParseTestFlag(true, arraysize(argv) - 1, argv),
1469
"ERROR: --test_flag must be set on the commandline");
1473
TEST(FlagsValidator, InvalidFlagPtr) {
1475
EXPECT_FALSE(RegisterFlagValidator(NULL, &ValidateTestFlagIs5));
1476
EXPECT_FALSE(RegisterFlagValidator(&dummy, &ValidateTestFlagIs5));
1479
TEST(FlagsValidator, RegisterValidatorTwice) {
1480
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
1481
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
1482
EXPECT_FALSE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs10));
1483
EXPECT_FALSE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs10));
1484
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
1485
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
1486
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs10));
1487
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
1490
TEST(FlagsValidator, CommandLineFlagInfo) {
1491
CommandLineFlagInfo info;
1492
info = GetCommandLineFlagInfoOrDie("test_flag");
1493
EXPECT_FALSE(info.has_validator_fn);
1495
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
1496
info = GetCommandLineFlagInfoOrDie("test_flag");
1497
EXPECT_TRUE(info.has_validator_fn);
1499
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
1500
info = GetCommandLineFlagInfoOrDie("test_flag");
1501
EXPECT_FALSE(info.has_validator_fn);
1504
TEST(FlagsValidator, FlagSaver) {
1507
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
1508
EXPECT_EQ("", SetCommandLineOption("test_flag", "50")); // fails validation
1510
EXPECT_NE("", SetCommandLineOption("test_flag", "50")); // validator is gone
1512
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
1515
EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
1516
EXPECT_NE("", SetCommandLineOption("test_flag", "50")); // no validator
1518
EXPECT_EQ("", SetCommandLineOption("test_flag", "50")); // validator is back
1522
} // unnamed namespace
1524
static int main(int argc, char **argv) {
1526
// Run unit tests only if called without arguments, otherwise this program
1527
// is used by an "external" usage test
1528
const bool run_tests = (argc == 1);
1530
// We need to call SetArgv before parsing flags, so our "test" argv will
1531
// win out over this executable's real argv. That makes running this
1532
// test with a real --help flag kinda annoying, unfortunately.
1533
const char* test_argv[] = { "/test/argv/for/gflags_unittest",
1534
"argv 2", "3rd argv", "argv #4" };
1535
SetArgv(arraysize(test_argv), test_argv);
1537
// The first arg is the usage message, also important for testing.
1538
string usage_message = (string(GetArgv0()) +
1539
": <useless flag> [...]\nDoes something useless.\n");
1541
// We test setting tryfromenv manually, and making sure
1542
// ParseCommandLineFlags still evaluates it.
1543
FLAGS_tryfromenv = "test_tryfromenv";
1544
setenv("FLAGS_test_tryfromenv", "pre-set", 1);
1546
// Modify flag values from declared default value in two ways.
1547
// The recommended way:
1548
SetCommandLineOptionWithMode("changed_bool1", "true", SET_FLAGS_DEFAULT);
1550
// The non-recommended way:
1551
FLAGS_changed_bool2 = true;
1553
SetUsageMessage(usage_message);
1554
SetVersionString("test_version");
1555
ParseCommandLineFlags(&argc, &argv, true);
1556
MakeTmpdir(&FLAGS_test_tmpdir);
1558
int exit_status = 0;
1560
fprintf(stdout, "Running the unit tests now...\n\n"); fflush(stdout);
1561
exit_status = RUN_ALL_TESTS();
1562
} else fprintf(stderr, "\n\nPASS\n");
1563
ShutDownCommandLineFlags();
1567
} // GFLAGS_NAMESPACE
1569
int main(int argc, char** argv) {
1570
return GFLAGS_NAMESPACE::main(argc, argv);