1#ifndef TESTUTILS_OUTPUTCHECK_H
2#define TESTUTILS_OUTPUTCHECK_H
6#include <cppunit/extensions/HelperMacros.h>
22 OutputCheck(
const std::string &expectedOutput, std::ostream &os = std::cout);
23 OutputCheck(std::string &&expectedOutput, std::string &&alternativeOutput, std::ostream &os = std::cout);
24 OutputCheck(std::function<
void(
const std::string &output)> &&customCheck, std::ostream &os = std::cout);
29 const
std::function<
void(const
std::
string &output)> m_customCheck;
30 const
std::
string m_expectedOutput;
31 const
std::
string m_alternativeOutput;
32 std::stringstream m_buffer;
33 std::streambuf *const m_regularOutputBuffer;
41 , m_expectedOutput(expectedOutput)
43 , m_regularOutputBuffer(os.rdbuf(m_buffer.rdbuf()))
52 , m_expectedOutput(expectedOutput)
53 , m_alternativeOutput(alternativeOutput)
55 , m_regularOutputBuffer(os.rdbuf(m_buffer.rdbuf()))
64 , m_customCheck(customCheck)
66 , m_regularOutputBuffer(os.rdbuf(m_buffer.rdbuf()))
76#define ExpectEqual(a, b) \
79 throw std::logic_error("bad assert")
81 ADD_FAILURE() << msg; \
82 throw std::logic_error("bad assert")
84#define ExpectEqual(a, b) CPPUNIT_ASSERT_EQUAL(a, b)
85#define Fail(msg) CPPUNIT_FAIL(msg)
87 m_os.rdbuf(m_regularOutputBuffer);
88 const std::string actualOutput(m_buffer.str());
90 m_customCheck(actualOutput);
93 if (m_alternativeOutput.empty()) {
97 if (m_expectedOutput != actualOutput && m_alternativeOutput != actualOutput) {
99 Fail(
"Output is not either \"" % m_expectedOutput %
"\" or \"" % m_alternativeOutput %
"\". Got instead:\n" + actualOutput);
OutputCheck(std::function< void(const std::string &output)> &&customCheck, std::ostream &os=std::cout)
~OutputCheck() noexcept(false)
Asserts the buffered standard output and restores the regular behaviour of std::cout.
OutputCheck(const std::string &expectedOutput, std::ostream &os=std::cout)
Redirects standard output to an internal buffer.
Contains all utilities provided by the c++utilities library.
#define ExpectEqual(a, b)