Belle II Software light-2406-ragdoll
LogMessageTest Class Reference

Test fixture to be able to check the contents and types of emitted log messages in detail. More...

#include <Fixtures.h>

Inheritance diagram for LogMessageTest:
Collaboration diagram for LogMessageTest:

Protected Member Functions

void SetUp () override
 Add a log message interceptor.
 
void TearDown () override
 And try to reset logging system to default.
 
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
 
void expectMessageContent (LogConfig::ELogLevel level, const std::string &message)
 check the content of the last log message against the given values
 
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.
 
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 present and that it contains the given variables.
 
void clearMessages ()
 clear the list of save log messages
 

Protected Attributes

std::vector< LogMessagem_messages
 list of log messages
 

Detailed Description

Test fixture to be able to check the contents and types of emitted log messages in detail.

In contrast to EXPECT_B2ERROR() and others defined here this test fixture will keep a copy of every message emitted and all messages can be expected in detail. There are a few convenient members to check for message counts and message contents.

Definition at line 23 of file Fixtures.h.

Member Function Documentation

◆ clearMessages()

void clearMessages ( )
inlineprotected

clear the list of save log messages

Definition at line 62 of file Fixtures.h.

63 {
64 m_messages.clear();
65 }
std::vector< LogMessage > m_messages
list of log messages
Definition: Fixtures.h:26

◆ expectErrorWithVariables()

void expectErrorWithVariables ( std::map< std::string, std::string > &&  variables,
bool  strict = false 
)
protected

Expect an error with variables: This is a shorthand to check that there is exactly one error message present and that it contains the given variables.

This also clears the list of messages for convenience so after each call which is supposed to create an error this function can be called to handle the error message and afterwards the list of messages will be clean again for the next test.

This is a shorthand to just call expectMessage(), expectMessageVariables() and clearMessages()

Parameters
variablesmap of name->value mapping for all expected variables
strictif true also fail the test if there's any other variable in the message

Definition at line 75 of file Fixtures.cc.

76 {
78 expectMessageVariables(std::move(variables), strict);
80 }
@ c_Error
Error: for things that went wrong and have to be fixed.
Definition: LogConfig.h:30
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
void clearMessages()
clear the list of save log messages
Definition: Fixtures.h:62
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

◆ expectMessage()

void expectMessage ( LogConfig::ELogLevel  level = LogConfig::c_Error,
int  count = 1,
bool  strict = false 
)
protected

check if there is a certain number of log messages of a given level

Parameters
levelwhich log level to check for
counthow many message should be expected
strictif true don't allow any other messages: the total number of messages must be equal to count and all need to have severity level

Definition at line 47 of file Fixtures.cc.

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 }
static const char * logLevelToString(ELogLevel logLevelType)
Converts a log level type to a string.
Definition: LogConfig.cc:42

◆ expectMessageContent()

void expectMessageContent ( LogConfig::ELogLevel  level,
const std::string &  message 
)
protected

check the content of the last log message against the given values

Parameters
levellog level we expect for the message
messageexpected string content of the message.

Definition at line 82 of file Fixtures.cc.

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 }

◆ expectMessageVariables()

void expectMessageVariables ( std::map< std::string, std::string > &&  variables,
bool  strict = false 
)
protected

check if the last message emitted contains the given variables and their values.

Parameters
variablesmap of name->value mapping for all expected variables
strictif true also fail the test if there's any other variable in the message

Definition at line 59 of file Fixtures.cc.

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 }

◆ SetUp()

void SetUp ( )
overrideprotected

Add a log message interceptor.

Definition at line 33 of file Fixtures.cc.

34 {
35 m_messages.clear();
37 LogSystem::Instance().addLogConnection(new LogInterceptor(m_messages));
38 LogSystem::Instance().addLogConnection(new LogConnectionConsole(STDOUT_FILENO));
39 }
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

◆ TearDown()

void TearDown ( )
overrideprotected

And try to reset logging system to default.

Definition at line 42 of file Fixtures.cc.

43 {
45 }
void resetLogging()
Reset logging system to defaults: empty all log messages and reset connections to the default.
Definition: LogSystem.cc:180

Member Data Documentation

◆ m_messages

std::vector<LogMessage> m_messages
protected

list of log messages

Definition at line 26 of file Fixtures.h.


The documentation for this class was generated from the following files: