From 17e8e7e1d489d84f2b59e88f9651d1c1248016bf Mon Sep 17 00:00:00 2001 From: Mate Pek Date: Tue, 26 Apr 2022 19:34:40 +0700 Subject: [PATCH] master: gunit basics --- CHANGELOG.md | 6 +++ src/framework/GoogleTestExecutable.ts | 42 ++++++++++++------ src/framework/GoogleTestTest.ts | 2 +- test/cpp/CMakeLists.txt | 1 + test/cpp/gtest/GoogleTest.cmake | 62 +++++++++++++-------------- test/cpp/gtest/gtest2.cpp | 2 +- test/cpp/gunit/CMakeLists.txt | 3 ++ test/cpp/gunit/GUnit.cmake | 52 ++++++++++++++++++++++ test/cpp/gunit/gunit1.cpp | 12 ++++++ 9 files changed, 136 insertions(+), 46 deletions(-) create mode 100644 test/cpp/gunit/CMakeLists.txt create mode 100644 test/cpp/gunit/GUnit.cmake create mode 100644 test/cpp/gunit/gunit1.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 73e205f4..1f58e831 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] +## [4.1.1] + +### Added + +- experimental basic support for GUnit framework. + ## [4.1.0] - 2022-04-26 ### Added diff --git a/src/framework/GoogleTestExecutable.ts b/src/framework/GoogleTestExecutable.ts index 3b453811..a30c7dc6 100644 --- a/src/framework/GoogleTestExecutable.ts +++ b/src/framework/GoogleTestExecutable.ts @@ -109,17 +109,33 @@ export class GoogleTestExecutable extends AbstractExecutable { valueParam: string | undefined, ): Promise => { const resolvedFile = this.findSourceFilePath(file); - return this._createTreeAndAddTest( - this.getTestGrouping(), - testName, - resolvedFile, - line, - [suiteName], - undefined, - (parent: TestItemParent) => - new GoogleTestTest(this, parent, testName, suiteName, typeParam, valueParam, resolvedFile, line), - (test: GoogleTestTest) => test.update2(testName, suiteName, resolvedFile, line, typeParam, valueParam), - ); + const id = suiteName + '.' + testName; + // gunit + if (testName === '') { + return this._createTreeAndAddTest( + this.getTestGrouping(), + '', + resolvedFile, + line, + [suiteName], + undefined, + (parent: TestItemParent) => + new GoogleTestTest(this, parent, id, '', suiteName, typeParam, valueParam, resolvedFile, line), + (test: GoogleTestTest) => test.update2('', suiteName, resolvedFile, line, typeParam, valueParam), + ); + } else { + return this._createTreeAndAddTest( + this.getTestGrouping(), + testName, + resolvedFile, + line, + [suiteName], + undefined, + (parent: TestItemParent) => + new GoogleTestTest(this, parent, id, testName, suiteName, typeParam, valueParam, resolvedFile, line), + (test: GoogleTestTest) => test.update2(testName, suiteName, resolvedFile, line, typeParam, valueParam), + ); + } }; protected async _reloadChildren(cancellationToken: CancellationToken): Promise { @@ -314,7 +330,7 @@ class TestSuiteListingProcessor implements XmlTagProcessor { switch (tag.name) { case 'testcase': { assert(this.suiteName); - assert(tag.attribs.name); + assert(typeof tag.attribs.name == 'string'); // for gunit it can be empty await this.create( tag.attribs.name, this.suiteName!, @@ -334,7 +350,7 @@ class TestSuiteListingProcessor implements XmlTagProcessor { /// // Remark: not necessarily starts like this so do not use: ^ -const testBeginRe = /\[ RUN \] ((.+)\.(.+))$/m; +const testBeginRe = /\[ RUN \] ((.+)\.(.*))$/m; // Ex: "Is True[ OK ] TestCas1.test5 (0 ms)" // m[1] == '[ ' // m[2] == 'OK' diff --git a/src/framework/GoogleTestTest.ts b/src/framework/GoogleTestTest.ts index e6015258..4adbeffa 100644 --- a/src/framework/GoogleTestTest.ts +++ b/src/framework/GoogleTestTest.ts @@ -7,6 +7,7 @@ export class GoogleTestTest extends AbstractTest { constructor( executable: AbstractExecutable, parent: TestItemParent, + testNameAsId: string, testName: string, suiteName: string, typeParam: string | undefined, @@ -14,7 +15,6 @@ export class GoogleTestTest extends AbstractTest { file: string | undefined, line: string | undefined, ) { - const testNameAsId = suiteName + '.' + testName; super( executable, parent, diff --git a/test/cpp/CMakeLists.txt b/test/cpp/CMakeLists.txt index 8e318180..d2c76cc1 100644 --- a/test/cpp/CMakeLists.txt +++ b/test/cpp/CMakeLists.txt @@ -30,6 +30,7 @@ add_custom_target( # add_subdirectory("catch2") +add_subdirectory("gunit") add_subdirectory("gtest") add_subdirectory("doctest") add_subdirectory("gbenchmark") diff --git a/test/cpp/gtest/GoogleTest.cmake b/test/cpp/gtest/GoogleTest.cmake index 6db89c5e..6f974888 100644 --- a/test/cpp/gtest/GoogleTest.cmake +++ b/test/cpp/gtest/GoogleTest.cmake @@ -1,34 +1,34 @@ -include(FetchContent) - -# Prevent overriding the parent project's compiler/linker -# settings on Windows -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) - -FetchContent_Declare(googletest - GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG release-1.10.0) - -FetchContent_GetProperties(googletest) -if(NOT googletest_POPULATED) - FetchContent_Populate(googletest) - add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} - EXCLUDE_FROM_ALL) -endif() - -mark_as_advanced(BUILD_GMOCK - BUILD_GTEST - BUILD_SHARED_LIBS - gmock_build_tests - gtest_build_samples - gtest_build_tests - gtest_disable_pthreads - gtest_force_shared_crt - gtest_hide_internal_symbols) - -set_target_properties(gtest PROPERTIES FOLDER extern) -set_target_properties(gtest_main PROPERTIES FOLDER extern) -set_target_properties(gmock PROPERTIES FOLDER extern) -set_target_properties(gmock_main PROPERTIES FOLDER extern) +# include(FetchContent) + +# # Prevent overriding the parent project's compiler/linker +# # settings on Windows +# set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + +# FetchContent_Declare(googletest +# GIT_REPOSITORY https://github.com/google/googletest.git +# GIT_TAG release-1.10.0) + +# FetchContent_GetProperties(googletest) +# if(NOT googletest_POPULATED) +# FetchContent_Populate(googletest) +# add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} +# EXCLUDE_FROM_ALL) +# endif() + +# mark_as_advanced(BUILD_GMOCK +# BUILD_GTEST +# BUILD_SHARED_LIBS +# gmock_build_tests +# gtest_build_samples +# gtest_build_tests +# gtest_disable_pthreads +# gtest_force_shared_crt +# gtest_hide_internal_symbols) + +# set_target_properties(gtest PROPERTIES FOLDER extern) +# set_target_properties(gtest_main PROPERTIES FOLDER extern) +# set_target_properties(gmock PROPERTIES FOLDER extern) +# set_target_properties(gmock_main PROPERTIES FOLDER extern) # diff --git a/test/cpp/gtest/gtest2.cpp b/test/cpp/gtest/gtest2.cpp index 9140b131..ac8f3fd8 100644 --- a/test/cpp/gtest/gtest2.cpp +++ b/test/cpp/gtest/gtest2.cpp @@ -47,7 +47,7 @@ GTEST_TEST(TestCas3, test5) { GTEST_TEST(TestCas3, test6) { std::vector v({5, 10}); - ASSERT_THAT(v, ::testing::ElementsAre(5, 10, 15)); + ASSERT_THAT(v, ::testing::ElementsAre(5, 10, 15)) << "extra message"; } // with custom matcher diff --git a/test/cpp/gunit/CMakeLists.txt b/test/cpp/gunit/CMakeLists.txt new file mode 100644 index 00000000..5bccdd5b --- /dev/null +++ b/test/cpp/gunit/CMakeLists.txt @@ -0,0 +1,3 @@ +include("GUnit.cmake") + +add_gunit_with_main(gunit1 "gunit1.cpp") \ No newline at end of file diff --git a/test/cpp/gunit/GUnit.cmake b/test/cpp/gunit/GUnit.cmake new file mode 100644 index 00000000..d510f625 --- /dev/null +++ b/test/cpp/gunit/GUnit.cmake @@ -0,0 +1,52 @@ +include(FetchContent) + +# Prevent overriding the parent project's compiler/linker +# settings on Windows +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + +FetchContent_Declare(gunit + GIT_REPOSITORY https://github.com/cpp-testing/GUnit.git + GIT_TAG v1.11.0) + +FetchContent_GetProperties(gunit) +if(NOT gunit_POPULATED) + FetchContent_Populate(gunit) + add_subdirectory(${gunit_SOURCE_DIR} ${gunit_BINARY_DIR} + EXCLUDE_FROM_ALL) +endif() + +mark_as_advanced(BUILD_GMOCK + BUILD_GTEST + BUILD_SHARED_LIBS + gmock_build_tests + gtest_build_samples + gtest_build_tests + gtest_disable_pthreads + gtest_force_shared_crt + gtest_hide_internal_symbols) + +set_target_properties(gtest PROPERTIES FOLDER extern) +set_target_properties(gtest_main PROPERTIES FOLDER extern) +set_target_properties(gmock PROPERTIES FOLDER extern) +set_target_properties(gmock_main PROPERTIES FOLDER extern) + +# + +add_library(ThirdParty.GUnit INTERFACE) + +target_link_libraries(ThirdParty.GUnit + INTERFACE gunit) + +target_compile_features(ThirdParty.GUnit INTERFACE cxx_std_14) + +target_include_directories( + ThirdParty.GUnit + INTERFACE "${gunit_SOURCE_DIR}/include") + +# + +function(add_gunit_with_main target cpp_file) + add_executable(${target} "${cpp_file}") + target_link_libraries(${target} PUBLIC ThirdParty.GUnit) +endfunction() + diff --git a/test/cpp/gunit/gunit1.cpp b/test/cpp/gunit/gunit1.cpp new file mode 100644 index 00000000..ff53d4cb --- /dev/null +++ b/test/cpp/gunit/gunit1.cpp @@ -0,0 +1,12 @@ +#include + +GTEST("Calc Test") { + // SetUp + SHOULD("return succ") { + EXPECT(5 == 5); + } + SHOULD("return fail") { + EXPECT(5 == 6); + } + // TearDown +} \ No newline at end of file