| 1 |
#ifndef TESTUTILS_OUTPUTCHECK_H |
| 2 |
#define TESTUTILS_OUTPUTCHECK_H |
| 3 |
|
| 4 |
#include "../conversion/stringbuilder.h" |
| 5 |
|
| 6 |
#include <cppunit/extensions/HelperMacros.h> |
| 7 |
|
| 8 |
#include <functional> |
| 9 |
#include <iostream> |
| 10 |
#include <sstream> |
| 11 |
#include <string> |
| 12 |
|
| 13 |
namespace CppUtilities { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class OutputCheck { |
| 21 |
public: |
| 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); |
| 25 |
~OutputCheck() noexcept(false); |
| 26 |
|
| 27 |
private: |
| 28 |
std::ostream &m_os; |
| 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; |
| 34 |
}; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
inline OutputCheck::OutputCheck(const std::string &expectedOutput, std::ostream &os) |
| 40 |
: m_os(os) |
| 41 |
, m_expectedOutput(expectedOutput) |
| 42 |
, m_buffer() |
| 43 |
, m_regularOutputBuffer(os.rdbuf(m_buffer.rdbuf())) |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
inline OutputCheck::OutputCheck(std::string &&expectedOutput, std::string &&alternativeOutput, std::ostream &os) |
| 51 |
: m_os(os) |
| 52 |
, m_expectedOutput(expectedOutput) |
| 53 |
, m_alternativeOutput(alternativeOutput) |
| 54 |
, m_buffer() |
| 55 |
, m_regularOutputBuffer(os.rdbuf(m_buffer.rdbuf())) |
| 56 |
{ |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
inline OutputCheck::OutputCheck(std::function<void(const std::string &)> &&customCheck, std::ostream &os) |
| 63 |
: m_os(os) |
| 64 |
, m_customCheck(customCheck) |
| 65 |
, m_buffer() |
| 66 |
, m_regularOutputBuffer(os.rdbuf(m_buffer.rdbuf())) |
| 67 |
{ |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
inline OutputCheck::~OutputCheck() noexcept(false) |
| 74 |
{ |
| 75 |
#ifdef CppUnit2Gtest |
| 76 |
#define ExpectEqual(a, b) \ |
| 77 |
EXPECT_EQ(a, b); \ |
| 78 |
if (!(a == b)) \ |
| 79 |
throw std::logic_error("bad assert") |
| 80 |
#define Fail(msg) \ |
| 81 |
ADD_FAILURE() << msg; \ |
| 82 |
throw std::logic_error("bad assert") |
| 83 |
#else |
| 84 |
#define ExpectEqual(a, b) CPPUNIT_ASSERT_EQUAL(a, b) |
| 85 |
#define Fail(msg) CPPUNIT_FAIL(msg) |
| 86 |
#endif |
| 87 |
m_os.rdbuf(m_regularOutputBuffer); |
| 88 |
const std::string actualOutput(m_buffer.str()); |
| 89 |
if (m_customCheck) { |
| 90 |
m_customCheck(actualOutput); |
| 91 |
return; |
| 92 |
} |
| 93 |
if (m_alternativeOutput.empty()) { |
| 94 |
ExpectEqual(m_expectedOutput, actualOutput); |
| 95 |
return; |
| 96 |
} |
| 97 |
if (m_expectedOutput != actualOutput && m_alternativeOutput != actualOutput) { |
| 98 |
using namespace CppUtilities; |
| 99 |
Fail("Output is not either \"" % m_expectedOutput % "\" or \"" % m_alternativeOutput % "\". Got instead:\n" + actualOutput); |
| 100 |
} |
| 101 |
#undef Fail |
| 102 |
#undef ExpectEqual |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
#endif |