Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cucumber-cpp support for cgreen test framework. #129

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ env:
global:
- DISPLAY=:99
matrix:
- GMOCK_VER=1.8.0
- GMOCK_VER=1.7.0
- GMOCK_VER=1.8.0
- GMOCK_VER=1.7.0
- GMOCK_PATH=/usr/src/gmock #1.6.0 from ubuntu trusty repo
matrix:
include:
Expand All @@ -31,14 +31,22 @@ matrix:
compiler: clang
env: GMOCK_VER=1.7.0

before_script:
- git clone https://github.com/cgreen-devs/cgreen.git
- git -C cgreen checkout 7030597
- cmake -E make_directory cgreen/build
- cmake -E chdir cgreen/build cmake -G Ninja -DCMAKE_INSTALL_PREFIX:PATH=${HOME}/usr ..
- cmake --build cgreen/build
- cmake --build cgreen/build --target install

addons:
apt:
packages:
- clang-format-3.8
- libboost-thread-dev
- libboost-thread-dev
- libboost-system-dev
- libboost-regex-dev
- libboost-date-time-dev
- libboost-date-time-dev
- libboost-filesystem-dev
- libboost-program-options-dev
- libboost-test-dev
Expand Down
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ option(CUKE_USE_STATIC_BOOST "Statically link Boost (except boost::test)" ${W
option(CUKE_USE_STATIC_GTEST "Statically link Google Test" ON)
option(CUKE_DISABLE_BOOST_TEST "Disable boost:test" OFF)
option(CUKE_DISABLE_GTEST "Disable Google Test framework" OFF)
option(CUKE_DISABLE_CGREEN "Disable Cgreen Test framework" OFF)
option(CUKE_DISABLE_UNIT_TESTS "Disable unit tests" OFF)
option(CUKE_DISABLE_E2E_TESTS "Disable end-to-end tests" OFF)
option(CUKE_ENABLE_EXAMPLES "Enable the examples" OFF)
Expand Down Expand Up @@ -138,6 +139,14 @@ if(NOT CUKE_DISABLE_QT)
endif()
endif()

#
# cgreen
#

if(NOT CUKE_DISABLE_CGREEN)
find_package(cgreen)
endif()

#
# Valgrind
#
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ It relies on a few libraries:
Optional library for Boost Test driver: *test*.
* [GTest](http://code.google.com/p/googletest/) 1.6 or later.
Optional for the GTest driver. By default downloaded and built by CMake.
* [Cgreen](https://github.com/cgreen-devs/cgreen) builds from master branch since 04/28/2017 or later.
Optional for the Cgreen driver.
* [GMock](http://code.google.com/p/googlemock/) 1.6 or later.
Optional for the internal test suite. By default downloaded and built by CMake.
* [Qt 4 or 5](http://qt-project.org/). Optional for the CalcQt example and QtTest driver (only Qt 5).
Expand All @@ -49,7 +51,7 @@ to run the functional test suite.
To install the Ruby prerequisites:

```
gem install bundler
gem install bundler // For windows: gem install bundle
bundle install
```

Expand Down
55 changes: 55 additions & 0 deletions cmake/modules/Findcgreen.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#.rst:
# Findcgreen
# ----------
#
# Find the cgreen testing framework.
#
# Result variables
# ^^^^^^^^^^^^^^^^
#
# This module will set the following variables in your project:
#
# ``CGREEN_FOUND``
# true if the cgreen headers, libraries and executable were found
# ``CGREEN_INCLUDE_DIRS``
# the directory containing the cgreen headers
# ``CGREEN_LIBRARIES``
# cgreen libraries to be linked
# ``CGREEN_EXECUTABLE``
# cgreen executable
#
# Cache variables
# ^^^^^^^^^^^^^^^
#
# The following cache variables may also be set:
#
# ``CGREEN_INCLUDE_DIR``
# the directory containing the cgreen headers
# ``CGREEN_LIBRARY``
# the path to the cgreen library
# ``CGREEN_RUNNER``
# cgreen-runner executable

include(FindPackageHandleStandardArgs)

find_path(CGREEN_INCLUDE_DIR "cgreen/cgreen.h")
find_library(CGREEN_LIBRARY NAMES cgreen libcgreen)
find_program(CGREEN_RUNNER cgreen-runner)
mark_as_advanced(CGREEN_INCLUDE_DIR CGREEN_LIBRARY CGREEN_RUNNER)

find_package_handle_standard_args(cgreen
REQUIRED_VARS CGREEN_LIBRARY CGREEN_INCLUDE_DIR CGREEN_RUNNER)

if(CGREEN_FOUND)
add_library(Cgreen::Cgreen UNKNOWN IMPORTED)
set_target_properties(Cgreen::Cgreen PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${CGREEN_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${CGREEN_INCLUDE_DIR}"
)
add_executable(Cgreen::runner IMPORTED)
set_target_properties(Cgreen::runner PROPERTIES
IMPORTED_LOCATION "${CGREEN_RUNNER}"
)
endif()

1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(Calc)
add_subdirectory(CalcQt)
add_subdirectory(FizzBuzz)
add_subdirectory(FeatureShowcase)
5 changes: 5 additions & 0 deletions examples/Calc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
target_link_libraries(BoostCalculatorSteps Calc ${CUKE_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
endif()

if(CGREEN_FOUND)
add_executable(CgreenCalculatorSteps features/step_definitions/CgreenCalculatorSteps)
target_link_libraries(CgreenCalculatorSteps Calc ${CUKE_LIBRARIES} Cgreen::Cgreen)
endif()

if(Qt5TEST_FOUND)
add_executable(QtTestCalculatorSteps features/step_definitions/QtTestCalculatorSteps)
target_link_libraries(QtTestCalculatorSteps Calc Qt5::Test ${CUKE_LIBRARIES})
Expand Down
38 changes: 38 additions & 0 deletions examples/Calc/features/step_definitions/CgreenCalculatorSteps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <cgreen/cgreen.h> // general unit testing
#include <cgreen/mocks.h> // mocking functionality
#include <cucumber-cpp/autodetect.hpp>

#include <Calculator.h>

using cucumber::ScenarioScope;

struct CalcCtx {
Calculator calc;
double result;
};

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

context->calc.push(n);
}

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

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

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

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

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

assert_that_double(expected, is_equal_to_double(context->result));
}
4 changes: 4 additions & 0 deletions examples/CalcQt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ if(QT_LIBRARIES)
target_link_libraries(GTestCalculatorQtSteps libcalcqt ${CUKE_LIBRARIES} ${CUKE_GTEST_LIBRARIES} ${QT_LIBRARIES})
endif()

if(CGREEN_FOUND)
add_executable(CgreenCalculatorQtSteps features/step_definitions/CgreenCalculatorQtSteps)
target_link_libraries(CgreenCalculatorQtSteps libcalcqt ${CUKE_LIBRARIES} Cgreen::Cgreen ${QT_LIBRARIES})
endif()
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <cstdlib>
#include <cgreen/cgreen.h> // general unit testing
#include <cgreen/mocks.h> // mocking functionality
#include <cucumber-cpp/autodetect.hpp>
#include <QApplication>
#include <QTest>

#include "CalculatorWidget.h"

static int argc = 0;
static QApplication app(argc, 0);
static int milliseconds = -1;

int millisecondsToWait() {
if (milliseconds < 0)
{
char* envVariable = getenv("CALCQT_STEP_DELAY");
milliseconds = (0 != envVariable) ? atoi(envVariable) : 0;
}
return milliseconds;
}

std::istream& operator>> (std::istream& in, QString& val) { std::string s; in >> s; val = s.c_str(); return in; }
std::ostream& operator<< (std::ostream& out, const QString& val) { out << val.toLatin1().data(); return out; }

GIVEN("^I just turned on the calculator$") {
cucumber::ScenarioScope<CalculatorWidget> calculator;
calculator->move(0, 0);
calculator->show();
#if QT_VERSION >= 0x050000
QTest::qWaitForWindowExposed(calculator.get());
#else
QTest::qWaitForWindowShown(calculator.get());
#endif
QTest::qWait(millisecondsToWait());
}

WHEN("^I press (\\d+)$") {
REGEX_PARAM(unsigned int, n);
cucumber::ScenarioScope<CalculatorWidget> calculator;
QTest::keyClick(calculator.get(), Qt::Key_0 + n, Qt::NoModifier, millisecondsToWait());
}

WHEN("^I press add") {
cucumber::ScenarioScope<CalculatorWidget> calculator;
QTest::keyClick(calculator.get(), Qt::Key_Plus, Qt::NoModifier, millisecondsToWait());
}

WHEN("^I press calculate") {
cucumber::ScenarioScope<CalculatorWidget> calculator;
QTest::keyClick(calculator.get(), Qt::Key_Return, Qt::NoModifier, millisecondsToWait());
}

WHEN("^I press clear") {
cucumber::ScenarioScope<CalculatorWidget> calculator;
QTest::keyClick(calculator.get(), Qt::Key_Escape, Qt::NoModifier, millisecondsToWait());
}

WHEN("^I press subtract") {
cucumber::ScenarioScope<CalculatorWidget> calculator;
QTest::keyClick(calculator.get(), Qt::Key_Minus, Qt::NoModifier, millisecondsToWait());
}

THEN("^the display should be empty$") {
cucumber::ScenarioScope<CalculatorWidget> calculator;
assert_equal(calculator->display().size(), 0);
QTest::qWait(millisecondsToWait());
}

THEN("^the display should show (.*)$") {
REGEX_PARAM(QString, expected);
cucumber::ScenarioScope<CalculatorWidget> calculator;
assert_string_equal(expected.toLatin1().data(), calculator->display().toLatin1().data());
QTest::qWait(millisecondsToWait());
}
10 changes: 10 additions & 0 deletions examples/FizzBuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
project(FizzBuzz)

include_directories(${CUKE_INCLUDE_DIRS} src)

add_library(FizzBuzz src/FizzBuzzReporter)

if(CGREEN_FOUND)
add_executable(FizzBuzzSteps features/step_definitions/FizzBuzzSteps)
target_link_libraries(FizzBuzzSteps FizzBuzz ${CUKE_LIBRARIES} Cgreen::Cgreen)
endif()
17 changes: 17 additions & 0 deletions examples/FizzBuzz/features/FizzBuzz.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# language: en

Feature: fizz_buzz_reporter
In order to practice for programming interviews
As someone who is bad at the FIZZ, BUZZ exercise
I want to be told whether an unsigned integer corresponds to FIZZ, BUZZ, or FIZZBUZZ

Scenario Outline: Report the FIZZBUZZ string associated with an integer
Given I have passed <input> into fizzBuzzReporter
Then the result should be <output>

Examples:
| input | output |
| 3 | FIZZ |
| 5 | BUZZ |
| 15 | FIZZBUZZ |
| 11 | 11 |
39 changes: 39 additions & 0 deletions examples/FizzBuzz/features/step_definitions/FizzBuzzSteps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
#include <cucumber-cpp/autodetect.hpp>
#include "FizzBuzzReporter.h"

using cucumber::ScenarioScope;
using namespace cgreen;

static const size_t REPORT_STRING_LEN = 512;

struct FizzBuzzState {
char reportBuffer[REPORT_STRING_LEN];
};

// mocked version of printf
int printf(const char *, ...) {
// do nothing
return (int)mock();
}

GIVEN("^I have passed (.*) into fizzBuzzReporter") {
REGEX_PARAM(unsigned int, input);

ScenarioScope<FizzBuzzState> context;

// expect mocked function to be called
expect(printf);

fizzBuzzReporter(input, context->reportBuffer, REPORT_STRING_LEN);
}

THEN("^the result should be (.*)$") {

REGEX_PARAM(std::string, expected);

ScenarioScope<FizzBuzzState> context;

assert_that(context->reportBuffer, is_equal_to_string(expected.c_str()));
}
2 changes: 2 additions & 0 deletions examples/FizzBuzz/features/step_definitions/cucumber.wire
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
host: localhost
port: 3902
Empty file.
22 changes: 22 additions & 0 deletions examples/FizzBuzz/src/FizzBuzzReporter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>

#include "FizzBuzzReporter.h"

/*
* Written as an example of a C program we want to test.
*/

// printf included to provide and example of how to mock with cgreen
void fizzBuzzReporter(const unsigned int input, char* const reportBuffer, const size_t bufferSize) {
if((input % 15) == 0) {
snprintf(reportBuffer, bufferSize, "FIZZBUZZ");
} else if((input % 3) == 0) {
snprintf(reportBuffer, bufferSize, "FIZZ");
} else if((input % 5) == 0) {
snprintf(reportBuffer, bufferSize, "BUZZ");
} else {
snprintf(reportBuffer, bufferSize, "%u", input);
}

printf("%u reports %s\n", input, reportBuffer);
}
16 changes: 16 additions & 0 deletions examples/FizzBuzz/src/FizzBuzzReporter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef CUKE_FIZZBUZZREPORTER_H_
#define CUKE_FIZZBUZZREPORTER_H_

#ifdef __cplusplus
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an include guard to this header, e.g.:

#ifndef CUKE_FIZZBUZZREPORTER_H_
#define CUKE_FIZZBUZZREPORTER_H_

// contents

#endif

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extern "C" {
#endif

#include <stddef.h>

void fizzBuzzReporter(unsigned int input, char * reportBuffer, size_t bufferSize);

#ifdef __cplusplus
}
#endif

#endif // CUKE_FIZZBUZZREPORTER_H_
Loading