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 <AsgTools/MessagePrinterErrorCollect.h>
00015 #include <AsgTools/MessagePrinterOverlay.h>
00016 #include <string>
00017 #include <exception>
00018 #include <gtest/gtest.h>
00019
00020 namespace asg
00021 {
00022 namespace detail
00023 {
00030 bool matchesRegex (const std::string& regex, const std::string& str);
00031 }
00032
00033 template<typename T> struct CheckHelper;
00034 }
00035
00036 #define ASSERT_SUCCESS(x) \
00037 ASSERT_EQ (asg::CheckHelper<decltype(x)>::successCode(), x)
00038
00039 #define EXPECT_SUCCESS(x) \
00040 EXPECT_EQ (asg::CheckHelper<decltype(x)>::successCode(), x)
00041
00042 #define ASSERT_FAILURE(x) \
00043 ASSERT_EQ (asg::CheckHelper<decltype(x)>::failureCode(), x)
00044
00045 #define EXPECT_FAILURE(x) \
00046 EXPECT_EQ (asg::CheckHelper<decltype(x)>::failureCode(), x)
00047
00048
00049 #ifndef ROOTCORE
00050 #define ASSERT_FAILURE_REGEX(x,regex) \
00051 ASSERT_FAILURE(x)
00052 #define EXPECT_FAILURE_REGEX(x,regex) \
00053 EXPECT_FAILURE(x)
00054 #else
00055 #define ASSERT_FAILURE_REGEX(x,regex) \
00056 { MessagePrinterErrorCollect errorPrinter; \
00057 MessagePrinterOverlay overlayPrinter (&errorPrinter); \
00058 auto code = (x); \
00059 if (asg::CheckHelper<decltype(x)>::isSuccess (code)) { \
00060 FAIL () << "command didn't fail as expected: " << #x; \
00061 } else if (errorPrinter.empty()) { \
00062 FAIL () << "command printed no error message on failure: " << #x; \
00063 } else if (!errorPrinter.matchesRegex (regex)) { \
00064 FAIL () << "invalid error message for: " << #x \
00065 << "\nexpected:\n " << regex << "\nfound:\n" \
00066 << errorPrinter.asString (" "); \
00067 } }
00068 #define EXPECT_FAILURE_REGEX(x,regex) \
00069 { MessagePrinterErrorCollect errorPrinter; \
00070 MessagePrinterOverlay overlayPrinter (&errorPrinter); \
00071 auto code = (x); \
00072 if (asg::CheckHelper<decltype(x)>::isSuccess (code)) { \
00073 ADD_FAILURE () << "command didn't fail as expected: " << #x; \
00074 } else if (errorPrinter.empty()) { \
00075 ADD_FAILURE () << "command printed no error message on failure: " << #x; \
00076 } else if (!errorPrinter.matchesRegex (regex)) { \
00077 ADD_FAILURE () << "invalid error message for: " << #x \
00078 << "\nexpected:\n " << regex << "\nfound:\n" \
00079 << errorPrinter.asString (" "); \
00080 } }
00081 #endif
00082
00083 #define ASSERT_THROW_REGEX(x,regex) \
00084 { std::string internalTestMessage; try { \
00085 x; internalTestMessage = std::string ("expected statement ") + #x " to throw, but it didn't"; \
00086 } catch (std::exception& e) { \
00087 if (!::asg::detail::matchesRegex ((regex), e.what())) { \
00088 internalTestMessage = std::string ("expected statement ") + #x " to throw message matching " + (regex) + ", but actual message didn't match: " + e.what(); \
00089 } } catch (...) { \
00090 internalTestMessage = std::string ("statement ") + #x " threw an exception that didn't derive from std::exception"; } \
00091 if (!internalTestMessage.empty()) \
00092 FAIL() << internalTestMessage; \
00093 else SUCCEED(); }
00094
00095 #define EXPECT_THROW_REGEX(x,regex) \
00096 { std::string internalTestMessage; try { \
00097 x; internalTestMessage = std::string ("expected statement ") + #x " to throw, but it didn't"; \
00098 } catch (std::exception& e) { \
00099 if (!::asg::detail::matchesRegex ((regex), e.what())) { \
00100 internalTestMessage = std::string ("expected statement ") + #x " to throw message matching " + (regex) + ", but actual message didn't match: " + e.what(); \
00101 } } catch (...) { \
00102 internalTestMessage = std::string ("statement ") + #x " threw an exception that didn't derive from std::exception"; } \
00103 if (!internalTestMessage.empty()) \
00104 ADD_FAILURE() << internalTestMessage; \
00105 else SUCCEED(); }
00106
00107 #define ASSERT_MATCH_REGEX(reg,str) \
00108 ASSERT_PRED2 (::asg::detail::matchesRegex, reg, str)
00109
00110 #define EXPECT_MATCH_REGEX(reg,str) \
00111 EXPECT_PRED2 (::asg::detail::matchesRegex, reg, str)
00112
00113
00114
00115 #ifndef ASGTOOL_STANDALONE
00116
00117 #define ATLAS_GOOGLE_TEST_MAIN \
00118 int main (int argc, char **argv) \
00119 { \
00120 ::testing::InitGoogleTest (&argc, argv); \
00121 return RUN_ALL_TESTS(); \
00122 }
00123
00124 #else
00125
00126 #include <xAODRootAccess/Init.h>
00127
00128 #define ATLAS_GOOGLE_TEST_MAIN \
00129 int main (int argc, char **argv) \
00130 { \
00131 using namespace asg::msgUserCode; \
00132 ANA_CHECK_SET_TYPE (int); \
00133 ::StatusCode::enableFailure(); \
00134 ANA_CHECK (xAOD::Init ()); \
00135 ::testing::InitGoogleTest (&argc, argv); \
00136 return RUN_ALL_TESTS(); \
00137 }
00138
00139 #endif
00140
00141 #endif