00001 #ifndef ASG_TOOLS__UNIT_TEST_H
00002 #define ASG_TOOLS__UNIT_TEST_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <AsgTools/MessageCheck.h>
00014 #include <string>
00015 #include <exception>
00016 #include <gtest/gtest.h>
00017
00018 namespace asg
00019 {
00020 namespace detail
00021 {
00028 bool matchesRegex (const std::string& regex, const std::string& str);
00029 }
00030
00031 template<typename T> struct CheckHelper;
00032 }
00033
00034 #define ASSERT_SUCCESS(x) \
00035 ASSERT_EQ (asg::CheckHelper<decltype(x)>::successCode(), x)
00036
00037 #define EXPECT_SUCCESS(x) \
00038 EXPECT_EQ (asg::CheckHelper<decltype(x)>::successCode(), x)
00039
00040 #define ASSERT_FAILURE(x) \
00041 ASSERT_EQ (asg::CheckHelper<decltype(x)>::failureCode(), x)
00042
00043 #define EXPECT_FAILURE(x) \
00044 EXPECT_EQ (asg::CheckHelper<decltype(x)>::failureCode(), x)
00045
00046 #define ASSERT_THROW_REGEX(x,regex) \
00047 { std::string internalTestMessage; try { \
00048 x; internalTestMessage = std::string ("expected statement ") + #x " to throw, but it didn't"; \
00049 } catch (std::exception& e) { \
00050 if (!::asg::detail::matchesRegex ((regex), e.what())) { \
00051 internalTestMessage = std::string ("expected statement ") + #x " to throw message matching " + (regex) + ", but actual message didn't match: " + e.what(); \
00052 } } catch (...) { \
00053 internalTestMessage = std::string ("statement ") + #x " threw an exception that didn't derive from std::exception"; } \
00054 if (!internalTestMessage.empty()) \
00055 FAIL() << internalTestMessage; \
00056 else SUCCEED(); }
00057
00058 #define EXPECT_THROW_REGEX(x,regex) \
00059 { std::string internalTestMessage; try { \
00060 x; internalTestMessage = std::string ("expected statement ") + #x " to throw, but it didn't"; \
00061 } catch (std::exception& e) { \
00062 if (!::asg::detail::matchesRegex ((regex), e.what())) { \
00063 internalTestMessage = std::string ("expected statement ") + #x " to throw message matching " + (regex) + ", but actual message didn't match: " + e.what(); \
00064 } } catch (...) { \
00065 internalTestMessage = std::string ("statement ") + #x " threw an exception that didn't derive from std::exception"; } \
00066 if (!internalTestMessage.empty()) \
00067 ADD_FAILURE() << internalTestMessage; \
00068 else SUCCEED(); }
00069
00070 #define ASSERT_MATCH_REGEX(reg,str) \
00071 ASSERT_PRED2 (::asg::detail::matchesRegex, reg, str)
00072
00073 #define EXPECT_MATCH_REGEX(reg,str) \
00074 EXPECT_PRED2 (::asg::detail::matchesRegex, reg, str)
00075
00076
00077
00078 #ifndef ASGTOOL_STANDALONE
00079
00080 #define ATLAS_GOOGLE_TEST_MAIN \
00081 int main (int argc, char **argv) \
00082 { \
00083 ::testing::InitGoogleTest (&argc, argv); \
00084 return RUN_ALL_TESTS(); \
00085 }
00086
00087 #else
00088
00089 #include <xAODRootAccess/Init.h>
00090
00091 #define ATLAS_GOOGLE_TEST_MAIN \
00092 int main (int argc, char **argv) \
00093 { \
00094 using namespace asg::msgUserCode; \
00095 ANA_CHECK_SET_TYPE (int); \
00096 ::StatusCode::enableFailure(); \
00097 ANA_CHECK (xAOD::Init ()); \
00098 ::testing::InitGoogleTest (&argc, argv); \
00099 return RUN_ALL_TESTS(); \
00100 }
00101
00102 #endif
00103
00104 #endif