Skip to content

Commit

Permalink
testrunner: make sure all redirected output is being consumed / some …
Browse files Browse the repository at this point in the history
…cleanups (danmar#5714)
  • Loading branch information
firewave authored Dec 1, 2023
1 parent 55c2b75 commit ec9dbb3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 83 deletions.
3 changes: 0 additions & 3 deletions test/fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@

#include "xml.h"

std::ostringstream errout;
std::ostringstream output;

/**
* TestRegistry
**/
Expand Down
14 changes: 7 additions & 7 deletions test/fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,16 @@ class TestFixture : public ErrorLogger {
return SettingsBuilder(*this, std::move(settings));
}

public:
// TODO: make sure the output has been consumed in the test
std::ostringstream errout;
std::ostringstream output;

private:
void reportOut(const std::string &outmsg, Color c = Color::Reset) override;
void reportErr(const ErrorMessage &msg) override;
void run(const std::string &str);

public:
static void printHelp();
const std::string classname;

Expand All @@ -248,12 +254,6 @@ class TestFixture : public ErrorLogger {
static std::size_t runTests(const options& args);
};

// TODO: fix these
// NOLINTNEXTLINE(readability-redundant-declaration)
extern std::ostringstream errout;
// NOLINTNEXTLINE(readability-redundant-declaration)
extern std::ostringstream output;

// TODO: most asserts do not actually assert i.e. do not return
#define TEST_CASE( NAME ) do { if (prepareTest(#NAME)) { setVerbose(false); NAME(); teardownTest(); } } while (false)
#define ASSERT( CONDITION ) if (!assert_(__FILE__, __LINE__, (CONDITION))) return
Expand Down
46 changes: 21 additions & 25 deletions test/redirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@

#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>

// NOLINTNEXTLINE(readability-redundant-declaration) - TODO: fix this
extern std::ostringstream errout;
// NOLINTNEXTLINE(readability-redundant-declaration) - TODO: fix this
extern std::ostringstream output;
/**
* @brief Utility class for capturing cout and cerr to ostringstream buffers
* for later use. Uses RAII to stop redirection when the object goes out of
Expand All @@ -47,34 +44,35 @@ class RedirectOutputError {
}

/** Revert cout and cerr behaviour */
~RedirectOutputError() {
~RedirectOutputError() noexcept(false) {
std::cout.rdbuf(_oldCout); // restore cout's original streambuf
std::cerr.rdbuf(_oldCerr); // restore cerrs's original streambuf

errout << _err.str();
output << _out.str();
{
const std::string s = _out.str();
if (!s.empty())
throw std::runtime_error("unconsumed stdout: " + s); // cppcheck-suppress exceptThrowInDestructor - FP #11031
}

{
const std::string s = _err.str();
if (!s.empty())
throw std::runtime_error("consumed stderr: " + s);
}
}

/** Return what would be printed to cout. See also clearOutput() */
std::string getOutput() const {
return _out.str();
}

/** Normally called after getOutput() to prevent same text to be returned
twice. */
void clearOutput() {
/** Return what would be printed to cout. */
std::string getOutput() {
std::string s = _out.str();
_out.str("");
return s;
}

/** Return what would be printed to cerr. See also clearErrout() */
std::string getErrout() const {
return _err.str();
}

/** Normally called after getErrout() to prevent same text to be returned
twice. */
void clearErrout() {
/** Return what would be printed to cerr. */
std::string getErrout() {
std::string s = _err.str();
_err.str("");
return s;
}

private:
Expand Down Expand Up @@ -112,9 +110,7 @@ class SuppressOutput {

#define REDIRECT RedirectOutputError redir
#define GET_REDIRECT_OUTPUT redir.getOutput()
#define CLEAR_REDIRECT_OUTPUT redir.clearOutput()
#define GET_REDIRECT_ERROUT redir.getErrout()
#define CLEAR_REDIRECT_ERROUT redir.clearErrout()

#define SUPPRESS SuppressOutput supprout

Expand Down
Loading

0 comments on commit ec9dbb3

Please sign in to comment.