summaryrefslogtreecommitdiffstats
path: root/googlemock/test
diff options
context:
space:
mode:
authorAaron Jacobs <jacobsa@google.com>2022-04-27 15:48:35 (GMT)
committerCopybara-Service <copybara-worker@google.com>2022-04-27 15:49:18 (GMT)
commit830fb567285c63ab5b5873e2e8b02f2249864916 (patch)
tree6e7af05a8663ae974f3b7daf49bbf111eaefb055 /googlemock/test
parentc144d78f8295da3dbae3ad2d5fe66a9a42f8ce74 (diff)
downloadgoogletest-830fb567285c63ab5b5873e2e8b02f2249864916.zip
googletest-830fb567285c63ab5b5873e2e8b02f2249864916.tar.gz
googletest-830fb567285c63ab5b5873e2e8b02f2249864916.tar.bz2
gmock-actions: improve comments and tests for the implicit cast in Return.
Commit a070cbd91c added an implicit cast to this path but didn't leave a very clear explanation for why it was desirable, a clear example, or even test coverage. Add a better comment and a test that fails when the implicit cast is removed. PiperOrigin-RevId: 444871311 Change-Id: I127982fa8d5bce9b6d1b68177c12dc0709449164
Diffstat (limited to 'googlemock/test')
-rw-r--r--googlemock/test/gmock-actions_test.cc31
1 files changed, 31 insertions, 0 deletions
diff --git a/googlemock/test/gmock-actions_test.cc b/googlemock/test/gmock-actions_test.cc
index a33358a..db112f1 100644
--- a/googlemock/test/gmock-actions_test.cc
+++ b/googlemock/test/gmock-actions_test.cc
@@ -667,6 +667,37 @@ TEST(ReturnTest, SupportsWrapperReturnType) {
EXPECT_THAT(result, ::testing::ElementsAre(0, 1, 2, 3, 4));
}
+TEST(ReturnTest, PrefersConversionOperator) {
+ // Define types In and Out such that:
+ //
+ // * In is implicitly convertible to Out.
+ // * Out also has an explicit constructor from In.
+ //
+ struct In;
+ struct Out {
+ int x;
+
+ explicit Out(const int x) : x(x) {}
+ explicit Out(const In&) : x(0) {}
+ };
+
+ struct In {
+ operator Out() const { return Out{19}; } // NOLINT
+ };
+
+ // Assumption check: the C++ language rules are such that a function that
+ // returns Out which uses In a return statement will use the implicit
+ // conversion path rather than the explicit constructor.
+ EXPECT_THAT([]() -> Out { return In(); }(), Field(&Out::x, 19));
+
+ // Return should work the same way: if the mock function's return type is Out
+ // and we feed Return an In value, then the Out should be created through the
+ // implicit conversion path rather than the explicit constructor.
+ MockFunction<Out()> mock;
+ EXPECT_CALL(mock, Call).WillOnce(Return(In()));
+ EXPECT_THAT(mock.AsStdFunction()(), Field(&Out::x, 19));
+}
+
// Tests that Return(v) is covaraint.
struct Base {