gflags_unittest.cc 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. // Copyright (c) 2005, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  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
  13. // distribution.
  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.
  17. //
  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.
  29. // ---
  30. //
  31. // For now, this unit test does not cover all features of
  32. // gflags.cc
  33. #include <gflags/gflags.h>
  34. #include "config.h"
  35. #include "util.h"
  36. #include <math.h> // for isinf() and isnan()
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #ifdef HAVE_UNISTD_H
  41. # include <unistd.h> // for unlink()
  42. #endif
  43. #include <vector>
  44. #include <string>
  45. TEST_INIT
  46. EXPECT_DEATH_INIT
  47. // I don't actually use this header file, but #include it under the
  48. // old location to make sure that the include-header-forwarding
  49. // works. But don't bother on windows; the windows port is so new
  50. // it never had the old location-names.
  51. #ifndef _MSC_VER
  52. #include <gflags/gflags_completions.h>
  53. void (*unused_fn)() = &GFLAGS_NAMESPACE::HandleCommandLineCompletions;
  54. #endif
  55. using std::string;
  56. using std::vector;
  57. using GFLAGS_NAMESPACE::int32;
  58. using GFLAGS_NAMESPACE::FlagRegisterer;
  59. using GFLAGS_NAMESPACE::StringFromEnv;
  60. using GFLAGS_NAMESPACE::RegisterFlagValidator;
  61. using GFLAGS_NAMESPACE::CommandLineFlagInfo;
  62. using GFLAGS_NAMESPACE::GetAllFlags;
  63. DEFINE_string(test_tmpdir, "", "Dir we use for temp files");
  64. DEFINE_string(srcdir, StringFromEnv("SRCDIR", "."), "Source-dir root, needed to find gflags_unittest_flagfile");
  65. DECLARE_string(tryfromenv); // in gflags.cc
  66. DEFINE_bool(test_bool, false, "tests bool-ness");
  67. DEFINE_int32(test_int32, -1, "");
  68. DEFINE_int64(test_int64, -2, "");
  69. DEFINE_uint32(test_uint32, 1, "");
  70. DEFINE_uint64(test_uint64, 2, "");
  71. DEFINE_double(test_double, -1.0, "");
  72. DEFINE_string(test_string, "initial", "");
  73. //
  74. // The below ugliness gets some additional code coverage in the -helpxml
  75. // and -helpmatch test cases having to do with string lengths and formatting
  76. //
  77. DEFINE_bool(test_bool_with_quite_quite_quite_quite_quite_quite_quite_quite_quite_quite_quite_quite_quite_quite_long_name,
  78. false,
  79. "extremely_extremely_extremely_extremely_extremely_extremely_extremely_extremely_long_meaning");
  80. DEFINE_string(test_str1, "initial", "");
  81. DEFINE_string(test_str2, "initial", "");
  82. DEFINE_string(test_str3, "initial", "");
  83. // This is used to test setting tryfromenv manually
  84. DEFINE_string(test_tryfromenv, "initial", "");
  85. // Don't try this at home!
  86. static int changeable_var = 12;
  87. DEFINE_int32(changeable_var, ++changeable_var, "");
  88. static int changeable_bool_var = 8008;
  89. DEFINE_bool(changeable_bool_var, ++changeable_bool_var == 8009, "");
  90. static int changeable_string_var = 0;
  91. static string ChangeableString() {
  92. char r[] = {static_cast<char>('0' + ++changeable_string_var), '\0'};
  93. return r;
  94. }
  95. DEFINE_string(changeable_string_var, ChangeableString(), "");
  96. // These are never used in this unittest, but can be used by
  97. // gflags_unittest.sh when it needs to specify flags
  98. // that are legal for gflags_unittest but don't need to
  99. // be a particular value.
  100. DEFINE_bool(unused_bool, true, "unused bool-ness");
  101. DEFINE_int32(unused_int32, -1001, "");
  102. DEFINE_int64(unused_int64, -2001, "");
  103. DEFINE_uint32(unused_uint32, 1000, "");
  104. DEFINE_uint64(unused_uint64, 2000, "");
  105. DEFINE_double(unused_double, -1000.0, "");
  106. DEFINE_string(unused_string, "unused", "");
  107. // These flags are used by gflags_unittest.sh
  108. DEFINE_bool(changed_bool1, false, "changed");
  109. DEFINE_bool(changed_bool2, false, "changed");
  110. DEFINE_bool(long_helpstring, false,
  111. "This helpstring goes on forever and ever and ever and ever and "
  112. "ever and ever and ever and ever and ever and ever and ever and "
  113. "ever and ever and ever and ever and ever and ever and ever and "
  114. "ever and ever and ever and ever and ever and ever and ever and "
  115. "ever and ever and ever and ever and ever and ever and ever and "
  116. "ever and ever and ever and ever and ever and ever and ever and "
  117. "ever and ever and ever and ever and ever and ever and ever and "
  118. "ever and ever and ever and ever and ever and ever and ever and "
  119. "ever and ever and ever and ever and ever and ever and ever and "
  120. "ever and ever and ever and ever and ever and ever and ever and "
  121. "ever. This is the end of a long helpstring");
  122. static bool AlwaysFail(const char* flag, bool value) { return value == false; }
  123. DEFINE_bool(always_fail, false, "will fail to validate when you set it");
  124. DEFINE_validator(always_fail, AlwaysFail);
  125. // See the comment by GetAllFlags in gflags.h
  126. static bool DeadlockIfCantLockInValidators(const char* flag, bool value) {
  127. if (!value) {
  128. return true;
  129. }
  130. vector<CommandLineFlagInfo> dummy;
  131. GetAllFlags(&dummy);
  132. return true;
  133. }
  134. DEFINE_bool(deadlock_if_cant_lock,
  135. false,
  136. "will deadlock if set to true and "
  137. "if locking of registry in validators fails.");
  138. DEFINE_validator(deadlock_if_cant_lock, DeadlockIfCantLockInValidators);
  139. #define MAKEFLAG(x) DEFINE_int32(test_flag_num##x, x, "Test flag")
  140. // Define 10 flags
  141. #define MAKEFLAG10(x) \
  142. MAKEFLAG(x##0); \
  143. MAKEFLAG(x##1); \
  144. MAKEFLAG(x##2); \
  145. MAKEFLAG(x##3); \
  146. MAKEFLAG(x##4); \
  147. MAKEFLAG(x##5); \
  148. MAKEFLAG(x##6); \
  149. MAKEFLAG(x##7); \
  150. MAKEFLAG(x##8); \
  151. MAKEFLAG(x##9)
  152. // Define 100 flags
  153. #define MAKEFLAG100(x) \
  154. MAKEFLAG10(x##0); \
  155. MAKEFLAG10(x##1); \
  156. MAKEFLAG10(x##2); \
  157. MAKEFLAG10(x##3); \
  158. MAKEFLAG10(x##4); \
  159. MAKEFLAG10(x##5); \
  160. MAKEFLAG10(x##6); \
  161. MAKEFLAG10(x##7); \
  162. MAKEFLAG10(x##8); \
  163. MAKEFLAG10(x##9)
  164. // Define a bunch of command-line flags. Each occurrence of the MAKEFLAG100
  165. // macro defines 100 integer flags. This lets us test the effect of having
  166. // many flags on startup time.
  167. MAKEFLAG100(1);
  168. MAKEFLAG100(2);
  169. MAKEFLAG100(3);
  170. MAKEFLAG100(4);
  171. MAKEFLAG100(5);
  172. MAKEFLAG100(6);
  173. MAKEFLAG100(7);
  174. MAKEFLAG100(8);
  175. MAKEFLAG100(9);
  176. MAKEFLAG100(10);
  177. MAKEFLAG100(11);
  178. MAKEFLAG100(12);
  179. MAKEFLAG100(13);
  180. MAKEFLAG100(14);
  181. MAKEFLAG100(15);
  182. #undef MAKEFLAG100
  183. #undef MAKEFLAG10
  184. #undef MAKEFLAG
  185. // This is a pseudo-flag -- we want to register a flag with a filename
  186. // at the top level, but there is no way to do this except by faking
  187. // the filename.
  188. namespace fLI {
  189. static const int32 FLAGS_nonotldflag1 = 12;
  190. int32 FLAGS_tldflag1 = FLAGS_nonotldflag1;
  191. int32 FLAGS_notldflag1 = FLAGS_nonotldflag1;
  192. static FlagRegisterer o_tldflag1(
  193. "tldflag1",
  194. "should show up in --helpshort", "gflags_unittest.cc",
  195. &FLAGS_tldflag1, &FLAGS_notldflag1);
  196. }
  197. using fLI::FLAGS_tldflag1;
  198. namespace fLI {
  199. static const int32 FLAGS_nonotldflag2 = 23;
  200. int32 FLAGS_tldflag2 = FLAGS_nonotldflag2;
  201. int32 FLAGS_notldflag2 = FLAGS_nonotldflag2;
  202. static FlagRegisterer o_tldflag2(
  203. "tldflag2",
  204. "should show up in --helpshort", "gflags_unittest.",
  205. &FLAGS_tldflag2, &FLAGS_notldflag2);
  206. }
  207. using fLI::FLAGS_tldflag2;
  208. namespace GFLAGS_NAMESPACE {
  209. namespace {
  210. static string TmpFile(const string& basename) {
  211. #ifdef _MSC_VER
  212. return FLAGS_test_tmpdir + "\\" + basename;
  213. #else
  214. return FLAGS_test_tmpdir + "/" + basename;
  215. #endif
  216. }
  217. // Returns the definition of the --flagfile flag to be used in the tests.
  218. // Must be called after ParseCommandLineFlags().
  219. static const char* GetFlagFileFlag() {
  220. #ifdef _MSC_VER
  221. static const string flagfile = FLAGS_srcdir + "\\gflags_unittest_flagfile";
  222. #else
  223. static const string flagfile = FLAGS_srcdir + "/gflags_unittest_flagfile";
  224. #endif
  225. static const string flagfile_flag = string("--flagfile=") + flagfile;
  226. return flagfile_flag.c_str();
  227. }
  228. // Defining a variable of type CompileAssertTypesEqual<T1, T2> will cause a
  229. // compiler error iff T1 and T2 are different types.
  230. template <typename T1, typename T2>
  231. struct CompileAssertTypesEqual;
  232. template <typename T>
  233. struct CompileAssertTypesEqual<T, T> {
  234. };
  235. template <typename Expected, typename Actual>
  236. void AssertIsType(Actual& x) {
  237. CompileAssertTypesEqual<Expected, Actual>();
  238. }
  239. // Verify all the flags are the right type.
  240. TEST(FlagTypes, FlagTypes) {
  241. AssertIsType<bool>(FLAGS_test_bool);
  242. AssertIsType<int32>(FLAGS_test_int32);
  243. AssertIsType<int64>(FLAGS_test_int64);
  244. AssertIsType<uint32>(FLAGS_test_uint32);
  245. AssertIsType<uint64>(FLAGS_test_uint64);
  246. AssertIsType<double>(FLAGS_test_double);
  247. AssertIsType<string>(FLAGS_test_string);
  248. }
  249. #ifdef GTEST_HAS_DEATH_TEST
  250. // Death tests for "help" options.
  251. //
  252. // The help system automatically calls gflags_exitfunc(1) when you specify any of
  253. // the help-related flags ("-helpmatch", "-helpxml") so we can't test
  254. // those mainline.
  255. // Tests that "-helpmatch" causes the process to die.
  256. TEST(ReadFlagsFromStringDeathTest, HelpMatch) {
  257. EXPECT_DEATH(ReadFlagsFromString("-helpmatch=base", GetArgv0(), true),
  258. "");
  259. }
  260. // Tests that "-helpxml" causes the process to die.
  261. TEST(ReadFlagsFromStringDeathTest, HelpXml) {
  262. EXPECT_DEATH(ReadFlagsFromString("-helpxml", GetArgv0(), true),
  263. "");
  264. }
  265. #endif
  266. // A subroutine needed for testing reading flags from a string.
  267. void TestFlagString(const string& flags,
  268. const string& expected_string,
  269. bool expected_bool,
  270. int32 expected_int32,
  271. double expected_double) {
  272. EXPECT_TRUE(ReadFlagsFromString(flags,
  273. GetArgv0(),
  274. // errors are fatal
  275. true));
  276. EXPECT_EQ(expected_string, FLAGS_test_string);
  277. EXPECT_EQ(expected_bool, FLAGS_test_bool);
  278. EXPECT_EQ(expected_int32, FLAGS_test_int32);
  279. EXPECT_DOUBLE_EQ(expected_double, FLAGS_test_double);
  280. }
  281. // Tests reading flags from a string.
  282. TEST(FlagFileTest, ReadFlagsFromString) {
  283. TestFlagString(
  284. // Flag string
  285. "-test_string=continued\n"
  286. "# some comments are in order\n"
  287. "# some\n"
  288. " # comments\n"
  289. "#are\n"
  290. " #trickier\n"
  291. "# than others\n"
  292. "-test_bool=true\n"
  293. " -test_int32=1\n"
  294. "-test_double=0.0\n",
  295. // Expected values
  296. "continued",
  297. true,
  298. 1,
  299. 0.0);
  300. TestFlagString(
  301. // Flag string
  302. "# let's make sure it can update values\n"
  303. "-test_string=initial\n"
  304. "-test_bool=false\n"
  305. "-test_int32=123\n"
  306. "-test_double=123.0\n",
  307. // Expected values
  308. "initial",
  309. false,
  310. 123,
  311. 123.0);
  312. // Test that flags can use dashes instead of underscores.
  313. TestFlagString(
  314. // Flag string
  315. "-test-string=initial\n"
  316. "--test-bool=false\n"
  317. "--test-int32=123\n"
  318. "--test-double=123.0\n",
  319. // Expected values
  320. "initial",
  321. false,
  322. 123,
  323. 123.0);
  324. }
  325. // Tests the filename part of the flagfile
  326. TEST(FlagFileTest, FilenamesOurfileLast) {
  327. FLAGS_test_string = "initial";
  328. FLAGS_test_bool = false;
  329. FLAGS_test_int32 = -1;
  330. FLAGS_test_double = -1.0;
  331. TestFlagString(
  332. // Flag string
  333. "-test_string=continued\n"
  334. "# some comments are in order\n"
  335. "# some\n"
  336. " # comments\n"
  337. "#are\n"
  338. " #trickier\n"
  339. "# than others\n"
  340. "not_our_filename\n"
  341. "-test_bool=true\n"
  342. " -test_int32=1\n"
  343. "gflags_unittest\n"
  344. "-test_double=1000.0\n",
  345. // Expected values
  346. "continued",
  347. false,
  348. -1,
  349. 1000.0);
  350. }
  351. TEST(FlagFileTest, FilenamesOurfileFirst) {
  352. FLAGS_test_string = "initial";
  353. FLAGS_test_bool = false;
  354. FLAGS_test_int32 = -1;
  355. FLAGS_test_double = -1.0;
  356. TestFlagString(
  357. // Flag string
  358. "-test_string=continued\n"
  359. "# some comments are in order\n"
  360. "# some\n"
  361. " # comments\n"
  362. "#are\n"
  363. " #trickier\n"
  364. "# than others\n"
  365. "gflags_unittest\n"
  366. "-test_bool=true\n"
  367. " -test_int32=1\n"
  368. "not_our_filename\n"
  369. "-test_double=1000.0\n",
  370. // Expected values
  371. "continued",
  372. true,
  373. 1,
  374. -1.0);
  375. }
  376. #if defined(HAVE_FNMATCH_H) || defined(HAVE_SHLWAPI_H) // otherwise glob isn't supported
  377. TEST(FlagFileTest, FilenamesOurfileGlob) {
  378. FLAGS_test_string = "initial";
  379. FLAGS_test_bool = false;
  380. FLAGS_test_int32 = -1;
  381. FLAGS_test_double = -1.0;
  382. TestFlagString(
  383. // Flag string
  384. "-test_string=continued\n"
  385. "# some comments are in order\n"
  386. "# some\n"
  387. " # comments\n"
  388. "#are\n"
  389. " #trickier\n"
  390. "# than others\n"
  391. "*flags*\n"
  392. "-test_bool=true\n"
  393. " -test_int32=1\n"
  394. "flags\n"
  395. "-test_double=1000.0\n",
  396. // Expected values
  397. "continued",
  398. true,
  399. 1,
  400. -1.0);
  401. }
  402. TEST(FlagFileTest, FilenamesOurfileInBigList) {
  403. FLAGS_test_string = "initial";
  404. FLAGS_test_bool = false;
  405. FLAGS_test_int32 = -1;
  406. FLAGS_test_double = -1.0;
  407. TestFlagString(
  408. // Flag string
  409. "-test_string=continued\n"
  410. "# some comments are in order\n"
  411. "# some\n"
  412. " # comments\n"
  413. "#are\n"
  414. " #trickier\n"
  415. "# than others\n"
  416. "*first* *flags* *third*\n"
  417. "-test_bool=true\n"
  418. " -test_int32=1\n"
  419. "flags\n"
  420. "-test_double=1000.0\n",
  421. // Expected values
  422. "continued",
  423. true,
  424. 1,
  425. -1.0);
  426. }
  427. #endif // defined(HAVE_FNMATCH_H) || defined(HAVE_SHLWAPI_H)
  428. // Tests that a failed flag-from-string read keeps flags at default values
  429. TEST(FlagFileTest, FailReadFlagsFromString) {
  430. FLAGS_test_int32 = 119;
  431. string flags("# let's make sure it can update values\n"
  432. "-test_string=non_initial\n"
  433. "-test_bool=false\n"
  434. "-test_int32=123\n"
  435. "-test_double=illegal\n");
  436. EXPECT_FALSE(ReadFlagsFromString(flags,
  437. GetArgv0(),
  438. // errors are fatal
  439. false));
  440. EXPECT_EQ(119, FLAGS_test_int32);
  441. EXPECT_EQ("initial", FLAGS_test_string);
  442. }
  443. // Tests that flags can be set to ordinary values.
  444. TEST(SetFlagValueTest, OrdinaryValues) {
  445. EXPECT_EQ("initial", FLAGS_test_str1);
  446. SetCommandLineOptionWithMode("test_str1", "second", SET_FLAG_IF_DEFAULT);
  447. EXPECT_EQ("second", FLAGS_test_str1); // set; was default
  448. SetCommandLineOptionWithMode("test_str1", "third", SET_FLAG_IF_DEFAULT);
  449. EXPECT_EQ("second", FLAGS_test_str1); // already set once
  450. FLAGS_test_str1 = "initial";
  451. SetCommandLineOptionWithMode("test_str1", "third", SET_FLAG_IF_DEFAULT);
  452. EXPECT_EQ("initial", FLAGS_test_str1); // still already set before
  453. SetCommandLineOptionWithMode("test_str1", "third", SET_FLAGS_VALUE);
  454. EXPECT_EQ("third", FLAGS_test_str1); // changed value
  455. SetCommandLineOptionWithMode("test_str1", "fourth", SET_FLAGS_DEFAULT);
  456. EXPECT_EQ("third", FLAGS_test_str1);
  457. // value not changed (already set before)
  458. EXPECT_EQ("initial", FLAGS_test_str2);
  459. SetCommandLineOptionWithMode("test_str2", "second", SET_FLAGS_DEFAULT);
  460. EXPECT_EQ("second", FLAGS_test_str2); // changed (was default)
  461. FLAGS_test_str2 = "extra";
  462. EXPECT_EQ("extra", FLAGS_test_str2);
  463. FLAGS_test_str2 = "second";
  464. SetCommandLineOptionWithMode("test_str2", "third", SET_FLAGS_DEFAULT);
  465. EXPECT_EQ("third", FLAGS_test_str2); // still changed (was equal to default)
  466. SetCommandLineOptionWithMode("test_str2", "fourth", SET_FLAG_IF_DEFAULT);
  467. EXPECT_EQ("fourth", FLAGS_test_str2); // changed (was default)
  468. EXPECT_EQ("initial", FLAGS_test_str3);
  469. SetCommandLineOptionWithMode("test_str3", "second", SET_FLAGS_DEFAULT);
  470. EXPECT_EQ("second", FLAGS_test_str3); // changed
  471. FLAGS_test_str3 = "third";
  472. SetCommandLineOptionWithMode("test_str3", "fourth", SET_FLAGS_DEFAULT);
  473. EXPECT_EQ("third", FLAGS_test_str3); // not changed (was set)
  474. SetCommandLineOptionWithMode("test_str3", "fourth", SET_FLAG_IF_DEFAULT);
  475. EXPECT_EQ("third", FLAGS_test_str3); // not changed (was set)
  476. SetCommandLineOptionWithMode("test_str3", "fourth", SET_FLAGS_VALUE);
  477. EXPECT_EQ("fourth", FLAGS_test_str3); // changed value
  478. }
  479. // Tests that flags can be set to exceptional values.
  480. // Note: apparently MINGW doesn't parse inf and nan correctly:
  481. // http://www.mail-archive.com/bug-gnulib@gnu.org/msg09573.html
  482. // This url says FreeBSD also has a problem, but I didn't see that.
  483. TEST(SetFlagValueTest, ExceptionalValues) {
  484. #if defined(isinf) && !defined(__MINGW32__)
  485. EXPECT_EQ("test_double set to inf\n",
  486. SetCommandLineOption("test_double", "inf"));
  487. EXPECT_INF(FLAGS_test_double);
  488. EXPECT_EQ("test_double set to inf\n",
  489. SetCommandLineOption("test_double", "INF"));
  490. EXPECT_INF(FLAGS_test_double);
  491. #endif
  492. // set some bad values
  493. EXPECT_EQ("",
  494. SetCommandLineOption("test_double", "0.1xxx"));
  495. EXPECT_EQ("",
  496. SetCommandLineOption("test_double", " "));
  497. EXPECT_EQ("",
  498. SetCommandLineOption("test_double", ""));
  499. #if defined(isinf) && !defined(__MINGW32__)
  500. EXPECT_EQ("test_double set to -inf\n",
  501. SetCommandLineOption("test_double", "-inf"));
  502. EXPECT_INF(FLAGS_test_double);
  503. EXPECT_GT(0, FLAGS_test_double);
  504. #endif
  505. #if defined(isnan) && !defined(__MINGW32__)
  506. EXPECT_EQ("test_double set to nan\n",
  507. SetCommandLineOption("test_double", "NaN"));
  508. EXPECT_NAN(FLAGS_test_double);
  509. #endif
  510. }
  511. // Tests that integer flags can be specified in many ways
  512. TEST(SetFlagValueTest, DifferentRadices) {
  513. EXPECT_EQ("test_int32 set to 12\n",
  514. SetCommandLineOption("test_int32", "12"));
  515. EXPECT_EQ("test_int32 set to 16\n",
  516. SetCommandLineOption("test_int32", "0x10"));
  517. EXPECT_EQ("test_int32 set to 34\n",
  518. SetCommandLineOption("test_int32", "0X22"));
  519. // Leading 0 is *not* octal; it's still decimal
  520. EXPECT_EQ("test_int32 set to 10\n",
  521. SetCommandLineOption("test_int32", "010"));
  522. }
  523. // Tests what happens when you try to set a flag to an illegal value
  524. TEST(SetFlagValueTest, IllegalValues) {
  525. FLAGS_test_bool = true;
  526. FLAGS_test_int32 = 119;
  527. FLAGS_test_int64 = 1191;
  528. FLAGS_test_uint32 = 11911;
  529. FLAGS_test_uint64 = 119111;
  530. EXPECT_EQ("",
  531. SetCommandLineOption("test_bool", "12"));
  532. EXPECT_EQ("",
  533. SetCommandLineOption("test_uint32", "-1970"));
  534. EXPECT_EQ("",
  535. SetCommandLineOption("test_int32", "7000000000000"));
  536. EXPECT_EQ("",
  537. SetCommandLineOption("test_uint64", "-1"));
  538. EXPECT_EQ("",
  539. SetCommandLineOption("test_int64", "not a number!"));
  540. // Test the empty string with each type of input
  541. EXPECT_EQ("", SetCommandLineOption("test_bool", ""));
  542. EXPECT_EQ("", SetCommandLineOption("test_int32", ""));
  543. EXPECT_EQ("", SetCommandLineOption("test_int64", ""));
  544. EXPECT_EQ("", SetCommandLineOption("test_uint32", ""));
  545. EXPECT_EQ("", SetCommandLineOption("test_uint64", ""));
  546. EXPECT_EQ("", SetCommandLineOption("test_double", ""));
  547. EXPECT_EQ("test_string set to \n", SetCommandLineOption("test_string", ""));
  548. EXPECT_TRUE(FLAGS_test_bool);
  549. EXPECT_EQ(119, FLAGS_test_int32);
  550. EXPECT_EQ(1191, FLAGS_test_int64);
  551. EXPECT_EQ(11911, FLAGS_test_uint32);
  552. EXPECT_EQ(119111, FLAGS_test_uint64);
  553. }
  554. // Tests that we only evaluate macro args once
  555. TEST(MacroArgs, EvaluateOnce) {
  556. EXPECT_EQ(13, FLAGS_changeable_var);
  557. // Make sure we don't ++ the value somehow, when evaluating the flag.
  558. EXPECT_EQ(13, FLAGS_changeable_var);
  559. // Make sure the macro only evaluated this var once.
  560. EXPECT_EQ(13, changeable_var);
  561. // Make sure the actual value and default value are the same
  562. SetCommandLineOptionWithMode("changeable_var", "21", SET_FLAG_IF_DEFAULT);
  563. EXPECT_EQ(21, FLAGS_changeable_var);
  564. }
  565. TEST(MacroArgs, EvaluateOnceBool) {
  566. EXPECT_TRUE(FLAGS_changeable_bool_var);
  567. EXPECT_TRUE(FLAGS_changeable_bool_var);
  568. EXPECT_EQ(8009, changeable_bool_var);
  569. SetCommandLineOptionWithMode("changeable_bool_var", "false",
  570. SET_FLAG_IF_DEFAULT);
  571. EXPECT_FALSE(FLAGS_changeable_bool_var);
  572. }
  573. TEST(MacroArgs, EvaluateOnceStrings) {
  574. EXPECT_EQ("1", FLAGS_changeable_string_var);
  575. EXPECT_EQ("1", FLAGS_changeable_string_var);
  576. EXPECT_EQ(1, changeable_string_var);
  577. SetCommandLineOptionWithMode("changeable_string_var", "different",
  578. SET_FLAG_IF_DEFAULT);
  579. EXPECT_EQ("different", FLAGS_changeable_string_var);
  580. }
  581. // Tests that the FooFromEnv does the right thing
  582. TEST(FromEnvTest, LegalValues) {
  583. setenv("BOOL_VAL1", "true", 1);
  584. setenv("BOOL_VAL2", "false", 1);
  585. setenv("BOOL_VAL3", "1", 1);
  586. setenv("BOOL_VAL4", "F", 1);
  587. EXPECT_TRUE(BoolFromEnv("BOOL_VAL1", false));
  588. EXPECT_FALSE(BoolFromEnv("BOOL_VAL2", true));
  589. EXPECT_TRUE(BoolFromEnv("BOOL_VAL3", false));
  590. EXPECT_FALSE(BoolFromEnv("BOOL_VAL4", true));
  591. EXPECT_TRUE(BoolFromEnv("BOOL_VAL_UNKNOWN", true));
  592. EXPECT_FALSE(BoolFromEnv("BOOL_VAL_UNKNOWN", false));
  593. setenv("INT_VAL1", "1", 1);
  594. setenv("INT_VAL2", "-1", 1);
  595. EXPECT_EQ(1, Int32FromEnv("INT_VAL1", 10));
  596. EXPECT_EQ(-1, Int32FromEnv("INT_VAL2", 10));
  597. EXPECT_EQ(10, Int32FromEnv("INT_VAL_UNKNOWN", 10));
  598. setenv("INT_VAL3", "4294967295", 1);
  599. EXPECT_EQ(1, Uint32FromEnv("INT_VAL1", 10));
  600. EXPECT_EQ(4294967295L, Uint32FromEnv("INT_VAL3", 30));
  601. EXPECT_EQ(10, Uint32FromEnv("INT_VAL_UNKNOWN", 10));
  602. setenv("INT_VAL4", "1099511627776", 1);
  603. EXPECT_EQ(1, Int64FromEnv("INT_VAL1", 20));
  604. EXPECT_EQ(-1, Int64FromEnv("INT_VAL2", 20));
  605. EXPECT_EQ(1099511627776LL, Int64FromEnv("INT_VAL4", 20));
  606. EXPECT_EQ(20, Int64FromEnv("INT_VAL_UNKNOWN", 20));
  607. EXPECT_EQ(1, Uint64FromEnv("INT_VAL1", 30));
  608. EXPECT_EQ(1099511627776ULL, Uint64FromEnv("INT_VAL4", 30));
  609. EXPECT_EQ(30, Uint64FromEnv("INT_VAL_UNKNOWN", 30));
  610. // I pick values here that can be easily represented exactly in floating-point
  611. setenv("DOUBLE_VAL1", "0.0", 1);
  612. setenv("DOUBLE_VAL2", "1.0", 1);
  613. setenv("DOUBLE_VAL3", "-1.0", 1);
  614. EXPECT_EQ(0.0, DoubleFromEnv("DOUBLE_VAL1", 40.0));
  615. EXPECT_EQ(1.0, DoubleFromEnv("DOUBLE_VAL2", 40.0));
  616. EXPECT_EQ(-1.0, DoubleFromEnv("DOUBLE_VAL3", 40.0));
  617. EXPECT_EQ(40.0, DoubleFromEnv("DOUBLE_VAL_UNKNOWN", 40.0));
  618. setenv("STRING_VAL1", "", 1);
  619. setenv("STRING_VAL2", "my happy string!", 1);
  620. EXPECT_STREQ("", StringFromEnv("STRING_VAL1", "unknown"));
  621. EXPECT_STREQ("my happy string!", StringFromEnv("STRING_VAL2", "unknown"));
  622. EXPECT_STREQ("unknown", StringFromEnv("STRING_VAL_UNKNOWN", "unknown"));
  623. }
  624. #ifdef GTEST_HAS_DEATH_TEST
  625. // Tests that the FooFromEnv dies on parse-error
  626. TEST(FromEnvDeathTest, IllegalValues) {
  627. setenv("BOOL_BAD1", "so true!", 1);
  628. setenv("BOOL_BAD2", "", 1);
  629. EXPECT_DEATH(BoolFromEnv("BOOL_BAD1", false), "error parsing env variable");
  630. EXPECT_DEATH(BoolFromEnv("BOOL_BAD2", true), "error parsing env variable");
  631. setenv("INT_BAD1", "one", 1);
  632. setenv("INT_BAD2", "100000000000000000", 1);
  633. setenv("INT_BAD3", "0xx10", 1);
  634. setenv("INT_BAD4", "", 1);
  635. EXPECT_DEATH(Int32FromEnv("INT_BAD1", 10), "error parsing env variable");
  636. EXPECT_DEATH(Int32FromEnv("INT_BAD2", 10), "error parsing env variable");
  637. EXPECT_DEATH(Int32FromEnv("INT_BAD3", 10), "error parsing env variable");
  638. EXPECT_DEATH(Int32FromEnv("INT_BAD4", 10), "error parsing env variable");
  639. EXPECT_DEATH(Uint32FromEnv("INT_BAD1", 10), "error parsing env variable");
  640. EXPECT_DEATH(Uint32FromEnv("INT_BAD2", 10), "error parsing env variable");
  641. EXPECT_DEATH(Uint32FromEnv("INT_BAD3", 10), "error parsing env variable");
  642. EXPECT_DEATH(Uint32FromEnv("INT_BAD4", 10), "error parsing env variable");
  643. setenv("BIGINT_BAD1", "18446744073709551616000", 1);
  644. EXPECT_DEATH(Int64FromEnv("INT_BAD1", 20), "error parsing env variable");
  645. EXPECT_DEATH(Int64FromEnv("INT_BAD3", 20), "error parsing env variable");
  646. EXPECT_DEATH(Int64FromEnv("INT_BAD4", 20), "error parsing env variable");
  647. EXPECT_DEATH(Int64FromEnv("BIGINT_BAD1", 200), "error parsing env variable");
  648. setenv("BIGINT_BAD2", "-1", 1);
  649. EXPECT_DEATH(Uint64FromEnv("INT_BAD1", 30), "error parsing env variable");
  650. EXPECT_DEATH(Uint64FromEnv("INT_BAD3", 30), "error parsing env variable");
  651. EXPECT_DEATH(Uint64FromEnv("INT_BAD4", 30), "error parsing env variable");
  652. EXPECT_DEATH(Uint64FromEnv("BIGINT_BAD1", 30), "error parsing env variable");
  653. // TODO(csilvers): uncomment this when we disallow negative numbers for uint64
  654. #if 0
  655. EXPECT_DEATH(Uint64FromEnv("BIGINT_BAD2", 30), "error parsing env variable");
  656. #endif
  657. setenv("DOUBLE_BAD1", "0.0.0", 1);
  658. setenv("DOUBLE_BAD2", "", 1);
  659. EXPECT_DEATH(DoubleFromEnv("DOUBLE_BAD1", 40.0), "error parsing env variable");
  660. EXPECT_DEATH(DoubleFromEnv("DOUBLE_BAD2", 40.0), "error parsing env variable");
  661. }
  662. #endif
  663. // Tests that FlagSaver can save the states of string flags.
  664. TEST(FlagSaverTest, CanSaveStringFlagStates) {
  665. // 1. Initializes the flags.
  666. // State of flag test_str1:
  667. // default value - "initial"
  668. // current value - "initial"
  669. // not set - true
  670. SetCommandLineOptionWithMode("test_str2", "second", SET_FLAGS_VALUE);
  671. // State of flag test_str2:
  672. // default value - "initial"
  673. // current value - "second"
  674. // not set - false
  675. SetCommandLineOptionWithMode("test_str3", "second", SET_FLAGS_DEFAULT);
  676. // State of flag test_str3:
  677. // default value - "second"
  678. // current value - "second"
  679. // not set - true
  680. // 2. Saves the flag states.
  681. {
  682. FlagSaver fs;
  683. // 3. Modifies the flag states.
  684. SetCommandLineOptionWithMode("test_str1", "second", SET_FLAGS_VALUE);
  685. EXPECT_EQ("second", FLAGS_test_str1);
  686. // State of flag test_str1:
  687. // default value - "second"
  688. // current value - "second"
  689. // not set - true
  690. SetCommandLineOptionWithMode("test_str2", "third", SET_FLAGS_DEFAULT);
  691. EXPECT_EQ("second", FLAGS_test_str2);
  692. // State of flag test_str2:
  693. // default value - "third"
  694. // current value - "second"
  695. // not set - false
  696. SetCommandLineOptionWithMode("test_str3", "third", SET_FLAGS_VALUE);
  697. EXPECT_EQ("third", FLAGS_test_str3);
  698. // State of flag test_str1:
  699. // default value - "second"
  700. // current value - "third"
  701. // not set - false
  702. // 4. Restores the flag states.
  703. }
  704. // 5. Verifies that the states were restored.
  705. // Verifies that the value of test_str1 was restored.
  706. EXPECT_EQ("initial", FLAGS_test_str1);
  707. // Verifies that the "not set" attribute of test_str1 was restored to true.
  708. SetCommandLineOptionWithMode("test_str1", "second", SET_FLAG_IF_DEFAULT);
  709. EXPECT_EQ("second", FLAGS_test_str1);
  710. // Verifies that the value of test_str2 was restored.
  711. EXPECT_EQ("second", FLAGS_test_str2);
  712. // Verifies that the "not set" attribute of test_str2 was restored to false.
  713. SetCommandLineOptionWithMode("test_str2", "fourth", SET_FLAG_IF_DEFAULT);
  714. EXPECT_EQ("second", FLAGS_test_str2);
  715. // Verifies that the value of test_str3 was restored.
  716. EXPECT_EQ("second", FLAGS_test_str3);
  717. // Verifies that the "not set" attribute of test_str3 was restored to true.
  718. SetCommandLineOptionWithMode("test_str3", "fourth", SET_FLAG_IF_DEFAULT);
  719. EXPECT_EQ("fourth", FLAGS_test_str3);
  720. }
  721. // Tests that FlagSaver can save the values of various-typed flags.
  722. TEST(FlagSaverTest, CanSaveVariousTypedFlagValues) {
  723. // Initializes the flags.
  724. FLAGS_test_bool = false;
  725. FLAGS_test_int32 = -1;
  726. FLAGS_test_uint32 = 2;
  727. FLAGS_test_int64 = -3;
  728. FLAGS_test_uint64 = 4;
  729. FLAGS_test_double = 5.0;
  730. FLAGS_test_string = "good";
  731. // Saves the flag states.
  732. {
  733. FlagSaver fs;
  734. // Modifies the flags.
  735. FLAGS_test_bool = true;
  736. FLAGS_test_int32 = -5;
  737. FLAGS_test_uint32 = 6;
  738. FLAGS_test_int64 = -7;
  739. FLAGS_test_uint64 = 8;
  740. FLAGS_test_double = 8.0;
  741. FLAGS_test_string = "bad";
  742. // Restores the flag states.
  743. }
  744. // Verifies the flag values were restored.
  745. EXPECT_FALSE(FLAGS_test_bool);
  746. EXPECT_EQ(-1, FLAGS_test_int32);
  747. EXPECT_EQ(2, FLAGS_test_uint32);
  748. EXPECT_EQ(-3, FLAGS_test_int64);
  749. EXPECT_EQ(4, FLAGS_test_uint64);
  750. EXPECT_DOUBLE_EQ(5.0, FLAGS_test_double);
  751. EXPECT_EQ("good", FLAGS_test_string);
  752. }
  753. TEST(GetAllFlagsTest, BaseTest) {
  754. vector<CommandLineFlagInfo> flags;
  755. GetAllFlags(&flags);
  756. bool found_test_bool = false;
  757. vector<CommandLineFlagInfo>::const_iterator i;
  758. for (i = flags.begin(); i != flags.end(); ++i) {
  759. if (i->name == "test_bool") {
  760. found_test_bool = true;
  761. EXPECT_EQ(i->type, "bool");
  762. EXPECT_EQ(i->default_value, "false");
  763. EXPECT_EQ(i->flag_ptr, &FLAGS_test_bool);
  764. break;
  765. }
  766. }
  767. EXPECT_TRUE(found_test_bool);
  768. }
  769. TEST(ShowUsageWithFlagsTest, BaseTest) {
  770. // TODO(csilvers): test this by allowing output other than to stdout.
  771. // Not urgent since this functionality is tested via
  772. // gflags_unittest.sh, though only through use of --help.
  773. }
  774. TEST(ShowUsageWithFlagsRestrictTest, BaseTest) {
  775. // TODO(csilvers): test this by allowing output other than to stdout.
  776. // Not urgent since this functionality is tested via
  777. // gflags_unittest.sh, though only through use of --helpmatch.
  778. }
  779. // Note: all these argv-based tests depend on SetArgv being called
  780. // before ParseCommandLineFlags() in main(), below.
  781. TEST(GetArgvsTest, BaseTest) {
  782. vector<string> argvs = GetArgvs();
  783. EXPECT_EQ(4, argvs.size());
  784. EXPECT_EQ("/test/argv/for/gflags_unittest", argvs[0]);
  785. EXPECT_EQ("argv 2", argvs[1]);
  786. EXPECT_EQ("3rd argv", argvs[2]);
  787. EXPECT_EQ("argv #4", argvs[3]);
  788. }
  789. TEST(GetArgvTest, BaseTest) {
  790. EXPECT_STREQ("/test/argv/for/gflags_unittest "
  791. "argv 2 3rd argv argv #4", GetArgv());
  792. }
  793. TEST(GetArgv0Test, BaseTest) {
  794. EXPECT_STREQ("/test/argv/for/gflags_unittest", GetArgv0());
  795. }
  796. TEST(GetArgvSumTest, BaseTest) {
  797. // This number is just the sum of the ASCII values of all the chars
  798. // in GetArgv().
  799. EXPECT_EQ(4904, GetArgvSum());
  800. }
  801. TEST(ProgramInvocationNameTest, BaseTest) {
  802. EXPECT_STREQ("/test/argv/for/gflags_unittest",
  803. ProgramInvocationName());
  804. }
  805. TEST(ProgramInvocationShortNameTest, BaseTest) {
  806. EXPECT_STREQ("gflags_unittest", ProgramInvocationShortName());
  807. }
  808. TEST(ProgramUsageTest, BaseTest) { // Depends on 1st arg to ParseCommandLineFlags()
  809. EXPECT_STREQ("/test/argv/for/gflags_unittest: "
  810. "<useless flag> [...]\nDoes something useless.\n",
  811. ProgramUsage());
  812. }
  813. TEST(GetCommandLineOptionTest, NameExistsAndIsDefault) {
  814. string value("will be changed");
  815. bool r = GetCommandLineOption("test_bool", &value);
  816. EXPECT_TRUE(r);
  817. EXPECT_EQ("false", value);
  818. r = GetCommandLineOption("test_int32", &value);
  819. EXPECT_TRUE(r);
  820. EXPECT_EQ("-1", value);
  821. }
  822. TEST(GetCommandLineOptionTest, NameExistsAndWasAssigned) {
  823. FLAGS_test_int32 = 400;
  824. string value("will be changed");
  825. const bool r = GetCommandLineOption("test_int32", &value);
  826. EXPECT_TRUE(r);
  827. EXPECT_EQ("400", value);
  828. }
  829. TEST(GetCommandLineOptionTest, NameExistsAndWasSet) {
  830. SetCommandLineOption("test_int32", "700");
  831. string value("will be changed");
  832. const bool r = GetCommandLineOption("test_int32", &value);
  833. EXPECT_TRUE(r);
  834. EXPECT_EQ("700", value);
  835. }
  836. TEST(GetCommandLineOptionTest, NameExistsAndWasNotSet) {
  837. // This doesn't set the flag's value, but rather its default value.
  838. // is_default is still true, but the 'default' value returned has changed!
  839. SetCommandLineOptionWithMode("test_int32", "800", SET_FLAGS_DEFAULT);
  840. string value("will be changed");
  841. const bool r = GetCommandLineOption("test_int32", &value);
  842. EXPECT_TRUE(r);
  843. EXPECT_EQ("800", value);
  844. EXPECT_TRUE(GetCommandLineFlagInfoOrDie("test_int32").is_default);
  845. }
  846. TEST(GetCommandLineOptionTest, NameExistsAndWasConditionallySet) {
  847. SetCommandLineOptionWithMode("test_int32", "900", SET_FLAG_IF_DEFAULT);
  848. string value("will be changed");
  849. const bool r = GetCommandLineOption("test_int32", &value);
  850. EXPECT_TRUE(r);
  851. EXPECT_EQ("900", value);
  852. }
  853. TEST(GetCommandLineOptionTest, NameDoesNotExist) {
  854. string value("will not be changed");
  855. const bool r = GetCommandLineOption("test_int3210", &value);
  856. EXPECT_FALSE(r);
  857. EXPECT_EQ("will not be changed", value);
  858. }
  859. TEST(GetCommandLineFlagInfoTest, FlagExists) {
  860. CommandLineFlagInfo info;
  861. bool r = GetCommandLineFlagInfo("test_int32", &info);
  862. EXPECT_TRUE(r);
  863. EXPECT_EQ("test_int32", info.name);
  864. EXPECT_EQ("int32", info.type);
  865. EXPECT_EQ("", info.description);
  866. EXPECT_EQ("-1", info.current_value);
  867. EXPECT_EQ("-1", info.default_value);
  868. EXPECT_TRUE(info.is_default);
  869. EXPECT_FALSE(info.has_validator_fn);
  870. EXPECT_EQ(&FLAGS_test_int32, info.flag_ptr);
  871. FLAGS_test_bool = true;
  872. r = GetCommandLineFlagInfo("test_bool", &info);
  873. EXPECT_TRUE(r);
  874. EXPECT_EQ("test_bool", info.name);
  875. EXPECT_EQ("bool", info.type);
  876. EXPECT_EQ("tests bool-ness", info.description);
  877. EXPECT_EQ("true", info.current_value);
  878. EXPECT_EQ("false", info.default_value);
  879. EXPECT_FALSE(info.is_default);
  880. EXPECT_FALSE(info.has_validator_fn);
  881. EXPECT_EQ(&FLAGS_test_bool, info.flag_ptr);
  882. FLAGS_test_bool = false;
  883. r = GetCommandLineFlagInfo("test_bool", &info);
  884. EXPECT_TRUE(r);
  885. EXPECT_EQ("test_bool", info.name);
  886. EXPECT_EQ("bool", info.type);
  887. EXPECT_EQ("tests bool-ness", info.description);
  888. EXPECT_EQ("false", info.current_value);
  889. EXPECT_EQ("false", info.default_value);
  890. EXPECT_FALSE(info.is_default); // value is same, but flag *was* modified
  891. EXPECT_FALSE(info.has_validator_fn);
  892. EXPECT_EQ(&FLAGS_test_bool, info.flag_ptr);
  893. }
  894. TEST(GetCommandLineFlagInfoTest, FlagDoesNotExist) {
  895. CommandLineFlagInfo info;
  896. // Set to some random values that GetCommandLineFlagInfo should not change
  897. info.name = "name";
  898. info.type = "type";
  899. info.current_value = "curr";
  900. info.default_value = "def";
  901. info.filename = "/";
  902. info.is_default = false;
  903. info.has_validator_fn = true;
  904. info.flag_ptr = NULL;
  905. bool r = GetCommandLineFlagInfo("test_int3210", &info);
  906. EXPECT_FALSE(r);
  907. EXPECT_EQ("name", info.name);
  908. EXPECT_EQ("type", info.type);
  909. EXPECT_EQ("", info.description);
  910. EXPECT_EQ("curr", info.current_value);
  911. EXPECT_EQ("def", info.default_value);
  912. EXPECT_EQ("/", info.filename);
  913. EXPECT_FALSE(info.is_default);
  914. EXPECT_TRUE(info.has_validator_fn);
  915. EXPECT_EQ(NULL, info.flag_ptr);
  916. }
  917. TEST(GetCommandLineFlagInfoOrDieTest, FlagExistsAndIsDefault) {
  918. CommandLineFlagInfo info;
  919. info = GetCommandLineFlagInfoOrDie("test_int32");
  920. EXPECT_EQ("test_int32", info.name);
  921. EXPECT_EQ("int32", info.type);
  922. EXPECT_EQ("", info.description);
  923. EXPECT_EQ("-1", info.current_value);
  924. EXPECT_EQ("-1", info.default_value);
  925. EXPECT_TRUE(info.is_default);
  926. EXPECT_EQ(&FLAGS_test_int32, info.flag_ptr);
  927. info = GetCommandLineFlagInfoOrDie("test_bool");
  928. EXPECT_EQ("test_bool", info.name);
  929. EXPECT_EQ("bool", info.type);
  930. EXPECT_EQ("tests bool-ness", info.description);
  931. EXPECT_EQ("false", info.current_value);
  932. EXPECT_EQ("false", info.default_value);
  933. EXPECT_TRUE(info.is_default);
  934. EXPECT_FALSE(info.has_validator_fn);
  935. EXPECT_EQ(&FLAGS_test_bool, info.flag_ptr);
  936. }
  937. TEST(GetCommandLineFlagInfoOrDieTest, FlagExistsAndWasAssigned) {
  938. FLAGS_test_int32 = 400;
  939. CommandLineFlagInfo info;
  940. info = GetCommandLineFlagInfoOrDie("test_int32");
  941. EXPECT_EQ("test_int32", info.name);
  942. EXPECT_EQ("int32", info.type);
  943. EXPECT_EQ("", info.description);
  944. EXPECT_EQ("400", info.current_value);
  945. EXPECT_EQ("-1", info.default_value);
  946. EXPECT_FALSE(info.is_default);
  947. EXPECT_EQ(&FLAGS_test_int32, info.flag_ptr);
  948. FLAGS_test_bool = true;
  949. info = GetCommandLineFlagInfoOrDie("test_bool");
  950. EXPECT_EQ("test_bool", info.name);
  951. EXPECT_EQ("bool", info.type);
  952. EXPECT_EQ("tests bool-ness", info.description);
  953. EXPECT_EQ("true", info.current_value);
  954. EXPECT_EQ("false", info.default_value);
  955. EXPECT_FALSE(info.is_default);
  956. EXPECT_FALSE(info.has_validator_fn);
  957. EXPECT_EQ(&FLAGS_test_bool, info.flag_ptr);
  958. }
  959. #ifdef GTEST_HAS_DEATH_TEST
  960. TEST(GetCommandLineFlagInfoOrDieDeathTest, FlagDoesNotExist) {
  961. EXPECT_DEATH(GetCommandLineFlagInfoOrDie("test_int3210"),
  962. ".*: flag test_int3210 does not exist");
  963. }
  964. #endif
  965. // These are lightly tested because they're deprecated. Basically,
  966. // the tests are meant to cover how existing users use these functions,
  967. // but not necessarily how new users could use them.
  968. TEST(DeprecatedFunctionsTest, CommandlineFlagsIntoString) {
  969. string s = CommandlineFlagsIntoString();
  970. EXPECT_NE(string::npos, s.find("--test_bool="));
  971. }
  972. TEST(DeprecatedFunctionsTest, AppendFlagsIntoFile) {
  973. FLAGS_test_int32 = 10; // just to make the test more interesting
  974. string filename(TmpFile("flagfile"));
  975. unlink(filename.c_str()); // just to be safe
  976. const bool r = AppendFlagsIntoFile(filename, "not the real argv0");
  977. EXPECT_TRUE(r);
  978. FILE* fp;
  979. EXPECT_EQ(0, SafeFOpen(&fp, filename.c_str(), "r"));
  980. EXPECT_TRUE(fp != NULL);
  981. char line[8192];
  982. EXPECT_TRUE(fgets(line, sizeof(line)-1, fp) != NULL); // get the first line
  983. // First line should be progname.
  984. EXPECT_STREQ("not the real argv0\n", line);
  985. bool found_bool = false, found_int32 = false;
  986. while (fgets(line, sizeof(line)-1, fp)) {
  987. line[sizeof(line)-1] = '\0'; // just to be safe
  988. if (strcmp(line, "--test_bool=false\n") == 0)
  989. found_bool = true;
  990. if (strcmp(line, "--test_int32=10\n") == 0)
  991. found_int32 = true;
  992. }
  993. EXPECT_TRUE(found_int32);
  994. EXPECT_TRUE(found_bool);
  995. fclose(fp);
  996. }
  997. TEST(DeprecatedFunctionsTest, ReadFromFlagsFile) {
  998. FLAGS_test_int32 = -10; // just to make the test more interesting
  999. string filename(TmpFile("flagfile2"));
  1000. unlink(filename.c_str()); // just to be safe
  1001. bool r = AppendFlagsIntoFile(filename, GetArgv0());
  1002. EXPECT_TRUE(r);
  1003. FLAGS_test_int32 = -11;
  1004. r = ReadFromFlagsFile(filename, GetArgv0(), true);
  1005. EXPECT_TRUE(r);
  1006. EXPECT_EQ(-10, FLAGS_test_int32);
  1007. } // unnamed namespace
  1008. TEST(DeprecatedFunctionsTest, ReadFromFlagsFileFailure) {
  1009. FLAGS_test_int32 = -20;
  1010. string filename(TmpFile("flagfile3"));
  1011. FILE* fp;
  1012. EXPECT_EQ(0, SafeFOpen(&fp, filename.c_str(), "w"));
  1013. EXPECT_TRUE(fp != NULL);
  1014. // Note the error in the bool assignment below...
  1015. fprintf(fp, "%s\n--test_int32=-21\n--test_bool=not_a_bool!\n", GetArgv0());
  1016. fclose(fp);
  1017. FLAGS_test_int32 = -22;
  1018. const bool r = ReadFromFlagsFile(filename, GetArgv0(), false);
  1019. EXPECT_FALSE(r);
  1020. EXPECT_EQ(-22, FLAGS_test_int32); // the -21 from the flagsfile didn't take
  1021. }
  1022. TEST(FlagsSetBeforeInitTest, TryFromEnv) {
  1023. EXPECT_EQ("pre-set", FLAGS_test_tryfromenv);
  1024. }
  1025. // The following test case verifies that ParseCommandLineFlags() and
  1026. // ParseCommandLineNonHelpFlags() uses the last definition of a flag
  1027. // in case it's defined more than once.
  1028. DEFINE_int32(test_flag, -1, "used for testing gflags.cc");
  1029. // Parses and returns the --test_flag flag.
  1030. // If with_help is true, calls ParseCommandLineFlags; otherwise calls
  1031. // ParseCommandLineNonHelpFlags.
  1032. int32 ParseTestFlag(bool with_help, int argc, const char** const_argv) {
  1033. FlagSaver fs; // Restores the flags before returning.
  1034. // Makes a copy of the input array s.t. it can be reused
  1035. // (ParseCommandLineFlags() will alter the array).
  1036. char** const argv_save = new char*[argc + 1];
  1037. char** argv = argv_save;
  1038. memcpy(argv, const_argv, sizeof(*argv)*(argc + 1));
  1039. if (with_help) {
  1040. ParseCommandLineFlags(&argc, &argv, true);
  1041. } else {
  1042. ParseCommandLineNonHelpFlags(&argc, &argv, true);
  1043. }
  1044. delete[] argv_save;
  1045. return FLAGS_test_flag;
  1046. }
  1047. TEST(ParseCommandLineFlagsUsesLastDefinitionTest,
  1048. WhenFlagIsDefinedTwiceOnCommandLine) {
  1049. const char* argv[] = {
  1050. "my_test",
  1051. "--test_flag=1",
  1052. "--test_flag=2",
  1053. NULL,
  1054. };
  1055. EXPECT_EQ(2, ParseTestFlag(true, arraysize(argv) - 1, argv));
  1056. EXPECT_EQ(2, ParseTestFlag(false, arraysize(argv) - 1, argv));
  1057. }
  1058. TEST(ParseCommandLineFlagsUsesLastDefinitionTest,
  1059. WhenFlagIsDefinedTwiceInFlagFile) {
  1060. const char* argv[] = {
  1061. "my_test",
  1062. GetFlagFileFlag(),
  1063. NULL,
  1064. };
  1065. EXPECT_EQ(2, ParseTestFlag(true, arraysize(argv) - 1, argv));
  1066. EXPECT_EQ(2, ParseTestFlag(false, arraysize(argv) - 1, argv));
  1067. }
  1068. TEST(ParseCommandLineFlagsUsesLastDefinitionTest,
  1069. WhenFlagIsDefinedInCommandLineAndThenFlagFile) {
  1070. const char* argv[] = {
  1071. "my_test",
  1072. "--test_flag=0",
  1073. GetFlagFileFlag(),
  1074. NULL,
  1075. };
  1076. EXPECT_EQ(2, ParseTestFlag(true, arraysize(argv) - 1, argv));
  1077. EXPECT_EQ(2, ParseTestFlag(false, arraysize(argv) - 1, argv));
  1078. }
  1079. TEST(ParseCommandLineFlagsUsesLastDefinitionTest,
  1080. WhenFlagIsDefinedInFlagFileAndThenCommandLine) {
  1081. const char* argv[] = {
  1082. "my_test",
  1083. GetFlagFileFlag(),
  1084. "--test_flag=3",
  1085. NULL,
  1086. };
  1087. EXPECT_EQ(3, ParseTestFlag(true, arraysize(argv) - 1, argv));
  1088. EXPECT_EQ(3, ParseTestFlag(false, arraysize(argv) - 1, argv));
  1089. }
  1090. TEST(ParseCommandLineFlagsUsesLastDefinitionTest,
  1091. WhenFlagIsDefinedInCommandLineAndFlagFileAndThenCommandLine) {
  1092. const char* argv[] = {
  1093. "my_test",
  1094. "--test_flag=0",
  1095. GetFlagFileFlag(),
  1096. "--test_flag=3",
  1097. NULL,
  1098. };
  1099. EXPECT_EQ(3, ParseTestFlag(true, arraysize(argv) - 1, argv));
  1100. EXPECT_EQ(3, ParseTestFlag(false, arraysize(argv) - 1, argv));
  1101. }
  1102. TEST(ParseCommandLineFlagsAndDashArgs, TwoDashArgFirst) {
  1103. const char* argv[] = {
  1104. "my_test",
  1105. "--",
  1106. "--test_flag=0",
  1107. NULL,
  1108. };
  1109. EXPECT_EQ(-1, ParseTestFlag(true, arraysize(argv) - 1, argv));
  1110. EXPECT_EQ(-1, ParseTestFlag(false, arraysize(argv) - 1, argv));
  1111. }
  1112. TEST(ParseCommandLineFlagsAndDashArgs, TwoDashArgMiddle) {
  1113. const char* argv[] = {
  1114. "my_test",
  1115. "--test_flag=7",
  1116. "--",
  1117. "--test_flag=0",
  1118. NULL,
  1119. };
  1120. EXPECT_EQ(7, ParseTestFlag(true, arraysize(argv) - 1, argv));
  1121. EXPECT_EQ(7, ParseTestFlag(false, arraysize(argv) - 1, argv));
  1122. }
  1123. TEST(ParseCommandLineFlagsAndDashArgs, OneDashArg) {
  1124. const char* argv[] = {
  1125. "my_test",
  1126. "-",
  1127. "--test_flag=0",
  1128. NULL,
  1129. };
  1130. EXPECT_EQ(0, ParseTestFlag(true, arraysize(argv) - 1, argv));
  1131. EXPECT_EQ(0, ParseTestFlag(false, arraysize(argv) - 1, argv));
  1132. }
  1133. #ifdef GTEST_HAS_DEATH_TEST
  1134. TEST(ParseCommandLineFlagsUnknownFlagDeathTest,
  1135. FlagIsCompletelyUnknown) {
  1136. const char* argv[] = {
  1137. "my_test",
  1138. "--this_flag_does_not_exist",
  1139. NULL,
  1140. };
  1141. EXPECT_DEATH(ParseTestFlag(true, arraysize(argv) - 1, argv),
  1142. "unknown command line flag.*");
  1143. EXPECT_DEATH(ParseTestFlag(false, arraysize(argv) - 1, argv),
  1144. "unknown command line flag.*");
  1145. }
  1146. TEST(ParseCommandLineFlagsUnknownFlagDeathTest,
  1147. BoolFlagIsCompletelyUnknown) {
  1148. const char* argv[] = {
  1149. "my_test",
  1150. "--nothis_flag_does_not_exist",
  1151. NULL,
  1152. };
  1153. EXPECT_DEATH(ParseTestFlag(true, arraysize(argv) - 1, argv),
  1154. "unknown command line flag.*");
  1155. EXPECT_DEATH(ParseTestFlag(false, arraysize(argv) - 1, argv),
  1156. "unknown command line flag.*");
  1157. }
  1158. TEST(ParseCommandLineFlagsUnknownFlagDeathTest,
  1159. FlagIsNotABool) {
  1160. const char* argv[] = {
  1161. "my_test",
  1162. "--notest_string",
  1163. NULL,
  1164. };
  1165. EXPECT_DEATH(ParseTestFlag(true, arraysize(argv) - 1, argv),
  1166. "boolean value .* specified for .* command line flag");
  1167. EXPECT_DEATH(ParseTestFlag(false, arraysize(argv) - 1, argv),
  1168. "boolean value .* specified for .* command line flag");
  1169. }
  1170. #endif
  1171. TEST(ParseCommandLineFlagsWrongFields,
  1172. DescriptionIsInvalid) {
  1173. // These must not be automatic variables, since command line flags
  1174. // aren't unregistered and gUnit uses FlagSaver to save and restore
  1175. // command line flags' values. If these are on the stack, then when
  1176. // later tests attempt to save and restore their values, the stack
  1177. // addresses of these variables will be overwritten... Stack smash!
  1178. static bool current_storage;
  1179. static bool defvalue_storage;
  1180. FlagRegisterer fr("flag_name", NULL, "filename",
  1181. &current_storage, &defvalue_storage);
  1182. CommandLineFlagInfo fi;
  1183. EXPECT_TRUE(GetCommandLineFlagInfo("flag_name", &fi));
  1184. EXPECT_EQ("", fi.description);
  1185. EXPECT_EQ(&current_storage, fi.flag_ptr);
  1186. }
  1187. static bool ValidateTestFlagIs5(const char* flagname, int32 flagval) {
  1188. if (flagval == 5)
  1189. return true;
  1190. printf("%s isn't 5!\n", flagname);
  1191. return false;
  1192. }
  1193. static bool ValidateTestFlagIs10(const char* flagname, int32 flagval) {
  1194. return flagval == 10;
  1195. }
  1196. TEST(FlagsValidator, ValidFlagViaArgv) {
  1197. const char* argv[] = {
  1198. "my_test",
  1199. "--test_flag=5",
  1200. NULL,
  1201. };
  1202. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
  1203. EXPECT_EQ(5, ParseTestFlag(true, arraysize(argv) - 1, argv));
  1204. // Undo the flag validator setting
  1205. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
  1206. }
  1207. TEST(FlagsValidator, ValidFlagViaSetDefault) {
  1208. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
  1209. // SetCommandLineOptionWithMode returns the empty string on error.
  1210. EXPECT_NE("", SetCommandLineOptionWithMode("test_flag", "5",
  1211. SET_FLAG_IF_DEFAULT));
  1212. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
  1213. }
  1214. TEST(FlagsValidator, ValidFlagViaSetValue) {
  1215. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
  1216. FLAGS_test_flag = 100; // doesn't trigger the validator
  1217. // SetCommandLineOptionWithMode returns the empty string on error.
  1218. EXPECT_NE("", SetCommandLineOptionWithMode("test_flag", "5",
  1219. SET_FLAGS_VALUE));
  1220. EXPECT_NE("", SetCommandLineOptionWithMode("test_flag", "5",
  1221. SET_FLAGS_DEFAULT));
  1222. EXPECT_NE("", SetCommandLineOption("test_flag", "5"));
  1223. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
  1224. }
  1225. #ifdef GTEST_HAS_DEATH_TEST
  1226. TEST(FlagsValidatorDeathTest, InvalidFlagViaArgv) {
  1227. const char* argv[] = {
  1228. "my_test",
  1229. "--test_flag=50",
  1230. NULL,
  1231. };
  1232. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
  1233. EXPECT_DEATH(ParseTestFlag(true, arraysize(argv) - 1, argv),
  1234. "ERROR: failed validation of new value '50' for flag 'test_flag'");
  1235. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
  1236. }
  1237. #endif
  1238. TEST(FlagsValidator, InvalidFlagViaSetDefault) {
  1239. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
  1240. // SetCommandLineOptionWithMode returns the empty string on error.
  1241. EXPECT_EQ("", SetCommandLineOptionWithMode("test_flag", "50",
  1242. SET_FLAG_IF_DEFAULT));
  1243. EXPECT_EQ(-1, FLAGS_test_flag); // the setting-to-50 should have failed
  1244. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
  1245. }
  1246. TEST(FlagsValidator, InvalidFlagViaSetValue) {
  1247. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
  1248. FLAGS_test_flag = 100; // doesn't trigger the validator
  1249. // SetCommandLineOptionWithMode returns the empty string on error.
  1250. EXPECT_EQ("", SetCommandLineOptionWithMode("test_flag", "50",
  1251. SET_FLAGS_VALUE));
  1252. EXPECT_EQ("", SetCommandLineOptionWithMode("test_flag", "50",
  1253. SET_FLAGS_DEFAULT));
  1254. EXPECT_EQ("", SetCommandLineOption("test_flag", "50"));
  1255. EXPECT_EQ(100, FLAGS_test_flag); // the setting-to-50 should have failed
  1256. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
  1257. }
  1258. #ifdef GTEST_HAS_DEATH_TEST
  1259. TEST(FlagsValidatorDeathTest, InvalidFlagNeverSet) {
  1260. // If a flag keeps its default value, and that default value is
  1261. // invalid, we should die at argv-parse time.
  1262. const char* argv[] = {
  1263. "my_test",
  1264. NULL,
  1265. };
  1266. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
  1267. EXPECT_DEATH(ParseTestFlag(true, arraysize(argv) - 1, argv),
  1268. "ERROR: --test_flag must be set on the commandline");
  1269. }
  1270. #endif
  1271. TEST(FlagsValidator, InvalidFlagPtr) {
  1272. int32 dummy;
  1273. EXPECT_FALSE(RegisterFlagValidator(NULL, &ValidateTestFlagIs5));
  1274. EXPECT_FALSE(RegisterFlagValidator(&dummy, &ValidateTestFlagIs5));
  1275. }
  1276. TEST(FlagsValidator, RegisterValidatorTwice) {
  1277. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
  1278. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
  1279. EXPECT_FALSE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs10));
  1280. EXPECT_FALSE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs10));
  1281. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
  1282. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
  1283. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs10));
  1284. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
  1285. }
  1286. TEST(FlagsValidator, CommandLineFlagInfo) {
  1287. CommandLineFlagInfo info;
  1288. info = GetCommandLineFlagInfoOrDie("test_flag");
  1289. EXPECT_FALSE(info.has_validator_fn);
  1290. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
  1291. info = GetCommandLineFlagInfoOrDie("test_flag");
  1292. EXPECT_TRUE(info.has_validator_fn);
  1293. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
  1294. info = GetCommandLineFlagInfoOrDie("test_flag");
  1295. EXPECT_FALSE(info.has_validator_fn);
  1296. }
  1297. TEST(FlagsValidator, FlagSaver) {
  1298. {
  1299. FlagSaver fs;
  1300. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
  1301. EXPECT_EQ("", SetCommandLineOption("test_flag", "50")); // fails validation
  1302. }
  1303. EXPECT_NE("", SetCommandLineOption("test_flag", "50")); // validator is gone
  1304. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, &ValidateTestFlagIs5));
  1305. {
  1306. FlagSaver fs;
  1307. EXPECT_TRUE(RegisterFlagValidator(&FLAGS_test_flag, NULL));
  1308. EXPECT_NE("", SetCommandLineOption("test_flag", "50")); // no validator
  1309. }
  1310. EXPECT_EQ("", SetCommandLineOption("test_flag", "50")); // validator is back
  1311. }
  1312. } // unnamed namespace
  1313. static int main(int argc, char **argv) {
  1314. // Run unit tests only if called without arguments, otherwise this program
  1315. // is used by an "external" usage test
  1316. const bool run_tests = (argc == 1);
  1317. // We need to call SetArgv before parsing flags, so our "test" argv will
  1318. // win out over this executable's real argv. That makes running this
  1319. // test with a real --help flag kinda annoying, unfortunately.
  1320. const char* test_argv[] = { "/test/argv/for/gflags_unittest",
  1321. "argv 2", "3rd argv", "argv #4" };
  1322. SetArgv(arraysize(test_argv), test_argv);
  1323. // The first arg is the usage message, also important for testing.
  1324. string usage_message = (string(GetArgv0()) +
  1325. ": <useless flag> [...]\nDoes something useless.\n");
  1326. // We test setting tryfromenv manually, and making sure
  1327. // ParseCommandLineFlags still evaluates it.
  1328. FLAGS_tryfromenv = "test_tryfromenv";
  1329. setenv("FLAGS_test_tryfromenv", "pre-set", 1);
  1330. // Modify flag values from declared default value in two ways.
  1331. // The recommended way:
  1332. SetCommandLineOptionWithMode("changed_bool1", "true", SET_FLAGS_DEFAULT);
  1333. // The non-recommended way:
  1334. FLAGS_changed_bool2 = true;
  1335. SetUsageMessage(usage_message);
  1336. SetVersionString("test_version");
  1337. ParseCommandLineFlags(&argc, &argv, true);
  1338. MakeTmpdir(&FLAGS_test_tmpdir);
  1339. int exit_status = 0;
  1340. if (run_tests) {
  1341. fprintf(stdout, "Running the unit tests now...\n\n"); fflush(stdout);
  1342. exit_status = RUN_ALL_TESTS();
  1343. } else fprintf(stderr, "\n\nPASS\n");
  1344. ShutDownCommandLineFlags();
  1345. return exit_status;
  1346. }
  1347. } // GFLAGS_NAMESPACE
  1348. int main(int argc, char** argv) {
  1349. return GFLAGS_NAMESPACE::main(argc, argv);
  1350. }