Skip to content

Commit 84e394c

Browse files
committed
Deduplicate Boost/GTest calculator examples
Considering that the only difference is a single comparison macro: just define that macro to be the same as GTest when building for Boost.Test.
1 parent d27f6ce commit 84e394c

File tree

6 files changed

+117
-219
lines changed

6 files changed

+117
-219
lines changed
Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,4 @@
11
#include <boost/test/unit_test.hpp>
2-
#include <cucumber-cpp/autodetect.hpp>
3-
4-
#include <Calculator.h>
5-
6-
using cucumber::ScenarioScope;
7-
8-
struct CalcCtx {
9-
Calculator calc;
10-
double result;
11-
};
12-
13-
GIVEN("^I have entered (\\d+) into the calculator$") {
14-
REGEX_PARAM(double, n);
15-
ScenarioScope<CalcCtx> context;
16-
17-
context->calc.push(n);
18-
}
19-
20-
WHEN("^I press add") {
21-
ScenarioScope<CalcCtx> context;
22-
23-
context->result = context->calc.add();
24-
}
25-
26-
WHEN("^I press divide") {
27-
ScenarioScope<CalcCtx> context;
28-
29-
context->result = context->calc.divide();
30-
}
31-
32-
THEN("^the result should be (.*) on the screen$") {
33-
REGEX_PARAM(double, expected);
34-
ScenarioScope<CalcCtx> context;
35-
36-
BOOST_CHECK_EQUAL(expected, context->result);
37-
}
2+
// Pretend to be GTest
3+
#define EXPECT_EQ BOOST_CHECK_EQUAL
4+
#include "CalculatorSteps.cpp"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <cucumber-cpp/autodetect.hpp>
2+
3+
#include <Calculator.h>
4+
5+
using cucumber::ScenarioScope;
6+
7+
struct CalcCtx {
8+
Calculator calc;
9+
double result;
10+
};
11+
12+
GIVEN("^I have entered (\\d+) into the calculator$") {
13+
REGEX_PARAM(double, n);
14+
ScenarioScope<CalcCtx> context;
15+
16+
context->calc.push(n);
17+
}
18+
19+
WHEN("^I press add") {
20+
ScenarioScope<CalcCtx> context;
21+
22+
context->result = context->calc.add();
23+
}
24+
25+
WHEN("^I press divide") {
26+
ScenarioScope<CalcCtx> context;
27+
28+
context->result = context->calc.divide();
29+
}
30+
31+
THEN("^the result should be (.*) on the screen$") {
32+
REGEX_PARAM(double, expected);
33+
ScenarioScope<CalcCtx> context;
34+
35+
EXPECT_EQ(expected, context->result);
36+
}
Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,2 @@
11
#include <gtest/gtest.h>
2-
#include <cucumber-cpp/autodetect.hpp>
3-
4-
#include <Calculator.h>
5-
6-
using cucumber::ScenarioScope;
7-
8-
struct CalcCtx {
9-
Calculator calc;
10-
double result;
11-
};
12-
13-
GIVEN("^I have entered (\\d+) into the calculator$") {
14-
REGEX_PARAM(double, n);
15-
ScenarioScope<CalcCtx> context;
16-
17-
context->calc.push(n);
18-
}
19-
20-
WHEN("^I press add") {
21-
ScenarioScope<CalcCtx> context;
22-
23-
context->result = context->calc.add();
24-
}
25-
26-
WHEN("^I press divide") {
27-
ScenarioScope<CalcCtx> context;
28-
29-
context->result = context->calc.divide();
30-
}
31-
32-
THEN("^the result should be (.*) on the screen$") {
33-
REGEX_PARAM(double, expected);
34-
ScenarioScope<CalcCtx> context;
35-
36-
EXPECT_EQ(expected, context->result);
37-
}
38-
2+
#include "CalculatorSteps.cpp"
Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,4 @@
1-
#include <cstdlib>
21
#include <boost/test/unit_test.hpp>
3-
#include <cucumber-cpp/autodetect.hpp>
4-
#include <QApplication>
5-
#include <QTest>
6-
7-
#include "CalculatorWidget.h"
8-
9-
static int argc = 0;
10-
static QApplication app(argc, 0);
11-
static int milliseconds = -1;
12-
13-
int millisecondsToWait() {
14-
if (milliseconds < 0)
15-
{
16-
char* envVariable = getenv("CALCQT_STEP_DELAY");
17-
milliseconds = (0 != envVariable) ? atoi(envVariable) : 0;
18-
}
19-
return milliseconds;
20-
}
21-
22-
std::istream& operator>> (std::istream& in, QString& val) { std::string s; in >> s; val = s.c_str(); return in; }
23-
std::ostream& operator<< (std::ostream& out, const QString& val) { out << val.toLatin1().data(); return out; }
24-
25-
GIVEN("^I just turned on the calculator$") {
26-
cucumber::ScenarioScope<CalculatorWidget> calculator;
27-
calculator->move(0, 0);
28-
calculator->show();
29-
#if QT_VERSION >= 0x050000
30-
QTest::qWaitForWindowExposed(calculator.get());
31-
#else
32-
QTest::qWaitForWindowShown(calculator.get());
33-
#endif
34-
QTest::qWait(millisecondsToWait());
35-
}
36-
37-
WHEN("^I press (\\d+)$") {
38-
REGEX_PARAM(unsigned int, n);
39-
cucumber::ScenarioScope<CalculatorWidget> calculator;
40-
QTest::keyClick(calculator.get(), Qt::Key_0 + n, Qt::NoModifier, millisecondsToWait());
41-
}
42-
43-
WHEN("^I press add") {
44-
cucumber::ScenarioScope<CalculatorWidget> calculator;
45-
QTest::keyClick(calculator.get(), Qt::Key_Plus, Qt::NoModifier, millisecondsToWait());
46-
}
47-
48-
WHEN("^I press calculate") {
49-
cucumber::ScenarioScope<CalculatorWidget> calculator;
50-
QTest::keyClick(calculator.get(), Qt::Key_Return, Qt::NoModifier, millisecondsToWait());
51-
}
52-
53-
WHEN("^I press clear") {
54-
cucumber::ScenarioScope<CalculatorWidget> calculator;
55-
QTest::keyClick(calculator.get(), Qt::Key_Escape, Qt::NoModifier, millisecondsToWait());
56-
}
57-
58-
WHEN("^I press subtract") {
59-
cucumber::ScenarioScope<CalculatorWidget> calculator;
60-
QTest::keyClick(calculator.get(), Qt::Key_Minus, Qt::NoModifier, millisecondsToWait());
61-
}
62-
63-
THEN("^the display should be empty$") {
64-
cucumber::ScenarioScope<CalculatorWidget> calculator;
65-
BOOST_CHECK_EQUAL(calculator->display().size(), 0);
66-
QTest::qWait(millisecondsToWait());
67-
}
68-
69-
THEN("^the display should show (.*)$") {
70-
REGEX_PARAM(QString, expected);
71-
cucumber::ScenarioScope<CalculatorWidget> calculator;
72-
BOOST_CHECK_EQUAL(expected, calculator->display());
73-
QTest::qWait(millisecondsToWait());
74-
}
2+
// Pretend to be GTest
3+
#define EXPECT_EQ BOOST_CHECK_EQUAL
4+
#include "CalculatorQtSteps.cpp"
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <cstdlib>
2+
#include <cucumber-cpp/autodetect.hpp>
3+
#include <QApplication>
4+
#include <QTest>
5+
6+
#include "CalculatorWidget.h"
7+
8+
static int argc = 0;
9+
static QApplication app(argc, 0);
10+
static int milliseconds = -1;
11+
12+
int millisecondsToWait() {
13+
if (milliseconds < 0)
14+
{
15+
char* envVariable = getenv("CALCQT_STEP_DELAY");
16+
milliseconds = (0 != envVariable) ? atoi(envVariable) : 0;
17+
}
18+
return milliseconds;
19+
}
20+
21+
std::istream& operator>> (std::istream& in, QString& val) { std::string s; in >> s; val = s.c_str(); return in; }
22+
std::ostream& operator<< (std::ostream& out, const QString& val) { out << val.toLatin1().data(); return out; }
23+
24+
GIVEN("^I just turned on the calculator$") {
25+
cucumber::ScenarioScope<CalculatorWidget> calculator;
26+
calculator->move(0, 0);
27+
calculator->show();
28+
#if QT_VERSION >= 0x050000
29+
QTest::qWaitForWindowExposed(calculator.get());
30+
#else
31+
QTest::qWaitForWindowShown(calculator.get());
32+
#endif
33+
QTest::qWait(millisecondsToWait());
34+
}
35+
36+
WHEN("^I press (\\d+)$") {
37+
REGEX_PARAM(unsigned int, n);
38+
cucumber::ScenarioScope<CalculatorWidget> calculator;
39+
QTest::keyClick(calculator.get(), Qt::Key_0 + n, Qt::NoModifier, millisecondsToWait());
40+
}
41+
42+
WHEN("^I press add") {
43+
cucumber::ScenarioScope<CalculatorWidget> calculator;
44+
QTest::keyClick(calculator.get(), Qt::Key_Plus, Qt::NoModifier, millisecondsToWait());
45+
}
46+
47+
WHEN("^I press calculate") {
48+
cucumber::ScenarioScope<CalculatorWidget> calculator;
49+
QTest::keyClick(calculator.get(), Qt::Key_Return, Qt::NoModifier, millisecondsToWait());
50+
}
51+
52+
WHEN("^I press clear") {
53+
cucumber::ScenarioScope<CalculatorWidget> calculator;
54+
QTest::keyClick(calculator.get(), Qt::Key_Escape, Qt::NoModifier, millisecondsToWait());
55+
}
56+
57+
WHEN("^I press subtract") {
58+
cucumber::ScenarioScope<CalculatorWidget> calculator;
59+
QTest::keyClick(calculator.get(), Qt::Key_Minus, Qt::NoModifier, millisecondsToWait());
60+
}
61+
62+
THEN("^the display should be empty$") {
63+
cucumber::ScenarioScope<CalculatorWidget> calculator;
64+
EXPECT_EQ(calculator->display().size(), 0);
65+
QTest::qWait(millisecondsToWait());
66+
}
67+
68+
THEN("^the display should show (.*)$") {
69+
REGEX_PARAM(QString, expected);
70+
cucumber::ScenarioScope<CalculatorWidget> calculator;
71+
EXPECT_EQ(expected, calculator->display());
72+
QTest::qWait(millisecondsToWait());
73+
}
Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,2 @@
1-
#include <cstdlib>
21
#include <gtest/gtest.h>
3-
#include <cucumber-cpp/autodetect.hpp>
4-
#include <QApplication>
5-
#include <QTest>
6-
7-
#include "CalculatorWidget.h"
8-
9-
static int argc = 0;
10-
static QApplication app(argc, 0);
11-
static int milliseconds = -1;
12-
13-
int millisecondsToWait() {
14-
if (milliseconds < 0)
15-
{
16-
char* envVariable = getenv("CALCQT_STEP_DELAY");
17-
milliseconds = (0 != envVariable) ? atoi(envVariable) : 0;
18-
}
19-
return milliseconds;
20-
}
21-
22-
std::istream& operator>> (std::istream& in, QString& val) { std::string s; in >> s; val = s.c_str(); return in; }
23-
std::ostream& operator<< (std::ostream& out, const QString& val) { out << val.toLatin1().data(); return out; }
24-
25-
GIVEN("^I just turned on the calculator$") {
26-
cucumber::ScenarioScope<CalculatorWidget> calculator;
27-
calculator->move(0, 0);
28-
calculator->show();
29-
#if QT_VERSION >= 0x050000
30-
QTest::qWaitForWindowExposed(calculator.get());
31-
#else
32-
QTest::qWaitForWindowShown(calculator.get());
33-
#endif
34-
QTest::qWait(millisecondsToWait());
35-
}
36-
37-
WHEN("^I press (\\d+)$") {
38-
REGEX_PARAM(unsigned int, n);
39-
cucumber::ScenarioScope<CalculatorWidget> calculator;
40-
QTest::keyClick(calculator.get(), Qt::Key_0 + n, Qt::NoModifier, millisecondsToWait());
41-
}
42-
43-
WHEN("^I press add") {
44-
cucumber::ScenarioScope<CalculatorWidget> calculator;
45-
QTest::keyClick(calculator.get(), Qt::Key_Plus, Qt::NoModifier, millisecondsToWait());
46-
}
47-
48-
WHEN("^I press calculate") {
49-
cucumber::ScenarioScope<CalculatorWidget> calculator;
50-
QTest::keyClick(calculator.get(), Qt::Key_Return, Qt::NoModifier, millisecondsToWait());
51-
}
52-
53-
WHEN("^I press clear") {
54-
cucumber::ScenarioScope<CalculatorWidget> calculator;
55-
QTest::keyClick(calculator.get(), Qt::Key_Escape, Qt::NoModifier, millisecondsToWait());
56-
}
57-
58-
WHEN("^I press subtract") {
59-
cucumber::ScenarioScope<CalculatorWidget> calculator;
60-
QTest::keyClick(calculator.get(), Qt::Key_Minus, Qt::NoModifier, millisecondsToWait());
61-
}
62-
63-
THEN("^the display should be empty$") {
64-
cucumber::ScenarioScope<CalculatorWidget> calculator;
65-
EXPECT_EQ(calculator->display().size(), 0);
66-
QTest::qWait(millisecondsToWait());
67-
}
68-
69-
THEN("^the display should show (.*)$") {
70-
REGEX_PARAM(QString, expected);
71-
cucumber::ScenarioScope<CalculatorWidget> calculator;
72-
EXPECT_EQ(expected, calculator->display());
73-
QTest::qWait(millisecondsToWait());
74-
}
2+
#include "CalculatorQtSteps.cpp"

0 commit comments

Comments
 (0)