Skip to content

Commit 49508c9

Browse files
committed
First commit
0 parents  commit 49508c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3886
-0
lines changed

CMakeLists.txt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 2.8)
2+
3+
project(CukeBins)
4+
5+
enable_testing()
6+
7+
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules)
8+
9+
# Activates the compiler's static analyzer
10+
if(CMAKE_COMPILER_IS_GNUCXX)
11+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Weffc++")
12+
elseif(MSVC)
13+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNOMINMAX") # exclude M$ min/max macros
14+
set (Boost_USE_STATIC_LIBS ON)
15+
set (Boost_USE_MULTITHREAD ON)
16+
# set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /analyze")
17+
endif()
18+
19+
find_package(Boost REQUIRED COMPONENTS thread system regex date_time)
20+
include_directories(${Boost_INCLUDE_DIRS})
21+
22+
find_package(GTest)
23+
24+
set(CUKE_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include)
25+
include_directories(${CUKE_INCLUDE_DIRS})
26+
27+
add_subdirectory(tests)
28+
add_subdirectory(examples)

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2010 Paolo Ambrosio
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.txt

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
CukeBins allows Cucumber to support step definitions written in C++
2+
3+
4+
It is designed to support several testing backends and communication
5+
methods, but for now there is just one implementation using:
6+
7+
- GTest (Google C++ Testing Framework)
8+
- Wire Protocol
9+
10+
11+
The current library is header-only, so it doesn't need to be compiled
12+
separately, but it needs the following libraries at link time:
13+
14+
- Boost 1.40 - http://www.boost.org/
15+
Should work with some previous versions too, but it wasn't tested
16+
- GTest 1.4+ - http://code.google.com/p/googletest/
17+
Needed by the test suite and the GTest implementation. Works with
18+
GTest 1.4 and 1.5 (not tested with 1.3)
19+
20+
This header-only library is included in the source code:
21+
22+
- JSON Spirit - http://www.codeproject.com/KB/recipes/JSON_Spirit.aspx
23+
Needed by the Wire Protocol connector
24+
25+
26+
CukeBins includes a sample borrowed from Cuke4Nuke that uses the same
27+
feature definitions but tests a C++ implementation. To run the tests
28+
and the example, you need the following external tool:
29+
30+
- CMake 2.8 - http://www.cmake.org/
31+
2.8 because it needs FindGTest
32+
33+
To build the tests and the sample application:
34+
35+
cmake -E make_directory build
36+
cmake -E chdir build cmake ..
37+
cmake --build build
38+
cmake --build build --target test
39+
40+
To run the example on Unix:
41+
42+
build/examples/Calc/CalculatorSteps >/dev/null &
43+
cucumber examples/Calc/CalcFeatures
44+
45+
To run the example on Windows (NMake):
46+
47+
start build\examples\Calc\CalculatorSteps.exe
48+
cucumber examples\Calc\CalcFeatures
49+
50+
51+
CMake hints:
52+
53+
- For GTest, set GTEST_ROOT to the location of the GTest install prefix
54+
If the GTest libraries were installed in /usr/local/lib and the includes
55+
are in /usr/local/include/gtest, then add "-DGTEST_ROOT=/usr/local" to
56+
the cmake command line
57+
58+
- For Boost, set BOOST_ROOT to the location of the Boost install prefix
59+
if CMake has troubles finding it
60+

examples/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_subdirectory(Calc)
2+

examples/Calc/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
project(Calc)
2+
3+
include_directories(${CUKE_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS} Calc)
4+
5+
add_library(Calc Calc/Calculator)
6+
7+
add_executable(CalculatorSteps CalcFeatures/CalculatorSteps)
8+
target_link_libraries(CalculatorSteps Calc ${GTEST_LIBRARIES} ${Boost_LIBRARIES})

examples/Calc/Calc/Calculator.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <limits>
2+
#include "Calculator.h"
3+
4+
void Calculator::push(double n) {
5+
values.push_back(n);
6+
}
7+
8+
double Calculator::add() {
9+
double result = 0;
10+
for(std::list<double>::const_iterator i = values.begin(); i != values.end(); ++i) {
11+
result += *i;
12+
}
13+
return result;
14+
}
15+
16+
double Calculator::divide() {
17+
double result = std::numeric_limits<double>::quiet_NaN();
18+
for(std::list<double>::const_iterator i = values.begin(); i != values.end(); ++i) {
19+
if (i == values.begin()) {
20+
result = *i;
21+
} else {
22+
result /= *i;
23+
}
24+
}
25+
return result;
26+
}
27+

examples/Calc/Calc/Calculator.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <list>
2+
3+
class Calculator {
4+
private:
5+
std::list<double> values;
6+
public:
7+
void push(double);
8+
double add();
9+
double divide();
10+
};
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// for now it works with gtest only, but it should switch library depending
2+
// on the testing library included
3+
#include <gtest/gtest.h>
4+
#include <cukebins/wireserver.hpp>
5+
6+
#include <Calculator.h>
7+
8+
CUKE_CONTEXT(CalcCtx) {
9+
Calculator calc;
10+
double result;
11+
};
12+
13+
GIVEN(CalcCtx, EnterNumber, "^I have entered (\\d+) into the calculator$") {
14+
CUKE_PARAM(1, double, n);
15+
context->calc.push(n);
16+
}
17+
18+
WHEN(CalcCtx, Add, "^I press add") {
19+
context->result = context->calc.add();
20+
}
21+
22+
WHEN(CalcCtx, Divide, "^I press divide") {
23+
context->result = context->calc.divide();
24+
}
25+
26+
THEN(CalcCtx, CheckResult, "^the result should be (.*) on the screen$") {
27+
CUKE_PARAM(1, double, expected);
28+
EXPECT_EQ(expected, context->result);
29+
}
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# language: en
2+
Feature: Addition
3+
In order to avoid silly mistakes
4+
As a math idiot
5+
I want to be told the sum of two numbers
6+
7+
Scenario Outline: Add two numbers
8+
Given I have entered <input_1> into the calculator
9+
And I have entered <input_2> into the calculator
10+
When I press <button>
11+
Then the result should be <output> on the screen
12+
13+
Examples:
14+
| input_1 | input_2 | button | output |
15+
| 20 | 30 | add | 50 |
16+
| 2 | 5 | add | 7 |
17+
| 0 | 40 | add | 40 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# language: en
2+
Feature: Division
3+
In order to avoid silly mistakes
4+
Cashiers must be able to calculate a fraction
5+
6+
Scenario: Regular numbers
7+
Given I have entered 3 into the calculator
8+
And I have entered 2 into the calculator
9+
When I press divide
10+
Then the result should be 1.5 on the screen
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
host: localhost
2+
port: 3902

examples/Calc/README.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This was inspired by Cuke4Nuke Calc sample
2+

include/cukebins/cukebins.hpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef CUKEBINS_HPP_
2+
#define CUKEBINS_HPP_
3+
4+
#include "internal/StepManager.hpp"
5+
#include "internal/ContextManager.hpp"
6+
#include "internal/CukeCommands.hpp"
7+
8+
#define CUKE_STEP_CLASS_NAME_(feature_class, step_name) feature_class##_##step_name##_Step
9+
10+
#ifdef GTEST_INCLUDE_GTEST_GTEST_H_
11+
#include "internal/drivers/GTestDriver.hpp"
12+
#else // No test framework
13+
#include "internal/drivers/FakeDriver.hpp"
14+
#endif
15+
16+
#define CUKE_TEST_REAL_NAME_(step_name) CUKE_TEST_NAME_PREFIX_ #step_name
17+
18+
// TODO: parent_class and parent_id should not be needed
19+
#define CUKE_STEP_(feature_class, step_name, step_matcher, parent_class, parent_id) \
20+
class CUKE_STEP_CLASS_NAME_(feature_class, step_name) CUKE_STEPCLASS_INHERITANCE_(parent_class) { \
21+
public: \
22+
CUKE_STEP_CLASS_NAME_(feature_class, step_name)() CUKE_INHERITED_CONSTRUCTOR_(parent_class) {} \
23+
private: \
24+
virtual void CUKE_STEPCLASS_TESTBODY_NAME_ (); \
25+
static const int cukeRegId; \
26+
CUKE_STEPCLASS_OTHER_DECLARATIONS_(feature_class, step_name) \
27+
}; \
28+
const int CUKE_STEP_CLASS_NAME_(feature_class, step_name)::cukeRegId = \
29+
::cukebins::internal::registerStep(step_matcher, CUKE_TEST_FULLNAME_(feature_class, step_name)); \
30+
CUKE_STEPCLASS_OTHER_DEFINITION_(feature_class, step_name, parent_class, parent_id) \
31+
void CUKE_STEP_CLASS_NAME_(feature_class, step_name):: CUKE_STEPCLASS_TESTBODY_NAME_ ()
32+
33+
#define CUKE_STEP_F_(feature_class, step_name, step_matcher) \
34+
CUKE_STEP_(feature_class, step_name, step_matcher, \
35+
feature_class, ::testing::internal::GetTypeId<feature_class>())
36+
#define CUKE_STEP_C_(context, step_name, step_matcher) CUKE_STEP_F_(context##_DefaultFixture, step_name, step_matcher)
37+
38+
#define GIVEN_F(fixture, step_name, step_matcher) CUKE_STEP_F_(fixture, step_name, step_matcher)
39+
#define WHEN_F(fixture, step_name, step_matcher) CUKE_STEP_F_(fixture, step_name, step_matcher)
40+
#define THEN_F(fixture, step_name, step_matcher) CUKE_STEP_F_(fixture, step_name, step_matcher)
41+
42+
#define GIVEN_C(context, step_name, step_matcher) CUKE_STEP_C_(context, step_name, step_matcher)
43+
#define WHEN_C(context, step_name, step_matcher) CUKE_STEP_C_(context, step_name, step_matcher)
44+
#define THEN_C(context, step_name, step_matcher) CUKE_STEP_C_(context, step_name, step_matcher)
45+
46+
#define GIVEN(context, step_name, step_matcher) GIVEN_C(context, step_name, step_matcher)
47+
#define WHEN(context, step_name, step_matcher) WHEN_C(context, step_name, step_matcher)
48+
#define THEN(context, step_name, step_matcher) THEN_C(context, step_name, step_matcher)
49+
50+
#define CUKE_FIXTURE(FixtureName, ...) \
51+
class FixtureName : public ::cukebins::internal::CukeFixture<__VA_ARGS__>
52+
53+
#define CUKE_CONTEXT(ContextName) \
54+
struct ContextName; \
55+
CUKE_FIXTURE(ContextName##_DefaultFixture, ContextName) {}; \
56+
struct ContextName
57+
58+
#endif /* CUKEBINS_HPP_ */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef CUKEBINS_CONTEXTMANAGER_HPP_
2+
#define CUKEBINS_CONTEXTMANAGER_HPP_
3+
4+
#include <vector>
5+
#include <sstream>
6+
#include <stdexcept>
7+
8+
#include <boost/shared_ptr.hpp>
9+
#include <boost/weak_ptr.hpp>
10+
11+
namespace cukebins {
12+
namespace internal {
13+
14+
using boost::shared_ptr;
15+
using boost::weak_ptr;
16+
17+
class ContextManager {
18+
public:
19+
typedef std::vector<shared_ptr<void> > contexts_type;
20+
21+
void purgeContexts() {
22+
contexts.clear();
23+
}
24+
25+
template<class T>
26+
weak_ptr<T> addContext() {
27+
shared_ptr<T> shared(new T);
28+
contexts.push_back(shared);
29+
return weak_ptr<T> (shared);
30+
}
31+
32+
protected:
33+
static contexts_type contexts;
34+
};
35+
36+
ContextManager::contexts_type ContextManager::contexts = ContextManager::contexts_type();
37+
38+
}
39+
}
40+
41+
#endif /* CUKEBINS_CONTEXTMANAGER_HPP_ */

0 commit comments

Comments
 (0)