Skip to content

Commit da236ee

Browse files
committed
testrunner: make sure all redirected output is being consumed
1 parent 3ad9bcc commit da236ee

3 files changed

Lines changed: 25 additions & 62 deletions

File tree

test/fixture.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include "xml.h"
3636

37+
// TODO: make sure the output has been consumed in the test
3738
std::ostringstream errout;
3839
std::ostringstream output;
3940

test/redirect.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <iostream>
2121
#include <sstream>
22+
#include <stdexcept>
2223
#include <string>
2324

2425
/**
@@ -43,31 +44,35 @@ class RedirectOutputError {
4344
}
4445

4546
/** Revert cout and cerr behaviour */
46-
~RedirectOutputError() {
47+
~RedirectOutputError() noexcept(false) {
4748
std::cout.rdbuf(_oldCout); // restore cout's original streambuf
4849
std::cerr.rdbuf(_oldCerr); // restore cerrs's original streambuf
49-
}
5050

51-
/** Return what would be printed to cout. See also clearOutput() */
52-
std::string getOutput() const {
53-
return _out.str();
51+
{
52+
const std::string s = _out.str();
53+
if (!s.empty())
54+
throw std::runtime_error("unconsumed stdout: " + s);
55+
}
56+
57+
{
58+
const std::string s = _err.str();
59+
if (!s.empty())
60+
throw std::runtime_error("consumed stderr: " + s);
61+
}
5462
}
5563

56-
/** Normally called after getOutput() to prevent same text to be returned
57-
twice. */
58-
void clearOutput() {
64+
/** Return what would be printed to cout. */
65+
std::string getOutput() {
66+
std::string s = _out.str();
5967
_out.str("");
68+
return s;
6069
}
6170

62-
/** Return what would be printed to cerr. See also clearErrout() */
63-
std::string getErrout() const {
64-
return _err.str();
65-
}
66-
67-
/** Normally called after getErrout() to prevent same text to be returned
68-
twice. */
69-
void clearErrout() {
71+
/** Return what would be printed to cerr. */
72+
std::string getErrout() {
73+
std::string s = _err.str();
7074
_err.str("");
75+
return s;
7176
}
7277

7378
private:
@@ -105,9 +110,7 @@ class SuppressOutput {
105110

106111
#define REDIRECT RedirectOutputError redir
107112
#define GET_REDIRECT_OUTPUT redir.getOutput()
108-
#define CLEAR_REDIRECT_OUTPUT redir.clearOutput()
109113
#define GET_REDIRECT_ERROUT redir.getErrout()
110-
#define CLEAR_REDIRECT_ERROUT redir.clearErrout()
111114

112115
#define SUPPRESS SuppressOutput supprout
113116

0 commit comments

Comments
 (0)