tesseract v5.3.3.20231005
sample9_unittest.cc File Reference
#include <stdio.h>
#include "gtest/gtest.h"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 104 of file sample9_unittest.cc.

104 {
105 InitGoogleTest(&argc, argv);
106
107 bool terse_output = false;
108 if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 )
109 terse_output = true;
110 else
111 printf("%s\n", "Run this program with --terse_output to change the way "
112 "it prints its output.");
113
114 UnitTest& unit_test = *UnitTest::GetInstance();
115
116 // If we are given the --terse_output command line flag, suppresses the
117 // standard output and attaches own result printer.
118 if (terse_output) {
119 TestEventListeners& listeners = unit_test.listeners();
120
121 // Removes the default console output listener from the list so it will
122 // not receive events from Google Test and won't print any output. Since
123 // this operation transfers ownership of the listener to the caller we
124 // have to delete it as well.
125 delete listeners.Release(listeners.default_result_printer());
126
127 // Adds the custom output listener to the list. It will now receive
128 // events from Google Test and print the alternative output. We don't
129 // have to worry about deleting it since Google Test assumes ownership
130 // over it after adding it to the list.
131 listeners.Append(new TersePrinter);
132 }
133 int ret_val = RUN_ALL_TESTS();
134
135 // This is an example of using the UnitTest reflection API to inspect test
136 // results. Here we discount failures from the tests we expected to fail.
137 int unexpectedly_failed_tests = 0;
138 for (int i = 0; i < unit_test.total_test_suite_count(); ++i) {
139 const testing::TestSuite& test_suite = *unit_test.GetTestSuite(i);
140 for (int j = 0; j < test_suite.total_test_count(); ++j) {
141 const TestInfo& test_info = *test_suite.GetTestInfo(j);
142 // Counts failed tests that were not meant to fail (those without
143 // 'Fails' in the name).
144 if (test_info.result()->Failed() &&
145 strcmp(test_info.name(), "Fails") != 0) {
146 unexpectedly_failed_tests++;
147 }
148 }
149 }
150
151 // Test that were meant to fail should not affect the test program outcome.
152 if (unexpectedly_failed_tests == 0)
153 ret_val = 0;
154
155 return ret_val;
156}
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2489
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:6660
int total_test_count() const
Definition: gtest.cc:2941
const TestInfo * GetTestInfo(int i) const
Definition: gtest.cc:2973