Skip to content
Closed

Qt5 #85

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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ It relies on a few libraries:
Optional for the CppSpec driver.
* [GMock](http://code.google.com/p/googlemock/) 1.6 or later.
Optional for the internal test suite.
* [Qt 4](http://qt-project.org/). Optional for the CalcQt example.
* [Qt 5](http://qt-project.org/). Optional for the CalcQt example.

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

Expand Down
52 changes: 40 additions & 12 deletions examples/CalcQt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
cmake_minimum_required(VERSION 2.8.11)

project(CalcQt)

find_package(Qt4 COMPONENTS QtCore QtGui QtTest)
# Find the Core library
find_package(Qt5Core REQUIRED)
# Find the QtWidgets library
find_package(Qt5Widgets REQUIRED)
# Find the QtTest library
find_package(Qt5Test REQUIRED)

set (QT5_FOUND ${Qt5Core_FOUND} AND ${Qt5Test_FOUND} AND ${Qt5Widgets_FOUND})

if(NOT QT5_FOUND)
message(WARNING "Qt5 not found")
endif()

if(QT5_FOUND)
message(STATUS "Using Qt version ${Qt5Core_VERSION_STRING} ")

if(QT4_FOUND)
include(${QT_USE_FILE})
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

set(CALCQT_HEADERS src/CalculatorWidget.h)
qt4_wrap_cpp(CALCQT_HEADERS_MOC ${CALCQT_HEADERS})
include_directories(${CUKE_INCLUDE_DIRS} src)
add_library(CalcQt src/CalculatorWidget ${CALCQT_HEADERS_MOC})
set(CALCQT_SOURCES src/CalcQt.cpp src/CalculatorWidget.cpp)
set(QT_LIBRARIES Qt5::Core Qt5::Widgets Qt5::Test)

add_library(libcalcqt src/CalculatorWidget.cpp ${CALCQT_HEADERS})
target_link_libraries(libcalcqt ${QT_LIBRARIES})

add_executable(calcqt ${CALCQT_SOURCES} ${CALCQT_HEADERS})
target_link_libraries(calcqt libcalcqt ${QT_LIBRARIES})

if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(BoostCalculatorQtSteps features/step_definitions/BoostCalculatorQtSteps)
target_link_libraries(BoostCalculatorQtSteps CalcQt ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ${CUKE_LIBRARIES} ${QT_LIBRARIES})
include_directories(${Boost_INCLUDE_DIRS} src/)
add_executable(BoostCalculatorQtSteps features/step_definitions/BoostCalculatorQtSteps ${CALCQT_HEADERS})
target_link_libraries(BoostCalculatorQtSteps libcalcqt ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ${CUKE_LIBRARIES} ${QT_LIBRARIES})
endif()

if(GTEST_FOUND)
include_directories(${GTEST_INCLUDE_DIRS} src/)
add_executable(GTestCalculatorQtSteps features/step_definitions/GTestCalculatorQtSteps)
target_link_libraries(GTestCalculatorQtSteps libcalcqt ${QT_LIBRARIES} ${GTEST_LIBRARIES} ${CUKE_LIBRARIES})
endif()

set(CALCQT_SOURCES src/CalcQt.cpp src/CalculatorWidget.cpp)
add_executable(calcqt ${CALCQT_SOURCES} ${CALCQT_HEADERS_MOC})
target_link_libraries(calcqt ${QT_LIBRARIES})
endif()

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <QApplication>
#include <QTest>

#include <CalculatorWidget.h>
#include "CalculatorWidget.h"

static int argc = 0;
static QApplication app(argc, 0);
Expand All @@ -23,13 +23,13 @@ int millisecondsToWait() {
}

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.toAscii().data(); return out; }
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();
QTest::qWaitForWindowShown(calculator.get());
QTest::qWaitForWindowExposed(calculator.get());
QTest::qWait(millisecondsToWait());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

#include <cstdlib>

#include <gtest/gtest.h>
#include <cucumber-cpp/defs.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();
QTest::qWaitForWindowExposed(calculator.get());
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;
EXPECT_EQ(calculator->display().size(), 0);
QTest::qWait(millisecondsToWait());
}

THEN("^the display should show (.*)$") {
REGEX_PARAM(QString, expected);
cucumber::ScenarioScope<CalculatorWidget> calculator;
EXPECT_EQ(expected, calculator->display());
QTest::qWait(millisecondsToWait());
}
6 changes: 3 additions & 3 deletions examples/CalcQt/src/CalculatorWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include "CalculatorWidget.h"

#include <stdio.h>
#include <QGridLayout>
#include <QKeyEvent>
#include <QLabel>
#include <QPushButton>
#include <QSignalMapper>

CalculatorWidget::CalculatorWidget(QWidget *parent, Qt::WindowFlags flags) : QWidget(parent, flags) {
QGridLayout *layout = new QGridLayout;
QGridLayout *layout = new QGridLayout(this);
layout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(layout);

Expand Down Expand Up @@ -69,7 +68,7 @@ int CalculatorWidget::calculate(const QString& expression) {
}
pos += regexp.matchedLength();
if (pos < expression.length()) {
operation = expression.at(pos).toAscii();
operation = expression.at(pos).toLatin1();
}
}
return result;
Expand Down Expand Up @@ -103,6 +102,7 @@ void CalculatorWidget::keyPressEvent(QKeyEvent *event) {
}

void CalculatorWidget::keyReleaseEvent(QKeyEvent *event) {
Q_UNUSED(event)
if (0 != keyclickedButton) {
keyclickedButton->setDown(false);
keyclickedButton = 0;
Expand Down
3 changes: 2 additions & 1 deletion examples/CalcQt/src/CalculatorWidget.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class QLabel;
class QPushButton;
#include <QString>
class QSignalMapper;

#include <QString>
#include <QWidget>

class CalculatorWidget : public QWidget {
Expand Down