C++ Utilities 5.24.8
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
outputcheck.h
Go to the documentation of this file.
1#ifndef TESTUTILS_OUTPUTCHECK_H
2#define TESTUTILS_OUTPUTCHECK_H
3
5
6#include <cppunit/extensions/HelperMacros.h>
7
8#include <functional>
9#include <iostream>
10#include <sstream>
11#include <string>
12
13namespace CppUtilities {
14
21public:
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);
26
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
40 : m_os(os)
41 , m_expectedOutput(expectedOutput)
42 , m_buffer()
43 , m_regularOutputBuffer(os.rdbuf(m_buffer.rdbuf()))
44{
45}
46
50inline 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
62inline 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
74{
75 m_os.rdbuf(m_regularOutputBuffer);
76 const std::string actualOutput(m_buffer.str());
77 if (m_customCheck) {
78 m_customCheck(actualOutput);
79 return;
80 }
81 if (m_alternativeOutput.empty()) {
82 CPPUNIT_ASSERT_EQUAL(m_expectedOutput, actualOutput);
83 return;
84 }
85 if (m_expectedOutput != actualOutput && m_alternativeOutput != actualOutput) {
86 using namespace CppUtilities;
87 CPPUNIT_FAIL("Output is not either \"" % m_expectedOutput % "\" or \"" % m_alternativeOutput % "\". Got instead:\n" + actualOutput);
88 }
89}
90
91} // namespace CppUtilities
92
93#endif // TESTUTILS_OUTPUTCHECK_H
The StandardOutputCheck class asserts whether the (standard) output written in the enclosing code blo...
Definition outputcheck.h:20
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.
Definition outputcheck.h:73
OutputCheck(const std::string &expectedOutput, std::ostream &os=std::cout)
Redirects standard output to an internal buffer.
Definition outputcheck.h:39
Contains all utilities provides by the c++utilities library.
IntegralType stringToNumber(const StringType &string, BaseType base=10)
Converts the given string to an unsigned/signed number assuming string uses the specified base.
STL namespace.