Skip to content

Commit

Permalink
Add sample and ci
Browse files Browse the repository at this point in the history
  • Loading branch information
SergiusTheBest committed Aug 1, 2024
1 parent 657fdb3 commit ff1a251
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 0 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build

# Prerequisites
*.d

Expand Down
35 changes: 35 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

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()
37 changes: 37 additions & 0 deletions sample/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
175 changes: 175 additions & 0 deletions sample/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <gmock-global/gmock-global.h>

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));
}

0 comments on commit ff1a251

Please sign in to comment.