Belle II Software  release-06-02-00
TestHelpers.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <framework/logging/LogSystem.h>
11 
12 #include <gtest/gtest.h>
13 
14 #include <string>
15 
16 namespace Belle2 {
28  namespace TestHelpers {
29 
42  class TestWithGearbox : public ::testing::Test {
43 
44  public:
46  static void SetUpTestCase();
47 
49  static void TearDownTestCase();
50  };
51 
65  public:
66  TempDirCreator();
67  ~TempDirCreator();
72  std::string getTempDir() const;
73  private:
74  std::string m_oldpwd;
75  std::string m_tmpdir;
76  };
77  }
79 }
80 
88 #define EXPECT_LOGMESSAGE(x, loglevel) \
89  do { \
90  int nmessages_before = Belle2::LogSystem::Instance().getMessageCounter(loglevel); \
91  { \
92  x; \
93  } \
94  int nmessages_after = Belle2::LogSystem::Instance().getMessageCounter(loglevel); \
95  EXPECT_TRUE(nmessages_after > nmessages_before) << "Message of level " << #loglevel << " expected, but not found."; \
96  } while(0)
97 
103 #define EXPECT_B2FATAL(x) EXPECT_EXIT(x, ::testing::ExitedWithCode(1),"")
104 
109 #define EXPECT_B2ERROR(x) EXPECT_LOGMESSAGE(x, Belle2::LogConfig::c_Error)
110 
115 #define EXPECT_B2WARNING(x) EXPECT_LOGMESSAGE(x, Belle2::LogConfig::c_Warning)
116 
124 #define EXPECT_NO_LOGMESSAGE(x, loglevel) \
125  do { \
126  int nmessages_before = Belle2::LogSystem::Instance().getMessageCounter(loglevel); \
127  { \
128  x; \
129  } \
130  int nmessages_after = Belle2::LogSystem::Instance().getMessageCounter(loglevel); \
131  EXPECT_TRUE(nmessages_after == nmessages_before) << "Message of level " << #loglevel << " found, but non expected."; \
132  } while(0)
133 
138 #define EXPECT_NO_B2FATAL(x) EXPECT_NO_LOGMESSAGE(x, Belle2::LogConfig::c_Fatal)
139 
144 #define EXPECT_NO_B2ERROR(x) EXPECT_NO_LOGMESSAGE(x, Belle2::LogConfig::c_Error)
145 
150 #define EXPECT_NO_B2WARNING(x) EXPECT_NO_LOGMESSAGE(x, Belle2::LogConfig::c_Warning)
151 
163 #define TEST_CONTEXT(message) SCOPED_TRACE([&](){std::ostringstream messageStream; messageStream << message; return messageStream.str();}())
164 
165 
169 #define EXPECT_ANGLE_NEAR(expected, actual, tolerance) EXPECT_PRED3(::Belle2::TestHelpers::angleNear, expected, actual, tolerance)
170 
174 #define ASSERT_ANGLE_NEAR(expected, actual, tolerance) ASSERT_PRED3(::Belle2::TestHelpers::angleNear, expected, actual, tolerance)
175 
179 #define EXPECT_SAME_SIGN(expected, actual) EXPECT_PRED2(::Belle2::TestHelpers::sameSign, expected, actual)
180 
184 #define ASSERT_SAME_SIGN(expected, actual) ASSERT_PRED2(::Belle2::TestHelpers::sameSign, expected, actual)
185 
186 
190 #define EXPECT_POSITIVE(expected) EXPECT_PRED1(::Belle2::TestHelpers::isPositive, expected)
191 
195 #define ASSERT_POSITIVE(expected) ASSERT_PRED1(::Belle2::TestHelpers::isPositive, expected)
196 
200 #define EXPECT_NEGATIVE(expected) EXPECT_PRED1(::Belle2::TestHelpers::isNegative, expected)
201 
205 #define ASSERT_NEGATIVE(expected) ASSERT_PRED1(::Belle2::TestHelpers::isNegative, expected)
206 
223 #define EXPECT_ALL_NEAR(expected, actual, tolerance) EXPECT_PRED3(::Belle2::TestHelpers::allNear<decltype(expected)>, expected, actual, tolerance)
224 
241 #define ASSERT_ALL_NEAR(expected, actual, tolerance) ASSERT_PRED3(::Belle2::TestHelpers::allNear<decltype(expected)>, expected, actual, tolerance)
242 
243 class TVector3;
244 namespace Belle2 {
249  namespace TestHelpers {
251  bool angleNear(double expected, double actual, double tolerance);
252 
254  bool sameSign(double expected, double actual);
255 
257  bool isPositive(double expected);
258 
260  bool isNegative(double expected);
261 
265  template<class T>
266  bool allNear(const T& expected, const T& actual, double tolerance)
267  {
268  using std::fabs;
269  return fabs(expected - actual) < tolerance;
270  }
271 
273  template<>
274  bool allNear<TVector3>(const TVector3& expected, const TVector3& actual, double tolerance);
275 
277  void PrintTo(const TVector3& tVector3, ::std::ostream& output);
278  }
280 }
281 
282 
283 
changes working directory into a newly created directory, and removes it (and contents) on destructio...
Definition: TestHelpers.h:64
std::string getTempDir() const
Returns path of temporary directory.
Definition: TestHelpers.cc:60
std::string m_tmpdir
path of temporary director.
Definition: TestHelpers.h:75
std::string m_oldpwd
previous working directory.
Definition: TestHelpers.h:74
This class provides a test fixture managing the opening and closing of the Gearbox with the default g...
Definition: TestHelpers.h:42
static void SetUpTestCase()
Sets up the Gearbox once for all test in this TestCase.
Definition: TestHelpers.cc:25
static void TearDownTestCase()
Closes the Gearbox once for all test in this TestCase.
Definition: TestHelpers.cc:39
void PrintTo(const TVector3 &tVector3, ::std::ostream &output)
Print function for the google test framework to print a TVector3 to an output stream.
Definition: TestHelpers.cc:100
bool isPositive(double expected)
Predicate checking that a value is bigger than zero.
Definition: TestHelpers.cc:79
bool angleNear(double expected, double actual, double tolerance)
Predicate checking that two angular values are close to each other modulus a 2 * PI difference.
Definition: TestHelpers.cc:65
bool isNegative(double expected)
Predicate checking that a value is smaller than zero.
Definition: TestHelpers.cc:84
bool allNear(const T &expected, const T &actual, double tolerance)
Templated version of predicate checking if two combound object containing some floating point are nea...
Definition: TestHelpers.h:266
bool sameSign(double expected, double actual)
Predicate checking that two values have the same sign.
Definition: TestHelpers.cc:70
bool allNear< TVector3 >(const TVector3 &expected, const TVector3 &actual, double tolerance)
Predicate checking that all three components of TVector3 are close by a maximal error of tolerance.
Definition: TestHelpers.cc:90
Abstract base class for different kinds of events.