From 5b4166f05fbc133d165b54e25fef2c88430bbc2c Mon Sep 17 00:00:00 2001 From: Maurice Gilden Date: Thu, 27 Jul 2017 11:12:12 +0200 Subject: Add function name to exception if there's no default action --- googlemock/src/gmock-spec-builders.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/googlemock/src/gmock-spec-builders.cc b/googlemock/src/gmock-spec-builders.cc index 2fa1ee4b..f761f97e 100644 --- a/googlemock/src/gmock-spec-builders.cc +++ b/googlemock/src/gmock-spec-builders.cc @@ -364,7 +364,7 @@ UntypedFunctionMockerBase::UntypedInvokeWith(const void* const untyped_args) if (!need_to_report_uninteresting_call) { // Perform the action without printing the call information. - return this->UntypedPerformDefaultAction(untyped_args, ""); + return this->UntypedPerformDefaultAction(untyped_args, "Function call: " + std::string(Name())); } // Warns about the uninteresting call. -- cgit v1.2.3 From 95f18d99383c27bf645e8dc4f5dcaa188f6bafe3 Mon Sep 17 00:00:00 2001 From: Maurice Gilden Date: Fri, 18 Aug 2017 11:21:28 +0200 Subject: adds test for NiceMock with unknown return value --- googlemock/test/gmock-nice-strict_test.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/googlemock/test/gmock-nice-strict_test.cc b/googlemock/test/gmock-nice-strict_test.cc index 5d6ccc4f..5e6d53be 100644 --- a/googlemock/test/gmock-nice-strict_test.cc +++ b/googlemock/test/gmock-nice-strict_test.cc @@ -79,6 +79,7 @@ class MockFoo : public Foo { MOCK_METHOD0(DoThis, void()); MOCK_METHOD1(DoThat, int(bool flag)); + MOCK_METHOD0(ReturnSomething, Mock()); private: GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo); @@ -207,6 +208,20 @@ TEST(NiceMockTest, AllowsExpectedCall) { nice_foo.DoThis(); } +// Tests that an unexpected call on a nice mock which returns a non-built in +// default value throws an exception and the exception contains the name of +// the method. +TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) { + NiceMock nice_foo; + try { + nice_foo.ReturnSomething(); + FAIL(); + } catch (const std::runtime_error& ex) { + const std::string exception_msg(ex.what()); + EXPECT_NE(exception_msg.find("ReturnSomething"), std::string::npos); + } +} + // Tests that an unexpected call on a nice mock fails. TEST(NiceMockTest, UnexpectedCallFails) { NiceMock nice_foo; -- cgit v1.2.3 From cc99900036ae3514d8918acba87817fa24f6c993 Mon Sep 17 00:00:00 2001 From: Maurice Gilden Date: Fri, 18 Aug 2017 11:46:15 +0200 Subject: Fix test if exceptions are not supported --- googlemock/test/gmock-nice-strict_test.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/googlemock/test/gmock-nice-strict_test.cc b/googlemock/test/gmock-nice-strict_test.cc index 5e6d53be..86706814 100644 --- a/googlemock/test/gmock-nice-strict_test.cc +++ b/googlemock/test/gmock-nice-strict_test.cc @@ -213,6 +213,7 @@ TEST(NiceMockTest, AllowsExpectedCall) { // the method. TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) { NiceMock nice_foo; +#if GTEST_HAS_EXCEPTIONS try { nice_foo.ReturnSomething(); FAIL(); @@ -220,6 +221,11 @@ TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) { const std::string exception_msg(ex.what()); EXPECT_NE(exception_msg.find("ReturnSomething"), std::string::npos); } +#else + EXPECT_DEATH_IF_SUPPORTED({ + nice_foo.ReturnSomething(); + }, ""); +#endif } // Tests that an unexpected call on a nice mock fails. -- cgit v1.2.3 From 36777251c07788549eaa72a9be0cf482ab322c46 Mon Sep 17 00:00:00 2001 From: Maurice Gilden Date: Fri, 18 Aug 2017 12:28:50 +0200 Subject: Switch return type to class without default constructor --- googlemock/test/gmock-nice-strict_test.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/googlemock/test/gmock-nice-strict_test.cc b/googlemock/test/gmock-nice-strict_test.cc index 86706814..1d7784b1 100644 --- a/googlemock/test/gmock-nice-strict_test.cc +++ b/googlemock/test/gmock-nice-strict_test.cc @@ -62,6 +62,12 @@ using testing::internal::CaptureStdout; using testing::internal::GetCapturedStdout; #endif +// Dummy class without default constructor. +class Dummy { + public: + Dummy(int) {} +}; + // Defines some mock classes needed by the tests. class Foo { @@ -79,7 +85,7 @@ class MockFoo : public Foo { MOCK_METHOD0(DoThis, void()); MOCK_METHOD1(DoThat, int(bool flag)); - MOCK_METHOD0(ReturnSomething, Mock()); + MOCK_METHOD0(ReturnSomething, Dummy()); private: GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo); -- cgit v1.2.3 From b0ed43e72447c99f297dc86a75d7d58d53af5a07 Mon Sep 17 00:00:00 2001 From: Maurice Gilden Date: Fri, 18 Aug 2017 15:27:02 +0200 Subject: Change tabs to spaces in test case --- googlemock/test/gmock-nice-strict_test.cc | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/googlemock/test/gmock-nice-strict_test.cc b/googlemock/test/gmock-nice-strict_test.cc index 1d7784b1..a8032e24 100644 --- a/googlemock/test/gmock-nice-strict_test.cc +++ b/googlemock/test/gmock-nice-strict_test.cc @@ -218,19 +218,19 @@ TEST(NiceMockTest, AllowsExpectedCall) { // default value throws an exception and the exception contains the name of // the method. TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) { - NiceMock nice_foo; + NiceMock nice_foo; #if GTEST_HAS_EXCEPTIONS - try { - nice_foo.ReturnSomething(); - FAIL(); - } catch (const std::runtime_error& ex) { - const std::string exception_msg(ex.what()); - EXPECT_NE(exception_msg.find("ReturnSomething"), std::string::npos); - } + try { + nice_foo.ReturnSomething(); + FAIL(); + } catch (const std::runtime_error& ex) { + const std::string exception_msg(ex.what()); + EXPECT_NE(exception_msg.find("ReturnSomething"), std::string::npos); + } #else - EXPECT_DEATH_IF_SUPPORTED({ - nice_foo.ReturnSomething(); - }, ""); + EXPECT_DEATH_IF_SUPPORTED({ + nice_foo.ReturnSomething(); + }, ""); #endif } -- cgit v1.2.3 From 026735daf34cf180e34a976b3167cc4b311e3f11 Mon Sep 17 00:00:00 2001 From: Gennadiy Civil Date: Sun, 20 Aug 2017 15:15:31 -0400 Subject: Proposing these changes, please review Slightly better names and cleaner tests. Please review --- googlemock/test/gmock-nice-strict_test.cc | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/googlemock/test/gmock-nice-strict_test.cc b/googlemock/test/gmock-nice-strict_test.cc index a8032e24..2cb0a96d 100644 --- a/googlemock/test/gmock-nice-strict_test.cc +++ b/googlemock/test/gmock-nice-strict_test.cc @@ -62,10 +62,10 @@ using testing::internal::CaptureStdout; using testing::internal::GetCapturedStdout; #endif -// Dummy class without default constructor. -class Dummy { +// Class without default constructor. +class NotDefaultConstructible { public: - Dummy(int) {} + NotDefaultConstructible(int) {} }; // Defines some mock classes needed by the tests. @@ -85,7 +85,7 @@ class MockFoo : public Foo { MOCK_METHOD0(DoThis, void()); MOCK_METHOD1(DoThat, int(bool flag)); - MOCK_METHOD0(ReturnSomething, Dummy()); + MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible()); private: GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo); @@ -214,23 +214,20 @@ TEST(NiceMockTest, AllowsExpectedCall) { nice_foo.DoThis(); } -// Tests that an unexpected call on a nice mock which returns a non-built in -// default value throws an exception and the exception contains the name of -// the method. +// Tests that an unexpected call on a nice mock which returns a not-default-constructible +// type throws an exception and the exception contains the method's name. TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) { NiceMock nice_foo; #if GTEST_HAS_EXCEPTIONS try { - nice_foo.ReturnSomething(); + nice_foo.ReturnNonDefaultConstructible(); FAIL(); } catch (const std::runtime_error& ex) { const std::string exception_msg(ex.what()); - EXPECT_NE(exception_msg.find("ReturnSomething"), std::string::npos); + EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible")); } #else - EXPECT_DEATH_IF_SUPPORTED({ - nice_foo.ReturnSomething(); - }, ""); + EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, ""); #endif } -- cgit v1.2.3 From 3cf65b5d86d46cceb96ac44672fad84e2d5ad5a7 Mon Sep 17 00:00:00 2001 From: Gennadiy Civil Date: Sun, 20 Aug 2017 15:20:13 -0400 Subject: Added "explicit" as per compiler suggestion --- googlemock/test/gmock-nice-strict_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/googlemock/test/gmock-nice-strict_test.cc b/googlemock/test/gmock-nice-strict_test.cc index 2cb0a96d..fce9ca5b 100644 --- a/googlemock/test/gmock-nice-strict_test.cc +++ b/googlemock/test/gmock-nice-strict_test.cc @@ -65,7 +65,7 @@ using testing::internal::GetCapturedStdout; // Class without default constructor. class NotDefaultConstructible { public: - NotDefaultConstructible(int) {} + explicit NotDefaultConstructible(int) {} }; // Defines some mock classes needed by the tests. -- cgit v1.2.3 From 1ee8079651584b6bcc444f4b7a66dd2c65a79eb6 Mon Sep 17 00:00:00 2001 From: Maurice Gilden Date: Mon, 21 Aug 2017 10:10:14 +0200 Subject: Remove unused variable --- googlemock/test/gmock-nice-strict_test.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/googlemock/test/gmock-nice-strict_test.cc b/googlemock/test/gmock-nice-strict_test.cc index fce9ca5b..0eac6439 100644 --- a/googlemock/test/gmock-nice-strict_test.cc +++ b/googlemock/test/gmock-nice-strict_test.cc @@ -223,7 +223,6 @@ TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) { nice_foo.ReturnNonDefaultConstructible(); FAIL(); } catch (const std::runtime_error& ex) { - const std::string exception_msg(ex.what()); EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible")); } #else -- cgit v1.2.3 From 966b549c88032ec43ecd344ab19ca9ca36c30ad9 Mon Sep 17 00:00:00 2001 From: Roman Perepelitsa Date: Tue, 22 Aug 2017 16:06:26 +0200 Subject: Support ref-qualified member functions in Property(). --- googlemock/include/gmock/gmock-matchers.h | 35 ++++++++++++++++++++++++------- googlemock/test/gmock-matchers_test.cc | 20 ++++++++++++++++++ 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index 3a97c438..c446bf7d 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -2232,7 +2232,10 @@ class FieldMatcher { // Implements the Property() matcher for matching a property // (i.e. return value of a getter method) of an object. -template +// +// Property is a const-qualified member function of Class returning +// PropertyType. +template class PropertyMatcher { public: // The property may have a reference type, so 'const PropertyType&' @@ -2241,8 +2244,7 @@ class PropertyMatcher { // PropertyType being a reference or not. typedef GTEST_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty; - PropertyMatcher(PropertyType (Class::*property)() const, - const Matcher& matcher) + PropertyMatcher(Property property, const Matcher& matcher) : property_(property), matcher_(matcher) {} void DescribeTo(::std::ostream* os) const { @@ -2295,7 +2297,7 @@ class PropertyMatcher { return MatchAndExplainImpl(false_type(), *p, listener); } - PropertyType (Class::*property_)() const; + Property property_; const Matcher matcher_; GTEST_DISALLOW_ASSIGN_(PropertyMatcher); @@ -3908,11 +3910,13 @@ inline PolymorphicMatcher< // Property(&Foo::str, StartsWith("hi")) // matches a Foo object x iff x.str() starts with "hi". template -inline PolymorphicMatcher< - internal::PropertyMatcher > Property( - PropertyType (Class::*property)() const, const PropertyMatcher& matcher) { +inline PolymorphicMatcher > +Property(PropertyType (Class::*property)() const, + const PropertyMatcher& matcher) { return MakePolymorphicMatcher( - internal::PropertyMatcher( + internal::PropertyMatcher( property, MatcherCast(matcher))); // The call to MatcherCast() is required for supporting inner @@ -3921,6 +3925,21 @@ inline PolymorphicMatcher< // to compile where bar() returns an int32 and m is a matcher for int64. } +#if GTEST_LANG_CXX11 +// The same as above but for reference-qualified member functions. +template +inline PolymorphicMatcher > +Property(PropertyType (Class::*property)() const &, + const PropertyMatcher& matcher) { + return MakePolymorphicMatcher( + internal::PropertyMatcher( + property, + MatcherCast(matcher))); +} +#endif + // Creates a matcher that matches an object iff the result of applying // a callable to x matches 'matcher'. // For example, diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc index f5ab7c81..fc867487 100644 --- a/googlemock/test/gmock-matchers_test.cc +++ b/googlemock/test/gmock-matchers_test.cc @@ -3588,10 +3588,15 @@ class AClass { // A getter that returns a reference to const. const std::string& s() const { return s_; } +#if GTEST_LANG_CXX11 + const std::string& s_ref() const & { return s_; } +#endif + void set_s(const std::string& new_s) { s_ = new_s; } // A getter that returns a reference to non-const. double& x() const { return x_; } + private: int n_; std::string s_; @@ -3635,6 +3640,21 @@ TEST(PropertyTest, WorksForReferenceToConstProperty) { EXPECT_FALSE(m.Matches(a)); } +#if GTEST_LANG_CXX11 +// Tests that Property(&Foo::property, ...) works when property() is +// ref-qualified. +TEST(PropertyTest, WorksForRefQualifiedProperty) { + Matcher m = Property(&AClass::s_ref, StartsWith("hi")); + + AClass a; + a.set_s("hill"); + EXPECT_TRUE(m.Matches(a)); + + a.set_s("hole"); + EXPECT_FALSE(m.Matches(a)); +} +#endif + // Tests that Property(&Foo::property, ...) works when property() // returns a reference to non-const. TEST(PropertyTest, WorksForReferenceToNonConstProperty) { -- cgit v1.2.3