Ion
|
The LogChecker class can be used inside unit tests to trap all log output and verify that it matches what is expected. More...
#include "logchecker.h"
Public Member Functions | |
LogChecker () | |
The constructor sets up to trap all log output. More... | |
~LogChecker () override | |
The destructor logs an error if unexpected messages were logged, and unconditionally restores the previous log writer. More... | |
void | Write (port::LogSeverity severity, const std::string &message) override |
LogEntryWriter impl. More... | |
bool | HasMessage (const std::string &severity, const std::string &substring) |
Returns true if exactly one message was logged since the LogChecker was constructed or since the last call to HasMessage(). More... | |
bool | HasNoMessage (const std::string &severity, const std::string &substring) |
Returns true if no message of the given severity and containing the given substring was logged since the LogChecker was constructed or since the last call to HasMessage(). More... | |
bool | HasAnyMessages () const |
Returns true if any messages were logged since the instance was constructed or since the last call to CheckLog(). More... | |
const std::vector< std::string > | GetAllMessages () |
This function is useful for testing code that produces more than one log message at a time. More... | |
void | ClearLog () |
Clears any messages that may be in the log. More... | |
const std::string | GetLogString () const |
Returns a string containing all current logged messages. More... | |
Protected Member Functions | |
const char * | GetSeverityName (LogSeverity severity) const |
Convenient way to map a severity-code to a printable represenation. More... | |
The LogChecker class can be used inside unit tests to trap all log output and verify that it matches what is expected.
The destructor will log an error (to the regular stream) that no unexpected messages were logged, but it is up to the client to test for this.
For example, if you have a test that is known to generate two log messages, you can set up the test like this:
TEST(MyTest, Something) { LogChecker log_checker;
}
Definition at line 55 of file logchecker.h.
ion::base::LogChecker::LogChecker | ( | ) |
The constructor sets up to trap all log output.
Definition at line 25 of file logchecker.cc.
References ion::base::SetLogEntryWriter().
|
override |
The destructor logs an error if unexpected messages were logged, and unconditionally restores the previous log writer.
Definition at line 30 of file logchecker.cc.
References ion::port::ERROR, GetLogString(), HasAnyMessages(), LOG, and ion::base::SetLogEntryWriter().
|
inline |
Clears any messages that may be in the log.
Definition at line 95 of file logchecker.h.
Referenced by GetAllMessages(), and HasMessage().
const std::vector< std::string > ion::base::LogChecker::GetAllMessages | ( | ) |
This function is useful for testing code that produces more than one log message at a time.
It returns a vector containing all message strings (split at newlines) logged since the LogChecker was constructed or the log was last cleared. This clears the log afterwards.
Definition at line 89 of file logchecker.cc.
References ClearLog(), GetLogString(), and ion::base::SplitString().
|
inline |
Returns a string containing all current logged messages.
Definition at line 100 of file logchecker.h.
Referenced by GetAllMessages(), HasMessage(), HasNoMessage(), and ~LogChecker().
|
protectedinherited |
Convenient way to map a severity-code to a printable represenation.
Maps a Severity code to a name for printing.
Definition at line 24 of file logging.cc.
References ion::port::DFATAL, ion::port::ERROR, ion::port::FATAL, ion::port::INFO, and ion::port::WARNING.
Referenced by Write().
|
inline |
Returns true if any messages were logged since the instance was constructed or since the last call to CheckLog().
Definition at line 84 of file logchecker.h.
Referenced by ~LogChecker().
bool ion::base::LogChecker::HasMessage | ( | const std::string & | severity, |
const std::string & | substring | ||
) |
Returns true if exactly one message was logged since the LogChecker was constructed or since the last call to HasMessage().
The message must be of the given severity and must contain the given substring. If this returns true, it clears the stream for the next call. If it returns false, it leaves the stream alone so the caller can use GetLogString() to examine the log.
Definition at line 40 of file logchecker.cc.
References ClearLog(), GetLogString(), and ion::base::SplitString().
bool ion::base::LogChecker::HasNoMessage | ( | const std::string & | severity, |
const std::string & | substring | ||
) |
Returns true if no message of the given severity and containing the given substring was logged since the LogChecker was constructed or since the last call to HasMessage().
This leaves the stream alone so the caller can use GetLogString() to examine the log.
Definition at line 69 of file logchecker.cc.
References GetLogString(), and ion::base::SplitString().
|
overridevirtual |
LogEntryWriter impl.
Implements ion::port::LogEntryWriter.
Definition at line 95 of file logchecker.cc.
References ion::port::LogEntryWriter::GetSeverityName().