Commit d64e4cf1 by Chris Baish

Merge remote-tracking branch 'upstream/master' into primer_md_table_update

parents adb49077 b77e5c76
## Googletest Mocking (gMock) Cookbook ## Googletest Mocking (gMock) Cookbook
<!-- GOOGLETEST_CM0011 DO NOT DELETE --> <!-- GOOGLETEST_CM0012 DO NOT DELETE -->
You can find recipes for using gMock here. If you haven't yet, please read You can find recipes for using gMock here. If you haven't yet, please read
[this](for_dummies.md) first to make sure you understand the basics. [this](for_dummies.md) first to make sure you understand the basics.
...@@ -154,7 +154,7 @@ class MockStack : public StackInterface<Elem> { ...@@ -154,7 +154,7 @@ class MockStack : public StackInterface<Elem> {
#### Mocking Non-virtual Methods {#MockingNonVirtualMethods} #### Mocking Non-virtual Methods {#MockingNonVirtualMethods}
gMock can mock non-virtual functions to be used in Hi-perf dependency gMock can mock non-virtual functions to be used in Hi-perf dependency
injection.<!-- GOOGLETEST_CM0016 DO NOT DELETE -->. injection.<!-- GOOGLETEST_CM0017 DO NOT DELETE -->.
In this case, instead of sharing a common base class with the real class, your In this case, instead of sharing a common base class with the real class, your
mock class will be *unrelated* to the real class, but contain methods with the mock class will be *unrelated* to the real class, but contain methods with the
...@@ -1455,7 +1455,7 @@ mock object and gMock. ...@@ -1455,7 +1455,7 @@ mock object and gMock.
#### Knowing When to Expect {#UseOnCall} #### Knowing When to Expect {#UseOnCall}
<!-- GOOGLETEST_CM0017 DO NOT DELETE --> <!-- GOOGLETEST_CM0018 DO NOT DELETE -->
**`ON_CALL`** is likely the *single most under-utilized construct* in gMock. **`ON_CALL`** is likely the *single most under-utilized construct* in gMock.
......
## Googletest Mocking (gMock) for Dummies {#GMockForDummies} ## Googletest Mocking (gMock) for Dummies {#GMockForDummies}
<!-- GOOGLETEST_CM0012 DO NOT DELETE --> <!-- GOOGLETEST_CM0013 DO NOT DELETE -->
### What Is gMock? ### What Is gMock?
...@@ -499,7 +499,7 @@ always return 100 as `n++` is only evaluated once. Similarly, `Return(new Foo)` ...@@ -499,7 +499,7 @@ always return 100 as `n++` is only evaluated once. Similarly, `Return(new Foo)`
will create a new `Foo` object when the `EXPECT_CALL()` is executed, and will will create a new `Foo` object when the `EXPECT_CALL()` is executed, and will
return the same pointer every time. If you want the side effect to happen every return the same pointer every time. If you want the side effect to happen every
time, you need to define a custom action, which we'll teach in the time, you need to define a custom action, which we'll teach in the
[cook book](http://<!-- GOOGLETEST_CM0011 DO NOT DELETE -->). [cook book](http://<!-- GOOGLETEST_CM0012 DO NOT DELETE -->).
Time for another quiz! What do you think the following means? Time for another quiz! What do you think the following means?
......
## Legacy gMock FAQ {#GMockFaq} ## Legacy gMock FAQ {#GMockFaq}
<!-- GOOGLETEST_CM0020 DO NOT DELETE --> <!-- GOOGLETEST_CM0021 DO NOT DELETE -->
### When I call a method on my mock object, the method for the real object is invoked instead. What's the problem? ### When I call a method on my mock object, the method for the real object is invoked instead. What's the problem?
......
...@@ -174,9 +174,9 @@ We list the most frequently used macros below. For a complete list, see file ...@@ -174,9 +174,9 @@ We list the most frequently used macros below. For a complete list, see file
### Multi-threaded Tests ### Multi-threaded Tests
Google Test is thread-safe where the pthread library is available. After Google Test is thread-safe where the pthread library is available. After
`#include "gtest/gtest.h"`, you can check the `GTEST_IS_THREADSAFE` macro to see `#include "gtest/gtest.h"`, you can check the
whether this is the case (yes if the macro is `#defined` to 1, no if it's `GTEST_IS_THREADSAFE` macro to see whether this is the case (yes if the macro is
undefined.). `#defined` to 1, no if it's undefined.).
If Google Test doesn't correctly detect whether pthread is available in your If Google Test doesn't correctly detect whether pthread is available in your
environment, you can force it with environment, you can force it with
......
# Advanced googletest Topics # Advanced googletest Topics
<!-- GOOGLETEST_CM0015 DO NOT DELETE --> <!-- GOOGLETEST_CM0016 DO NOT DELETE -->
## Introduction ## Introduction
...@@ -385,7 +385,7 @@ using ::testing::StartsWith; ...@@ -385,7 +385,7 @@ using ::testing::StartsWith;
``` ```
Read this Read this
[recipe](https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#using-matchers-in-googletest-assertions) [recipe](../../googlemock/docs/cook_book.md#using-matchers-in-googletest-assertions)
in the gMock Cookbook for more details. in the gMock Cookbook for more details.
gMock has a rich set of matchers. You can do many things googletest cannot do gMock has a rich set of matchers. You can do many things googletest cannot do
......
# Googletest FAQ # Googletest FAQ
<!-- GOOGLETEST_CM0013 DO NOT DELETE --> <!-- GOOGLETEST_CM0014 DO NOT DELETE -->
## Why should test suite names and test names not contain underscore? ## Why should test suite names and test names not contain underscore?
...@@ -332,7 +332,7 @@ You may still want to use `SetUp()/TearDown()` in the following cases: ...@@ -332,7 +332,7 @@ You may still want to use `SetUp()/TearDown()` in the following cases:
* In the body of a constructor (or destructor), it's not possible to use the * In the body of a constructor (or destructor), it's not possible to use the
`ASSERT_xx` macros. Therefore, if the set-up operation could cause a fatal `ASSERT_xx` macros. Therefore, if the set-up operation could cause a fatal
test failure that should prevent the test from running, it's necessary to test failure that should prevent the test from running, it's necessary to
use `abort` <!-- GOOGLETEST_CM0014 DO NOT DELETE --> and abort the whole test executable, use `abort` <!-- GOOGLETEST_CM0015 DO NOT DELETE --> and abort the whole test executable,
or to use `SetUp()` instead of a constructor. or to use `SetUp()` instead of a constructor.
* If the tear-down operation could throw an exception, you must use * If the tear-down operation could throw an exception, you must use
`TearDown()` as opposed to the destructor, as throwing in a destructor leads `TearDown()` as opposed to the destructor, as throwing in a destructor leads
......
## Using GoogleTest from various build systems ## ## Using GoogleTest from various build systems
GoogleTest comes with pkg-config files that can be used to determine all GoogleTest comes with pkg-config files that can be used to determine all
necessary flags for compiling and linking to GoogleTest (and GoogleMock). necessary flags for compiling and linking to GoogleTest (and GoogleMock).
Pkg-config is a standardised plain-text format containing Pkg-config is a standardised plain-text format containing
* the includedir (-I) path * the includedir (-I) path
* necessary macro (-D) definitions * necessary macro (-D) definitions
* further required flags (-pthread) * further required flags (-pthread)
* the library (-L) path * the library (-L) path
* the library (-l) to link to * the library (-l) to link to
All current build systems support pkg-config in one way or another. For All current build systems support pkg-config in one way or another. For all
all examples here we assume you want to compile the sample examples here we assume you want to compile the sample
`samples/sample3_unittest.cc`. `samples/sample3_unittest.cc`.
### CMake
### CMake ###
Using `pkg-config` in CMake is fairly easy: Using `pkg-config` in CMake is fairly easy:
``` cmake ```cmake
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0048 NEW) cmake_policy(SET CMP0048 NEW)
...@@ -43,11 +42,10 @@ that all libraries have been compiled with threading enabled. In addition, ...@@ -43,11 +42,10 @@ that all libraries have been compiled with threading enabled. In addition,
GoogleTest might also require `-pthread` in the compiling step, and as such GoogleTest might also require `-pthread` in the compiling step, and as such
splitting the pkg-config `Cflags` variable into include dirs and macros for splitting the pkg-config `Cflags` variable into include dirs and macros for
`target_compile_definitions()` might still miss this). The same recommendation `target_compile_definitions()` might still miss this). The same recommendation
goes for using `_LDFLAGS` over the more commonplace `_LIBRARIES`, which goes for using `_LDFLAGS` over the more commonplace `_LIBRARIES`, which happens
happens to discard `-L` flags and `-pthread`. to discard `-L` flags and `-pthread`.
### Autotools ### ### Autotools
Finding GoogleTest in Autoconf and using it from Automake is also fairly easy: Finding GoogleTest in Autoconf and using it from Automake is also fairly easy:
...@@ -77,8 +75,7 @@ testapp_CXXFLAGS = $(GTEST_CFLAGS) ...@@ -77,8 +75,7 @@ testapp_CXXFLAGS = $(GTEST_CFLAGS)
testapp_LDADD = $(GTEST_LIBS) testapp_LDADD = $(GTEST_LIBS)
``` ```
### Meson
### Meson ###
Meson natively uses pkgconfig to query dependencies: Meson natively uses pkgconfig to query dependencies:
...@@ -96,13 +93,12 @@ testapp = executable( ...@@ -96,13 +93,12 @@ testapp = executable(
test('first_and_only_test', testapp) test('first_and_only_test', testapp)
``` ```
### Plain Makefiles
### Plain Makefiles ### Since `pkg-config` is a small Unix command-line utility, it can be used in
handwritten `Makefile`s too:
Since `pkg-config` is a small Unix command-line utility, it can be used ```Makefile
in handwritten `Makefile`s too:
``` Makefile
GTEST_CFLAGS = `pkg-config --cflags gtest_main` GTEST_CFLAGS = `pkg-config --cflags gtest_main`
GTEST_LIBS = `pkg-config --libs gtest_main` GTEST_LIBS = `pkg-config --libs gtest_main`
...@@ -120,12 +116,11 @@ testapp.o: samples/sample3_unittest.cc ...@@ -120,12 +116,11 @@ testapp.o: samples/sample3_unittest.cc
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $< -c -o $@ $(GTEST_CFLAGS) $(CXX) $(CPPFLAGS) $(CXXFLAGS) $< -c -o $@ $(GTEST_CFLAGS)
``` ```
### Help! pkg-config can't find GoogleTest!
### Help! pkg-config can't find GoogleTest! ###
Let's say you have a `CMakeLists.txt` along the lines of the one in this Let's say you have a `CMakeLists.txt` along the lines of the one in this
tutorial and you try to run `cmake`. It is very possible that you get a tutorial and you try to run `cmake`. It is very possible that you get a failure
failure along the lines of: along the lines of:
``` ```
-- Checking for one of the modules 'gtest_main' -- Checking for one of the modules 'gtest_main'
...@@ -135,9 +130,9 @@ CMake Error at /usr/share/cmake/Modules/FindPkgConfig.cmake:640 (message): ...@@ -135,9 +130,9 @@ CMake Error at /usr/share/cmake/Modules/FindPkgConfig.cmake:640 (message):
These failures are common if you installed GoogleTest yourself and have not These failures are common if you installed GoogleTest yourself and have not
sourced it from a distro or other package manager. If so, you need to tell sourced it from a distro or other package manager. If so, you need to tell
pkg-config where it can find the `.pc` files containing the information. pkg-config where it can find the `.pc` files containing the information. Say you
Say you installed GoogleTest to `/usr/local`, then it might be that the installed GoogleTest to `/usr/local`, then it might be that the `.pc` files are
`.pc` files are installed under `/usr/local/lib64/pkgconfig`. If you set installed under `/usr/local/lib64/pkgconfig`. If you set
``` ```
export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig
......
...@@ -164,7 +164,7 @@ you'll get a compiler error. We used to require the arguments to support the ...@@ -164,7 +164,7 @@ you'll get a compiler error. We used to require the arguments to support the
`<<` is supported, it will be called to print the arguments when the assertion `<<` is supported, it will be called to print the arguments when the assertion
fails; otherwise googletest will attempt to print them in the best way it can. fails; otherwise googletest will attempt to print them in the best way it can.
For more details and how to customize the printing of the arguments, see For more details and how to customize the printing of the arguments, see
[documentation](https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#teaching-gmock-how-to-print-your-values) [documentation](../../googlemock/docs/cook_book.md#teaching-gmock-how-to-print-your-values)
These assertions can work with a user-defined type, but only if you define the These assertions can work with a user-defined type, but only if you define the
corresponding comparison operator (e.g. `==`, `<`, etc). Since this is corresponding comparison operator (e.g. `==`, `<`, etc). Since this is
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment