Skip to content

Commit

Permalink
#26: Replaced USING_CONTEXT with ScenarioScope<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloambrosio committed Nov 28, 2012
1 parent 0ee242d commit 27256e9
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 59 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ endif()
if(NOT CUKE_DISABLE_GTEST)
find_package(GTest)
find_package(GMock)
if(GMOCK_FOUND AND GTEST_FOUND)
set(CUKE_TEST_LIBRARIES ${GTEST_LIBRARY} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY})
if(UNIX)
find_package(Threads) # GTest needs this and it's a static library
set(CUKE_TEST_LIBRARIES ${CUKE_TEST_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
endif()
endif()
endif()

#
Expand Down
14 changes: 10 additions & 4 deletions examples/Calc/CalcFeatures/BoostCalculatorSteps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,35 @@

#include <Calculator.h>

using namespace cuke;

struct CalcCtx {
Calculator calc;
double result;
};

GIVEN("^I have entered (\\d+) into the calculator$") {
REGEX_PARAM(double, n);
USING_CONTEXT(CalcCtx, context);
ScenarioScope<CalcCtx> context;

context->calc.push(n);
}

WHEN("^I press add") {
USING_CONTEXT(CalcCtx, context);
ScenarioScope<CalcCtx> context;

context->result = context->calc.add();
}

WHEN("^I press divide") {
USING_CONTEXT(CalcCtx, context);
ScenarioScope<CalcCtx> context;

context->result = context->calc.divide();
}

THEN("^the result should be (.*) on the screen$") {
REGEX_PARAM(double, expected);
USING_CONTEXT(CalcCtx, context);
ScenarioScope<CalcCtx> context;

BOOST_CHECK_EQUAL(expected, context->result);
}
14 changes: 10 additions & 4 deletions examples/Calc/CalcFeatures/CppSpecCalculatorSteps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,35 @@

#include <Calculator.h>

using namespace cuke;

struct CalcCtx {
Calculator calc;
double result;
};

GIVEN("^I have entered (\\d+) into the calculator$") {
REGEX_PARAM(double, n);
USING_CONTEXT(CalcCtx, context);
ScenarioScope<CalcCtx> context;

context->calc.push(n);
}

WHEN("^I press add") {
USING_CONTEXT(CalcCtx, context);
ScenarioScope<CalcCtx> context;

context->result = context->calc.add();
}

WHEN("^I press divide") {
USING_CONTEXT(CalcCtx, context);
ScenarioScope<CalcCtx> context;

context->result = context->calc.divide();
}

THEN("^the result should be (.*) on the screen$") {
REGEX_PARAM(double, expected);
USING_CONTEXT(CalcCtx, context);
ScenarioScope<CalcCtx> context;

specify(context->result, should.equal(expected));
}
14 changes: 10 additions & 4 deletions examples/Calc/CalcFeatures/GTestCalculatorSteps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,36 @@

#include <Calculator.h>

using namespace cuke;

struct CalcCtx {
Calculator calc;
double result;
};

GIVEN("^I have entered (\\d+) into the calculator$") {
REGEX_PARAM(double, n);
USING_CONTEXT(CalcCtx, context);
ScenarioScope<CalcCtx> context;

context->calc.push(n);
}

WHEN("^I press add") {
USING_CONTEXT(CalcCtx, context);
ScenarioScope<CalcCtx> context;

context->result = context->calc.add();
}

WHEN("^I press divide") {
USING_CONTEXT(CalcCtx, context);
ScenarioScope<CalcCtx> context;

context->result = context->calc.divide();
}

THEN("^the result should be (.*) on the screen$") {
REGEX_PARAM(double, expected);
USING_CONTEXT(CalcCtx, context);
ScenarioScope<CalcCtx> context;

EXPECT_EQ(expected, context->result);
}

8 changes: 5 additions & 3 deletions examples/FeatureShowcase/table/TableSteps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <string>
#include <map>

using namespace cuke;

class ActiveActors {
public:
typedef std::string actor_name_type;
Expand Down Expand Up @@ -37,7 +39,7 @@ class ActiveActors {

GIVEN("^the following actors are still active") {
TABLE_PARAM(actorsParam);
USING_CONTEXT(ActiveActors, context);
ScenarioScope<ActiveActors> context;

const table_hashes_type & actors = actorsParam.hashes();
for (table_hashes_type::const_iterator ait = actors.begin(); ait != actors.end(); ++ait) {
Expand All @@ -50,14 +52,14 @@ GIVEN("^the following actors are still active") {

WHEN("^(.+) retires") {
REGEX_PARAM(std::string, retiringActor);
USING_CONTEXT(ActiveActors, context);
ScenarioScope<ActiveActors> context;

context->retireActor(retiringActor);
}

THEN("^the oldest actor should be (.+)$") {
REGEX_PARAM(std::string, oldestActor);
USING_CONTEXT(ActiveActors, context);
ScenarioScope<ActiveActors> context;

ASSERT_EQ(oldestActor, context->getOldestActor());
}
1 change: 1 addition & 0 deletions examples/FeatureShowcase/tag/TagSteps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <cucumber-cpp/defs.hpp>

#include <iostream>

using namespace std;


Expand Down
12 changes: 12 additions & 0 deletions include/cucumber-cpp/deprecated-defs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef CUKE_DEPRECATED_DEFS_HPP_
#define CUKE_DEPRECATED_DEFS_HPP_


// ************************************************************************** //
// ************** USING_CONTEXT ************** //
// ************************************************************************** //

#define USING_CONTEXT(type, name) ::cuke::ScenarioScope<type> name


#endif /* CUKE_DEPRECATED_DEFS_HPP_ */
35 changes: 18 additions & 17 deletions include/cucumber-cpp/internal/ContextManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
#include <boost/weak_ptr.hpp>

namespace cuke {
namespace internal {

using boost::shared_ptr;
using boost::weak_ptr;

namespace internal {

typedef std::vector<shared_ptr<void> > contexts_type;

class ContextManager {
Expand All @@ -24,49 +25,49 @@ class ContextManager {
};

template<class T>
class SessionContextPtr {
weak_ptr<T> ContextManager::addContext() {
shared_ptr<T> shared(new T);
contexts.push_back(shared);
return weak_ptr<T> (shared);
}

}

template<class T>
class ScenarioScope {
public:
SessionContextPtr();
ScenarioScope();

T& operator*();
T* operator->();

private:
ContextManager contextManager;
internal::ContextManager contextManager;
shared_ptr<T> context;
static weak_ptr<T> contextReference;
};


template<class T>
weak_ptr<T> ContextManager::addContext() {
shared_ptr<T> shared(new T);
contexts.push_back(shared);
return weak_ptr<T> (shared);
}

template<class T>
weak_ptr<T> SessionContextPtr<T>::contextReference;
weak_ptr<T> ScenarioScope<T>::contextReference;

template<class T>
SessionContextPtr<T>::SessionContextPtr() {
ScenarioScope<T>::ScenarioScope() {
if (contextReference.expired()) {
contextReference = contextManager.addContext<T> ();
}
context = contextReference.lock();
}

template<class T>
T& SessionContextPtr<T>::operator*() {
T& ScenarioScope<T>::operator*() {
return *(context.get());
}

template<class T>
T* SessionContextPtr<T>::operator->() {
T* ScenarioScope<T>::operator->() {
return (context.get());
}

}
}

#endif /* CUKE_CONTEXTMANAGER_HPP_ */
6 changes: 0 additions & 6 deletions include/cucumber-cpp/internal/step/StepMacros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,4 @@ ::cuke::internal::registerStep< step_name >( \
#define TABLE_PARAM(name) const ::cuke::internal::Table & name = \
getArgs()->getTableArg()

// ************************************************************************** //
// ************** USING_CONTEXT ************** //
// ************************************************************************** //

#define USING_CONTEXT(type, name) ::cuke::internal::SessionContextPtr<type> name

#endif /* CUKE_STEPMACROS_HPP_ */
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ function(cuke_add_driver_test TEST_FILE)
add_test(${TEST_NAME} ${TEST_NAME})
endfunction()

if(GTEST_FOUND AND GMOCK_FOUND)
if(CUKE_TEST_LIBRARIES)
include_directories(${GTEST_INCLUDE_DIRS})
include_directories(${GMOCK_INCLUDE_DIRS})

function(cuke_add_test TEST_FILE)
get_filename_component(TEST_NAME ${TEST_FILE} NAME)
message(STATUS "Adding " ${TEST_NAME})
add_executable(${TEST_NAME} ${TEST_FILE}.cpp)
target_link_libraries(${TEST_NAME} ${GTEST_LIBRARY} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} cucumber-cpp ${CUKE_LIBRARIES} ${ARGN})
target_link_libraries(${TEST_NAME} ${CUKE_TEST_LIBRARIES} cucumber-cpp ${CUKE_LIBRARIES} ${ARGN})
gtest_add_tests(${TEST_NAME} "" ${TEST_FILE}.cpp)
endfunction()

Expand Down
18 changes: 9 additions & 9 deletions tests/integration/ContextHandlingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,33 @@ struct Context2 {};

TEST_F(ContextHandlingTest, contextsAreCreatedWhenNeeded) {
ASSERT_EQ(0, contextManager.countContexts());
::cuke::internal::SessionContextPtr<Context1> context1;
::cuke::ScenarioScope<Context1> context1;
ASSERT_EQ(1, contextManager.countContexts());
::cuke::internal::SessionContextPtr<Context2> context2;
::cuke::ScenarioScope<Context2> context2;
ASSERT_EQ(2, contextManager.countContexts());
}

TEST_F(ContextHandlingTest, sameContextTypesShareTheSamePointer) {
::cuke::internal::SessionContextPtr<Context1> context1_a;
::cuke::internal::SessionContextPtr<Context1> context1_b;
::cuke::ScenarioScope<Context1> context1_a;
::cuke::ScenarioScope<Context1> context1_b;
context1_a->i = 42;
ASSERT_EQ(context1_a->i, context1_b->i);
}

TEST_F(ContextHandlingTest, theSameContextIsNotCreatedTwice) {
ASSERT_EQ(0, contextManager.countContexts());
::cuke::internal::SessionContextPtr<Context1> context1_a;
::cuke::ScenarioScope<Context1> context1_a;
ASSERT_EQ(1, contextManager.countContexts());
::cuke::internal::SessionContextPtr<Context1> context1_b;
::cuke::ScenarioScope<Context1> context1_b;
ASSERT_EQ(1, contextManager.countContexts());
}

TEST_F(ContextHandlingTest, contextsArePurgedExplicitlyOnly) {
ASSERT_EQ(0, contextManager.countContexts());
::cuke::internal::SessionContextPtr<Context1> context1_a;
::cuke::ScenarioScope<Context1> context1_a;
ASSERT_EQ(1, contextManager.countContexts());
::cuke::internal::SessionContextPtr<Context2> *context1_b =
new ::cuke::internal::SessionContextPtr<Context2>();
::cuke::ScenarioScope<Context2> *context1_b =
new ::cuke::ScenarioScope<Context2>();
ASSERT_EQ(2, contextManager.countContexts());
delete context1_b;
ASSERT_EQ(2, contextManager.countContexts());
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/drivers/BoostDriverTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

#include "../../utils/DriverTestRunner.hpp"

using namespace cuke;

THEN(SUCCEED_MATCHER) {
USING_CONTEXT(cuke::internal::SomeContext, ctx);
ScenarioScope<SomeContext> ctx;
BOOST_CHECK(true);
}

THEN(FAIL_MATCHER) {
USING_CONTEXT(cuke::internal::SomeContext, ctx);
ScenarioScope<SomeContext> ctx;
BOOST_CHECK(false);
}

Expand Down
6 changes: 4 additions & 2 deletions tests/integration/drivers/CppSpecDriverTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

#include "../../utils/DriverTestRunner.hpp"

using namespace cuke;

THEN(SUCCEED_MATCHER) {
USING_CONTEXT(cuke::internal::SomeContext, ctx);
ScenarioScope<SomeContext> ctx;
specify(true, should.equal(true));
}

THEN(FAIL_MATCHER) {
USING_CONTEXT(cuke::internal::SomeContext, ctx);
ScenarioScope<SomeContext> ctx;
specify(true, should.equal(false));
}

Expand Down
6 changes: 4 additions & 2 deletions tests/integration/drivers/GTestDriverTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

#include "../../utils/DriverTestRunner.hpp"

using namespace cuke;

THEN(SUCCEED_MATCHER) {
USING_CONTEXT(cuke::internal::SomeContext, ctx);
ScenarioScope<SomeContext> ctx;
ASSERT_TRUE(true);
}

THEN(FAIL_MATCHER) {
USING_CONTEXT(cuke::internal::SomeContext, ctx);
ScenarioScope<SomeContext> ctx;
ASSERT_TRUE(false);
}

Expand Down
Loading

0 comments on commit 27256e9

Please sign in to comment.