From f6f2ffde4e4c49834266210d8f9778660fca28f9 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Fri, 10 Sep 2021 11:12:20 +0200 Subject: AutoTest: Fix generated files for gtest When using gtest as test framework the wizard generated a header file containing the test code which had been included inside the main. As this works generally it is on the one hand not recommended and on the other hand it lets us fail when using cmake as build system. Create a cpp file instead and use this accordingly. Fixes: QTCREATORBUG-26253 Change-Id: Ibec8489258de0def27ee116eacbfc1bb22d3206b Reviewed-by: Eike Ziller --- .../templates/wizards/autotest/files/tst.pro | 6 ++---- .../templates/wizards/autotest/files/tst.qbs | 2 +- .../templates/wizards/autotest/files/tst.txt | 4 +++- .../templates/wizards/autotest/files/tst_main.cpp | 1 - .../templates/wizards/autotest/files/tst_src.h | 22 ---------------------- .../wizards/autotest/files/tst_src_gt.cpp | 13 +++++++++++++ .../templates/wizards/autotest/wizard.json | 8 ++++---- 7 files changed, 23 insertions(+), 33 deletions(-) delete mode 100644 share/qtcreator/templates/wizards/autotest/files/tst_src.h create mode 100644 share/qtcreator/templates/wizards/autotest/files/tst_src_gt.cpp diff --git a/share/qtcreator/templates/wizards/autotest/files/tst.pro b/share/qtcreator/templates/wizards/autotest/files/tst.pro index a92e58f06eb..87e6159c462 100644 --- a/share/qtcreator/templates/wizards/autotest/files/tst.pro +++ b/share/qtcreator/templates/wizards/autotest/files/tst.pro @@ -38,11 +38,9 @@ CONFIG -= app_bundle CONFIG += thread CONFIG -= qt -HEADERS += \\ - %{TestCaseFileWithHeaderSuffix} - SOURCES += \\ - %{MainCppName} + %{MainCppName} \ + %{TestCaseFileGTestWithCppSuffix} @endif @if "%{TestFrameWork}" == "BoostTest" TEMPLATE = app diff --git a/share/qtcreator/templates/wizards/autotest/files/tst.qbs b/share/qtcreator/templates/wizards/autotest/files/tst.qbs index a33cecd5990..26876461540 100644 --- a/share/qtcreator/templates/wizards/autotest/files/tst.qbs +++ b/share/qtcreator/templates/wizards/autotest/files/tst.qbs @@ -60,7 +60,7 @@ CppApplication { files: [ "%{MainCppName}", - "%{TestCaseFileWithHeaderSuffix}", + "%{TestCaseFileGTestWithCppSuffix}", ].concat(googleCommon.getGTestAll(qbs, googletestDir)) .concat(googleCommon.getGMockAll(qbs, googletestDir)) @endif diff --git a/share/qtcreator/templates/wizards/autotest/files/tst.txt b/share/qtcreator/templates/wizards/autotest/files/tst.txt index fe8e30e789d..516567cc633 100644 --- a/share/qtcreator/templates/wizards/autotest/files/tst.txt +++ b/share/qtcreator/templates/wizards/autotest/files/tst.txt @@ -57,6 +57,8 @@ target_link_libraries(%{TestCaseName} PRIVATE Qt${QT_VERSION_MAJOR}::QuickTest) @if "%{TestFrameWork}" == "GTest" @if "%{GTestCXX11}" == "true" +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) add_definitions(-DGTEST_LANGUAGE_CXX11) @endif @@ -92,7 +94,7 @@ endif () include_directories(${GTestIncludes}) -add_executable(%{TestCaseName} %{MainCppName} %{TestCaseFileWithHeaderSuffix} +add_executable(%{TestCaseName} %{MainCppName} %{TestCaseFileGTestWithCppSuffix} ${GTestFiles}) add_test(NAME %{TestCaseName} COMMAND %{TestCaseName}) target_link_libraries(%{TestCaseName} PRIVATE Threads::Threads) diff --git a/share/qtcreator/templates/wizards/autotest/files/tst_main.cpp b/share/qtcreator/templates/wizards/autotest/files/tst_main.cpp index e4f33d27164..2d12cccea15 100644 --- a/share/qtcreator/templates/wizards/autotest/files/tst_main.cpp +++ b/share/qtcreator/templates/wizards/autotest/files/tst_main.cpp @@ -5,7 +5,6 @@ QUICK_TEST_MAIN(example) @endif @if "%{TestFrameWork}" == "GTest" %{Cpp:LicenseTemplate}\ -#include "%{TestCaseFileWithHeaderSuffix}" #include diff --git a/share/qtcreator/templates/wizards/autotest/files/tst_src.h b/share/qtcreator/templates/wizards/autotest/files/tst_src.h deleted file mode 100644 index ef29240cd5e..00000000000 --- a/share/qtcreator/templates/wizards/autotest/files/tst_src.h +++ /dev/null @@ -1,22 +0,0 @@ -%{Cpp:LicenseTemplate}\ -@if '%{Cpp:PragmaOnce}' -#pragma once -@else -#ifndef %{GUARD} -#define %{GUARD} -@endif - -#include -#include - -using namespace testing; - -TEST(%{TestSuiteName}, %{TestCaseName}) -{ - EXPECT_EQ(1, 1); - ASSERT_THAT(0, Eq(0)); -} - -@if ! '%{Cpp:PragmaOnce}' -#endif // %{GUARD} -@endif diff --git a/share/qtcreator/templates/wizards/autotest/files/tst_src_gt.cpp b/share/qtcreator/templates/wizards/autotest/files/tst_src_gt.cpp new file mode 100644 index 00000000000..749ff06f0ac --- /dev/null +++ b/share/qtcreator/templates/wizards/autotest/files/tst_src_gt.cpp @@ -0,0 +1,13 @@ +%{Cpp:LicenseTemplate}\ + +#include +#include + +using namespace testing; + +TEST(%{TestSuiteName}, %{TestCaseName}) +{ + EXPECT_EQ(1, 1); + ASSERT_THAT(0, Eq(0)); +} + diff --git a/share/qtcreator/templates/wizards/autotest/wizard.json b/share/qtcreator/templates/wizards/autotest/wizard.json index 1ba0101a092..8f32cec145c 100644 --- a/share/qtcreator/templates/wizards/autotest/wizard.json +++ b/share/qtcreator/templates/wizards/autotest/wizard.json @@ -33,8 +33,8 @@ "value": "%{JS: 'main.' + Util.preferredSuffix('text/x-c++src') }" }, { - "key": "TestCaseFileWithHeaderSuffix", - "value": "%{JS: 'tst_' + value('TestCaseName').toLowerCase() + '.' + Util.preferredSuffix('text/x-c++hdr') }" + "key": "TestCaseFileGTestWithCppSuffix", + "value": "%{JS: 'tst_' + value('TestCaseName').toLowerCase() + '.' + Util.preferredSuffix('text/x-c++src') }" }, { "key": "GUARD", @@ -276,8 +276,8 @@ "openAsProject": true }, { - "source": "files/tst_src.h", - "target": "%{TestCaseFileWithHeaderSuffix}", + "source": "files/tst_src_gt.cpp", + "target": "%{TestCaseFileGTestWithCppSuffix}", "condition": "%{JS: value('TestFrameWork') == 'GTest'}", "openInEditor": true }, -- cgit v1.2.3