ATLAS Offline Software
Control
AthToolSupport
AsgTesting
AsgTesting
Control/AthToolSupport/AsgTesting/AsgTesting/UnitTest.h
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3
*/
4
6
7
8
9
#ifndef ASG_TOOLS__UNIT_TEST_H
10
#define ASG_TOOLS__UNIT_TEST_H
11
12
#include <
AsgMessaging/MessageCheck.h
>
13
#include <
AsgMessaging/MessagePrinterErrorCollect.h
>
14
#include <
AsgMessaging/MessagePrinterOverlay.h
>
15
#include <string>
16
#include <exception>
17
#include <gtest/gtest.h>
18
19
namespace
asg
20
{
21
namespace
detail
22
{
29
bool
matchesRegex
(
const
std::string&
regex
,
const
std::string&
str
);
30
}
31
32
template
<
typename
T>
struct
CheckHelper;
33
}
34
35
#define ASSERT_SUCCESS(x) \
36
ASSERT_EQ (asg::CheckHelper<decltype(x)>::successCode(), x)
37
38
#define EXPECT_SUCCESS(x) \
39
EXPECT_EQ (asg::CheckHelper<decltype(x)>::successCode(), x)
40
41
#define ASSERT_FAILURE(x) \
42
ASSERT_EQ (asg::CheckHelper<decltype(x)>::failureCode(), x)
43
44
#define EXPECT_FAILURE(x) \
45
EXPECT_EQ (asg::CheckHelper<decltype(x)>::failureCode(), x)
46
47
48
#ifndef XAOD_STANDALONE
49
#define ASSERT_FAILURE_REGEX(x,regex) \
50
ASSERT_FAILURE(x)
51
#define EXPECT_FAILURE_REGEX(x,regex) \
52
EXPECT_FAILURE(x)
53
#else
54
#define ASSERT_FAILURE_REGEX(x,regex) \
55
{ MessagePrinterErrorCollect errorPrinter; \
56
MessagePrinterOverlay overlayPrinter (&errorPrinter); \
57
auto code = (x); \
58
if (asg::CheckHelper<decltype(x)>::isSuccess (code)) { \
59
FAIL () << "command didn't fail as expected: " << #x; \
60
} else if (errorPrinter.empty()) { \
61
FAIL () << "command printed no error message on failure: " << #x; \
62
} else if (!errorPrinter.matchesRegex (regex)) { \
63
FAIL () << "invalid error message for: " << #x \
64
<< "\nexpected:\n " << regex << "\nfound:\n" \
65
<< errorPrinter.asString (" "); \
66
} }
67
#define EXPECT_FAILURE_REGEX(x,regex) \
68
{ MessagePrinterErrorCollect errorPrinter; \
69
MessagePrinterOverlay overlayPrinter (&errorPrinter); \
70
auto code = (x); \
71
if (asg::CheckHelper<decltype(x)>::isSuccess (code)) { \
72
ADD_FAILURE () << "command didn't fail as expected: " << #x; \
73
} else if (errorPrinter.empty()) { \
74
ADD_FAILURE () << "command printed no error message on failure: " << #x; \
75
} else if (!errorPrinter.matchesRegex (regex)) { \
76
ADD_FAILURE () << "invalid error message for: " << #x \
77
<< "\nexpected:\n " << regex << "\nfound:\n" \
78
<< errorPrinter.asString (" "); \
79
} }
80
#endif
81
82
#define ASSERT_THROW_REGEX(x,regex) \
83
{ std::string internalTestMessage; try { \
84
x; internalTestMessage = std::string ("expected statement ") + #x " to throw, but it didn't"; \
85
} catch (std::exception& e) { \
86
if (!::asg::detail::matchesRegex ((regex), e.what())) { \
87
internalTestMessage = std::string ("expected statement ") + #x " to throw message matching " + (regex) + ", but actual message didn't match: " + e.what(); \
88
} } catch (...) { \
89
internalTestMessage = std::string ("statement ") + #x " threw an exception that didn't derive from std::exception"; } \
90
if (!internalTestMessage.empty()) \
91
FAIL() << internalTestMessage; \
92
else SUCCEED(); }
93
94
#define EXPECT_THROW_REGEX(x,regex) \
95
{ std::string internalTestMessage; try { \
96
x; internalTestMessage = std::string ("expected statement ") + #x " to throw, but it didn't"; \
97
} catch (std::exception& e) { \
98
if (!::asg::detail::matchesRegex ((regex), e.what())) { \
99
internalTestMessage = std::string ("expected statement ") + #x " to throw message matching " + (regex) + ", but actual message didn't match: " + e.what(); \
100
} } catch (...) { \
101
internalTestMessage = std::string ("statement ") + #x " threw an exception that didn't derive from std::exception"; } \
102
if (!internalTestMessage.empty()) \
103
ADD_FAILURE() << internalTestMessage; \
104
else SUCCEED(); }
105
106
#define ASSERT_MATCH_REGEX(reg,str) \
107
ASSERT_PRED2 (::asg::detail::matchesRegex, reg, str)
108
109
#define EXPECT_MATCH_REGEX(reg,str) \
110
EXPECT_PRED2 (::asg::detail::matchesRegex, reg, str)
111
112
113
114
#ifndef XAOD_STANDALONE
115
116
#define ATLAS_GOOGLE_TEST_MAIN \
117
int main (int argc, char **argv) \
118
{ \
119
::testing::InitGoogleTest (&argc, argv); \
120
return RUN_ALL_TESTS(); \
121
}
122
123
#else
124
125
#include <
xAODRootAccess/Init.h
>
126
127
#define ATLAS_GOOGLE_TEST_MAIN \
128
int main (int argc, char **argv) \
129
{ \
130
using namespace asg::msgUserCode; \
131
ANA_CHECK_SET_TYPE (int); \
132
::StatusCode::enableFailure(); \
133
ANA_CHECK (xAOD::Init ()); \
134
::testing::InitGoogleTest (&argc, argv); \
135
return RUN_ALL_TESTS(); \
136
}
137
138
#endif
139
140
#endif
asg
Definition:
DataHandleTestTool.h:28
detail
Definition:
extract_histogram_tag.cxx:14
PrepareReferenceFile.regex
regex
Definition:
PrepareReferenceFile.py:43
MessagePrinterOverlay.h
asg::detail::matchesRegex
bool matchesRegex(const std::string ®ex, const std::string &str)
return whether str matches regex
Definition:
Control/AthToolSupport/AsgTesting/Root/UnitTest.cxx:25
MessageCheck.h
macros for messaging and checking status codes
Init.h
MessagePrinterErrorCollect.h
str
Definition:
BTagTrackIpAccessor.cxx:11
Generated on Sun Jun 30 2024 21:32:07 for ATLAS Offline Software by
1.8.18