Skip to content

Commit b3e089b

Browse files
committed
Merge #98 'Qt5 support for CalcQt example'
2 parents 9d28279 + 55bcf56 commit b3e089b

File tree

7 files changed

+122
-22
lines changed

7 files changed

+122
-22
lines changed

HISTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Support for Boost 1.60 ([#101](https://github.com./cucumber/cucumber-cpp/pull/101) Kai Unger)
66
* Support for CMake inclusion in other projects ([#76](https://github.com./cucumber/cucumber-cpp/pull/76) Eric Brayet)
7+
* Added Qt5 suppor in CalcQt example ([#98](https://github.com./cucumber/cucumber-cpp/pull/98) Kamil Strzempowicz)
78

89
### Bugfixes
910

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ It relies on a few libraries:
2525
Optional for the CppSpec driver.
2626
* [GMock](http://code.google.com/p/googlemock/) 1.6 or later.
2727
Optional for the internal test suite. By default downloaded and built by CMake.
28-
* [Qt 4](http://qt-project.org/). Optional for the CalcQt example.
28+
* [Qt 4 or 5](http://qt-project.org/). Optional for the CalcQt example.
2929

3030
This header-only library is included in the source code:
3131

examples/CalcQt/CMakeLists.txt

+35-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
11
project(CalcQt)
22

3-
find_package(Qt4 COMPONENTS QtCore QtGui QtTest)
3+
set(CALCQT_HEADERS src/CalculatorWidget.h)
4+
set(CALCQT_SOURCES src/CalcQt.cpp src/CalculatorWidget.cpp)
5+
include_directories(${CUKE_INCLUDE_DIRS} src)
46

5-
if(QT4_FOUND)
6-
include(${QT_USE_FILE})
7-
set(CALCQT_HEADERS src/CalculatorWidget.h)
8-
qt4_wrap_cpp(CALCQT_HEADERS_MOC ${CALCQT_HEADERS})
9-
include_directories(${CUKE_INCLUDE_DIRS} src)
10-
add_library(CalcQt src/CalculatorWidget ${CALCQT_HEADERS_MOC})
7+
find_package(Qt5Core QUIET)
8+
find_package(Qt5Widgets QUIET)
9+
find_package(Qt5Test QUIET)
1110

11+
if(${Qt5Core_FOUND} AND ${Qt5Widgets_FOUND} AND ${Qt5Test_FOUND})
12+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
13+
set(CMAKE_AUTOMOC ON)
14+
set(QT_LIBRARIES Qt5::Core Qt5::Widgets Qt5::Test)
15+
16+
add_library(libcalcqt src/CalculatorWidget.cpp ${CALCQT_HEADERS})
17+
target_link_libraries(libcalcqt ${QT_LIBRARIES})
18+
19+
add_executable(calcqt ${CALCQT_SOURCES})
20+
target_link_libraries(calcqt libcalcqt ${QT_LIBRARIES})
21+
else()
22+
find_package(Qt4 COMPONENTS QtCore QtGui QtTest)
23+
if(QT4_FOUND)
24+
include(${QT_USE_FILE})
25+
qt4_wrap_cpp(CALCQT_HEADERS_MOC ${CALCQT_HEADERS})
26+
add_library(libcalcqt src/CalculatorWidget ${CALCQT_HEADERS_MOC})
27+
28+
add_executable(calcqt ${CALCQT_SOURCES} ${CALCQT_HEADERS_MOC})
29+
target_link_libraries(calcqt ${QT_LIBRARIES})
30+
endif()
31+
endif()
32+
33+
if(QT_LIBRARIES)
1234
if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
1335
include_directories(${Boost_INCLUDE_DIRS})
1436
add_executable(BoostCalculatorQtSteps features/step_definitions/BoostCalculatorQtSteps)
15-
target_link_libraries(BoostCalculatorQtSteps CalcQt ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ${CUKE_LIBRARIES} ${QT_LIBRARIES})
37+
target_link_libraries(BoostCalculatorQtSteps libcalcqt ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ${CUKE_LIBRARIES} ${QT_LIBRARIES})
38+
endif()
39+
if(GTEST_FOUND)
40+
include_directories(${GTEST_INCLUDE_DIRS})
41+
add_executable(GTestCalculatorQtSteps features/step_definitions/GTestCalculatorQtSteps)
42+
target_link_libraries(GTestCalculatorQtSteps libcalcqt ${CUKE_LIBRARIES} ${CUKE_GTEST_LIBRARIES} ${QT_LIBRARIES})
1643
endif()
17-
18-
set(CALCQT_SOURCES src/CalcQt.cpp src/CalculatorWidget.cpp)
19-
add_executable(calcqt ${CALCQT_SOURCES} ${CALCQT_HEADERS_MOC})
20-
target_link_libraries(calcqt ${QT_LIBRARIES})
2144
endif()

examples/CalcQt/features/step_definitions/BoostCalculatorQtSteps.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
21
#include <cstdlib>
3-
42
#include <boost/test/unit_test.hpp>
53
#include <cucumber-cpp/defs.hpp>
6-
74
#include <QApplication>
85
#include <QTest>
96

10-
#include <CalculatorWidget.h>
7+
#include "CalculatorWidget.h"
118

129
static int argc = 0;
1310
static QApplication app(argc, 0);
@@ -23,13 +20,17 @@ int millisecondsToWait() {
2320
}
2421

2522
std::istream& operator>> (std::istream& in, QString& val) { std::string s; in >> s; val = s.c_str(); return in; }
26-
std::ostream& operator<< (std::ostream& out, const QString& val) { out << val.toAscii().data(); return out; }
23+
std::ostream& operator<< (std::ostream& out, const QString& val) { out << val.toLatin1().data(); return out; }
2724

2825
GIVEN("^I just turned on the calculator$") {
2926
cucumber::ScenarioScope<CalculatorWidget> calculator;
3027
calculator->move(0, 0);
3128
calculator->show();
29+
#if QT_VERSION >= 0x050000
30+
QTest::qWaitForWindowExposed(calculator.get());
31+
#else
3232
QTest::qWaitForWindowShown(calculator.get());
33+
#endif
3334
QTest::qWait(millisecondsToWait());
3435
}
3536

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <cstdlib>
2+
#include <gtest/gtest.h>
3+
#include <cucumber-cpp/defs.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+
}

examples/CalcQt/src/CalculatorWidget.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#include "CalculatorWidget.h"
22

3-
#include <stdio.h>
43
#include <QGridLayout>
54
#include <QKeyEvent>
65
#include <QLabel>
76
#include <QPushButton>
87
#include <QSignalMapper>
98

109
CalculatorWidget::CalculatorWidget(QWidget *parent, Qt::WindowFlags flags) : QWidget(parent, flags) {
11-
QGridLayout *layout = new QGridLayout;
10+
QGridLayout *layout = new QGridLayout(this);
1211
layout->setSizeConstraint(QLayout::SetFixedSize);
1312
setLayout(layout);
1413

@@ -69,7 +68,7 @@ int CalculatorWidget::calculate(const QString& expression) {
6968
}
7069
pos += regexp.matchedLength();
7170
if (pos < expression.length()) {
72-
operation = expression.at(pos).toAscii();
71+
operation = expression.at(pos).toLatin1();
7372
}
7473
}
7574
return result;
@@ -103,6 +102,7 @@ void CalculatorWidget::keyPressEvent(QKeyEvent *event) {
103102
}
104103

105104
void CalculatorWidget::keyReleaseEvent(QKeyEvent *event) {
105+
Q_UNUSED(event)
106106
if (0 != keyclickedButton) {
107107
keyclickedButton->setDown(false);
108108
keyclickedButton = 0;

examples/CalcQt/src/CalculatorWidget.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
class QLabel;
22
class QPushButton;
3-
#include <QString>
43
class QSignalMapper;
4+
5+
#include <QString>
56
#include <QWidget>
67

78
class CalculatorWidget : public QWidget {

0 commit comments

Comments
 (0)