Belle II Software  release-08-01-10
Fixtures.cc
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 
9 #include <framework/utilities/testhelpers/Fixtures.h>
10 #include <framework/logging/LogConnectionBase.h>
11 #include <framework/logging/LogConnectionConsole.h>
12 
13 #include <boost/algorithm/string.hpp>
14 #include <boost/range/adaptors.hpp>
15 
16 namespace Belle2::TestHelpers {
17  namespace {
19  class LogInterceptor final: public Belle2::LogConnectionBase {
21  std::vector<Belle2::LogMessage>& m_messages;
22  public:
24  explicit LogInterceptor(std::vector<Belle2::LogMessage>& messages): m_messages(messages) {}
26  bool isConnected() override { return true; }
28  bool sendMessage(Belle2::LogMessage const& message) override {m_messages.push_back(message); return false; }
29  };
30  }
31 
34  {
35  m_messages.clear();
37  LogSystem::Instance().addLogConnection(new LogInterceptor(m_messages));
39  }
40 
43  {
45  }
46 
47  void LogMessageTest::expectMessage(LogConfig::ELogLevel level, int count, bool strict)
48  {
49  int messages = std::count_if(m_messages.begin(), m_messages.end(), [level](const LogMessage & m) {
50  return m.getLogLevel() == level;
51  });
52  EXPECT_EQ(count, messages) << "Expected " << count << " messages of loglevel " << LogConfig::logLevelToString(level)
53  << ", found " << messages;
54  if (strict) {
55  EXPECT_EQ(messages, m_messages.size());
56  }
57  }
58 
59  void LogMessageTest::expectMessageVariables(std::map<std::string, std::string>&& variables, bool strict)
60  {
61  ASSERT_FALSE(m_messages.empty()) << "No message available to check";
62  const auto& msg = m_messages.back();
63  for (const auto& var : msg.getLogVariables()) {
64  if (auto it = variables.find(var.getName()); it != variables.end()) {
65  EXPECT_EQ(it->second, var.getValue());
66  variables.erase(it);
67  } else {
68  EXPECT_FALSE(strict) << "extra variable: " << var.getName() << std::endl;
69  }
70  }
71  EXPECT_TRUE(variables.empty()) << "Some requested log variables were not present in the log message: "
72  << boost::algorithm::join(variables | boost::adaptors::map_keys, ", ");
73  }
74 
75  void LogMessageTest::expectErrorWithVariables(std::map<std::string, std::string>&& variables, bool strict)
76  {
78  expectMessageVariables(std::move(variables), strict);
79  clearMessages();
80  }
81 
82  void LogMessageTest::expectMessageContent(LogConfig::ELogLevel level, const std::string& message)
83  {
84  ASSERT_FALSE(m_messages.empty()) << "No message available to check";
85  const auto& msg = m_messages.back();
86  EXPECT_EQ(msg.getLogLevel(), level);
87  EXPECT_EQ(msg.getMessage(), message);
88  }
89 } // namespace Belle2::TestHelpers
ELogLevel
Definition of the supported log levels.
Definition: LogConfig.h:26
@ c_Error
Error: for things that went wrong and have to be fixed.
Definition: LogConfig.h:30
static const char * logLevelToString(ELogLevel logLevelType)
Converts a log level type to a string.
Definition: LogConfig.cc:42
Abstract base class for the different types of log connections.
Implements a log connection to an IO Stream.
The LogMessage class.
Definition: LogMessage.h:29
void resetLogging()
Reset logging system to defaults: empty all log messages and reset connections to the default.
Definition: LogSystem.cc:180
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
Definition: LogSystem.cc:31
void resetLogConnections()
Removes all log connections.
Definition: LogSystem.cc:44
void addLogConnection(LogConnectionBase *logConnection)
Adds a log connection object which is used to the send the logging messages.
Definition: LogSystem.cc:38
void expectMessageVariables(std::map< std::string, std::string > &&variables, bool strict=false)
check if the last message emitted contains the given variables and their values.
Definition: Fixtures.cc:59
std::vector< LogMessage > m_messages
list of log messages
Definition: Fixtures.h:26
void clearMessages()
clear the list of save log messages
Definition: Fixtures.h:62
void expectMessageContent(LogConfig::ELogLevel level, const std::string &message)
check the content of the last log message against the given values
Definition: Fixtures.cc:82
void SetUp() override
Add a log message interceptor.
Definition: Fixtures.cc:33
void expectErrorWithVariables(std::map< std::string, std::string > &&variables, bool strict=false)
Expect an error with variables: This is a shorthand to check that there is exactly one error message ...
Definition: Fixtures.cc:75
void expectMessage(LogConfig::ELogLevel level=LogConfig::c_Error, int count=1, bool strict=false)
check if there is a certain number of log messages of a given level
Definition: Fixtures.cc:47
void TearDown() override
And try to reset logging system to default.
Definition: Fixtures.cc:42
Some utilities to help with writing unit tests.
Definition: Helix.cc:33