From ff1a251f6a2d0f8574f8dcc4af1d4d43acf0caa8 Mon Sep 17 00:00:00 2001 From: Sergey Podobry Date: Thu, 1 Aug 2024 23:17:04 +0300 Subject: [PATCH] Add sample and ci --- .github/workflows/ci.yml | 61 ++++++++++++++ .gitignore | 2 + CMakeLists.txt | 35 ++++++++ sample/CMakeLists.txt | 37 +++++++++ sample/main.cpp | 175 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 310 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 CMakeLists.txt create mode 100644 sample/CMakeLists.txt create mode 100644 sample/main.cpp diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..f53379e --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,61 @@ +name: CI + +on: + push: + paths-ignore: + - LICENSE + - README.md + pull_request: + paths-ignore: + - LICENSE + - README.md + +permissions: + contents: read + +jobs: + build: + name: ${{ matrix.cxx }}, ${{ matrix.os }} + + strategy: + fail-fast: true + matrix: + include: [ + # You can access the following values via ${{ matrix.??? }} + # + # cxx : C++ compiler executable + # os : GitHub Actions YAML workflow label. See https://github.com/actions/virtual-environments#available-environments + + # linux: gcc + { os: ubuntu-22.04, cxx: g++-11, }, # (default on Jammy 22.04) + # linux: clang + { os: ubuntu-22.04, cxx: clang++-14 }, + # windows: msvc + { os: windows-2022, cxx: 'vs2022' }, + ] + + runs-on: ${{ matrix.os }} + + env: + CXX: ${{ matrix.cxx }} + + steps: + - name: checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: build with gtest:release-1.8.1 + run: | + cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -DGMOCK_GLOBAL_GTEST_GIT_TAG=release-1.8.1 . + cmake --build build --parallel + + - name: build with gtest:release-1.11.0 + run: | + cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -DGMOCK_GLOBAL_GTEST_GIT_TAG=release-1.11.0 . + cmake --build build --parallel + + - name: build with gtest:v1.15.2 + run: | + cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -DGMOCK_GLOBAL_GTEST_GIT_TAG=v1.15.2 . + cmake --build build --parallel diff --git a/.gitignore b/.gitignore index 94a6b07..d78b236 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +build + # Prerequisites *.d diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a687d08 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,35 @@ +cmake_minimum_required(VERSION 3.6) + +project(gmock-global LANGUAGES CXX) + +# check if building as a stand-alone project +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + set(IS_TOPLEVEL_PROJECT TRUE) +else() + set(IS_TOPLEVEL_PROJECT FALSE) +endif() + +# options +option(GMOCK_GLOBAL_BUILD_SAMPLES "Build ${PROJECT_NAME} samples" ${IS_TOPLEVEL_PROJECT}) + +# make sure install paths work on all platforms +include(GNUInstallDirs) + +add_library(${PROJECT_NAME} INTERFACE) +add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) + +target_include_directories(${PROJECT_NAME} + INTERFACE + $ + $ +) + +if(GMOCK_GLOBAL_BUILD_SAMPLES) + # add a pseudo-project to make plog headers visible in IDE + file(GLOB_RECURSE ${PROJECT_NAME}_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h) + add_custom_target(${PROJECT_NAME}-headers SOURCES ${${PROJECT_NAME}_HEADERS}) + set_target_properties(${PROJECT_NAME}-headers PROPERTIES FOLDER Include) + + # add samples + add_subdirectory(sample) +endif() \ No newline at end of file diff --git a/sample/CMakeLists.txt b/sample/CMakeLists.txt new file mode 100644 index 0000000..f430c4f --- /dev/null +++ b/sample/CMakeLists.txt @@ -0,0 +1,37 @@ +#################################################### +# Reference values for GMOCK_GLOBAL_GTEST_GIT_TAG: +# +# release-1.8.1 +# release-1.10.0 +# release-1.11.0 +# release-1.12.0 +# release-1.12.1 +# v1.13.0 +# v1.14.0 +# v1.15.0 +# v1.15.1 +# v1.15.2 +# +#################################################### + +if(NOT DEFINED GMOCK_GLOBAL_GTEST_GIT_TAG) + set(GMOCK_GLOBAL_GTEST_GIT_TAG "release-1.11.0") +endif() + +include(FetchContent) + +FetchContent_Declare( + googletest + GIT_REPOSITORY "https://github.com/google/googletest.git" + GIT_TAG ${GMOCK_GLOBAL_GTEST_GIT_TAG} +) + +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + +FetchContent_MakeAvailable(googletest) + +target_compile_options(gtest PUBLIC -Wno-maybe-uninitialized) + +add_executable(sample main.cpp) +target_link_libraries(sample gmock-global::gmock-global gmock_main) \ No newline at end of file diff --git a/sample/main.cpp b/sample/main.cpp new file mode 100644 index 0000000..96a9bce --- /dev/null +++ b/sample/main.cpp @@ -0,0 +1,175 @@ +#include +#include +#include + +using namespace testing; + +// +// Mocks for test sum functions +// + +#if 0 // skip 11-15 args for now +MOCK_GLOBAL_FUNC15(sum15, int(int, int, int, int, int, int, int, int, int, int, int, int, int, int, int)); + +MOCK_GLOBAL_FUNC14(sum14, int(int, int, int, int, int, int, int, int, int, int, int, int, int, int)); + +MOCK_GLOBAL_FUNC13(sum13, int(int, int, int, int, int, int, int, int, int, int, int, int, int)); + +MOCK_GLOBAL_FUNC12(sum12, int(int, int, int, int, int, int, int, int, int, int, int, int)); + +MOCK_GLOBAL_FUNC11(sum11, int(int, int, int, int, int, int, int, int, int, int, int)); +#endif + +MOCK_GLOBAL_FUNC10(sum10, int(int, int, int, int, int, int, int, int, int, int)); + +MOCK_GLOBAL_FUNC9(sum9, int(int, int, int, int, int, int, int, int, int)); + +MOCK_GLOBAL_FUNC8(sum8, int(int, int, int, int, int, int, int, int)); + +MOCK_GLOBAL_FUNC7(sum7, int(int, int, int, int, int, int, int)); + +MOCK_GLOBAL_FUNC6(sum6, int(int, int, int, int, int, int)); + +MOCK_GLOBAL_FUNC5(sum5, int(int, int, int, int, int)); + +MOCK_GLOBAL_FUNC4(sum4, int(int, int, int, int)); + +MOCK_GLOBAL_FUNC3(sum3, int(int, int, int)); + +MOCK_GLOBAL_FUNC2(sum2, int(int, int)); + +MOCK_GLOBAL_FUNC1(sum1, int(int)); + +MOCK_GLOBAL_FUNC0(sum0, int(void)); + + +// +// Test for mocks +// +TEST(SeveralSumsInOne, CanSumSeveral) +{ + using ::testing::AtLeast; + EXPECT_GLOBAL_CALL(sum2, sum2(1, 2)).Times(1); + sum2(1, 2); + EXPECT_GLOBAL_CALL(sum2, sum2(1, 2)).Times(AtLeast(1)); + sum2(1, 2); + + EXPECT_GLOBAL_CALL(sum3, sum3(1, 2, 3)).Times(2); + sum3(1, 2, 3); + sum3(1, 2, 3); +} + +#if 0 // skip 11-15 args for now +TEST(SummizerTestGlobal14, CanSumGlobal15) +{ + EXPECT_GLOBAL_CALL(sum15, sum15(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)).Times(1); + sum14(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); +} + +TEST(SummizerTestGlobal14, CanSumGlobal14) +{ + EXPECT_GLOBAL_CALL(sum14, sum14(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)).Times(1); + sum14(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14); +} + +TEST(SummizerTestGlobal13, CanSumGlobal13) +{ + EXPECT_GLOBAL_CALL(sum13, sum13(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)).Times(1); + sum13(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13); +} + +TEST(SummizerTestGlobal12, CanSumGlobal12) +{ + EXPECT_GLOBAL_CALL(sum12, sum12(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)).Times(1); + sum12(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); +} + +TEST(SummizerTestGlobal11, CanSumGlobal11) +{ + EXPECT_GLOBAL_CALL(sum11, sum11(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)).Times(1); + sum11(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); +} +#endif + +TEST(SummizerTestGlobal10, CanSumGlobal10) +{ + EXPECT_GLOBAL_CALL(sum10, sum10(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)).Times(1); + sum10(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +} + +TEST(SummizerTestGlobal9, CanSumGlobal9) +{ + EXPECT_GLOBAL_CALL(sum9, sum9(1, 2, 3, 4, 5, 6, 7, 8, 9)).Times(1); + sum9(1, 2, 3, 4, 5, 6, 7, 8, 9); +} + +TEST(SummizerTestGlobal8, CanSumGlobal8) +{ + EXPECT_GLOBAL_CALL(sum8, sum8(1, 2, 3, 4, 5, 6, 7, 8)).Times(1); + sum8(1, 2, 3, 4, 5, 6, 7, 8); +} + +TEST(SummizerTestGlobal7, CanSumGlobal7) +{ + EXPECT_GLOBAL_CALL(sum7, sum7(1, 2, 3, 4, 5, 6, 7)).Times(1); + sum7(1, 2, 3, 4, 5, 6, 7); +} + +TEST(SummizerTestGlobal6, CanSumGlobal6) +{ + EXPECT_GLOBAL_CALL(sum6, sum6(1, 2, 3, 4, 5, 6)).Times(1); + sum6(1, 2, 3, 4, 5, 6); +} + +TEST(SummizerTestGlobal5, CanSumGlobal5) +{ + EXPECT_GLOBAL_CALL(sum5, sum5(1, 2, 3, 4, 5)).Times(1); + sum5(1, 2, 3, 4, 5); +} + +TEST(SummizerTestGlobal4, CanSumGlobal4) +{ + EXPECT_GLOBAL_CALL(sum4, sum4(1, 2, 3, 4)).Times(1); + sum4(1, 2, 3, 4); +} + +TEST(SummizerTestGlobal3, CanSumGlobal3) +{ + EXPECT_GLOBAL_CALL(sum3, sum3(1, 2, 3)).Times(1); + sum3(1, 2, 3); +} + +TEST(SummizerTestGlobal2, CanSumGlobal2) +{ + EXPECT_GLOBAL_CALL(sum2, sum2(1, 2)).Times(1); + EXPECT_GLOBAL_CALL(sum2, sum2(3, 4)).Times(1); + sum2(1, 2); + sum2(3, 4); +} + +TEST(SummizerTestGlobal1, CanSumGlobal1) +{ + EXPECT_GLOBAL_CALL(sum1, sum1(1)).Times(1); + EXPECT_GLOBAL_CALL(sum1, sum1(2)).Times(1); + sum1(1); + sum1(2); +} + +TEST(SummizerTestGlobal0, CanSumGlobal0) +{ + EXPECT_GLOBAL_CALL(sum0, sum0()).Times(1); + sum0(); +} + +TEST(SummizerTestGlobal1, CanSumGlobal1WithOnCall) +{ + ON_GLOBAL_NICE_CALL(sum1, sum1(_)).WillByDefault(Return(42)); + + ASSERT_EQ(sum1(0), 42); + ASSERT_EQ(sum1(1), 42); +} + +TEST(SummizerTestGlobal1, ExceptionOnCallWithoutExpect) +{ + ASSERT_ANY_THROW(sum1(0)); +} \ No newline at end of file