C++ Utilities  4.11.0
Common C++ classes and routines used by my applications such as argument parser, IO and conversion utilities
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 TestUtilities {
14 
19 class OutputCheck {
20 public:
21  OutputCheck(const std::string &expectedOutput, std::ostream &os = std::cout);
22  OutputCheck(std::string &&expectedOutput, std::string &&alternativeOutput, std::ostream &os = std::cout);
23  OutputCheck(std::function<void(const std::string &output)> &&customCheck, std::ostream &os = std::cout);
24  ~OutputCheck();
25 
26 private:
27  std::ostream &m_os;
28  const std::function<void(const std::string &output)> m_customCheck;
29  const std::string m_expectedOutput;
30  const std::string m_alternativeOutput;
31  std::stringstream m_buffer;
32  std::streambuf *const m_regularOutputBuffer;
33 };
34 
38 inline OutputCheck::OutputCheck(const std::string &expectedOutput, std::ostream &os)
39  : m_os(os)
40  , m_expectedOutput(expectedOutput)
41  , m_buffer()
42  , m_regularOutputBuffer(os.rdbuf(m_buffer.rdbuf()))
43 {
44 }
45 
49 inline OutputCheck::OutputCheck(std::string &&expectedOutput, std::string &&alternativeOutput, std::ostream &os)
50  : m_os(os)
51  , m_expectedOutput(expectedOutput)
52  , m_alternativeOutput(alternativeOutput)
53  , m_buffer()
54  , m_regularOutputBuffer(os.rdbuf(m_buffer.rdbuf()))
55 {
56 }
57 
61 inline OutputCheck::OutputCheck(std::function<void(const std::string &)> &&customCheck, std::ostream &os)
62  : m_os(os)
63  , m_customCheck(customCheck)
64  , m_buffer()
65  , m_regularOutputBuffer(os.rdbuf(m_buffer.rdbuf()))
66 {
67 }
68 
73 {
74  m_os.rdbuf(m_regularOutputBuffer);
75  if (m_customCheck) {
76  m_customCheck(m_buffer.str());
77  return;
78  }
79  if (m_alternativeOutput.empty()) {
80  CPPUNIT_ASSERT_EQUAL(m_expectedOutput, m_buffer.str());
81  return;
82  }
83  const std::string actualOutput(m_buffer.str());
84  if (m_expectedOutput != actualOutput && m_alternativeOutput != actualOutput) {
85  using namespace ConversionUtilities;
86  CPPUNIT_FAIL("Output is not either \"" % m_expectedOutput % "\" or \"" % m_alternativeOutput % "\". Got instead:\n" + actualOutput);
87  }
88 }
89 
90 } // namespace TestUtilities
91 
92 #endif // TESTUTILS_OUTPUTCHECK_H
Contains classes and functions utilizing creating of test applications.
Definition: testutils.h:12
Contains several functions providing conversions between different data types.
OutputCheck(const std::string &expectedOutput, std::ostream &os=std::cout)
Redirects standard output to an internal buffer.
Definition: outputcheck.h:38
The StandardOutputCheck class asserts whether the (standard) output written in the enclosing code blo...
Definition: outputcheck.h:19
~OutputCheck()
Asserts the buffered standard output and restores the regular behaviour of std::cout.
Definition: outputcheck.h:72