tesseract  5.0.0-alpha-619-ge9db
commandlineflags_test.cc
Go to the documentation of this file.
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 
12 #include "commandlineflags.h"
13 
14 #include "include_gunit.h"
15 
16 // Flags used for testing parser.
17 INT_PARAM_FLAG(foo_int, 0, "Integer flag for testing");
18 INT_PARAM_FLAG(bar_int, 0, "Integer flag for testing");
19 DOUBLE_PARAM_FLAG(foo_double, 0.1, "Double flag for testing");
20 DOUBLE_PARAM_FLAG(bar_double, 0.2, "Double flag for testing");
21 STRING_PARAM_FLAG(foo_string, "foo", "String flag for testing");
22 STRING_PARAM_FLAG(bar_string, "bar", "String flag for testing");
23 BOOL_PARAM_FLAG(foo_bool, false, "Bool flag for testing");
24 BOOL_PARAM_FLAG(bar_bool, false, "Bool flag for testing");
25 // A flag whose name is a single character, tested for backward
26 // compatibility. This should be selected to not conflict with existing flags
27 // in commontraining.cpp.
28 STRING_PARAM_FLAG(q, "", "Single character name");
29 
30 namespace {
31 
32 class CommandlineflagsTest : public ::testing::Test {
33  protected:
34  void SetUp() {
35  std::locale::global(std::locale(""));
36  }
37 
38  void TestParser(int argc, const char** const_argv) {
39  TestParser("", argc, const_argv);
40  }
41  void TestParser(const char* usage, int argc, const char** const_argv) {
42  // Make a copy of the pointer since it can be altered by the function.
43  char** argv = const_cast<char**>(const_argv);
44  tesseract::ParseCommandLineFlags(usage, &argc, &argv, true);
45  }
46 };
47 
48 TEST_F(CommandlineflagsTest, RemoveFlags) {
49  const char* const_argv[] = {"Progname", "--foo_int", "3", "file1.h",
50  "file2.h"};
51  int argc = ARRAYSIZE(const_argv);
52  char** argv = const_cast<char**>(const_argv);
53  tesseract::ParseCommandLineFlags(argv[0], &argc, &argv, true);
54 
55  // argv should be rearranged to look like { "Progname", "file1.h", "file2.h" }
56  EXPECT_EQ(3, argc);
57  EXPECT_STREQ("Progname", argv[0]);
58  EXPECT_STREQ("file1.h", argv[1]);
59  EXPECT_STREQ("file2.h", argv[2]);
60 }
61 
62 #if 0 // TODO: this test needs an update (it currently fails).
63 TEST_F(CommandlineflagsTest, PrintUsageAndExit) {
64  const char* argv[] = { "Progname", "--help" };
65  EXPECT_EXIT(TestParser("Progname [flags]", ARRAYSIZE(argv), argv),
66  ::testing::ExitedWithCode(0),
67  "USAGE: Progname \\[flags\\]");
68 }
69 #endif
70 
71 TEST_F(CommandlineflagsTest, ExitsWithErrorOnInvalidFlag) {
72  const char* argv[] = {"", "--test_nonexistent_flag"};
73  EXPECT_EXIT(TestParser(ARRAYSIZE(argv), argv), ::testing::ExitedWithCode(1),
74  "ERROR: Non-existent flag");
75 }
76 
77 TEST_F(CommandlineflagsTest, ParseIntegerFlags) {
78  const char* argv[] = {"", "--foo_int=3", "--bar_int", "-4"};
79  TestParser(ARRAYSIZE(argv), argv);
80  EXPECT_EQ(3, FLAGS_foo_int);
81  EXPECT_EQ(-4, FLAGS_bar_int);
82 
83  const char* arg_no_value[] = {"", "--bar_int"};
84  EXPECT_EXIT(TestParser(ARRAYSIZE(arg_no_value), arg_no_value),
85  ::testing::ExitedWithCode(1), "ERROR");
86 
87  const char* arg_invalid_value[] = {"", "--bar_int", "--foo_int=3"};
88  EXPECT_EXIT(TestParser(ARRAYSIZE(arg_invalid_value), arg_invalid_value),
89  ::testing::ExitedWithCode(1), "ERROR");
90 
91  const char* arg_bad_format[] = {"", "--bar_int="};
92  EXPECT_EXIT(TestParser(ARRAYSIZE(arg_bad_format), arg_bad_format),
93  ::testing::ExitedWithCode(1), "ERROR");
94 }
95 
96 TEST_F(CommandlineflagsTest, ParseDoubleFlags) {
97  const char* argv[] = {"", "--foo_double=3.14", "--bar_double", "1.2"};
98  TestParser(ARRAYSIZE(argv), argv);
99 
100  EXPECT_EQ(3.14, FLAGS_foo_double);
101  EXPECT_EQ(1.2, FLAGS_bar_double);
102 
103  const char* arg_no_value[] = {"", "--bar_double"};
104  EXPECT_EXIT(TestParser(2, arg_no_value), ::testing::ExitedWithCode(1),
105  "ERROR");
106 
107  const char* arg_bad_format[] = {"", "--bar_double="};
108  EXPECT_EXIT(TestParser(2, arg_bad_format), ::testing::ExitedWithCode(1),
109  "ERROR");
110 }
111 
112 TEST_F(CommandlineflagsTest, ParseStringFlags) {
113  const char* argv[] = {"", "--foo_string=abc", "--bar_string", "def"};
114  TestParser(ARRAYSIZE(argv), argv);
115 
116  EXPECT_STREQ("abc", FLAGS_foo_string.c_str());
117  EXPECT_STREQ("def", FLAGS_bar_string.c_str());
118 
119  const char* arg_no_value[] = {"", "--bar_string"};
120  EXPECT_EXIT(TestParser(2, arg_no_value), ::testing::ExitedWithCode(1),
121  "ERROR");
122 
123  FLAGS_bar_string.set_value("bar");
124  const char* arg_empty_string[] = {"", "--bar_string="};
125  TestParser(2, arg_empty_string);
126  EXPECT_STREQ("", FLAGS_bar_string.c_str());
127 }
128 
129 TEST_F(CommandlineflagsTest, ParseBoolFlags) {
130  const char* argv[] = {"", "--foo_bool=true", "--bar_bool=1"};
131  FLAGS_foo_bool.set_value(false);
132  FLAGS_bar_bool.set_value(false);
133  TestParser(ARRAYSIZE(argv), argv);
134  // Verify changed value
135  EXPECT_TRUE(FLAGS_foo_bool);
136  EXPECT_TRUE(FLAGS_bar_bool);
137 
138  const char* inv_argv[] = {"", "--foo_bool=false", "--bar_bool=0"};
139  FLAGS_foo_bool.set_value(true);
140  FLAGS_bar_bool.set_value(true);
141  TestParser(3, inv_argv);
142  // Verify changed value
143  EXPECT_FALSE(FLAGS_foo_bool);
144  EXPECT_FALSE(FLAGS_bar_bool);
145 
146  const char* arg_implied_true[] = {"", "--bar_bool"};
147  FLAGS_bar_bool.set_value(false);
148  TestParser(2, arg_implied_true);
149  EXPECT_TRUE(FLAGS_bar_bool);
150 
151  const char* arg_missing_val[] = {"", "--bar_bool="};
152  EXPECT_EXIT(TestParser(2, arg_missing_val), ::testing::ExitedWithCode(1),
153  "ERROR");
154 }
155 
156 TEST_F(CommandlineflagsTest, ParseOldFlags) {
157  EXPECT_STREQ("", FLAGS_q.c_str());
158  const char* argv[] = {"", "-q", "text"};
159  TestParser(ARRAYSIZE(argv), argv);
160  EXPECT_STREQ("text", FLAGS_q.c_str());
161 }
162 } // namespace
ARRAYSIZE
#define ARRAYSIZE(arr)
Definition: include_gunit.h:53
tesseract::ParseCommandLineFlags
void ParseCommandLineFlags(const char *usage, int *argc, char ***argv, const bool remove_flags)
Definition: commandlineflags.cpp:166
include_gunit.h
tesseract::TEST_F
TEST_F(EquationFinderTest, IdentifySpecialText)
Definition: equationdetect_test.cc:181
STRING_PARAM_FLAG
STRING_PARAM_FLAG(foo_string, "foo", "String flag for testing")
INT_PARAM_FLAG
INT_PARAM_FLAG(foo_int, 0, "Integer flag for testing")
commandlineflags.h
BOOL_PARAM_FLAG
BOOL_PARAM_FLAG(foo_bool, false, "Bool flag for testing")
DOUBLE_PARAM_FLAG
DOUBLE_PARAM_FLAG(foo_double, 0.1, "Double flag for testing")