diff --git a/googlemock/docs/cheat_sheet.md b/googlemock/docs/cheat_sheet.md index f2fb2723..a39c6e9f 100644 --- a/googlemock/docs/cheat_sheet.md +++ b/googlemock/docs/cheat_sheet.md @@ -1,12 +1,12 @@ -## gMock Cheat Sheet +# gMock Cheat Sheet -### Defining a Mock Class +## Defining a Mock Class -#### Mocking a Normal Class {#MockClass} +### Mocking a Normal Class {#MockClass} Given @@ -52,7 +52,7 @@ StrictMock strict_foo; // The type is a subclass of MockFoo. **Note:** A mock object is currently naggy by default. We may make it nice by default in the future. -#### Mocking a Class Template {#MockTemplate} +### Mocking a Class Template {#MockTemplate} Class templates can be mocked just like any class. @@ -80,7 +80,7 @@ class MockStack : public StackInterface { }; ``` -#### Specifying Calling Conventions for Mock Functions +### Specifying Calling Conventions for Mock Functions If your mock function doesn't use the default calling convention, you can specify it by adding `Calltype(convention)` to `MOCK_METHOD`'s 4th parameter. @@ -94,7 +94,7 @@ For example, where `STDMETHODCALLTYPE` is defined by `` on Windows. -### Using Mocks in Tests {#UsingMocks} +## Using Mocks in Tests {#UsingMocks} The typical work flow is: @@ -130,7 +130,7 @@ TEST(BarTest, DoesThis) { } // #6 ``` -### Setting Default Actions {#OnCall} +## Setting Default Actions {#OnCall} gMock has a **built-in default action** for any function that returns `void`, `bool`, a numeric value, or a pointer. In C++11, it will additionally returns @@ -186,7 +186,7 @@ ON_CALL(mock-object, method(matchers)) .WillByDefault(action); ``` -### Setting Expectations {#ExpectCall} +## Setting Expectations {#ExpectCall} `EXPECT_CALL()` sets **expectations** on a mock method (How will it be called? What will it do?): @@ -225,7 +225,7 @@ If `Times()` is omitted, the cardinality is assumed to be: A method with no `EXPECT_CALL()` is free to be invoked *any number of times*, and the default action will be taken each time. -### Matchers {#MatcherList} +## Matchers {#MatcherList} @@ -249,14 +249,14 @@ Built-in matchers (where `argument` is the function argument, e.g. `EXPECT_CALL(mock_object, method(matchers))`, the arguments of `method`) are divided into several categories: -#### Wildcard +### Wildcard Matcher | Description :-------------------------- | :----------------------------------------------- `_` | `argument` can be any value of the correct type. `A()` or `An()` | `argument` can be any value of type `type`. -#### Generic Comparison +### Generic Comparison | Matcher | Description | @@ -289,7 +289,7 @@ Boolean. In other cases, you can use the basic [`EXPECT_TRUE` and `EXPECT_FALSE`](../../googletest/docs/primer#basic-assertions) assertions. -#### Floating-Point Matchers {#FpMatchers} +### Floating-Point Matchers {#FpMatchers} | Matcher | Description | @@ -317,7 +317,7 @@ user wants. | `NanSensitiveFloatNear(a_float, max_abs_error)` | `argument` is a `float` value close to `a_float` (absolute error <= `max_abs_error`), treating two NaNs as equal. | -#### String Matchers +### String Matchers The `argument` can be either a C string or a C++ string object: @@ -341,7 +341,7 @@ use the regular expression syntax defined these matchers, except `ContainsRegex()` and `MatchesRegex()` work for wide strings as well. -#### Container Matchers +### Container Matchers Most STL-style containers support `==`, so you can use `Eq(expected_container)` or simply `expected_container` to match a container exactly. If you want to @@ -392,7 +392,7 @@ messages, you can use: EXPECT_THAT(actual_foos, Pointwise(FooEq(), expected_foos)); ``` -#### Member Matchers +### Member Matchers | Matcher | Description | @@ -403,7 +403,7 @@ messages, you can use: | `Property(&class::property, m)` | `argument.property()` (or `argument->property()` when `argument` is a plain pointer) matches matcher `m`, where `argument` is an object of type _class_. | -#### Matching the Result of a Function, Functor, or Callback +### Matching the Result of a Function, Functor, or Callback | Matcher | Description | @@ -411,7 +411,7 @@ messages, you can use: | `ResultOf(f, m)` | `f(argument)` matches matcher `m`, where `f` is a function or functor. | -#### Pointer Matchers +### Pointer Matchers | Matcher | Description | @@ -424,7 +424,7 @@ messages, you can use: -#### Multi-argument Matchers {#MultiArgMatchers} +### Multi-argument Matchers {#MultiArgMatchers} Technically, all matchers match a *single* value. A "multi-argument" matcher is just one that matches a *tuple*. The following matchers can be used to match a @@ -449,7 +449,7 @@ reorder them) to participate in the matching: | `Args(m)` | The tuple of the `k` selected (using 0-based indices) arguments matches `m`, e.g. `Args<1, 2>(Eq())`. | -#### Composite Matchers +### Composite Matchers You can make a matcher from one or more other matchers: @@ -465,7 +465,7 @@ You can make a matcher from one or more other matchers: -#### Adapters for Matchers +### Adapters for Matchers | Matcher | Description | @@ -478,7 +478,7 @@ You can make a matcher from one or more other matchers: `AddressSatisfies(callback)` and `Truly(callback)` take ownership of `callback`, which must be a permanent callback. -#### Using Matchers as Predicates {#MatchersAsPredicatesCheat} +### Using Matchers as Predicates {#MatchersAsPredicatesCheat} | Matcher | Description | @@ -488,7 +488,7 @@ which must be a permanent callback. | `Value(value, m)` | evaluates to `true` if `value` matches `m`. | -#### Defining Matchers +### Defining Matchers | Matcher | Description | @@ -507,11 +507,11 @@ which must be a permanent callback. 3. You can use `PrintToString(x)` to convert a value `x` of any type to a string. -### Actions {#ActionList} +## Actions {#ActionList} **Actions** specify what a mock function should do when invoked. -#### Returning a Value +### Returning a Value | | | @@ -527,7 +527,7 @@ which must be a permanent callback. | `ReturnRoundRobin({a1, ..., ak})` | Each call will return the next `ai` in the list, starting at the beginning when the end of the list is reached. | -#### Side Effects +### Side Effects | | | @@ -544,7 +544,7 @@ which must be a permanent callback. | `Throw(exception)` | Throws the given exception, which can be any copyable value. Available since v1.1.0. | -#### Using a Function, Functor, or Lambda as an Action +### Using a Function, Functor, or Lambda as an Action In the following, by "callable" we mean a free function, `std::function`, functor, or lambda. @@ -598,7 +598,7 @@ InvokeArgument<2>(5, string("Hi"), ByRef(foo)) calls the mock function's #2 argument, passing to it `5` and `string("Hi")` by value, and `foo` by reference. -#### Default Action +### Default Action | Matcher | Description | @@ -611,7 +611,7 @@ composite action - trying to do so will result in a run-time error. -#### Composite Actions +### Composite Actions | | | @@ -623,7 +623,7 @@ composite action - trying to do so will result in a run-time error. | `WithoutArgs(a)` | Perform action `a` without any arguments. | -#### Defining Actions +### Defining Actions | | | @@ -635,7 +635,7 @@ composite action - trying to do so will result in a run-time error. The `ACTION*` macros cannot be used inside a function or class. -### Cardinalities {#CardinalityList} +## Cardinalities {#CardinalityList} These are used in `Times()` to specify how many times a mock function will be called: @@ -650,13 +650,13 @@ called: | `Exactly(n) or n` | The call is expected exactly `n` times. In particular, the call should never happen when `n` is 0. | -### Expectation Order +## Expectation Order By default, the expectations can be matched in *any* order. If some or all expectations must be matched in a given order, there are two ways to specify it. They can be used either independently or together. -#### The After Clause {#AfterClause} +### The After Clause {#AfterClause} ```cpp using ::testing::Expectation; @@ -690,7 +690,7 @@ says that `Bar()` can be called only after all elements have been initialized Modifying an `ExpectationSet` after using it in an `.After()` doesn't affect the meaning of the `.After()`. -#### Sequences {#UsingSequences} +### Sequences {#UsingSequences} When you have a long chain of sequential expectations, it's easier to specify the order using **sequences**, which don't require you to given each expectation @@ -733,7 +733,7 @@ using ::testing::InSequence; says that all expected calls in the scope of `seq` must occur in strict order. The name `seq` is irrelevant. -### Verifying and Resetting a Mock +## Verifying and Resetting a Mock gMock will verify the expectations on a mock object when it is destructed, or you can do it earlier: @@ -758,7 +758,7 @@ verified: Mock::AllowLeak(&mock_obj); ``` -### Mock Classes +## Mock Classes gMock defines a convenient mock class template @@ -771,7 +771,7 @@ class MockFunction { See this [recipe](cook_book.md#using-check-points) for one application of it. -### Flags +## Flags | Flag | Description | diff --git a/googlemock/docs/for_dummies.md b/googlemock/docs/for_dummies.md index 8f5d17ae..4ce7b948 100644 --- a/googlemock/docs/for_dummies.md +++ b/googlemock/docs/for_dummies.md @@ -1,8 +1,8 @@ -## gMock for Dummies {#GMockForDummies} +# gMock for Dummies {#GMockForDummies} -### What Is gMock? +## What Is gMock? When you write a prototype or test, often it's not feasible or wise to rely on real objects entirely. A **mock object** implements the same interface as a real @@ -39,7 +39,7 @@ When using gMock, 3. then you exercise code that uses the mock objects. gMock will catch any violation to the expectations as soon as it arises. -### Why gMock? +## Why gMock? While mock objects help you remove unnecessary dependencies in tests and make them fast and reliable, using mocks manually in C++ is *hard*: @@ -85,11 +85,11 @@ We encourage you to use gMock as * a *testing* tool to cut your tests' outbound dependencies and probe the interaction between your module and its collaborators. -### Getting Started +## Getting Started gMock is bundled with googletest. -### A Case for Mock Turtles +## A Case for Mock Turtles Let's look at an example. Suppose you are developing a graphics program that relies on a [LOGO](http://en.wikipedia.org/wiki/Logo_programming_language)-like @@ -135,13 +135,13 @@ because your new machine does anti-aliasing differently), easier to read and maintain (the intent of a test is expressed in the code, not in some binary images), and run *much, much faster*. -### Writing the Mock Class +## Writing the Mock Class If you are lucky, the mocks you need to use have already been implemented by some nice people. If, however, you find yourself in the position to write a mock class, relax - gMock turns this task into a fun game! (Well, almost.) -#### How to Define It +### How to Define It Using the `Turtle` interface as example, here are the simple steps you need to follow: @@ -184,7 +184,7 @@ class MockTurtle : public Turtle { You don't need to define these mock methods somewhere else - the `MOCK_METHOD` macro will generate the definitions for you. It's that simple! -#### Where to Put It +### Where to Put It When you define a mock class, you need to decide where to put its definition. Some people put it in a `_test.cc`. This is fine when the interface being mocked @@ -208,7 +208,7 @@ specific domain much better than `Foo` does. -### Using Mocks in Tests +## Using Mocks in Tests Once you have a mock class, using it is easy. The typical work flow is: @@ -279,7 +279,7 @@ Admittedly, this test is contrived and doesn't do much. You can easily achieve the same effect without using gMock. However, as we shall reveal soon, gMock allows you to do *so much more* with the mocks. -### Setting Expectations +## Setting Expectations The key to using a mock object successfully is to set the *right expectations* on it. If you set the expectations too strict, your test will fail as the result @@ -288,7 +288,7 @@ to do it just right such that your test can catch exactly the kind of bugs you intend it to catch. gMock provides the necessary means for you to do it "just right." -#### General Syntax +### General Syntax In gMock we use the `EXPECT_CALL()` macro to set an expectation on a mock method. The general syntax is: @@ -343,7 +343,7 @@ it makes expectations easily identifiable (either by `gsearch` or by a human reader), and second it allows gMock to include the source file location of a failed expectation in messages, making debugging easier. -#### Matchers: What Arguments Do We Expect? +### Matchers: What Arguments Do We Expect? When a mock function takes arguments, we may specify what arguments we are expecting, for example: @@ -399,7 +399,7 @@ to help gMock resolve which overload is expected by specifying the number of arguments and possibly also the [types of the arguments](cook_book.md#SelectOverload). -#### Cardinalities: How Many Times Will It Be Called? +### Cardinalities: How Many Times Will It Be Called? The first clause we can specify following an `EXPECT_CALL()` is `Times()`. We call its argument a **cardinality** as it tells *how many times* the call should @@ -429,7 +429,7 @@ the cardinality for you.** The rules are easy to remember: **Quick quiz:** what do you think will happen if a function is expected to be called twice but actually called four times? -#### Actions: What Should It Do? +### Actions: What Should It Do? Remember that a mock object doesn't really have a working implementation? We as users have to tell it what to do when a method is invoked. This is easy in @@ -522,7 +522,7 @@ will be taken afterwards. So the right answer is that `turtle.GetY()` will return 100 the first time, but **return 0 from the second time on**, as returning 0 is the default action for `int` functions. -#### Using Multiple Expectations {#MultiExpectations} +### Using Multiple Expectations {#MultiExpectations} So far we've only shown examples where you have a single expectation. More realistically, you'll specify expectations on multiple mock methods which may be @@ -563,7 +563,7 @@ useful for methods that have some expectations, but for which other calls are ok. See [Understanding Uninteresting vs Unexpected Calls](cook_book.md#uninteresting-vs-unexpected). -#### Ordered vs Unordered Calls {#OrderedCalls} +### Ordered vs Unordered Calls {#OrderedCalls} By default, an expectation can match a call even though an earlier expectation hasn't been satisfied. In other words, the calls don't have to occur in the @@ -600,7 +600,7 @@ order as written. If a call is made out-of-order, it will be an error. them? Can you specify an arbitrary partial order? The answer is ... yes! The details can be found [here](cook_book.md#OrderedCalls).) -#### All Expectations Are Sticky (Unless Said Otherwise) {#StickyExpectations} +### All Expectations Are Sticky (Unless Said Otherwise) {#StickyExpectations} Now let's do a quick quiz to see how well you can use this mock stuff already. How would you test that the turtle is asked to go to the origin *exactly twice* @@ -688,7 +688,7 @@ it's in a sequence - as soon as another expectation that comes after it in the sequence has been used, it automatically retires (and will never be used to match any call). -#### Uninteresting Calls +### Uninteresting Calls A mock object may have many methods, and not all of them are that interesting. For example, in some tests we may not care about how many times `GetX()` and