From d254d115e1a39de01bbb6eb503db86c5e662bded Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Thu, 11 Aug 2022 12:12:28 -0600 Subject: [PATCH] Add tests to verify std::function works with capturing lambda Introduce a test that uses a capturing lambda as a custom_fake. Signed-off-by: Yuval Peress --- test/CMakeLists.txt | 16 ++++++++++ test/include/custom_function.hpp | 10 +++++++ test/include/fff_wrapper.hpp | 11 +++++++ .../src/fff_test_custom_function_template.cpp | 29 +++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 test/include/custom_function.hpp create mode 100644 test/include/fff_wrapper.hpp create mode 100644 test/src/fff_test_custom_function_template.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 995ba7d..7b41be3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -38,6 +38,22 @@ add_executable(cpp_global_test target_include_directories(cpp_global_test PRIVATE include) target_link_libraries(cpp_global_test PRIVATE gtest fff) +# Create the C++ custom function signature executable +add_executable(cpp_custom_fn_signature_test + src/fff_test_custom_function_template.cpp + ${COMMON_FILE_LIST} +) +target_include_directories(cpp_custom_fn_signature_test PRIVATE include) +target_link_libraries(cpp_custom_fn_signature_test PRIVATE gtest fff) +# Due to a bug in WinLibs for Windows it's not currently possible to use: +# target_precompile_headers(cpp_custom_fn_signature_test PUBLIC include/custom_function.hpp) +# See more info at target_precompile_headers(cpp_custom_fn_signature_test PUBLIC include/custom_function.hpp) + +add_test( + NAME cpp_custom_fn_signature_test + COMMAND $ + ) + # Add the tests for ctest add_test( NAME c_test diff --git a/test/include/custom_function.hpp b/test/include/custom_function.hpp new file mode 100644 index 0000000..d9f3934 --- /dev/null +++ b/test/include/custom_function.hpp @@ -0,0 +1,10 @@ +/* Copyright 2022 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +#define CUSTOM_FFF_FUNCTION_TEMPLATE(RETURN, FUNCNAME, ...) \ + std::function FUNCNAME diff --git a/test/include/fff_wrapper.hpp b/test/include/fff_wrapper.hpp new file mode 100644 index 0000000..e3c0cac --- /dev/null +++ b/test/include/fff_wrapper.hpp @@ -0,0 +1,11 @@ +/* Copyright 2022 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +#include "custom_function.hpp" + +#include \ No newline at end of file diff --git a/test/src/fff_test_custom_function_template.cpp b/test/src/fff_test_custom_function_template.cpp new file mode 100644 index 0000000..53689c3 --- /dev/null +++ b/test/src/fff_test_custom_function_template.cpp @@ -0,0 +1,29 @@ +/* Copyright 2022 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "fff_wrapper.hpp" + +DEFINE_FFF_GLOBALS + +FAKE_VOID_FUNC(do_stuff, int); + +class FFFCustomFakeSuite : public ::testing::Test { + public: + void SetUp() override { + RESET_FAKE(do_stuff); + FFF_RESET_HISTORY(); + } +}; + +TEST_F(FFFCustomFakeSuite, custom_cpp_fake_function) +{ + int x = 0; + do_stuff_fake.custom_fake = [&x](int i) { + x = i; + }; + do_stuff(5); + ASSERT_EQ(5, x); +}