51 #ifndef GTEST_INCLUDE_GTEST_GTEST_H_
52 #define GTEST_INCLUDE_GTEST_GTEST_H_
152 class DefaultGlobalTestPartResultReporter;
154 class NoExecDeathTest;
155 class FinalSuccessChecker;
156 class GTestFlagSaver;
157 class StreamingListenerTest;
158 class TestResultAccessor;
159 class TestEventListenersAccessor;
160 class TestEventRepeater;
161 class UnitTestRecordPropertyTestHelper;
162 class WindowsDeathTest;
271 template <
typename T>
274 typename internal::EnableIf<
277 : success_(success) {}
288 operator bool()
const {
return success_; }
298 return message_.get() != NULL ? message_->c_str() :
"";
306 AppendMessage(
Message() << value);
313 ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) {
314 AppendMessage(
Message() << basic_manipulator);
321 if (message_.get() == NULL)
323 message_->append(a_message.
GetString().c_str());
400 static bool HasFatalFailure();
403 static bool HasNonfatalFailure();
407 static bool HasFailure() {
return HasFatalFailure() || HasNonfatalFailure(); }
430 virtual void SetUp();
433 virtual void TearDown();
438 static bool HasSameFixtureClass();
446 virtual void TestBody() = 0;
533 int total_part_count()
const;
536 int test_property_count()
const;
539 bool Passed()
const {
return !Failed(); }
545 bool HasFatalFailure()
const;
548 bool HasNonfatalFailure()
const;
568 friend class internal::ExecDeathTest;
571 friend class internal::WindowsDeathTest;
575 return test_part_results_;
580 return test_properties_;
592 void RecordProperty(
const std::string& xml_element,
598 static bool ValidateTestProperty(
const std::string& xml_element,
611 void ClearTestPartResults();
659 if (type_param_.get() != NULL)
660 return type_param_->c_str();
667 if (value_param_.get() != NULL)
668 return value_param_->c_str();
673 const char*
file()
const {
return location_.file.c_str(); }
676 int line()
const {
return location_.line; }
701 return matches_filter_;
708 #if GTEST_HAS_DEATH_TEST
709 friend class internal::DefaultDeathTestFactory;
710 #endif // GTEST_HAS_DEATH_TEST
714 friend class internal::StreamingListenerTest;
716 const char* test_case_name,
718 const char* type_param,
719 const char* value_param,
730 const char* a_type_param,
731 const char* a_value_param,
739 return result_.increment_death_test_count();
805 if (type_param_.get() != NULL)
806 return type_param_->c_str();
814 int successful_test_count()
const;
817 int failed_test_count()
const;
820 int reportable_disabled_test_count()
const;
823 int disabled_test_count()
const;
826 int reportable_test_count()
const;
829 int test_to_run_count()
const;
832 int total_test_count()
const;
835 bool Passed()
const {
return !Failed(); }
838 bool Failed()
const {
return failed_test_count() > 0; }
845 const TestInfo* GetTestInfo(
int i)
const;
860 return test_info_list_;
872 void AddTestInfo(
TestInfo * test_info);
928 void UnshuffleTests();
1085 return default_result_printer_;
1096 return default_xml_generator_;
1103 friend class internal::NoExecDeathTest;
1127 bool EventForwardingEnabled()
const;
1128 void SuppressEventForwarding();
1168 const char* original_working_dir()
const;
1172 const TestCase* current_test_case()
const
1177 const TestInfo* current_test_info()
const
1181 int random_seed()
const;
1183 #if GTEST_HAS_PARAM_TEST
1188 internal::ParameterizedTestCaseRegistry& parameterized_test_registry()
1190 #endif // GTEST_HAS_PARAM_TEST
1193 int successful_test_case_count()
const;
1196 int failed_test_case_count()
const;
1199 int total_test_case_count()
const;
1203 int test_case_to_run_count()
const;
1206 int successful_test_count()
const;
1209 int failed_test_count()
const;
1212 int reportable_disabled_test_count()
const;
1215 int disabled_test_count()
const;
1218 int reportable_test_count()
const;
1221 int total_test_count()
const;
1224 int test_to_run_count()
const;
1234 bool Passed()
const;
1238 bool Failed()
const;
1242 const TestCase* GetTestCase(
int i)
const;
1246 const TestResult& ad_hoc_test_result()
const;
1269 const char* file_name,
1284 TestCase* GetMutableTestCase(
int i);
1295 friend class internal::StreamingListenerTest;
1315 void PopGTestTrace()
1316 GTEST_LOCK_EXCLUDED_(mutex_);
1320 mutable internal::Mutex mutex_;
1326 internal::UnitTestImpl* impl_;
1369 namespace internal {
1374 template <
typename T1,
typename T2>
1376 const char* rhs_expression,
1386 template <
typename T1,
typename T2>
1388 const char* rhs_expression,
1404 const char* rhs_expression,
1412 template <
bool lhs_is_null_literal>
1416 template <
typename T1,
typename T2>
1418 const char* rhs_expression,
1421 return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs);
1431 const char* rhs_expression,
1434 return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs);
1447 template <
typename T1,
typename T2>
1449 const char* lhs_expression,
1450 const char* rhs_expression,
1459 return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs);
1464 template <
typename T>
1466 const char* lhs_expression,
1467 const char* rhs_expression,
1477 return CmpHelperEQ(lhs_expression, rhs_expression,
1478 static_cast<T*>(NULL), rhs);
1485 template <
typename T1,
typename T2>
1487 const T1& val1,
const T2& val2,
1490 <<
"Expected: (" << expr1 <<
") " << op <<
" (" << expr2
1506 #define GTEST_IMPL_CMP_HELPER_(op_name, op)\
1507 template <typename T1, typename T2>\
1508 AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
1509 const T1& val1, const T2& val2) {\
1510 if (val1 op val2) {\
1511 return AssertionSuccess();\
1513 return CmpHelperOpFailure(expr1, expr2, val1, val2, #op);\
1516 GTEST_API_ AssertionResult CmpHelper##op_name(\
1517 const char* expr1, const char* expr2, BiggestInt val1, BiggestInt val2)
1532 #undef GTEST_IMPL_CMP_HELPER_
1538 const char* s2_expression,
1546 const char* s2_expression,
1554 const char* s2_expression,
1562 const char* s2_expression,
1571 const char* s2_expression,
1579 const char* s2_expression,
1594 const char* needle_expr,
const char* haystack_expr,
1595 const char* needle,
const char* haystack);
1597 const char* needle_expr,
const char* haystack_expr,
1598 const wchar_t* needle,
const wchar_t* haystack);
1600 const char* needle_expr,
const char* haystack_expr,
1601 const char* needle,
const char* haystack);
1603 const char* needle_expr,
const char* haystack_expr,
1604 const wchar_t* needle,
const wchar_t* haystack);
1606 const char* needle_expr,
const char* haystack_expr,
1609 const char* needle_expr,
const char* haystack_expr,
1612 #if GTEST_HAS_STD_WSTRING
1614 const char* needle_expr,
const char* haystack_expr,
1617 const char* needle_expr,
const char* haystack_expr,
1619 #endif // GTEST_HAS_STD_WSTRING
1621 namespace internal {
1630 template <
typename RawType>
1632 const char* rhs_expression,
1634 RawType rhs_value) {
1637 if (
lhs.AlmostEquals(rhs)) {
1641 ::std::stringstream lhs_ss;
1642 lhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
1645 ::std::stringstream rhs_ss;
1646 rhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
1661 const char* abs_error_expr,
1674 const char* message);
1688 const char* srcfile,
1691 : type(t), file(srcfile), line(line_num), message(msg) { }
1709 #if GTEST_HAS_PARAM_TEST
1744 template <
typename T>
1745 class WithParamInterface {
1747 typedef T ParamType;
1748 virtual ~WithParamInterface() {}
1755 const ParamType& GetParam()
const {
1757 <<
"GetParam() can only be called inside a value-parameterized test "
1758 <<
"-- did you intend to write TEST_P instead of TEST_F?";
1765 static void SetParam(
const ParamType* parameter) {
1766 parameter_ = parameter;
1770 static const ParamType* parameter_;
1773 template <
class TestClass>
friend class internal::ParameterizedTestFactory;
1776 template <
typename T>
1777 const T* WithParamInterface<T>::parameter_ = NULL;
1782 template <
typename T>
1783 class TestWithParam :
public Test,
public WithParamInterface<T> {
1786 #endif // GTEST_HAS_PARAM_TEST
1808 #define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed")
1812 #define ADD_FAILURE_AT(file, line) \
1813 GTEST_MESSAGE_AT_(file, line, "Failed", \
1814 ::testing::TestPartResult::kNonFatalFailure)
1817 #define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed")
1821 #if !GTEST_DONT_DEFINE_FAIL
1822 # define FAIL() GTEST_FAIL()
1826 #define GTEST_SUCCEED() GTEST_SUCCESS_("Succeeded")
1830 #if !GTEST_DONT_DEFINE_SUCCEED
1831 # define SUCCEED() GTEST_SUCCEED()
1843 #define EXPECT_THROW(statement, expected_exception) \
1844 GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_)
1845 #define EXPECT_NO_THROW(statement) \
1846 GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_)
1847 #define EXPECT_ANY_THROW(statement) \
1848 GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_)
1849 #define ASSERT_THROW(statement, expected_exception) \
1850 GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_)
1851 #define ASSERT_NO_THROW(statement) \
1852 GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_)
1853 #define ASSERT_ANY_THROW(statement) \
1854 GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_)
1859 #define EXPECT_TRUE(condition) \
1860 GTEST_TEST_BOOLEAN_((condition), #condition, false, true, \
1861 GTEST_NONFATAL_FAILURE_)
1862 #define EXPECT_FALSE(condition) \
1863 GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
1864 GTEST_NONFATAL_FAILURE_)
1865 #define ASSERT_TRUE(condition) \
1866 GTEST_TEST_BOOLEAN_((condition), #condition, false, true, \
1867 GTEST_FATAL_FAILURE_)
1868 #define ASSERT_FALSE(condition) \
1869 GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
1870 GTEST_FATAL_FAILURE_)
1922 #define EXPECT_EQ(val1, val2) \
1923 EXPECT_PRED_FORMAT2(::testing::internal:: \
1924 EqHelper<GTEST_IS_NULL_LITERAL_(val1)>::Compare, \
1926 #define EXPECT_NE(val1, val2) \
1927 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
1928 #define EXPECT_LE(val1, val2) \
1929 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
1930 #define EXPECT_LT(val1, val2) \
1931 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
1932 #define EXPECT_GE(val1, val2) \
1933 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
1934 #define EXPECT_GT(val1, val2) \
1935 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
1937 #define GTEST_ASSERT_EQ(val1, val2) \
1938 ASSERT_PRED_FORMAT2(::testing::internal:: \
1939 EqHelper<GTEST_IS_NULL_LITERAL_(val1)>::Compare, \
1941 #define GTEST_ASSERT_NE(val1, val2) \
1942 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
1943 #define GTEST_ASSERT_LE(val1, val2) \
1944 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
1945 #define GTEST_ASSERT_LT(val1, val2) \
1946 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
1947 #define GTEST_ASSERT_GE(val1, val2) \
1948 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
1949 #define GTEST_ASSERT_GT(val1, val2) \
1950 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
1955 #if !GTEST_DONT_DEFINE_ASSERT_EQ
1956 # define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2)
1959 #if !GTEST_DONT_DEFINE_ASSERT_NE
1960 # define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2)
1963 #if !GTEST_DONT_DEFINE_ASSERT_LE
1964 # define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2)
1967 #if !GTEST_DONT_DEFINE_ASSERT_LT
1968 # define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2)
1971 #if !GTEST_DONT_DEFINE_ASSERT_GE
1972 # define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2)
1975 #if !GTEST_DONT_DEFINE_ASSERT_GT
1976 # define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2)
1995 #define EXPECT_STREQ(s1, s2) \
1996 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2)
1997 #define EXPECT_STRNE(s1, s2) \
1998 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
1999 #define EXPECT_STRCASEEQ(s1, s2) \
2000 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2)
2001 #define EXPECT_STRCASENE(s1, s2)\
2002 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
2004 #define ASSERT_STREQ(s1, s2) \
2005 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2)
2006 #define ASSERT_STRNE(s1, s2) \
2007 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
2008 #define ASSERT_STRCASEEQ(s1, s2) \
2009 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2)
2010 #define ASSERT_STRCASENE(s1, s2)\
2011 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
2027 #define EXPECT_FLOAT_EQ(val1, val2)\
2028 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
2031 #define EXPECT_DOUBLE_EQ(val1, val2)\
2032 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
2035 #define ASSERT_FLOAT_EQ(val1, val2)\
2036 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
2039 #define ASSERT_DOUBLE_EQ(val1, val2)\
2040 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
2043 #define EXPECT_NEAR(val1, val2, abs_error)\
2044 EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \
2045 val1, val2, abs_error)
2047 #define ASSERT_NEAR(val1, val2, abs_error)\
2048 ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \
2049 val1, val2, abs_error)
2059 float val1,
float val2);
2061 double val1,
double val2);
2064 #if GTEST_OS_WINDOWS
2075 # define EXPECT_HRESULT_SUCCEEDED(expr) \
2076 EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
2078 # define ASSERT_HRESULT_SUCCEEDED(expr) \
2079 ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
2081 # define EXPECT_HRESULT_FAILED(expr) \
2082 EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
2084 # define ASSERT_HRESULT_FAILED(expr) \
2085 ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
2087 #endif // GTEST_OS_WINDOWS
2099 #define ASSERT_NO_FATAL_FAILURE(statement) \
2100 GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_)
2101 #define EXPECT_NO_FATAL_FAILURE(statement) \
2102 GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_)
2115 #define SCOPED_TRACE(message) \
2116 ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\
2117 __FILE__, __LINE__, ::testing::Message() << (message))
2149 template <
typename T1,
typename T2>
2151 (void)internal::StaticAssertTypeEqHelper<T1, T2>();
2180 #define GTEST_TEST(test_case_name, test_name)\
2181 GTEST_TEST_(test_case_name, test_name, \
2182 ::testing::Test, ::testing::internal::GetTestTypeId())
2186 #if !GTEST_DONT_DEFINE_TEST
2187 # define TEST(test_case_name, test_name) GTEST_TEST(test_case_name, test_name)
2216 #define TEST_F(test_fixture, test_name)\
2217 GTEST_TEST_(test_fixture, test_name, test_fixture, \
2218 ::testing::internal::GetTypeId<test_fixture>())
2233 return ::testing::UnitTest::GetInstance()->Run();
2236 #endif // GTEST_INCLUDE_GTEST_GTEST_H_