From 2006dc4d835b1b6263a2290306acf4ffbb200970 Mon Sep 17 00:00:00 2001 From: Kamil Strzempowicz Date: Fri, 25 Aug 2017 23:10:42 +0200 Subject: [PATCH] [QtTestDriver] Qt test driver for cucumber-cpp. Implementation based on temporary file. When building QtTest driver C++11 is enabled globally to prevent issue #141. --- CMakeLists.txt | 4 ++ .../internal/drivers/DriverSelector.hpp | 2 + .../internal/drivers/QtTestDriver.hpp | 40 +++++++++++++++++++ src/CMakeLists.txt | 7 ++++ src/drivers/QtTestDriver.cpp | 33 +++++++++++++++ 5 files changed, 86 insertions(+) create mode 100644 include/cucumber-cpp/internal/drivers/QtTestDriver.hpp create mode 100644 src/drivers/QtTestDriver.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e0482730..d4dba726 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -124,6 +124,10 @@ if(NOT CUKE_DISABLE_QT) if(${Qt5Core_FOUND} AND ${Qt5Widgets_FOUND} AND ${Qt5Test_FOUND}) message(STATUS "Found Qt version: ${Qt5Core_VERSION_STRING}") set(QT_LIBRARIES Qt5::Core Qt5::Widgets Qt5::Test) + if(NOT Qt5Core_VERSION_STRING VERSION_LESS 5.7 AND (NOT DEFINED CMAKE_CXX_STANDARD OR NOT CMAKE_CXX_STANDARD STREQUAL 98)) + message(STATUS "C++11 is needed from Qt version 5.7.0, building with c++11 enabled") + set(CMAKE_CXX_STANDARD 11) + endif() else() find_package(Qt4 COMPONENTS QtCore QtGui QtTest) if(QT4_FOUND) diff --git a/include/cucumber-cpp/internal/drivers/DriverSelector.hpp b/include/cucumber-cpp/internal/drivers/DriverSelector.hpp index f4ac8567..559be1ab 100644 --- a/include/cucumber-cpp/internal/drivers/DriverSelector.hpp +++ b/include/cucumber-cpp/internal/drivers/DriverSelector.hpp @@ -2,4 +2,6 @@ #include "GTestDriver.hpp" #elif defined(BOOST_TEST_CASE) #include "BoostDriver.hpp" +#elif defined(QTEST_H) + #include "QtTestDriver.hpp" #endif diff --git a/include/cucumber-cpp/internal/drivers/QtTestDriver.hpp b/include/cucumber-cpp/internal/drivers/QtTestDriver.hpp new file mode 100644 index 00000000..ab6d3439 --- /dev/null +++ b/include/cucumber-cpp/internal/drivers/QtTestDriver.hpp @@ -0,0 +1,40 @@ +#ifndef CUKE_QTTESTDRIVER_HPP_ +#define CUKE_QTTESTDRIVER_HPP_ + +#include "../step/StepManager.hpp" +#include + +namespace cucumber { +namespace internal { + +class QtTestStep : public BasicStep { + friend class QtTestObject; + +public: + QtTestStep() : BasicStep() {} + +protected: + const InvokeResult invokeStepBody(); +}; + +#define STEP_INHERITANCE(step_name) ::cucumber::internal::QtTestStep + +class QtTestObject : public QObject { + Q_OBJECT +public: + QtTestObject(QtTestStep* qtTestStep): step(qtTestStep) {} + virtual ~QtTestObject() {} + +protected: + QtTestStep* step; + +private slots: + void test() const { + step->body(); + } +}; + +} +} + +#endif /* CUKE_QTTESTDRIVER_HPP_ */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 818d7b83..1430b9db 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -26,6 +26,13 @@ if(Boost_UNIT_TEST_FRAMEWORK_FOUND) list(APPEND CUKE_SOURCES drivers/BoostDriver.cpp) endif() +if(Qt5TEST_FOUND) + qt5_wrap_cpp(MOC_FILE ../include/cucumber-cpp/internal/drivers/QtTestDriver.hpp) + list(APPEND CUKE_SOURCES ${MOC_FILE}) + list(APPEND CUKE_SOURCES drivers/QtTestDriver.cpp) + list(APPEND CUKE_DEP_LIBRARIES Qt5::Test) +endif() + if(CMAKE_EXTRA_GENERATOR OR MSVC_IDE) message(STATUS "Adding header files to project") file(GLOB_RECURSE CUKE_HEADERS "${CUKE_INCLUDE_DIR}/cucumber-cpp/*.hpp") diff --git a/src/drivers/QtTestDriver.cpp b/src/drivers/QtTestDriver.cpp new file mode 100644 index 00000000..f55b3005 --- /dev/null +++ b/src/drivers/QtTestDriver.cpp @@ -0,0 +1,33 @@ +#include "cucumber-cpp/internal/drivers/QtTestDriver.hpp" + +#include +#include +#include + +namespace cucumber { +namespace internal { + +const InvokeResult QtTestStep::invokeStepBody() { + QTemporaryFile file; + if (!file.open()) { + return InvokeResult::failure("Unable to open temporary file needed for this test"); + } + file.close(); + + QtTestObject testObject(this); + int returnValue = QTest::qExec(&testObject, + QStringList() << "test" + << "-o" + << file.fileName()); + if (returnValue == 0) { + return InvokeResult::success(); + } else { + file.open(); + QTextStream ts(&file); + return InvokeResult::failure(ts.readAll().toLocal8Bit()); + } +} + +} +} +