17 #include "my_config.h"
18 #include <gtest/gtest.h>
24 using testing::TestEventListeners;
25 using testing::TestCase;
26 using testing::TestEventListener;
27 using testing::TestInfo;
28 using testing::TestPartResult;
29 using testing::UnitTest;
44 virtual void OnTestProgramStart(
const UnitTest& ) {}
45 virtual void OnTestIterationStart(
const UnitTest& unit_test,
int iteration);
46 virtual void OnEnvironmentsSetUpStart(
const UnitTest& unit_test);
47 virtual void OnEnvironmentsSetUpEnd(
const UnitTest& ) {}
48 virtual void OnTestCaseStart(
const TestCase&
test_case);
49 virtual void OnTestStart(
const TestInfo& test_info);
50 virtual void OnTestPartResult(
const TestPartResult& test_part_result);
51 virtual void OnTestEnd(
const TestInfo& test_info);
52 virtual void OnTestCaseEnd(
const TestCase& ) {};
53 virtual void OnEnvironmentsTearDownStart(
const UnitTest& unit_test);
54 virtual void OnEnvironmentsTearDownEnd(
const UnitTest& ) {}
55 virtual void OnTestIterationEnd(
const UnitTest& unit_test,
int iteration);
56 virtual void OnTestProgramEnd(
const UnitTest& ) {}
59 std::string m_test_case_name;
66 static void tap_diagnostic_printf(
const std::stringstream &str_stream)
68 std::string
message= str_stream.str();
70 while((pos = message.find(
"\n", pos)) != std::string::npos)
72 message.replace(pos, 1,
"\n# ");
75 printf(
"# %s\n", message.c_str());
80 static void tap_diagnostic_printf(
const std::string &txt)
82 std::stringstream str_str;
84 tap_diagnostic_printf(str_str);
88 static void tap_diagnostic_printf(
const char *txt)
90 tap_diagnostic_printf(std::string(txt));
98 num_tests(
int num) : m_num(num) {}
102 std::ostream &operator<< (std::ostream &s,
const num_tests &num)
104 return s << num.m_num << (num.m_num == 1 ?
" test" :
" tests");
108 struct num_test_cases
110 num_test_cases(
int num) : m_num(num) {}
114 std::ostream &operator<< (std::ostream &s,
const num_test_cases &num)
116 return s << num.m_num << (num.m_num == 1 ?
" test case" :
" test cases");
125 static std::string test_part_result_type_tostring(TestPartResult::Type
type)
129 case TestPartResult::kSuccess:
132 case TestPartResult::kNonFatalFailure:
133 case TestPartResult::kFatalFailure:
144 static std::string format_file_location(
const TestPartResult &test_part_result)
146 const char*
const file= test_part_result.file_name();
147 const char*
const file_name = file == NULL ?
"unknown file" :
file;
148 const int line= test_part_result.line_number();
149 std::stringstream str_stream;
150 str_stream << file_name <<
":";
152 str_stream << line <<
":";
153 return str_stream.str();
160 static std::string test_part_result_tostring(
const TestPartResult
163 return format_file_location(test_part_result)
165 + test_part_result_type_tostring(test_part_result.type())
166 + test_part_result.message();
170 void TapEventListener::OnTestIterationStart(
const UnitTest& unit_test,
173 std::stringstream str_stream;
174 str_stream <<
"Running " << num_tests(unit_test.test_to_run_count())
175 <<
" from " << num_test_cases(unit_test.test_case_to_run_count());
176 tap_diagnostic_printf(str_stream);
177 printf(
"%d..%d\n", 1, unit_test.test_to_run_count());
182 void TapEventListener::OnEnvironmentsSetUpStart(
const UnitTest& unit_test)
184 tap_diagnostic_printf(
"Global test environment set-up");
188 void TapEventListener::OnTestCaseStart(
const TestCase&
test_case)
190 m_test_case_name = test_case.name();
194 void TapEventListener::OnTestStart(
const TestInfo& test_info)
197 std::stringstream str_stream;
198 str_stream <<
"Run " << m_test_number <<
" "
199 << m_test_case_name <<
"." << test_info.name();
200 tap_diagnostic_printf(str_stream);
204 void TapEventListener::OnTestPartResult(
const TestPartResult& test_part_result)
206 if (test_part_result.passed())
209 fprintf(stderr,
"%s\n", test_part_result_tostring(test_part_result).c_str());
213 void TapEventListener::OnTestEnd(
const TestInfo& test_info)
215 if (test_info.result()->Passed())
216 printf(
"ok %d\n", m_test_number);
218 printf(
"not ok %d\n", m_test_number);
223 void TapEventListener::OnEnvironmentsTearDownStart(
const UnitTest& unit_test)
225 tap_diagnostic_printf(
"Global test environment tear-down");
229 void TapEventListener::OnTestIterationEnd(
const UnitTest& unit_test,
232 std::stringstream str_stream;
233 str_stream <<
"Ran " << num_tests(unit_test.test_to_run_count())
234 <<
" from " << num_test_cases(unit_test.test_case_to_run_count())
236 <<
"Passed " << num_tests(unit_test.successful_test_count());
238 if (!unit_test.Passed())
240 <<
"Failed " << num_tests(unit_test.failed_test_count());
242 const int num_disabled = unit_test.disabled_test_count();
243 if (num_disabled && !testing::GTEST_FLAG(also_run_disabled_tests))
245 <<
"YOU HAVE " << num_disabled <<
" DISABLED "
246 << (num_disabled == 1 ?
"TEST" :
"TESTS");
248 tap_diagnostic_printf(str_stream);
256 void install_tap_listener()
258 TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
259 delete listeners.Release(listeners.default_result_printer());