Lines Matching +full:long +full:- +full:press +full:- +full:ms
30 * **`const`** - Makes the mocked method a `const` method. Required if
32 * **`override`** - Marks the method with `override`. Recommended if overriding
34 * **`noexcept`** - Marks the method with `noexcept`. Required if overriding a
36 * **`Calltype(...)`** - Sets the call type for the method (e.g. to
38 * **`ref(...)`** - Marks the method with the reference qualification
56 Solution 1 - wrap with parentheses:
70 Solution 2 - define an alias:
132 // Overloaded on the const-ness of this object.
185 ### Mocking Non-virtual Methods {#MockingNonVirtualMethods}
187 gMock can mock non-virtual functions to be used in Hi-perf dependency injection.
191 same signatures. The syntax for mocking non-virtual methods is the *same* as
215 That's fine as long as the test doesn't need to call it.
254 It is not possible to directly mock a free function (i.e. a C-style function or
281 related functions that you can put in the same interface, so the per-function
286 recipe for [mocking non-virtual methods](#MockingNonVirtualMethods).
292 ### Old-Style `MOCK_METHODn` Macros
403 on a per-mock-object basis.
465 [Understanding Uninteresting vs Unexpected Calls](#uninteresting-vs-unexpected).
493 Sometimes a method has a long list of arguments that is mostly uninteresting.
508 argument is not even 0-terminated). If we mock it as is, using the mock will be
538 class more user-friendly.
571 Making a non-virtual function virtual is a big decision. It creates an extension
578 between the class and the tests - any small change in the class may invalidate
597 have a chance to tailor it to your need - you may add higher-level
613 own domain-specific interface on top of `Concrete`, and they will not be the
618 sub-directory) and let many projects use it.
622 long time and it's a proven effective technique applicable in a wide variety of
623 situations. :-)
627 Some times you have a non-trivial fake implementation of an interface. For
642 (n < 0) ? '-' : '0';
722 (The strange-looking thing inside the angled brackets of `static_cast` is
726 Perhaps you haven't got used to the interaction-based way of testing yet. Or
732 be a bad sign: Suppose you have a class `System` for low-level system
752 You can use the *delegating-to-real* technique to ensure that your mock has the
754 This technique is very similar to the [delegating-to-fake](#DelegatingToFake)
821 oh-so painful to have to define a new mock class whenever you don't need to mock
896 // The first argument must not contain sub-string "blah".
902 like any other function. However because their types can be long and rarely
924 some slack. One example is that you have a matcher for `long` and the argument
926 is nothing really wrong with using a `Matcher<long>` to match an `int` - after
927 all, we can first convert the `int` argument to a `long` losslessly before
935 2. When both `T` and `U` are built-in arithmetic types (`bool`, integers, and
936 floating-point numbers), the conversion from `T` to `U` is not lossy (in
965 `MatcherCast<T>(m)`. The difference is that `MatcherCast` works as long as you
977 To disambiguate functions overloaded on the const-ness of this object, use the
992 EXPECT_CALL(foo, GetBar()) // The non-const GetBar().
1093 As a convenience and example, gMock provides some matchers for 2-tuples,
1095 [Multi-argument Matchers](reference/matchers.md#MultiArgMatchers) for the
1148 gMock provides a set of built-in matchers for matching arguments with expected
1150 In case you find the built-in set lacking, you can use an arbitrary unary
1151 predicate function or functor as a matcher - as long as the predicate accepts a
1165 works as long as the return value can be used as the condition in the statement
1201 object, as that may be over-specification. Instead, you may need to validate a
1222 | :--------------------------- | :--------------------------------------- |
1241 matches a plain pointer `p` where `p->number >= 3`. If `p` is `NULL`, the match
1301 What if you have a pointer to pointer? You guessed it - you can use nested
1365 elements, and having to define the expected container out-of-line is a bit of a
1399 As an alternative you can place the arguments in a C-style array and use
1446 container types yet to be written - as long as they follows the above
1448 * You can use nested `ElementsAre*()` to match nested (multi-dimensional)
1458 Under the hood, a gMock matcher object consists of a pointer to a ref-counted
1477 ### Matchers must have no side-effects {#PureMatchers}
1494 **`ON_CALL`** is likely the *single most under-utilized construct* in gMock.
1506 more constraints than necessary is *baaad* - even worse than not having enough
1509 This may be counter-intuitive. How could tests that verify more be worse than
1513 contract of the code.** If a test over-specifies, it doesn't leave enough
1582 ### Understanding Uninteresting vs Unexpected Calls {#uninteresting-vs-unexpected}
1711 a call to `foo.DoThis(6)`. If a call occurred out-of-order, gMock will report an
1727 more convenient when you have long chains of sequential calls, as it doesn't
1759 specifies the following DAG (where `s1` is `A -> B`, and `s2` is `A -> C -> D`):
1762 +---> B
1764 A ---|
1766 +---> C ---> D
1808 and result in an upper-bound-violated error.
1822 `"File too large."`, the first will match #2 and the second will match #1 -
1967 If all you need to do is to change an output argument, the built-in
1986 `int` variable pointed to by argument #1 (0-based).
2020 array pointed to by the `N`-th (0-based) argument:
2105 If a mock method's return type is a built-in C++ type or pointer, by default it
2107 return type has a default constructor will return a default-constructed value by
2160 .WillByDefault(Return(-1));
2170 foo.Sign(-9); // This should return -1.
2178 the test fixture's set-up phase and specialize the mock's behavior later.
2186 If the built-in actions don't suit you, you can use an existing callable
2220 foo.ComplexJob(-1); // Invokes the inline lambda.
2228 mock function, as long as it's safe to do so - nice, huh?
2253 pre-bound arguments. Here's an example:
2265 return (sum > 0) ? '+' : (sum < 0) ? '-' : '0';
2355 // Will execute callback->Run(5), where callback is the
2363 (yet), so you have to define your own action. :-( Or do you really?
2371 will invoke the `N`-th (0-based) argument the mock function receives, with
2383 // Will execute callback->Run(5), where callback is the
2387 What if the callable takes an argument by reference? No problem - just wrap it
2491 .WillOnce(Invoke(IsVisibleInQuadrant1)); // Uh, won't compile. :-(
2521 indices (0-based) to the inner `action` and performs it. Using `WithArgs`, our
2545 `Invoke()` -- it can be anything.
2550 the inner action exactly. It works as long as they can be implicitly
2552 if the 4-th argument of the mock function is an `int` and `my_action` takes
2557 The [selecting-an-action's-arguments](#SelectingArgs) recipe showed us one way
2614 Just like matchers, a gMock action object consists of a pointer to a ref-counted
2649 foo.DoThat(); // Returns 1 - DoThat() uses a different
2665 foo.DoThat(); // Returns 3 - the counter is shared.
2670 One oft-encountered problem with gMock is that it can be hard to test
2706 our test will run forever. It will eventually time-out and fail, but it will
2708 use `WaitForNotificationWithTimeout(ms)` instead of `WaitForNotification()`.
2712 ### Mocking Methods That Use Move-Only Types
2714 C++11 introduced *move-only types*. A move-only-typed value can be moved from
2716 the most commonly used move-only type.
2718 Mocking a method that takes and/or returns move-only types presents some
2720 Note that the support for move-only method arguments was only introduced to
2750 To mock a method that accepts or returns move-only types, you just use the
2792 If you just need to return a move-only value, you can use it in combination with
2804 consumed (since it’s a move-only value), so the next time around, there’s no
2805 value to move from -- you’ll get a run-time error that `Return(std::move(...))`
2808 If you need your mock method to do more than just moving a pre-defined value,
2825 That covers returning move-only values; but how do we work with methods
2826 accepting move-only arguments? The answer is that they work normally, although
2827 some actions will not compile when any of method's arguments are move-only. You
2842 Many built-in actions (`WithArgs`, `WithoutArgs`,`DeleteArg`, `SaveArg`, ...)
2843 could in principle support move-only arguments, but the support for this is not
2847 work with non-copyable objects; you'll have to use functors instead.
2849 #### Legacy workarounds for move-only types {#LegacyMoveOnly}
2851 Support for move-only function arguments was only introduced to gMock in April
2853 of this feature (it is no longer necessary - we're including it just for
2867 it `DoShareBuzz()`) that does not take move-only parameters. Then, instead of
2885 non-trivial tasks (e.g. verification of the expectations). What's more, mock
2955 there's a bug in that code and it doesn't delete the mock object properly - you
2989 See [Using Mocks in Tests](gmock_for_dummies.md#using-mocks-in-tests) for more
3044 e.g. after `bar->A()` is called but before `bar->B()` is called. We already know
3080 // Expects *foo to die after bar->A() and before bar->B().
3092 single-threaded context. That avoids race conditions and dead locks, and makes
3095 Yet most programs are multi-threaded, and sometimes to test something we need to
3116 * #3 and #4 can be done either in one thread or in multiple threads - anyway
3117 you want. gMock takes care of the locking, so you don't have to do any -
3141 `action1` and `action2` to make the test thread-safe.
3162 You can control how much gMock tells you using the `--gmock_verbose=LEVEL`
3163 command-line flag, where `LEVEL` is a string with three possible values:
3182 `--gtest_stack_trace_depth=max_depth` flag.
3193 Won't it be nice if you have X-ray vision and can actually see the trace of all
3199 You can unlock this power by running your test with the `--gmock_verbose=info`
3225 if you run it with `--gmock_verbose=info`, you will see this output:
3250 Actual: never called - unsatisfied and active
3261 combine `--gmock_verbose=info` with `--gtest_stack_trace_depth=0` on the test
3266 If you build and run your tests in Emacs using the `M-x google-compile` command
3268 errors will be highlighted. Just press `<Enter>` on one of them and you'll be
3269 taken to the offending line. Or, you can just type `C-x`` to jump to the next
3275 (global-set-key "\M-m" 'google-compile) ; m is for make
3276 (global-set-key [M-down] 'next-error)
3277 (global-set-key [M-up] '(lambda () (interactive) (next-error -1)))
3280 Then you can type `M-m` to start a build (if you want to run the test as well,
3282 after typing `M-m`), or `M-up`/`M-down` to move back and forth between errors.
3305 The *description string* is a `string`-typed expression that documents what the
3355 As you may have noticed, the auto-generated descriptions (especially those for
3416 match succeeds in case of a success (unless it's obvious) - this is useful when
3428 `int`, `arg_type` will be `int`; if it takes an `unsigned long`, `arg_type` will
3429 be `unsigned long`; and so on.
3460 Actual: -9
3464 message human-friendly.
3472 support multi-parameter matchers:
3536 template, as in `Foo<long, bool>(5, false)`. As said earlier, you don't get to
3543 special types: you can assign `Foo()` to a `FooMatcher`-typed variable, and
3544 assign `Foo(p)` to a `FooMatcherP<p_type>`-typed variable.
3564 general leads to better compiler error messages that pay off in the long run.
3699 EXPECT_CALL(foo, Bar(NotNull())); // The argument must be a non-NULL pointer.
3781 EXPECT_CALL(foo, Bar(NotNull())); // The argument must be a non-NULL pointer.
3798 If the [built-in set](gmock_cheat_sheet.md#CardinalityList) of cardinalities
3853 If the built-in actions don't work for you, you can easily define your own one.
3887 once and is allowed to be a move-only type:
3890 // An action that contains move-only types and has an &&-qualified operator,
3907 weaker requirements (`Action` requires a copy-constructible input that can be
3908 called repeatedly whereas `OnceAction` requires only move-constructible and
3909 supports `&&`-qualified call operators), but can be used only with `WillOnce`.
3910 `OnceAction` is typically relevant only when supporting move-only types or
3911 actions that want a type-system guarantee that they will be called at most once.
3917 operators to make that possible. See `gmock-actions.h` for examples.
3919 #### Legacy macro-based Actions
3921 Before C++11, the functor-based actions were not supported; the old way of
3924 harder-to-understand compiler errors. Nevertheless, we cover them here for
3936 statements, you can refer to the K-th (0-based) argument of the mock function as
3950 Rest assured that your code is type-safe though: you'll get a compiler error if
3969 For more convenience and flexibility, you can also use the following pre-defined
3972 `argK_type` | The type of the K-th (0-based) argument of the mock function
3973 :-------------- | :-----------------------------------------------------------
3987 Pre-defined Symbol | Is Bound To
3988 ------------------ | ---------------------------------
3998 #### Legacy macro-based parameterized Actions
4025 the parameter is named `param`, you can also use the gMock-defined symbol
4030 gMock also provides `ACTION_P2`, `ACTION_P3`, and etc to support multi-parameter
4035 double dx = arg0 - x;
4036 double dy = arg1 - y;
4082 where `StaticAssertTypeEq` is a compile-time assertion in googletest that
4101 name of the *i*-th template parameter, and `kind_i` specifies whether it's a
4102 `typename`, an integral constant, or a template. `p_i` is the name of the *i*-th
4108 // DuplicateArg<k, T>(output) converts the k-th argument of the mock
4151 Are we using a single-template-parameter action where `bool` refers to the type
4152 of `x`, or a two-template-parameter action where the compiler is asked to infer
4163 | ----------------------------- | ------------------- | --------------------- |
4262 // To get the i-th (0-based) argument, use std::get(args).
4289 Now, you can use this polymorphic action the same way you use the built-in ones:
4315 assertion fails. gMock and googletest do this using googletest's user-extensible
4318 This printer knows how to print built-in C++ types, native arrays, STL
4321 [The GoogleTest advanced guide](advanced.md#teaching-googletest-how-to-print-your-values)
4327 <!--#include file="includes/g3_testing_LOGs.md"-->
4328 <!--#include file="includes/g3_mock_callbacks.md"-->
4335 But fear not - `MockFunction` can help you with that.
4379 callback has bigger problems than being mockable. :-)