Skip to content

Commit 27256e9

Browse files
committed
#26: Replaced USING_CONTEXT with ScenarioScope<T>
1 parent 0ee242d commit 27256e9

15 files changed

+99
-59
lines changed

CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ endif()
7777
if(NOT CUKE_DISABLE_GTEST)
7878
find_package(GTest)
7979
find_package(GMock)
80+
if(GMOCK_FOUND AND GTEST_FOUND)
81+
set(CUKE_TEST_LIBRARIES ${GTEST_LIBRARY} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY})
82+
if(UNIX)
83+
find_package(Threads) # GTest needs this and it's a static library
84+
set(CUKE_TEST_LIBRARIES ${CUKE_TEST_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
85+
endif()
86+
endif()
8087
endif()
8188

8289
#

examples/Calc/CalcFeatures/BoostCalculatorSteps.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,35 @@
33

44
#include <Calculator.h>
55

6+
using namespace cuke;
7+
68
struct CalcCtx {
79
Calculator calc;
810
double result;
911
};
1012

1113
GIVEN("^I have entered (\\d+) into the calculator$") {
1214
REGEX_PARAM(double, n);
13-
USING_CONTEXT(CalcCtx, context);
15+
ScenarioScope<CalcCtx> context;
16+
1417
context->calc.push(n);
1518
}
1619

1720
WHEN("^I press add") {
18-
USING_CONTEXT(CalcCtx, context);
21+
ScenarioScope<CalcCtx> context;
22+
1923
context->result = context->calc.add();
2024
}
2125

2226
WHEN("^I press divide") {
23-
USING_CONTEXT(CalcCtx, context);
27+
ScenarioScope<CalcCtx> context;
28+
2429
context->result = context->calc.divide();
2530
}
2631

2732
THEN("^the result should be (.*) on the screen$") {
2833
REGEX_PARAM(double, expected);
29-
USING_CONTEXT(CalcCtx, context);
34+
ScenarioScope<CalcCtx> context;
35+
3036
BOOST_CHECK_EQUAL(expected, context->result);
3137
}

examples/Calc/CalcFeatures/CppSpecCalculatorSteps.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,35 @@
33

44
#include <Calculator.h>
55

6+
using namespace cuke;
7+
68
struct CalcCtx {
79
Calculator calc;
810
double result;
911
};
1012

1113
GIVEN("^I have entered (\\d+) into the calculator$") {
1214
REGEX_PARAM(double, n);
13-
USING_CONTEXT(CalcCtx, context);
15+
ScenarioScope<CalcCtx> context;
16+
1417
context->calc.push(n);
1518
}
1619

1720
WHEN("^I press add") {
18-
USING_CONTEXT(CalcCtx, context);
21+
ScenarioScope<CalcCtx> context;
22+
1923
context->result = context->calc.add();
2024
}
2125

2226
WHEN("^I press divide") {
23-
USING_CONTEXT(CalcCtx, context);
27+
ScenarioScope<CalcCtx> context;
28+
2429
context->result = context->calc.divide();
2530
}
2631

2732
THEN("^the result should be (.*) on the screen$") {
2833
REGEX_PARAM(double, expected);
29-
USING_CONTEXT(CalcCtx, context);
34+
ScenarioScope<CalcCtx> context;
35+
3036
specify(context->result, should.equal(expected));
3137
}

examples/Calc/CalcFeatures/GTestCalculatorSteps.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,36 @@
33

44
#include <Calculator.h>
55

6+
using namespace cuke;
7+
68
struct CalcCtx {
79
Calculator calc;
810
double result;
911
};
1012

1113
GIVEN("^I have entered (\\d+) into the calculator$") {
1214
REGEX_PARAM(double, n);
13-
USING_CONTEXT(CalcCtx, context);
15+
ScenarioScope<CalcCtx> context;
16+
1417
context->calc.push(n);
1518
}
1619

1720
WHEN("^I press add") {
18-
USING_CONTEXT(CalcCtx, context);
21+
ScenarioScope<CalcCtx> context;
22+
1923
context->result = context->calc.add();
2024
}
2125

2226
WHEN("^I press divide") {
23-
USING_CONTEXT(CalcCtx, context);
27+
ScenarioScope<CalcCtx> context;
28+
2429
context->result = context->calc.divide();
2530
}
2631

2732
THEN("^the result should be (.*) on the screen$") {
2833
REGEX_PARAM(double, expected);
29-
USING_CONTEXT(CalcCtx, context);
34+
ScenarioScope<CalcCtx> context;
35+
3036
EXPECT_EQ(expected, context->result);
3137
}
3238

examples/FeatureShowcase/table/TableSteps.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <string>
55
#include <map>
66

7+
using namespace cuke;
8+
79
class ActiveActors {
810
public:
911
typedef std::string actor_name_type;
@@ -37,7 +39,7 @@ class ActiveActors {
3739

3840
GIVEN("^the following actors are still active") {
3941
TABLE_PARAM(actorsParam);
40-
USING_CONTEXT(ActiveActors, context);
42+
ScenarioScope<ActiveActors> context;
4143

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

5153
WHEN("^(.+) retires") {
5254
REGEX_PARAM(std::string, retiringActor);
53-
USING_CONTEXT(ActiveActors, context);
55+
ScenarioScope<ActiveActors> context;
5456

5557
context->retireActor(retiringActor);
5658
}
5759

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

6264
ASSERT_EQ(oldestActor, context->getOldestActor());
6365
}

examples/FeatureShowcase/tag/TagSteps.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <cucumber-cpp/defs.hpp>
33

44
#include <iostream>
5+
56
using namespace std;
67

78

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef CUKE_DEPRECATED_DEFS_HPP_
2+
#define CUKE_DEPRECATED_DEFS_HPP_
3+
4+
5+
// ************************************************************************** //
6+
// ************** USING_CONTEXT ************** //
7+
// ************************************************************************** //
8+
9+
#define USING_CONTEXT(type, name) ::cuke::ScenarioScope<type> name
10+
11+
12+
#endif /* CUKE_DEPRECATED_DEFS_HPP_ */

include/cucumber-cpp/internal/ContextManager.hpp

+18-17
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
#include <boost/weak_ptr.hpp>
88

99
namespace cuke {
10-
namespace internal {
1110

1211
using boost::shared_ptr;
1312
using boost::weak_ptr;
1413

14+
namespace internal {
15+
1516
typedef std::vector<shared_ptr<void> > contexts_type;
1617

1718
class ContextManager {
@@ -24,49 +25,49 @@ class ContextManager {
2425
};
2526

2627
template<class T>
27-
class SessionContextPtr {
28+
weak_ptr<T> ContextManager::addContext() {
29+
shared_ptr<T> shared(new T);
30+
contexts.push_back(shared);
31+
return weak_ptr<T> (shared);
32+
}
33+
34+
}
35+
36+
template<class T>
37+
class ScenarioScope {
2838
public:
29-
SessionContextPtr();
39+
ScenarioScope();
3040

3141
T& operator*();
3242
T* operator->();
3343

3444
private:
35-
ContextManager contextManager;
45+
internal::ContextManager contextManager;
3646
shared_ptr<T> context;
3747
static weak_ptr<T> contextReference;
3848
};
3949

40-
41-
template<class T>
42-
weak_ptr<T> ContextManager::addContext() {
43-
shared_ptr<T> shared(new T);
44-
contexts.push_back(shared);
45-
return weak_ptr<T> (shared);
46-
}
47-
4850
template<class T>
49-
weak_ptr<T> SessionContextPtr<T>::contextReference;
51+
weak_ptr<T> ScenarioScope<T>::contextReference;
5052

5153
template<class T>
52-
SessionContextPtr<T>::SessionContextPtr() {
54+
ScenarioScope<T>::ScenarioScope() {
5355
if (contextReference.expired()) {
5456
contextReference = contextManager.addContext<T> ();
5557
}
5658
context = contextReference.lock();
5759
}
5860

5961
template<class T>
60-
T& SessionContextPtr<T>::operator*() {
62+
T& ScenarioScope<T>::operator*() {
6163
return *(context.get());
6264
}
6365

6466
template<class T>
65-
T* SessionContextPtr<T>::operator->() {
67+
T* ScenarioScope<T>::operator->() {
6668
return (context.get());
6769
}
6870

69-
}
7071
}
7172

7273
#endif /* CUKE_CONTEXTMANAGER_HPP_ */

include/cucumber-cpp/internal/step/StepMacros.hpp

-6
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,4 @@ ::cuke::internal::registerStep< step_name >( \
4141
#define TABLE_PARAM(name) const ::cuke::internal::Table & name = \
4242
getArgs()->getTableArg()
4343

44-
// ************************************************************************** //
45-
// ************** USING_CONTEXT ************** //
46-
// ************************************************************************** //
47-
48-
#define USING_CONTEXT(type, name) ::cuke::internal::SessionContextPtr<type> name
49-
5044
#endif /* CUKE_STEPMACROS_HPP_ */

tests/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ function(cuke_add_driver_test TEST_FILE)
66
add_test(${TEST_NAME} ${TEST_NAME})
77
endfunction()
88

9-
if(GTEST_FOUND AND GMOCK_FOUND)
9+
if(CUKE_TEST_LIBRARIES)
1010
include_directories(${GTEST_INCLUDE_DIRS})
1111
include_directories(${GMOCK_INCLUDE_DIRS})
1212

1313
function(cuke_add_test TEST_FILE)
1414
get_filename_component(TEST_NAME ${TEST_FILE} NAME)
1515
message(STATUS "Adding " ${TEST_NAME})
1616
add_executable(${TEST_NAME} ${TEST_FILE}.cpp)
17-
target_link_libraries(${TEST_NAME} ${GTEST_LIBRARY} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} cucumber-cpp ${CUKE_LIBRARIES} ${ARGN})
17+
target_link_libraries(${TEST_NAME} ${CUKE_TEST_LIBRARIES} cucumber-cpp ${CUKE_LIBRARIES} ${ARGN})
1818
gtest_add_tests(${TEST_NAME} "" ${TEST_FILE}.cpp)
1919
endfunction()
2020

tests/integration/ContextHandlingTest.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,33 @@ struct Context2 {};
2626

2727
TEST_F(ContextHandlingTest, contextsAreCreatedWhenNeeded) {
2828
ASSERT_EQ(0, contextManager.countContexts());
29-
::cuke::internal::SessionContextPtr<Context1> context1;
29+
::cuke::ScenarioScope<Context1> context1;
3030
ASSERT_EQ(1, contextManager.countContexts());
31-
::cuke::internal::SessionContextPtr<Context2> context2;
31+
::cuke::ScenarioScope<Context2> context2;
3232
ASSERT_EQ(2, contextManager.countContexts());
3333
}
3434

3535
TEST_F(ContextHandlingTest, sameContextTypesShareTheSamePointer) {
36-
::cuke::internal::SessionContextPtr<Context1> context1_a;
37-
::cuke::internal::SessionContextPtr<Context1> context1_b;
36+
::cuke::ScenarioScope<Context1> context1_a;
37+
::cuke::ScenarioScope<Context1> context1_b;
3838
context1_a->i = 42;
3939
ASSERT_EQ(context1_a->i, context1_b->i);
4040
}
4141

4242
TEST_F(ContextHandlingTest, theSameContextIsNotCreatedTwice) {
4343
ASSERT_EQ(0, contextManager.countContexts());
44-
::cuke::internal::SessionContextPtr<Context1> context1_a;
44+
::cuke::ScenarioScope<Context1> context1_a;
4545
ASSERT_EQ(1, contextManager.countContexts());
46-
::cuke::internal::SessionContextPtr<Context1> context1_b;
46+
::cuke::ScenarioScope<Context1> context1_b;
4747
ASSERT_EQ(1, contextManager.countContexts());
4848
}
4949

5050
TEST_F(ContextHandlingTest, contextsArePurgedExplicitlyOnly) {
5151
ASSERT_EQ(0, contextManager.countContexts());
52-
::cuke::internal::SessionContextPtr<Context1> context1_a;
52+
::cuke::ScenarioScope<Context1> context1_a;
5353
ASSERT_EQ(1, contextManager.countContexts());
54-
::cuke::internal::SessionContextPtr<Context2> *context1_b =
55-
new ::cuke::internal::SessionContextPtr<Context2>();
54+
::cuke::ScenarioScope<Context2> *context1_b =
55+
new ::cuke::ScenarioScope<Context2>();
5656
ASSERT_EQ(2, contextManager.countContexts());
5757
delete context1_b;
5858
ASSERT_EQ(2, contextManager.countContexts());

tests/integration/drivers/BoostDriverTest.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

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

6+
using namespace cuke;
7+
68
THEN(SUCCEED_MATCHER) {
7-
USING_CONTEXT(cuke::internal::SomeContext, ctx);
9+
ScenarioScope<SomeContext> ctx;
810
BOOST_CHECK(true);
911
}
1012

1113
THEN(FAIL_MATCHER) {
12-
USING_CONTEXT(cuke::internal::SomeContext, ctx);
14+
ScenarioScope<SomeContext> ctx;
1315
BOOST_CHECK(false);
1416
}
1517

tests/integration/drivers/CppSpecDriverTest.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

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

6+
using namespace cuke;
7+
68
THEN(SUCCEED_MATCHER) {
7-
USING_CONTEXT(cuke::internal::SomeContext, ctx);
9+
ScenarioScope<SomeContext> ctx;
810
specify(true, should.equal(true));
911
}
1012

1113
THEN(FAIL_MATCHER) {
12-
USING_CONTEXT(cuke::internal::SomeContext, ctx);
14+
ScenarioScope<SomeContext> ctx;
1315
specify(true, should.equal(false));
1416
}
1517

tests/integration/drivers/GTestDriverTest.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

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

6+
using namespace cuke;
7+
68
THEN(SUCCEED_MATCHER) {
7-
USING_CONTEXT(cuke::internal::SomeContext, ctx);
9+
ScenarioScope<SomeContext> ctx;
810
ASSERT_TRUE(true);
911
}
1012

1113
THEN(FAIL_MATCHER) {
12-
USING_CONTEXT(cuke::internal::SomeContext, ctx);
14+
ScenarioScope<SomeContext> ctx;
1315
ASSERT_TRUE(false);
1416
}
1517

0 commit comments

Comments
 (0)