From f6f2ffde4e4c49834266210d8f9778660fca28f9 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Fri, 10 Sep 2021 11:12:20 +0200 Subject: [PATCH] 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 --- share/qtcreator/templates/wizards/autotest/files/tst.pro | 6 ++---- share/qtcreator/templates/wizards/autotest/files/tst.qbs | 2 +- share/qtcreator/templates/wizards/autotest/files/tst.txt | 4 +++- .../templates/wizards/autotest/files/tst_main.cpp | 1 - .../wizards/autotest/files/{tst_src.h => tst_src_gt.cpp} | 9 --------- share/qtcreator/templates/wizards/autotest/wizard.json | 8 ++++---- 6 files changed, 10 insertions(+), 20 deletions(-) rename share/qtcreator/templates/wizards/autotest/files/{tst_src.h => tst_src_gt.cpp} (60%) 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_gt.cpp similarity index 60% rename from share/qtcreator/templates/wizards/autotest/files/tst_src.h rename to share/qtcreator/templates/wizards/autotest/files/tst_src_gt.cpp index ef29240cd5e..749ff06f0ac 100644 --- a/share/qtcreator/templates/wizards/autotest/files/tst_src.h +++ b/share/qtcreator/templates/wizards/autotest/files/tst_src_gt.cpp @@ -1,10 +1,4 @@ %{Cpp:LicenseTemplate}\ -@if '%{Cpp:PragmaOnce}' -#pragma once -@else -#ifndef %{GUARD} -#define %{GUARD} -@endif #include #include @@ -17,6 +11,3 @@ TEST(%{TestSuiteName}, %{TestCaseName}) ASSERT_THAT(0, Eq(0)); } -@if ! '%{Cpp:PragmaOnce}' -#endif // %{GUARD} -@endif 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 },