Skip to content

Commit

Permalink
Make StdEater more general.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Aug 18, 2024
1 parent 0f65c3e commit fbd1809
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 46 deletions.
4 changes: 2 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ set(${PROJECT_TEST}_SOURCES
ProgressBarSequentialTest.cpp
ProgressBarOverallTest.cpp
RunnerTest.cpp
CoutEater.h
CoutEater.cpp
StdStreamEater.h
StdStreamEater.cpp
)

add_executable(${PROJECT_TEST} ${${PROJECT_TEST}_SOURCES})
Expand Down
6 changes: 0 additions & 6 deletions test/CoutEater.cpp

This file was deleted.

17 changes: 0 additions & 17 deletions test/CoutEater.h

This file was deleted.

22 changes: 8 additions & 14 deletions test/ProgressBarOverallTest.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include <iostream>
#include <sstream>

#include <catch2/catch_test_macros.hpp>

#include <src/Config.h>
#include <src/ProgressBarOverall.h>

#include "StdStreamEater.h"

TEST_CASE("Overall Progress Callback")
{
const int years{1000};
Expand All @@ -18,41 +17,36 @@ TEST_CASE("Overall Progress Callback")
const int length{config::progressBarLength};
const int firstYearWithMarker{(years / length) - 1};

std::streambuf* oldCoutBuffer{std::cout.rdbuf()};
std::ostringstream output;
std::cout.rdbuf(output.rdbuf());
output.str("");
StdStreamEater eater(std::cout);

SECTION("start")
{
progressBar.update(0, simId);
REQUIRE(output.str() == "[");
REQUIRE(eater.getOutput() == "[");
}

SECTION("before progress")
{
progressBar.update(firstYearWithMarker - 1, simId);
REQUIRE(output.str() == "");
REQUIRE(eater.getOutput() == "");
}

SECTION("progress")
{
progressBar.update(firstYearWithMarker, simId);
REQUIRE(output.str() == "*");
REQUIRE(eater.getOutput() == "*");
}

SECTION("after progress")
{
progressBar.update(firstYearWithMarker + 1, simId);
REQUIRE(output.str() == "");
REQUIRE(eater.getOutput() == "");
}

SECTION("end")
{
const int lastYear{years - 1};
progressBar.update(lastYear, simId);
REQUIRE(output.str() == "*]\n");
REQUIRE(eater.getOutput() == "*]\n");
}

std::cout.rdbuf(oldCoutBuffer);
}
14 changes: 7 additions & 7 deletions test/ProgressBarSequentialTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <src/Config.h>
#include <src/ProgressBarSequential.h>

#include "CoutEater.h"
#include "StdStreamEater.h"

TEST_CASE("Sequential Progress Callback")
{
Expand All @@ -16,38 +16,38 @@ TEST_CASE("Sequential Progress Callback")
const int length{config::progressBarLength};
const int firstYearWithMarker{(years / length) - 1};

CoutEater coutEater;
StdStreamEater eater(std::cout);

SECTION("start")
{
progressBar.update(0, simId);
const std::string expectedOutput{
"1/" + std::to_string(params.simulationsCount_) + " ["};
REQUIRE(coutEater.getOutput() == expectedOutput);
REQUIRE(eater.getOutput() == expectedOutput);
}

SECTION("before progress")
{
progressBar.update(firstYearWithMarker - 1, simId);
REQUIRE(coutEater.getOutput() == "");
REQUIRE(eater.getOutput() == "");
}

SECTION("progress")
{
progressBar.update(firstYearWithMarker, simId);
REQUIRE(coutEater.getOutput() == "*");
REQUIRE(eater.getOutput() == "*");
}

SECTION("after progress")
{
progressBar.update(firstYearWithMarker + 1, simId);
REQUIRE(coutEater.getOutput() == "");
REQUIRE(eater.getOutput() == "");
}

SECTION("end")
{
const int lastYear{years - 1};
progressBar.update(lastYear, simId);
REQUIRE(coutEater.getOutput() == "*]\n");
REQUIRE(eater.getOutput() == "*]\n");
}
}
10 changes: 10 additions & 0 deletions test/StdStreamEater.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "StdStreamEater.h"

StdStreamEater::StdStreamEater(std::ostream& stream) : stream_(stream)
{
stream_.rdbuf(fakeOutput_.rdbuf());
}

StdStreamEater::~StdStreamEater() { stream_.rdbuf(stdBuffer_); }

std::string StdStreamEater::getOutput() const { return fakeOutput_.str(); }
19 changes: 19 additions & 0 deletions test/StdStreamEater.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <iostream>
#include <sstream>

class StdStreamEater
{
public:
explicit StdStreamEater(std::ostream& stream);
~StdStreamEater();

std::string getOutput() const;

private:
std::ostream& stream_;

std::streambuf* stdBuffer_{stream_.rdbuf()};
std::ostringstream fakeOutput_;
};

0 comments on commit fbd1809

Please sign in to comment.