### CMake: Install Rules for Google Mock Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/CMakeLists.txt This snippet defines the installation rules for the Google Mock targets and headers. It specifies that the `gmock` and `gmock_main` libraries should be installed in the `lib` directory, and the `gmock` header files should be installed in the `include` directory relative to the installation prefix. ```cmake # Install rules install(TARGETS gmock gmock_main DESTINATION lib) install(DIRECTORY ${gmock_SOURCE_DIR}/include/gmock DESTINATION include) ``` -------------------------------- ### Basic Google Mock Test Example in C++ Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/docs/v1_6/ForDummies.md A C++ example demonstrating how to use Google Mock to set expectations on a mock object and exercise code that uses it. It includes initializing Google Mock and running tests. ```cpp #include "path/to/mock-turtle.h" #include "gmock/gmock.h" #include "gtest/gtest.h" using ::testing::AtLeast; TEST(PainterTest, CanDrawSomething) { MockTurtle turtle; EXPECT_CALL(turtle, PenDown()) .Times(AtLeast(1)); Painter painter(&turtle); EXPECT_TRUE(painter.DrawCircle(0, 0, 10)); } int main(int argc, char** argv) { ::testing::InitGoogleMock(&argc, argv); return RUN_ALL_TESTS(); } ``` -------------------------------- ### Install and Use gmock_doctor.py Script in Bash Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/docs/FrequentlyAskedQuestions.md This example shows how to set up and use the `gmock_doctor.py` script, a utility for diagnosing GCC compiler errors related to Google Mock. It involves creating a shell alias and piping build command output to the script for analysis. ```bash alias gmd='/scripts/gmock_doctor.py' 2>&1 | gmd Or you can run `gmd` and copy-n-paste gcc's error messages to it. ``` -------------------------------- ### Global Test Environment Setup and Teardown in C++ with Google Test Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/docs/V1_5_AdvancedGuide.md Illustrates how to implement global setup and teardown logic for all tests in a program using Google Test. This involves subclassing the 'Environment' class and registering an instance using 'AddGlobalTestEnvironment'. ```cpp class Environment { public: virtual ~Environment() {} // Override this to define how to set up the environment. virtual void SetUp() {} // Override this to define how to tear down the environment. virtual void TearDown() {} }; Environment* AddGlobalTestEnvironment(Environment* env); // Example of registering an environment with a global variable (not recommended) // ::testing::Environment* const foo_env = ::testing::AddGlobalTestEnvironment(new FooEnvironment); // Recommended approach: Call AddGlobalTestEnvironment in main() int main(int argc, char **argv) { ::testing::AddGlobalTestEnvironment(new FooEnvironment); ::testing::InitGoogleMock(&argc, argv); return RUN_ALL_TESTS(); } ``` -------------------------------- ### Google Mock Test Example: Setting Expectations and Using Mocks Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/docs/v1_6/CheatSheet.md Provides a practical example of using Google Mock in a test case. It covers importing necessary names, creating mock objects, setting default actions using `ON_CALL`, defining expectations using `EXPECT_CALL`, and exercising the code under test. ```cpp using ::testing::Return; TEST(BarTest, DoesThis) { MockFoo foo; ON_CALL(foo, GetSize()) .WillByDefault(Return(1)); // ... other default actions ... EXPECT_CALL(foo, Describe(5)) .Times(3) .WillRepeatedly(Return("Category 5")); // ... other expectations ... EXPECT_EQ("good", MyProductionFunction(&foo)); } ``` -------------------------------- ### Global Test Environment Setup and Teardown in Google Test (C++) Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/docs/V1_7_AdvancedGuide.md Illustrates how to implement global setup and teardown routines for a Google Test suite by subclassing the `Environment` class and registering an instance using `AddGlobalTestEnvironment`. This is useful for initializing and cleaning up resources used across all tests in a program. ```cpp class Environment { public: virtual ~Environment() {} // Override this to define how to set up the environment. virtual void SetUp() {} // Override this to define how to tear down the environment. virtual void TearDown() {} }; Environment* AddGlobalTestEnvironment(Environment* env); ``` ```cpp ::testing::Environment* const foo_env = ::testing::AddGlobalTestEnvironment(new FooEnvironment); ``` -------------------------------- ### Google Mock: Sticky Expectations with Multiple Calls Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/docs/ForDummies.md This example illustrates the default 'sticky' behavior of Google Mock expectations. It sets up an expectation for turtle.GetX() within a loop. Due to the sticky nature, the last expectation is repeatedly matched, leading to an 'upper bound exceeded' error if not handled properly. This code is presented as an example of what *not* to do without considering sticky expectations. ```cpp using ::testing::Return; ... for (int i = n; i > 0; i--) { EXPECT_CALL(turtle, GetX()) .WillOnce(Return(10*i)); } ``` -------------------------------- ### Install and Use gmock_doctor Script for GCC Errors Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/docs/v1_5/FrequentlyAskedQuestions.md This section details how to install and use the `gmock_doctor.py` script to diagnose GCC compiler errors related to Google Mock. It provides an alias command for installation and examples of its usage with build commands. ```bash alias gmd='/scripts/gmock_doctor.py' 2>&1 | gmd make my_test 2>&1 | gmd ``` -------------------------------- ### Prepare Autotools Build System (Unix) Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/README.md Commands to prepare the Autotools build system for Google Mock on Unix-like systems. This involves navigating to the googlemock directory and running autoreconf. ```bash cd googlemock autoreconf -fvi ``` -------------------------------- ### Google Mock: Implementing ActionInterface for Custom Actions Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/docs/FrequentlyAskedQuestions.md Illustrates how to implement the `ActionInterface` for more precise control over custom actions in Google Mock. This approach is suitable when actions need to be reusable across different function types or require specific type constraints. The `Return()` action in `gmock-actions.h` serves as an example. ```cpp // Example of implementing ActionInterface // class MyAction : public ::testing::ActionInterface { // public: // void operator()(int value) const override { // // Action logic here // } // }; // auto MyActionFunc() { return ::testing::MakeAction(MyAction()); } ``` -------------------------------- ### Basic Disruptor Usage Example Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/README.md A comprehensive C++ example demonstrating the setup and usage of the Disruptor-cpp library. It includes event factory, task scheduler, event handler instantiation, publishing events, and graceful shutdown. ```Cpp auto const ExpectedNumberOfEvents = 10000; auto const RingBufferSize = 1024; // Instantiate and start the disruptor auto eventFactory = []() { return LongEvent(); }; auto taskScheduler = std::make_shared< Disruptor::ThreadPerTaskScheduler >(); auto disruptor = std::make_shared< Disruptor::disruptor >(eventFactory, RingBufferSize, taskScheduler); auto printingEventHandler = std::make_shared< PrintingEventHandler >(ExpectedNumberOfEvents); disruptor->handleEventsWith(printingEventHandler); taskScheduler->start(); disruptor->start(); // Publish events auto ringBuffer = disruptor->ringBuffer(); for (auto i = 0; inext(); (*ringBuffer)[nextSequence].value = i; ringBuffer->publish(nextSequence); } // Wait for the end of execution and shutdown printingEventHandler->waitEndOfProcessing(); disruptor->shutdown(); taskScheduler->stop(); ``` -------------------------------- ### Build Google Mock with MSBuild (Windows) Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/README.md Instructions to build Google Mock and its tests on Windows using MSVC 2005 or 2010 projects. It involves navigating to the respective directory and running msbuild. ```bash msbuild gmock.sln ``` -------------------------------- ### Google Test Assertion Example with Mixed Frameworks (C++) Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/docs/V1_7_AdvancedGuide.md Demonstrates using a Google Test assertion (EXPECT_LE) alongside a native assertion from another framework (CPPUNIT_ASSERT). This example assumes the setup for integration is already in place. ```c++ void TestFooDoesBar() { Foo foo; EXPECT_LE(foo.Bar(1), 100); // A Google Test assertion. CPPUNIT_ASSERT(foo.IsEmpty()); // A native assertion. } ``` -------------------------------- ### Build Google Mock with Make (Unix) Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/README.md Builds the Google Mock library and a sample test using the provided Makefile. Assumes GNU make is available and can be used as a starting point for custom build scripts. ```bash cd ${GMOCK_DIR}/make make ./gmock_test ``` -------------------------------- ### Manually Print Values Using Google Test's PrintToString Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/docs/AdvancedGuide.md This example shows how to use Google Test's `PrintToString` function to get a string representation of a value, which can then be used in custom assertion messages. This is useful for debugging complex data structures within test failures. ```cpp vector > bar_ints = GetBarIntVector(); EXPECT_TRUE(IsCorrectBarIntVector(bar_ints)) << "bar_ints = " << ::testing::PrintToString(bar_ints); ``` -------------------------------- ### Visual Studio 2008: Configure for 64-bit Binaries on Windows Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/docs/FAQ.md Steps to set up Visual Studio 2008 to build 64-bit binaries. This involves creating a new solution platform (x64) and adjusting intermediate directory settings to prevent file overwrites. ```csharp // Load solution: msvc\gtest-md.sln or msvc\gtest.sln // Migrate via wizard to VS 2008. // Build -> Configuration Manager... // Active solution platform: // New platform: x64 // Copy settings from: Win32 // Create new project platforms: checked // OK // To prevent build output overwrites: // Solution Explorer: Multi-select all projects (shift-click). // Right-click -> Properties. // Left pane: Configuration Properties. // Configuration dropdown: All Configurations. // Platform: x64. // Intermediate Directory: Change from "$(PlatformName)\$(ConfigurationName)" to "$(OutDir)\$(ProjectName)". // OK, then build solution. ``` -------------------------------- ### Build Google Test with GNU Make Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/README.md Builds the Google Test library and a sample test using GNU Make. This is a simplified approach for systems with GNU make available. It assumes default settings are correct for the environment. ```bash cd ${GTEST_DIR}/make make ./sample1_unittest ``` -------------------------------- ### C++ Google Test Setup Function Naming Convention Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/docs/FAQ.md Google Test requires specific naming conventions for setup functions. SetUp() and SetUpTestCase() are case-sensitive and must be spelled exactly as shown. Incorrectly spelled names like Setup() or SetupTestCase() will result in these functions not being called. ```cpp // Correct usage: void SetUp() {} void SetUpTestCase() {} // Incorrect usage: void Setup() {} void SetupTestCase() {} ``` -------------------------------- ### Install Disruptor Headers and Libraries Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/Disruptor/CMakeLists.txt Installs the header files for the Disruptor library into the 'include/Disruptor' directory. It also installs the built shared and static libraries to the 'lib' directory. ```cmake install(FILES ${Disruptor_headers} DESTINATION include/Disruptor) install(TARGETS DisruptorShared DisruptorStatic LIBRARY DESTINATION lib ARCHIVE DESTINATION lib ) ``` -------------------------------- ### Google Mock: Defining Custom Actions with Invoke() Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/docs/FrequentlyAskedQuestions.md Demonstrates defining custom actions in Google Mock using the `Invoke()` helper. This is generally easier for actions specific to a particular function type. The example shows a basic structure for creating an action. ```cpp // Example of defining a custom action using Invoke() // ACTION_P(MyAction, arg) { /* ... use arg ... */ } ``` -------------------------------- ### Correcting Test Fixture Setup Function Names Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/docs/V1_5_FAQ.md Google Test requires specific naming conventions for test fixture setup and teardown methods. Mismatched casing or typos, such as using `Setup()` instead of `SetUp()` or `SetupTestCase()` instead of `SetUpTestCase()`, will result in these methods not being called. This highlights the importance of adhering to the exact method names. ```c++ class MyTest : public ::testing::Test { protected: static void SetUpTestCase() { /* Called once before all tests in the class */ } static void TearDownTestCase() { /* Called once after all tests in the class */ } void SetUp() override { /* Called before each test */ } void TearDown() override { /* Called after each test */ } }; // TEST_F(MyTest, MyTest1) { ... } // TEST_F(MyTest, MyTest2) { ... } ``` -------------------------------- ### Instantiate C++ Action Template Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/docs/v1_5/CookBook.md Demonstrates how to instantiate a C++ action template. The syntax involves providing the template arguments followed by the value arguments, similar to calling a regular function or class constructor. ```cpp ActionName(v1, ..., v_n) ``` -------------------------------- ### Correcting misspelled Google Test setup function names Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/docs/V1_7_FAQ.md Google Test relies on specific naming conventions for setup and teardown functions. Misspelling these functions, such as using `Setup()` instead of `SetUp()` or `SetupTestCase()` instead of `SetUpTestCase()`, will prevent them from being automatically called by the test runner. Ensure the correct capitalization and spelling are used. ```cpp // Incorrect: // void Setup() {} // void SetupTestCase() {} // Correct: void SetUp() {} void SetUpTestCase() {} ``` -------------------------------- ### Compile and Link User Test with GCC Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/README.md Compiles a user's test source file and links it with the Google Test library. It requires specifying the system header path for Google Test and includes the necessary libraries for linking. The output is an executable file. ```bash g++ -isystem ${GTEST_DIR}/include -pthread path/to/your_test.cc libgtest.a \ -o your_test ``` -------------------------------- ### Installing Google Test in CMake Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/CMakeLists.txt Specifies the installation rules for Google Test. It installs the `gtest` and `gtest_main` libraries to the `lib` directory and the Google Test include headers to the `include` directory, making them available for external projects. ```cmake # Install rules install(TARGETS gtest gtest_main DESTINATION lib) install(DIRECTORY ${gtest_SOURCE_DIR}/include/gtest DESTINATION include) ``` -------------------------------- ### CMake: Configure Project and Add Subdirectories Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/CMakeLists.txt This snippet shows the fundamental CMake commands for setting up a project, including specifying the minimum required version, defining project name and languages, and including subdirectories like Google Test. It also demonstrates conditional execution of hermetic build setup functions. ```cmake cmake_minimum_required(VERSION 2.6.2) project(gmock CXX C) # Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build(). include("${gtest_dir}/cmake/hermetic_build.cmake" OPTIONAL) if (COMMAND pre_project_set_up_hermetic_build) pre_project_set_up_hermetic_build() endif() if (COMMAND set_up_hermetic_build) set_up_hermetic_build() endif() # Instructs CMake to process Google Test's CMakeLists.txt and add its # targets to the current scope. add_subdirectory("${gtest_dir}" "${gmock_BINARY_DIR}/gtest") config_compiler_and_linker() # from ${gtest_dir}/cmake/internal_utils.cmake ``` -------------------------------- ### Google Test C++ Test Case Example Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/docs/V1_6_AdvancedGuide.md A simple C++ example demonstrating Google Test's TEST macro for defining test cases and functions. ```C++ TEST(MathTest, Addition) { ... } TEST(MathTest, Subtraction) { ... } TEST(LogicTest, NonContradiction) { ... } ``` -------------------------------- ### Google Test XML Report Detailed Example Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/docs/V1_6_AdvancedGuide.md An example of a detailed XML report generated by Google Test, showing test suites, test cases, failures, and timing information. ```XML ``` -------------------------------- ### Global Test Environment Setup/Teardown in C++ Google Test Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/docs/AdvancedGuide.md This C++ code outlines how to implement global setup and teardown routines for the entire test program using Google Test. It involves subclassing the `::testing::Environment` class to define custom `SetUp()` and `TearDown()` methods, and then registering an instance of this environment using `::testing::AddGlobalTestEnvironment()`. This allows for initialization and cleanup that affects all tests in the suite. ```cpp class Environment { public: virtual ~Environment() {} // Override this to define how to set up the environment. virtual void SetUp() {} // Override this to define how to tear down the environment. virtual void TearDown() {} }; Environment* AddGlobalTestEnvironment(Environment* env); // Example registration (preferably in main() or before RUN_ALL_TESTS()): // ::testing::Environment* const foo_env = ::testing::AddGlobalTestEnvironment(new FooEnvironment); ``` -------------------------------- ### Basic Google Mock Usage in C++ Test Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/docs/ForDummies.md This snippet demonstrates the fundamental workflow of using Google Mock in a C++ test case. It includes setting up mock expectations, exercising code under test, and initializing the Google Mock framework. The example assumes the existence of a `MockTurtle` class and a `Painter` class that uses it. ```cpp #include "path/to/mock-turtle.h" #include "gmock/gmock.h" #include "gtest/gtest.h" using ::testing::AtLeast; TEST(PainterTest, CanDrawSomething) { MockTurtle turtle; EXPECT_CALL(turtle, PenDown()) .Times(AtLeast(1)); Painter painter(&turtle); EXPECT_TRUE(painter.DrawCircle(0, 0, 10)); } int main(int argc, char** argv) { // The following line must be executed to initialize Google Mock // (and Google Test) before running the tests. ::testing::InitGoogleMock(&argc, argv); return RUN_ALL_TESTS(); } ``` -------------------------------- ### Simple IsDivisibleBy7 Matcher Example Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/docs/v1_7/CookBook.md An example demonstrating a simple custom matcher, IsDivisibleBy7, which checks if the argument is divisible by 7. It uses an empty description string, relying on Google Mock to auto-generate the description from the matcher name. ```c++ MATCHER(IsDivisibleBy7, "") { return (arg % 7) == 0; } ``` -------------------------------- ### Compile and Link Test File (GCC) Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/README.md Compiles a user's test source file and links it with the Google Mock library. It ensures the necessary include paths are set and includes the -pthread flag. ```bash g++ -isystem ${GTEST_DIR}/include -isystem ${GMOCK_DIR}/include \ -pthread path/to/your_test.cc libgmock.a -o your_test ``` -------------------------------- ### Configure Project for Google Mock (MSVC) Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googlemock/README.md Steps to configure a Visual Studio project to use Google Mock by adding the gmock_config property sheet and setting additional include directories. ```xml ... v143 ... WindowsLocalDebugger ``` -------------------------------- ### Google Test XML Report Structure Example Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/docs/V1_7_AdvancedGuide.md An example illustrating the structure of the XML report generated by Google Test, which is based on the junitreport Ant task format and can be parsed by build systems like Jenkins. ```xml ``` -------------------------------- ### C++ Google Test Initialization and Test Execution Source: https://github.com/abc-arbitrage/disruptor-cpp/blob/master/googletest-release-1.8.0/googletest/docs/V1_7_Primer.md This C++ code snippet shows the essential parts of a Google Test executable's main function. It includes initializing Google Test with command-line arguments using ::testing::InitGoogleTest() and then invoking all registered tests using the RUN_ALL_TESTS() macro. The return value of RUN_ALL_TESTS() must be returned by main() for automated testing services. ```cpp int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } ```