From 9f981e7e239c1b188b320017f064c633795714ae Mon Sep 17 00:00:00 2001 From: Jochen Seemann Date: Thu, 9 May 2019 19:36:49 +0200 Subject: [PATCH] AutoTest: Wizard: Add Support for Catch2 Done-with: Christian Stenger Change-Id: Idc59cf962e54845746a645efb5e931502deae38e Reviewed-by: David Schulz --- .../wizards/autotest/files/catch2_tst.cpp | 6 ++++ .../templates/wizards/autotest/files/tst.pro | 27 ++++++++++++++ .../templates/wizards/autotest/files/tst.qbs | 35 +++++++++++++++++++ .../templates/wizards/autotest/files/tst.txt | 24 +++++++++++++ .../wizards/autotest/files/tst_main.cpp | 17 +++++++++ .../templates/wizards/autotest/wizard.json | 31 +++++++++++++++- 6 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 share/qtcreator/templates/wizards/autotest/files/catch2_tst.cpp diff --git a/share/qtcreator/templates/wizards/autotest/files/catch2_tst.cpp b/share/qtcreator/templates/wizards/autotest/files/catch2_tst.cpp new file mode 100644 index 00000000000..604b339e1cc --- /dev/null +++ b/share/qtcreator/templates/wizards/autotest/files/catch2_tst.cpp @@ -0,0 +1,6 @@ +#include + +TEST_CASE("My first test with Catch2", "[fancy]") +{ + REQUIRE(0 == 0); +} diff --git a/share/qtcreator/templates/wizards/autotest/files/tst.pro b/share/qtcreator/templates/wizards/autotest/files/tst.pro index 1d59f9c05fc..a92e58f06eb 100644 --- a/share/qtcreator/templates/wizards/autotest/files/tst.pro +++ b/share/qtcreator/templates/wizards/autotest/files/tst.pro @@ -64,3 +64,30 @@ isEmpty(BOOST_INCLUDE_DIR): { SOURCES += \\ %{MainCppName} @endif +@if "%{TestFrameWork}" == "Catch2" +TEMPLATE = app +@if "%{Catch2NeedsQt}" == "true" +QT += gui +@else +CONFIG -= qt +CONFIG -= app_bundle +CONFIG += console +@endif + +CONFIG += c++11 + +isEmpty(CATCH_INCLUDE_DIR): CATCH_INCLUDE_DIR=$$(CATCH_INCLUDE_DIR) +@if "%{CatchIncDir}" != "" +# set by Qt Creator wizard +isEmpty(CATCH_INCLUDE_DIR): CATCH_INCLUDE_DIR="%{CatchIncDir}" +@endif +!isEmpty(CATCH_INCLUDE_DIR): INCLUDEPATH *= $${CATCH_INCLUDE_DIR} + +isEmpty(CATCH_INCLUDE_DIR): { + message("CATCH_INCLUDE_DIR is not set, assuming Catch2 can be found automatically in your system") +} + +SOURCES += \ + main.cpp \ + %{TestCaseFileWithCppSuffix} +@endif diff --git a/share/qtcreator/templates/wizards/autotest/files/tst.qbs b/share/qtcreator/templates/wizards/autotest/files/tst.qbs index e66605f0a5f..a33cecd5990 100644 --- a/share/qtcreator/templates/wizards/autotest/files/tst.qbs +++ b/share/qtcreator/templates/wizards/autotest/files/tst.qbs @@ -7,6 +7,10 @@ import "googlecommon.js" as googleCommon import qbs.Environment import qbs.File @endif +@if "%{TestFrameWork}" == "Catch2" +import qbs.Environment +import qbs.File +@endif CppApplication { @if "%{TestFrameWork}" == "QtTest" @@ -104,4 +108,35 @@ CppApplication { files: [ "%{MainCppName}" ] @endif +@if "%{TestFrameWork}" == "Catch2" + type: "application" + +@if "%{Catch2NeedsQt}" == "true" + Depends { name: "Qt.gui" } +@endif + + property string catchIncDir: { + if (typeof Environment.getEnv("CATCH_INCLUDE_DIR") !== 'undefined') + return Environment.getEnv("CATCH_INCLUDE_DIR"); + return "%{CatchIncDir}"; // set by Qt Creator wizard + } + + Properties { + condition: catchIncDir && File.exists(catchIncDir) + cpp.includePaths: [catchIncDir]; + } + + condition: { + if (!catchIncDir) + console.log("CATCH_INCLUDE_DIR is not set, assuming Catch2 can be " + + "found automatically in your system"); + return true; + } + + files: [ + "%{MainCppName}", + "%{TestCaseFileWithCppSuffix}", + ] +@endif + } diff --git a/share/qtcreator/templates/wizards/autotest/files/tst.txt b/share/qtcreator/templates/wizards/autotest/files/tst.txt index 93e51d3695e..569bf4d01d6 100644 --- a/share/qtcreator/templates/wizards/autotest/files/tst.txt +++ b/share/qtcreator/templates/wizards/autotest/files/tst.txt @@ -116,3 +116,27 @@ elseif (EXISTS ${BOOST_INCLUDE_DIR}) include_directories(${BOOST_INCLUDE_DIR}) endif () @endif +@if "%{TestFrameWork}" == "Catch2" +SET(CMAKE_CXX_STANDARD 11) + +@if "%{Catch2NeedsQt}" == "true" +find_package(Qt5Gui REQUIRED) +@endif + +add_executable(${PROJECT_NAME} %{TestCaseFileWithCppSuffix} main.cpp) + +@if "%{Catch2NeedsQt}" == "true" +target_link_libraries(%{TestCaseName} PRIVATE Qt5::Gui) +@endif + +if (DEFINED ENV{CATCH_INCLUDE_DIR}) + set(CATCH_INCLUDE_DIR $ENV{CATCH_INCLUDE_DIR}) +else () + set(CATCH_INCLUDE_DIR "%{CatchIncDir}") # set by Qt Creator wizard +endif () +if (CATCH_INCLUDE_DIR STREQUAL "") + message("CATCH_INCLUDE_DIR is not set, assuming Catch2 can be found automatically in your system") +elseif (EXISTS ${CATCH_INCLUDE_DIR}) + include_directories(${CATCH_INCLUDE_DIR}) +endif () +@endif diff --git a/share/qtcreator/templates/wizards/autotest/files/tst_main.cpp b/share/qtcreator/templates/wizards/autotest/files/tst_main.cpp index 923fad9f9ba..e4f33d27164 100644 --- a/share/qtcreator/templates/wizards/autotest/files/tst_main.cpp +++ b/share/qtcreator/templates/wizards/autotest/files/tst_main.cpp @@ -24,3 +24,20 @@ BOOST_AUTO_TEST_CASE( %{TestCaseName} ) BOOST_TEST( true /* test assertion */ ); } @endif +@if "%{TestFrameWork}" == "Catch2" +@if "%{Catch2NeedsQt}" == "true" +#define CATCH_CONFIG_RUNNER +@else +#define CATCH_CONFIG_MAIN +@endif +#include +@if "%{Catch2NeedsQt}" == "true" +#include + +int main(int argc, char** argv) +{ + QGuiApplication app(argc, argv); + return Catch::Session().run(argc, argv); +} +@endif +@endif diff --git a/share/qtcreator/templates/wizards/autotest/wizard.json b/share/qtcreator/templates/wizards/autotest/wizard.json index e35f9cd9805..1ba0101a092 100644 --- a/share/qtcreator/templates/wizards/autotest/wizard.json +++ b/share/qtcreator/templates/wizards/autotest/wizard.json @@ -91,6 +91,10 @@ { "trKey": "Boost Test", "value": "BoostTest" + }, + { + "trKey": "Catch2", + "value": "Catch2" } ] @@ -167,6 +171,25 @@ "kind": "existingDirectory" } }, + { + "name": "CatchIncDir", + "trDisplayName": "Catch2 include directory (optional):", + "visible": "%{JS: value('TestFrameWork') === 'Catch2'}", + "mandatory": false, + "type": "PathChooser", + "data": { + "kind": "existingDirectory" + } + }, + { + "name": "Catch2NeedsQt", + "trDisplayName": "Use Qt libraries", + "visible": "%{JS: '%{TestFrameWork}' === 'Catch2'}", + "type": "CheckBox", + "data": { + "checked": true + } + }, { "name": "BuildSystem", "trDisplayName": "Build system:", @@ -267,7 +290,7 @@ { "source": "files/tst_main.cpp", "target": "%{MainCppName}", - "condition": "%{JS: ['GTest', 'QtQuickTest', 'BoostTest'].indexOf(value('TestFrameWork')) >= 0}", + "condition": "%{JS: ['GTest', 'QtQuickTest', 'BoostTest', 'Catch2'].indexOf(value('TestFrameWork')) >= 0}", "openInEditor": true }, { @@ -276,6 +299,12 @@ "condition": "%{JS: value('TestFrameWork') === 'QtQuickTest'}", "openInEditor": true }, + { + "source": "files/catch2_tst.cpp", + "target": "%{TestCaseFileWithCppSuffix}", + "condition": "%{JS: '%{TestFrameWork}' === 'Catch2'}", + "openInEditor": true + }, { "source": "../projects/git.ignore", "target": ".gitignore",