Change Matcher<T> to allow binding an implementation by value directly:
- Drop the requirement of MatcherInterface. Doing manual type erasure avoid
extra layers in many cases.
- Avoid the adaptor for `MatcherInterface<T>` and `MatcherInterface<const T&>` mismatch.
- Use a small object optimization when possible. This makes things like
`_` and `Eq(1)` really cheap and do not require memory allocations.
- Migrate some matchers to the new model to speed them up and to test the new framework. More matchers to come in future changes.
PiperOrigin-RevId: 350580998
Use an OrderedDict to store templated_types in the AST so that gmock knows how to properly construct the templated Mock class.
This is necessary for functions that make use of the templated typename as an argument or return type.
PiperOrigin-RevId: 349405731
Give each of Naggy/Nice/StrictMock a base class whose constructor runs before
the mocked class's constructor, and a destructor that runs after the mocked
class's destructor, so that any mock methods run in either the constructor or
destructor use the same strictness as other calls.
PiperOrigin-RevId: 348511612
Add static_asserts that verify that no class hierarchy contains more than one
of {NiceMock, NaggyMock, StrictMock}. This sort of nesting has always been
disallowed, but this CL adds a compile-time check to prevent it.
PiperOrigin-RevId: 347037822
Add a `Pointer` matcher as an analog to `Pointee`.
Similar to `Pointee`, `Pointer` works with either raw or smart pointers and
allows creating a matcher like Pointer(Eq(foo)) for smart pointers.
PiperOrigin-RevId: 346164768
Disable a gmock matcher test under MSVC 2015 (version 14) and earlier. It interacts badly with Windows structured exceptions in a way we do not have the resources to investigate. This test passes under MSVC 2017 and SEH.
PiperOrigin-RevId: 345496734
Use override instead of virtual for destructor
https://google.github.io/styleguide/cppguide.html says: "Explicitly annotate overrides of virtual functions or virtual destructors with exactly one of an override or (less frequently) final specifier. Do not use virtual when declaring an override". The mocked class _should_ have a virtual destructor most of the times.
PiperOrigin-RevId: 342082140
Use a tagged constructor for FlatTuple instead.
Some versions of MSVC are getting confused with that constructor and generating invalid code.
PiperOrigin-RevId: 342050957
Rollback change from
https://github.com/google/googletest/pull/1836. This change generates
a script on Windows to actually run each test, but the script itself
doesn't correctly report if the test passed.
This change will "break tests" that were already broken on Windows,
but weren't being reported as such.
PiperOrigin-RevId: 341850671
Change ACTION{,_Pn,_TEMPLATE} macros to build functors rather than ActionInterface<> subclasses, thus changing the Action<> wrappers they create to use the modernized (non-const) argument tuple type, allowing these macros to mutate their arguments.
Functor-based Action<>s deep-copy the implementing object, so have the functors use a shared_ptr to the non-trivial state of bound value parameters. No longer specialize that shared state to the particular action signature, encoding that information instead only in the instantiation of the implementation function.
PiperOrigin-RevId: 341116208
GMock: Make Truly explain when it fails
I just wrote a test that had a matcher of the form
Optional(AllOf(
SomeMatcher,
SomeOtherMatcher,
Truly(SomePredicate)))
The predicate failed, the other two matchers succeeded, and I got a hard-to-interpret message saying that the value in the optional "didn't match". Didn't match what?
This change improves situations like that slightly by having Truly explain to its result listener when it fails. When there are multiple Trulys in an AllOf, there will be some ambiguity, but it will at least provide more information than right now.
PiperOrigin-RevId: 341105141
Remove obsolete comment about non-const references.
They used to be banned by the C++ style guide, but positions shifted, and this comment is no longer up to date.
There's another reference (heh) on lines 816-819 to how gmock is a "general framework", with the possible implication that support for non-const references was weird in some way and only there to be "general", but I left it alone because I don't really feel I understand what it's saying.
PiperOrigin-RevId: 339323428
Fix some issues when running fuse_gmock_files.
The module path should be updated before importing `fuse_gtest_files`, since
the script may not run from the googletest repo root. We also need a non-frozen
set in order to track progress.
PiperOrigin-RevId: 337380466
Suggest using generic lambdas for composing macros.
Long chains of macros hurt legibility; generic lambdas are an easy way to abbreviate them, but are not an obvious solution to casual users.
Compare:
EXPECT_THAT(f(), ElementsAre(
Property(&MyClass::foo, Property(&OtherClass::bar, Contains("x"))),
Property(&MyClass::foo, Property(&OtherClass::bar, Contains("y"))));
to:
EXPECT_THAT(f(), ElementsAre(HasFooBar("x"), HasFooBar("y")));
PiperOrigin-RevId: 336870137
Please note that changing the default value for a type can make [you]* test.....
Here "you" word doesn't make sense.. rather "your" seems to make it understandable.
Mark ACTION_Pn()-generated functions as must-use-result.
This catches when a client creates an action and discards it, thinking that the action has actually been applied to something.
This will help people who make the mistake of defining, for example, both `void Use(Foo*)` and `ACTION(Use) { Use(arg); }` for later application to a Foo. With such an overload, a client may then write `Use();`, forgetting the param and being confused why nothing happens.
This also catches when a client defines their own action in terms of an ACTION()-generated one, invokes the ACTION's builder, and then fails to invoke the resulting action, thinking it's operating on the outer action's parameters.
PiperOrigin-RevId: 330614454
Replace uses of ACTION_TEMPLATE and ACTION_P with manually written functors.
The latter provide better error diagnostics.
This fixes https://github.com/google/googletest/issues/2729.
PiperOrigin-RevId: 328573022
Workaround static assert in early versions libc++
The error is "Attempted to construct a reference element in a tuple with an
rvalue". We can fix this by putting everything into a non temporary tuple_args
and implitly convert to the other tuple types. This avoids binding an rvalue
reference to an lvalue reference inside the tuple.
PiperOrigin-RevId: 327624990
Fix DoAll to work with move-only sink arguments.
This changes types of the first n - 1 actions so that they only get a readonly
view of the arguments. The last action will accept move only objects.
PiperOrigin-RevId: 327031893
Fixing exception-safety bug in googletest. Previously, if an exception was
thrown during a call to a mock that would have triggered an error, the error
was discarded.
Fixes#2890
PiperOrigin-RevId: 325017806
Fix DoAll to work with move-only sink arguments.
This changes types of the first n - 1 actions so that they only get a readonly
view of the arguments. The last action will accept move only objects.
PiperOrigin-RevId: 324619666
Fix DoAll to work with move-only sink arguments.
This changes types of the first n - 1 actions so that they only get a readonly
view of the arguments. The last action will accept move only objects.
PiperOrigin-RevId: 324600664
Improve compatibility with strict compilers targeting Windows
Remove an unnecessary ##, which could result in warnings about invalid preprocessor tokens when pasting to an initial '('
PiperOrigin-RevId: 319277617
gMock Cookbook: Fix incorrect comment about EXPECT priority order
It's actually the last matching expectation that's used, not the first.
PiperOrigin-RevId: 316490770
gMock Cookbook: Slight rewording
Remove "I" because documentation can have multiple authors. And remove unnecessary "guy".
PiperOrigin-RevId: 314533746
Note that EXPECT_EQ(actual_value, expected_value) or EXPECT_THAT(actual_value, Eq(expected_value)) is preferred over EXPECT_THAT(actual_value, expected_value).
PiperOrigin-RevId: 314350852
Silence MSVC C4100 (unused formal parameter) to fix breakage from recently added testcase. This warning is silenced in many files throughout googletest, but was not needed here until this testcase was added.
PiperOrigin-RevId: 312121200
Mark ACTION_Pn()-generated functions as must-use-result.
This catches when a client creates an action and discards it, thinking that the action has actually been applied to something.
This will help people who make the mistake of defining, for example, both `void Use(Foo*)` and `ACTION(Use) { Use(arg); }` for later application to a Foo. With such an overload, a client may then write `Use();`, forgetting the param and being confused why nothing happens.
This also catches when a client defines their own action in terms of an ACTION()-generated one, invokes the ACTION's builder, and then fails to invoke the resulting action, thinking it's operating on the outer action's parameters.
PiperOrigin-RevId: 312108101
Fix the ACTION* macros to allow for more than 10 arguments in the action.
Only the first 10 will be passed as individual arguments as `argN`, but the rest
can be accessed from the `args` tuple.
PiperOrigin-RevId: 311542098
Fix `-Wgnu-zero-variadic-macro-arguments` in GMock
Passing zero arguments to the variadic part of a macro is a GNU
extension and triggers warnings when build projects using GMock with
`-pedantic`.
- Fix uses of `GMOCK_PP_INTERNAL_16TH` to always receive at least 17
arguments. (this was triggered when `GMOCK_PP_NARG` or `GMOCK_PP_HAS_COMMA`
were used with an argument containing no commas).
- Fix `GMOCK_PP_HEAD` to append a dummy unused argument so that
`GMOCK_PP_INTERNAL_HEAD` always has two arguments.
PiperOrigin-RevId: 310414611
gmock-spec-builders.h:503:3: error:
definition of implicit copy constructor for 'Expectation' is deprecated
because it has a user-declared destructor [-Werror,-Wdeprecated]
~Expectation();
^
None of these are strictly needed for correctness.
A large number of them (maybe all of them?) trigger `-Wdeprecated`
warnings on Clang trunk as soon as you try to use the implicitly
defaulted (but deprecated) copy constructor of a class that has
deleted its copy assignment operator.
By declaring a deleted copy assignment operator, the old code
also caused the move constructor and move assignment operator
to be non-declared. This means that the old code never got move
semantics -- "move-construction" would simply call the defaulted
(but deprecated) copy constructor instead. With the new code,
"move-construction" calls the defaulted move constructor, which
I believe is what we want to happen. So this is a runtime
performance optimization.
Unfortunately we can't yet physically remove the definitions
of these macros from gtest-port.h, because they are being used
by other code internally at Google (according to zhangxy988).
But no new uses should be added going forward.
We are about to remove all uses of GTEST_DISALLOW_ASSIGN_ in favor
of using the Rule of Zero everywhere.
Unfortunately, if we use the Rule of Zero here, then when the compiler
needs to figure out if VariadicMatcher is move-constructible, it will
recurse down into `tuple<Args...>`, which on libstdc++ recurses too deeply.
In file included from googlemock/test/gmock-matchers_test.cc:43:
In file included from googlemock/include/gmock/gmock-matchers.h:258:
In file included from /usr/include/c++/5.5.0/algorithm:60:
In file included from /usr/include/c++/5.5.0/utility:70:
In file included from /usr/include/c++/5.5.0/bits/stl_pair.h:59:
In file included from /usr/include/c++/5.5.0/bits/move.h:57:
/usr/bin/include/c++/5.5.0/type_traits:115:26: fatal error:
recursive template instantiation exceeded maximum depth of 256
: public conditional<_B1::value, _B1, _B2>::type
^
The move constructor is the only problematic case, for some unknown reason.
With GTEST_DISALLOW_ASSIGN_, the presence of a copy assignment operator
causes the move constructor to be non-declared, thus non-defaulted, thus
non-problematic. Without GTEST_DISALLOW_ASSIGN_, we have to do one of the
following:
- Default the copy constructor, so that the move constructor will be non-declared.
- Define our own non-defaulted move constructor.
...except that doing the latter STILL did not work!
Fortunately, the former (default the copy constructor, don't provide
any move constructor) both works in practice and is semantically
equivalent to the old code.
Google Test and Google Mock require matching versions to work,
so this requirement should be described in the pkgconfig files.
This change is derived from the one used for the Fedora gtest package.
Signed-off-by: Neal Gompa <ngompa13@gmail.com>
When building packaged shared libraries for use, having the version
set makes it so that the soname is set correctly for parallel installation.
This change is derived from the one used for the Fedora gtest package.
Signed-off-by: Neal Gompa <ngompa13@gmail.com>
Add tests checking that ::testing::MockFunction template argument can
be deduced in a function call context. This is a property raised in the
review, however, not checked before by any tests.
Relax the implementation of MatcherCast to allow conversion of `Matcher<T>` to
`Matcher<const T&>`. They have the same match signature.
PiperOrigin-RevId: 297115843
Allow construction of an Action from a callable of zero args
Action already allows construction from a callable with the same args as the mocked function, without needing to wrap the callable in Invoke. However, if you don't care about the arguments to the mocked function you need to either accept all of them or wrap your callable in InvokeWithoutArgs. This change makes both of those unnecessary, since it allows you to pass a no-args callable to Action directly.
PiperOrigin-RevId: 296117034
Fix gmock_gen to use MOCK_METHOD instead of old style macros. Fix several
related bugs in argument parsing and return types.
- handle commas more correctly in return types
- handle commas correctly in arguments
- handle default values more correctly
PiperOrigin-RevId: 294435093
Get rid of gmock-generated-matchers.h and gmock-generated-matchers.h.pump.
Stop using pump for MATCHER* macroses generation.
PiperOrigin-RevId: 293878808
Tag the function generated by MATCHER with GTEST_ATTRIBUTE_UNUSED_ to fix CI builds of gmock-matchers_test.cc vs. -Wunused-function.
See https://github.com/google/googletest/pull/2697 for breakage.
PiperOrigin-RevId: 293669752
Get rid of gmock-generated-function-mockers.h and
gmock-generated-function-mockers.h.pump.
Stop using pump for GMOCK_METHOD* macroses generation.
PiperOrigin-RevId: 293454519
Pass method's parameters count to internal GMOCK_METHOD* macro.
This will help removing copypaste in every GMOCK_METHOD* macro in future.
PiperOrigin-RevId: 292932554
Create implementation macroses for matchers to move variadic parameters to the
end of parameters list.
To save backward compatibility, old macroses will be still taking `description`
parameter as the last one. But they will use INTERNAL macro that takes
`description` as the second parameter.
PiperOrigin-RevId: 291724469
Move part of functionality of Matcher* class to the base one. Reduce copypaste.
Make constructor and conversion operator of Matcher* class independent of pump.
PiperOrigin-RevId: 291405510
Add missing explicit keyword for gmock_Impl constructor.
When switching to using GMOCK_PP in ACTION* macroses `explicit` keyword was
missed in gmock_Impl constructor causing ClangTidy warnings in ACTION_P macro.
PiperOrigin-RevId: 291159975
Allow copying of the string in MatchAndExplain.
Otherwise, conversions from std::string_view to std::string will fail as being
explicit
PiperOrigin-RevId: 290301103
Use GMOCK_PP to create GMOCK_INTERNAL_ACTION macro.
Create GMOCK_INTERNAL_ACTION macro that generates ACTION_P* macroses using
GMOCK_PP.
PiperOrigin-RevId: 289815906
Use GMOCK_PP to generate args boilerplate.
Move common args describing part to separate macroses that uses GMOCK_PP to
generate sequences.
PiperOrigin-RevId: 289655624
Move part of functionality of Action* class to the base one. Reduce copypaste.
Make constructor and conversion operator of Action* class independent of pump.
PiperOrigin-RevId: 288907005
Use C++11 variadic templates for Invoke in gmock-generated-actions.h.
Replace InvokeArgumentAdl with Invoke that uses C++11 variadic templates.
PiperOrigin-RevId: 288449236
Explicitly default copy constructor in BoundSecondMatcher
Since C++11, implicit defaulting of copy constructors is deprecated for types
with user-defined copy assignment operators, so we should explicitly default the
copy constructor of BoundSecondMatcher.
PiperOrigin-RevId: 287587847
Use C++11 variadic templates for InvokeArgumentAdl in gmock-generated-actions.h.
Make InvokeArgumentAdl use variadic templates to generate its overloads instead
of using pump.py syntax.
PiperOrigin-RevId: 286267615
Use C++11 variadic templates for InvokeArgumentAdl in gmock-generated-actions.h.
Make InvokeArgumentAdl use variadic templates to generate its overloads instead
of using pump.py syntax.
PiperOrigin-RevId: 286148805
Don't use fully qualified ::std types in code examples.
Having a nested user-defined 'std' namespace anywhere in a program is a minefield and shouldn't be either explicitly supported or implicitly condoned.
PiperOrigin-RevId: 285790182
Use C++11 variadic templates for ActionHelper in gmock-generated-actions.h.
Make ActionHelper use variadic templates to generate Perform static member function specializations instead of using pump.py syntax.
PiperOrigin-RevId: 284988441
Clarify use of IsTrue and IsFalse matchers.
These matchers are subtle and confusing: what are they for?
The docs in the code are clear, but not very accessible.
googletest/googlemock/include/gmock/gmock-more-matchers.h
PiperOrigin-RevId: 283393275
Use standard C++11 integer types in gtest-port.h.
Remove testing::internal::{Int,Uint}{32,64} in favor of types
guaranteed to be in <cstdint> since C++11.
Tests for built-in integer type coverage are switched from
{Int,Uint}64 to [unsigned] long long, which is guaranteed by
C++11 to exist and be at least 64-bit wide.
PiperOrigin-RevId: 281565263
Add a breadcrumb about nullopt comparison near the Optional() matcher.
Also add a note about how otherwise-spurious 'Eq()' may be needed in some cases. Without this, something like Field(&MyStruct::optional_field_without_equals_equals, absl::nullopt) doesn't work - it converts the nullopt to an optional<> of the non-equalable type, and fails to select the operator==(optional<>, nullopt_t) overload. The Eq() lets the type persist later into the match.
PiperOrigin-RevId: 281305519
Remove the threads link library variable references from the pkg-config
Cflags: field, removing -lpthread(s) from the compile flags.
"-l*" linker flags should only be part of the Libs: section and should
not be part of the Cflags: section in pkg-config files.
This was first suggested in
https://github.com/google/googletest/pull/2006 and further discussed in
https://github.com/google/googletest/pull/2483 .
Merge 65032e28cba171c000accc85ffaf6f1e62921b86 into 8c91ecef29Closes#2470
COPYBARA_INTEGRATE_REVIEW=https://github.com/google/googletest/pull/2470 from hermas55:bugfix/default_const_param 65032e28cba171c000accc85ffaf6f1e62921b86
PiperOrigin-RevId: 277118535
Avoid temporary matcher instances.
They are unnecessary and can be relatively more expensive than the rest of the
algorithm.
PiperOrigin-RevId: 277084853
Add a matcher `testing::ReturnRoundRobin` which, on each call, returns the next element in the sequence, restarting at the beginning once it has reached the end.
PiperOrigin-RevId: 276312136
Remove bool_constant in favor of std::integral_constant<bool, ...>;
The one non-trivial use of bool_constant has been changed to have significantly
fewer template specializations.
PiperOrigin-RevId: 275842490
Rolling forward IsNan() matcher with fixes in test for -Wconversion issues. Use
std::nanf and std::nanl where appropriate.
PiperOrigin-RevId: 275523003