C++ Utilities
5.36.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
outputcheck.h
Go to the documentation of this file.
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
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
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
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
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
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
}
// namespace CppUtilities
106
107
#endif
// TESTUTILS_OUTPUTCHECK_H
CppUtilities::OutputCheck::OutputCheck
OutputCheck(std::function< void(const std::string &output)> &&customCheck, std::ostream &os=std::cout)
CppUtilities::OutputCheck::~OutputCheck
~OutputCheck() noexcept(false)
Asserts the buffered standard output and restores the regular behaviour of std::cout.
Definition
outputcheck.h:73
CppUtilities::OutputCheck::OutputCheck
OutputCheck(const std::string &expectedOutput, std::ostream &os=std::cout)
Redirects standard output to an internal buffer.
Definition
outputcheck.h:39
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition
argumentparser.h:18
std
STL namespace.
Fail
#define Fail(msg)
ExpectEqual
#define ExpectEqual(a, b)
stringbuilder.h
Generated on
for C++ Utilities by
1.18.0