Skip to content

Commit

Permalink
[QtTestDriver] Qt test driver for cucumber-cpp.
Browse files Browse the repository at this point in the history
Implementation based on temporary file.
When building QtTest driver C++11 is enabled
globally to prevent issue cucumber#141.
  • Loading branch information
konserw committed Aug 25, 2017
1 parent 5f7ffe0 commit 2006dc4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions include/cucumber-cpp/internal/drivers/DriverSelector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
#include "GTestDriver.hpp"
#elif defined(BOOST_TEST_CASE)
#include "BoostDriver.hpp"
#elif defined(QTEST_H)
#include "QtTestDriver.hpp"
#endif
40 changes: 40 additions & 0 deletions include/cucumber-cpp/internal/drivers/QtTestDriver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef CUKE_QTTESTDRIVER_HPP_
#define CUKE_QTTESTDRIVER_HPP_

#include "../step/StepManager.hpp"
#include <QObject>

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_ */
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
33 changes: 33 additions & 0 deletions src/drivers/QtTestDriver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "cucumber-cpp/internal/drivers/QtTestDriver.hpp"

#include <QtTest/QtTest>
#include <QTextStream>
#include <QTemporaryFile>

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());
}
}

}
}

0 comments on commit 2006dc4

Please sign in to comment.