Skip to content

Commit

Permalink
Move default TestListener creation and add tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
emil-e committed Oct 24, 2015
1 parent c1efdcf commit 03f93e3
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 13 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_library(rapidcheck
src/detail/Assertions.cpp
src/detail/Base64.cpp
src/detail/Configuration.cpp
src/detail/DefaultTestListener.cpp
src/detail/FrequencyMap.cpp
src/detail/ImplicitParam.cpp
src/detail/LogTestListener.cpp
Expand Down
15 changes: 2 additions & 13 deletions src/Check.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
#include "rapidcheck/Check.h"

#include "detail/LogTestListener.h"
#include "detail/DefaultTestListener.h"
#include "detail/Testing.h"

namespace rc {
namespace detail {

namespace {

TestListener &defaultTestListener() {
const auto &config = configuration();
static LogTestListener listener(
std::cerr, config.verboseProgress, config.verboseShrinking);
return listener;
}

} // namespace

TestResult checkProperty(const Property &property,
const TestMetadata &metadata,
const TestParams &params,
Expand All @@ -27,7 +16,7 @@ TestResult checkProperty(const Property &property,
TestResult checkProperty(const Property &property,
const TestMetadata &metadata,
const TestParams &params) {
return checkProperty(property, metadata, params, defaultTestListener());
return checkProperty(property, metadata, params, globalTestListener());
}

TestResult checkProperty(const Property &property,
Expand Down
21 changes: 21 additions & 0 deletions src/detail/DefaultTestListener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "DefaultTestListener.h"

#include "LogTestListener.h"

namespace rc {
namespace detail {

std::unique_ptr<TestListener>
makeDefaultTestListener(const Configuration &config, std::ostream &os) {
return std::unique_ptr<TestListener>(
new LogTestListener(os, config.verboseProgress, config.verboseShrinking));
}

TestListener &globalTestListener() {
static const auto listener =
makeDefaultTestListener(configuration(), std::cerr);
return *listener;
}

} // namespace detail
} // namespace rc
22 changes: 22 additions & 0 deletions src/detail/DefaultTestListener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <memory>

#include "rapidcheck/detail/TestListener.h"
#include "rapidcheck/detail/Configuration.h"

namespace rc {
namespace detail {

/// Creates a default `TestListener`.
///
/// @param config The configuration describing the listener.
/// @param os The output stream to print information to.
std::unique_ptr<TestListener>
makeDefaultTestListener(const Configuration &config, std::ostream &os);

/// Returns the global default `TestListener`.
TestListener &globalTestListener();

} // namespace detail
} // namespace rc
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_executable(rapidcheck_tests
detail/BitStreamTests.cpp
detail/CaptureTests.cpp
detail/ConfigurationTests.cpp
detail/DefaultTestListenerTests.cpp
detail/FrequencyMapTests.cpp
detail/ImplicitParamTests.cpp
detail/LogTestListenerTests.cpp
Expand Down
49 changes: 49 additions & 0 deletions test/detail/DefaultTestListenerTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <catch.hpp>

#include "detail/DefaultTestListener.h"

using namespace rc;
using namespace rc::detail;

TEST_CASE("makeDefaultTestListener") {
std::ostringstream os;

SECTION(
"should not print anything on test case finished if verboseProgress == "
"false") {
Configuration config;
config.verboseProgress = false;
const auto listener = makeDefaultTestListener(config, os);
listener->onTestCaseFinished(CaseDescription());
REQUIRE(os.str().empty());
}

SECTION("should print . on test case success if verboseProgress == true") {
Configuration config;
config.verboseProgress = true;
const auto listener = makeDefaultTestListener(config, os);
CaseDescription desc;
desc.result = CaseResult(CaseResult::Type::Success);
listener->onTestCaseFinished(desc);
REQUIRE(os.str() == ".");
}

SECTION(
"should not print anything on shrink tried if verboseShrinking == false") {
Configuration config;
config.verboseShrinking = false;
const auto listener = makeDefaultTestListener(config, os);
listener->onShrinkTried(CaseDescription(), false);
REQUIRE(os.str().empty());
}

SECTION("should print . on unaccepted shrink if verboseShrinking == true") {
Configuration config;
config.verboseShrinking = true;
const auto listener = makeDefaultTestListener(config, os);
CaseDescription desc;
desc.result = CaseResult(CaseResult::Type::Success);
listener->onShrinkTried(desc, false);
REQUIRE(os.str() == ".");
}
}

0 comments on commit 03f93e3

Please sign in to comment.