-
Notifications
You must be signed in to change notification settings - Fork 131
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
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a505781
cucumber-cpp support for cgreen test framework.
f836357
Added Cgreen Calc and CalcQt examples.
68ecb47
checkout known good cgreen state before build
f0d4ab2
Update cgreen version hash
c3fe42b
Fortify code, address change requests.
94e5a09
update feature / PR branch to cucumber-cpp master, fix resulting conf…
51705a6
update README with Cgreen reference, resolve travis.sh and CalcQt CMa…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
examples/Calc/features/step_definitions/CgreenCalculatorSteps.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
examples/CalcQt/features/step_definitions/CgreenCalculatorQtSteps.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
examples/FizzBuzz/features/step_definitions/FizzBuzzSteps.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
host: localhost | ||
port: 3902 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef CUKE_FIZZBUZZREPORTER_H_ | ||
#define CUKE_FIZZBUZZREPORTER_H_ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <stddef.h> | ||
|
||
void fizzBuzzReporter(unsigned int input, char * reportBuffer, size_t bufferSize); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // CUKE_FIZZBUZZREPORTER_H_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added