commandlineflags_test.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // (C) Copyright 2017, Google Inc.
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. // http://www.apache.org/licenses/LICENSE-2.0
  6. // Unless required by applicable law or agreed to in writing, software
  7. // distributed under the License is distributed on an "AS IS" BASIS,
  8. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. // See the License for the specific language governing permissions and
  10. // limitations under the License.
  11. #include "commandlineflags.h"
  12. #include "include_gunit.h"
  13. // Flags used for testing parser.
  14. INT_PARAM_FLAG(foo_int, 0, "Integer flag for testing");
  15. INT_PARAM_FLAG(bar_int, 0, "Integer flag for testing");
  16. DOUBLE_PARAM_FLAG(foo_double, 0.1, "Double flag for testing");
  17. DOUBLE_PARAM_FLAG(bar_double, 0.2, "Double flag for testing");
  18. STRING_PARAM_FLAG(foo_string, "foo", "String flag for testing");
  19. STRING_PARAM_FLAG(bar_string, "bar", "String flag for testing");
  20. BOOL_PARAM_FLAG(foo_bool, false, "Bool flag for testing");
  21. BOOL_PARAM_FLAG(bar_bool, false, "Bool flag for testing");
  22. // A flag whose name is a single character, tested for backward
  23. // compatibility. This should be selected to not conflict with existing flags
  24. // in commontraining.cpp.
  25. STRING_PARAM_FLAG(q, "", "Single character name");
  26. namespace tesseract {
  27. class CommandlineflagsTest : public ::testing::Test {
  28. protected:
  29. void TestParser(int argc, const char **const_argv) {
  30. TestParser("", argc, const_argv);
  31. }
  32. void TestParser(const char *usage, int argc, const char **const_argv) {
  33. // Make a copy of the pointer since it can be altered by the function.
  34. char **argv = const_cast<char **>(const_argv);
  35. tesseract::ParseCommandLineFlags(usage, &argc, &argv, true);
  36. }
  37. };
  38. TEST_F(CommandlineflagsTest, RemoveFlags) {
  39. const char *const_argv[] = {"Progname", "--foo_int", "3", "file1.h", "file2.h"};
  40. int argc = countof(const_argv);
  41. char **argv = const_cast<char **>(const_argv);
  42. tesseract::ParseCommandLineFlags(argv[0], &argc, &argv, true);
  43. // argv should be rearranged to look like { "Progname", "file1.h", "file2.h" }
  44. EXPECT_EQ(3, argc);
  45. EXPECT_STREQ("Progname", argv[0]);
  46. EXPECT_STREQ("file1.h", argv[1]);
  47. EXPECT_STREQ("file2.h", argv[2]);
  48. }
  49. #if 0 // TODO: this test needs an update (it currently fails).
  50. TEST_F(CommandlineflagsTest, PrintUsageAndExit) {
  51. const char* argv[] = { "Progname", "--help" };
  52. EXPECT_EXIT(TestParser("Progname [flags]", countof(argv), argv),
  53. ::testing::ExitedWithCode(0),
  54. "USAGE: Progname \\[flags\\]");
  55. }
  56. #endif
  57. TEST_F(CommandlineflagsTest, ExitsWithErrorOnInvalidFlag) {
  58. const char *argv[] = {"", "--test_nonexistent_flag"};
  59. EXPECT_EXIT(TestParser(countof(argv), argv), ::testing::ExitedWithCode(1),
  60. "ERROR: Non-existent flag");
  61. }
  62. TEST_F(CommandlineflagsTest, ParseIntegerFlags) {
  63. const char *argv[] = {"", "--foo_int=3", "--bar_int", "-4"};
  64. TestParser(countof(argv), argv);
  65. EXPECT_EQ(3, FLAGS_foo_int);
  66. EXPECT_EQ(-4, FLAGS_bar_int);
  67. const char *arg_no_value[] = {"", "--bar_int"};
  68. EXPECT_EXIT(TestParser(countof(arg_no_value), arg_no_value), ::testing::ExitedWithCode(1),
  69. "ERROR");
  70. const char *arg_invalid_value[] = {"", "--bar_int", "--foo_int=3"};
  71. EXPECT_EXIT(TestParser(countof(arg_invalid_value), arg_invalid_value),
  72. ::testing::ExitedWithCode(1), "ERROR");
  73. const char *arg_bad_format[] = {"", "--bar_int="};
  74. EXPECT_EXIT(TestParser(countof(arg_bad_format), arg_bad_format), ::testing::ExitedWithCode(1),
  75. "ERROR");
  76. }
  77. TEST_F(CommandlineflagsTest, ParseDoubleFlags) {
  78. const char *argv[] = {"", "--foo_double=3.14", "--bar_double", "1.2"};
  79. TestParser(countof(argv), argv);
  80. EXPECT_EQ(3.14, FLAGS_foo_double);
  81. EXPECT_EQ(1.2, FLAGS_bar_double);
  82. const char *arg_no_value[] = {"", "--bar_double"};
  83. EXPECT_EXIT(TestParser(2, arg_no_value), ::testing::ExitedWithCode(1), "ERROR");
  84. const char *arg_bad_format[] = {"", "--bar_double="};
  85. EXPECT_EXIT(TestParser(2, arg_bad_format), ::testing::ExitedWithCode(1), "ERROR");
  86. }
  87. TEST_F(CommandlineflagsTest, ParseStringFlags) {
  88. const char *argv[] = {"", "--foo_string=abc", "--bar_string", "def"};
  89. TestParser(countof(argv), argv);
  90. EXPECT_STREQ("abc", FLAGS_foo_string.c_str());
  91. EXPECT_STREQ("def", FLAGS_bar_string.c_str());
  92. const char *arg_no_value[] = {"", "--bar_string"};
  93. EXPECT_EXIT(TestParser(2, arg_no_value), ::testing::ExitedWithCode(1), "ERROR");
  94. FLAGS_bar_string.set_value("bar");
  95. const char *arg_empty_string[] = {"", "--bar_string="};
  96. TestParser(2, arg_empty_string);
  97. EXPECT_STREQ("", FLAGS_bar_string.c_str());
  98. }
  99. TEST_F(CommandlineflagsTest, ParseBoolFlags) {
  100. const char *argv[] = {"", "--foo_bool=true", "--bar_bool=1"};
  101. FLAGS_foo_bool.set_value(false);
  102. FLAGS_bar_bool.set_value(false);
  103. TestParser(countof(argv), argv);
  104. // Verify changed value
  105. EXPECT_TRUE(FLAGS_foo_bool);
  106. EXPECT_TRUE(FLAGS_bar_bool);
  107. const char *inv_argv[] = {"", "--foo_bool=false", "--bar_bool=0"};
  108. FLAGS_foo_bool.set_value(true);
  109. FLAGS_bar_bool.set_value(true);
  110. TestParser(3, inv_argv);
  111. // Verify changed value
  112. EXPECT_FALSE(FLAGS_foo_bool);
  113. EXPECT_FALSE(FLAGS_bar_bool);
  114. const char *arg_implied_true[] = {"", "--bar_bool"};
  115. FLAGS_bar_bool.set_value(false);
  116. TestParser(2, arg_implied_true);
  117. EXPECT_TRUE(FLAGS_bar_bool);
  118. const char *arg_missing_val[] = {"", "--bar_bool="};
  119. EXPECT_EXIT(TestParser(2, arg_missing_val), ::testing::ExitedWithCode(1), "ERROR");
  120. }
  121. TEST_F(CommandlineflagsTest, ParseOldFlags) {
  122. EXPECT_STREQ("", FLAGS_q.c_str());
  123. const char *argv[] = {"", "-q", "text"};
  124. TestParser(countof(argv), argv);
  125. EXPECT_STREQ("text", FLAGS_q.c_str());
  126. }
  127. } // namespace tesseract