2008-12-10 05:08:54 +00:00
|
|
|
// Copyright 2008, Google Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived from
|
|
|
|
// this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2018-08-14 14:04:07 -04:00
|
|
|
|
2018-12-21 04:17:43 -05:00
|
|
|
#include "gmock/gmock-nice-strict.h"
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
#include <string>
|
2018-04-12 14:00:38 -04:00
|
|
|
#include <utility>
|
2022-03-15 13:41:41 -07:00
|
|
|
|
2010-09-14 05:38:21 +00:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest-spi.h"
|
2018-04-12 14:00:38 -04:00
|
|
|
#include "gtest/gtest.h"
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2009-09-16 17:38:08 +00:00
|
|
|
// This must not be defined inside the ::testing namespace, or it will
|
|
|
|
// clash with ::testing::Mock.
|
|
|
|
class Mock {
|
|
|
|
public:
|
2023-04-21 10:40:36 -07:00
|
|
|
Mock() = default;
|
2009-12-23 00:13:23 +00:00
|
|
|
|
2009-09-16 17:38:08 +00:00
|
|
|
MOCK_METHOD0(DoThis, void());
|
2009-12-23 00:13:23 +00:00
|
|
|
|
|
|
|
private:
|
2022-04-22 11:25:26 -07:00
|
|
|
Mock(const Mock&) = delete;
|
|
|
|
Mock& operator=(const Mock&) = delete;
|
2009-09-16 17:38:08 +00:00
|
|
|
};
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
namespace testing {
|
|
|
|
namespace gmock_nice_strict_test {
|
|
|
|
|
|
|
|
using testing::HasSubstr;
|
2013-03-01 01:54:22 +00:00
|
|
|
using testing::NaggyMock;
|
2008-12-10 05:08:54 +00:00
|
|
|
using testing::NiceMock;
|
|
|
|
using testing::StrictMock;
|
|
|
|
|
2010-08-31 18:28:02 +00:00
|
|
|
#if GTEST_HAS_STREAM_REDIRECTION
|
2010-02-02 22:34:58 +00:00
|
|
|
using testing::internal::CaptureStdout;
|
|
|
|
using testing::internal::GetCapturedStdout;
|
2010-08-31 18:28:02 +00:00
|
|
|
#endif
|
2010-02-02 22:34:58 +00:00
|
|
|
|
2017-08-20 15:15:31 -04:00
|
|
|
// Class without default constructor.
|
|
|
|
class NotDefaultConstructible {
|
2017-08-18 12:28:50 +02:00
|
|
|
public:
|
2017-08-20 15:20:13 -04:00
|
|
|
explicit NotDefaultConstructible(int) {}
|
2017-08-18 12:28:50 +02:00
|
|
|
};
|
|
|
|
|
2020-12-21 15:41:17 -05:00
|
|
|
class CallsMockMethodInDestructor {
|
|
|
|
public:
|
|
|
|
~CallsMockMethodInDestructor() { OnDestroy(); }
|
|
|
|
MOCK_METHOD(void, OnDestroy, ());
|
|
|
|
};
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Defines some mock classes needed by the tests.
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
public:
|
2023-04-21 10:40:36 -07:00
|
|
|
virtual ~Foo() = default;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
virtual void DoThis() = 0;
|
|
|
|
virtual int DoThat(bool flag) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MockFoo : public Foo {
|
|
|
|
public:
|
2023-04-21 10:40:36 -07:00
|
|
|
MockFoo() = default;
|
2008-12-10 05:08:54 +00:00
|
|
|
void Delete() { delete this; }
|
|
|
|
|
|
|
|
MOCK_METHOD0(DoThis, void());
|
|
|
|
MOCK_METHOD1(DoThat, int(bool flag));
|
2017-08-20 15:15:31 -04:00
|
|
|
MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible());
|
2009-12-23 00:13:23 +00:00
|
|
|
|
|
|
|
private:
|
2022-04-22 11:25:26 -07:00
|
|
|
MockFoo(const MockFoo&) = delete;
|
|
|
|
MockFoo& operator=(const MockFoo&) = delete;
|
2008-12-10 05:08:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class MockBar {
|
|
|
|
public:
|
2017-05-15 17:07:03 -04:00
|
|
|
explicit MockBar(const std::string& s) : str_(s) {}
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2017-05-15 17:07:03 -04:00
|
|
|
MockBar(char a1, char a2, std::string a3, std::string a4, int a5, int a6,
|
|
|
|
const std::string& a7, const std::string& a8, bool a9, bool a10) {
|
|
|
|
str_ = std::string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
|
2022-03-15 13:41:41 -07:00
|
|
|
static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') +
|
|
|
|
(a10 ? 'T' : 'F');
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-21 10:40:36 -07:00
|
|
|
virtual ~MockBar() = default;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2017-05-15 17:07:03 -04:00
|
|
|
const std::string& str() const { return str_; }
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
MOCK_METHOD0(This, int());
|
2017-05-15 17:07:03 -04:00
|
|
|
MOCK_METHOD2(That, std::string(int, bool));
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
private:
|
2017-05-15 17:07:03 -04:00
|
|
|
std::string str_;
|
2009-12-23 00:13:23 +00:00
|
|
|
|
2022-04-22 11:25:26 -07:00
|
|
|
MockBar(const MockBar&) = delete;
|
|
|
|
MockBar& operator=(const MockBar&) = delete;
|
2008-12-10 05:08:54 +00:00
|
|
|
};
|
|
|
|
|
2018-04-12 14:00:38 -04:00
|
|
|
class MockBaz {
|
|
|
|
public:
|
|
|
|
class MoveOnly {
|
2018-12-17 18:59:00 -05:00
|
|
|
public:
|
2018-04-12 14:00:38 -04:00
|
|
|
MoveOnly() = default;
|
|
|
|
|
|
|
|
MoveOnly(const MoveOnly&) = delete;
|
2018-12-17 18:59:00 -05:00
|
|
|
MoveOnly& operator=(const MoveOnly&) = delete;
|
2018-04-12 14:00:38 -04:00
|
|
|
|
|
|
|
MoveOnly(MoveOnly&&) = default;
|
2018-12-17 18:59:00 -05:00
|
|
|
MoveOnly& operator=(MoveOnly&&) = default;
|
2018-04-12 14:00:38 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
MockBaz(MoveOnly) {}
|
2018-12-17 18:59:00 -05:00
|
|
|
};
|
2018-04-12 14:00:38 -04:00
|
|
|
|
2010-08-31 18:28:02 +00:00
|
|
|
#if GTEST_HAS_STREAM_REDIRECTION
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2013-03-01 07:10:07 +00:00
|
|
|
// Tests that a raw mock generates warnings for uninteresting calls.
|
|
|
|
TEST(RawMockTest, WarningForUninterestingCall) {
|
2021-09-14 14:59:42 -04:00
|
|
|
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
|
|
|
GMOCK_FLAG_SET(verbose, "warning");
|
2013-03-01 07:10:07 +00:00
|
|
|
|
|
|
|
MockFoo raw_foo;
|
|
|
|
|
|
|
|
CaptureStdout();
|
|
|
|
raw_foo.DoThis();
|
|
|
|
raw_foo.DoThat(true);
|
|
|
|
EXPECT_THAT(GetCapturedStdout(),
|
|
|
|
HasSubstr("Uninteresting mock function call"));
|
|
|
|
|
2021-09-14 14:59:42 -04:00
|
|
|
GMOCK_FLAG_SET(verbose, saved_flag);
|
2013-03-01 07:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that a raw mock generates warnings for uninteresting calls
|
|
|
|
// that delete the mock object.
|
|
|
|
TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
|
2021-09-14 14:59:42 -04:00
|
|
|
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
|
|
|
GMOCK_FLAG_SET(verbose, "warning");
|
2013-03-01 07:10:07 +00:00
|
|
|
|
|
|
|
MockFoo* const raw_foo = new MockFoo;
|
|
|
|
|
2022-03-15 13:41:41 -07:00
|
|
|
ON_CALL(*raw_foo, DoThis()).WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
|
2013-03-01 07:10:07 +00:00
|
|
|
|
|
|
|
CaptureStdout();
|
|
|
|
raw_foo->DoThis();
|
|
|
|
EXPECT_THAT(GetCapturedStdout(),
|
|
|
|
HasSubstr("Uninteresting mock function call"));
|
|
|
|
|
2021-09-14 14:59:42 -04:00
|
|
|
GMOCK_FLAG_SET(verbose, saved_flag);
|
2013-03-01 07:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that a raw mock generates informational logs for
|
|
|
|
// uninteresting calls.
|
|
|
|
TEST(RawMockTest, InfoForUninterestingCall) {
|
|
|
|
MockFoo raw_foo;
|
|
|
|
|
2021-09-14 14:59:42 -04:00
|
|
|
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
|
|
|
GMOCK_FLAG_SET(verbose, "info");
|
2013-03-01 07:10:07 +00:00
|
|
|
CaptureStdout();
|
|
|
|
raw_foo.DoThis();
|
|
|
|
EXPECT_THAT(GetCapturedStdout(),
|
|
|
|
HasSubstr("Uninteresting mock function call"));
|
|
|
|
|
2021-09-14 14:59:42 -04:00
|
|
|
GMOCK_FLAG_SET(verbose, saved_flag);
|
2013-03-01 07:10:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 08:07:15 +02:00
|
|
|
TEST(RawMockTest, IsNaggy_IsNice_IsStrict) {
|
|
|
|
MockFoo raw_foo;
|
2018-10-23 15:13:41 -04:00
|
|
|
EXPECT_TRUE(Mock::IsNaggy(&raw_foo));
|
2018-10-16 08:07:15 +02:00
|
|
|
EXPECT_FALSE(Mock::IsNice(&raw_foo));
|
|
|
|
EXPECT_FALSE(Mock::IsStrict(&raw_foo));
|
|
|
|
}
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Tests that a nice mock generates no warning for uninteresting calls.
|
|
|
|
TEST(NiceMockTest, NoWarningForUninterestingCall) {
|
|
|
|
NiceMock<MockFoo> nice_foo;
|
|
|
|
|
2010-02-02 22:34:58 +00:00
|
|
|
CaptureStdout();
|
2008-12-10 05:08:54 +00:00
|
|
|
nice_foo.DoThis();
|
|
|
|
nice_foo.DoThat(true);
|
2013-03-01 01:54:22 +00:00
|
|
|
EXPECT_EQ("", GetCapturedStdout());
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that a nice mock generates no warning for uninteresting calls
|
|
|
|
// that delete the mock object.
|
|
|
|
TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
|
|
|
|
NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
|
|
|
|
|
|
|
|
ON_CALL(*nice_foo, DoThis())
|
|
|
|
.WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
|
|
|
|
|
2010-02-02 22:34:58 +00:00
|
|
|
CaptureStdout();
|
2008-12-10 05:08:54 +00:00
|
|
|
nice_foo->DoThis();
|
2013-03-01 01:54:22 +00:00
|
|
|
EXPECT_EQ("", GetCapturedStdout());
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that a nice mock generates informational logs for
|
|
|
|
// uninteresting calls.
|
|
|
|
TEST(NiceMockTest, InfoForUninterestingCall) {
|
|
|
|
NiceMock<MockFoo> nice_foo;
|
|
|
|
|
2021-09-14 14:59:42 -04:00
|
|
|
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
|
|
|
GMOCK_FLAG_SET(verbose, "info");
|
2010-02-02 22:34:58 +00:00
|
|
|
CaptureStdout();
|
2008-12-10 05:08:54 +00:00
|
|
|
nice_foo.DoThis();
|
2013-03-01 01:54:22 +00:00
|
|
|
EXPECT_THAT(GetCapturedStdout(),
|
2008-12-10 05:08:54 +00:00
|
|
|
HasSubstr("Uninteresting mock function call"));
|
|
|
|
|
2021-09-14 14:59:42 -04:00
|
|
|
GMOCK_FLAG_SET(verbose, saved_flag);
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
2010-08-31 18:28:02 +00:00
|
|
|
#endif // GTEST_HAS_STREAM_REDIRECTION
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
// Tests that a nice mock allows expected calls.
|
|
|
|
TEST(NiceMockTest, AllowsExpectedCall) {
|
|
|
|
NiceMock<MockFoo> nice_foo;
|
|
|
|
|
|
|
|
EXPECT_CALL(nice_foo, DoThis());
|
|
|
|
nice_foo.DoThis();
|
|
|
|
}
|
|
|
|
|
2018-04-12 14:00:38 -04:00
|
|
|
// 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.
|
2017-08-18 11:21:28 +02:00
|
|
|
TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) {
|
2017-08-18 15:27:02 +02:00
|
|
|
NiceMock<MockFoo> nice_foo;
|
2017-08-18 11:46:15 +02:00
|
|
|
#if GTEST_HAS_EXCEPTIONS
|
2017-08-18 15:27:02 +02:00
|
|
|
try {
|
2017-08-20 15:15:31 -04:00
|
|
|
nice_foo.ReturnNonDefaultConstructible();
|
2017-08-18 15:27:02 +02:00
|
|
|
FAIL();
|
|
|
|
} catch (const std::runtime_error& ex) {
|
2017-08-20 15:15:31 -04:00
|
|
|
EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible"));
|
2017-08-18 15:27:02 +02:00
|
|
|
}
|
2017-08-18 11:46:15 +02:00
|
|
|
#else
|
2017-08-20 15:15:31 -04:00
|
|
|
EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, "");
|
2017-08-18 11:46:15 +02:00
|
|
|
#endif
|
2017-08-18 11:21:28 +02:00
|
|
|
}
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Tests that an unexpected call on a nice mock fails.
|
|
|
|
TEST(NiceMockTest, UnexpectedCallFails) {
|
|
|
|
NiceMock<MockFoo> nice_foo;
|
|
|
|
|
|
|
|
EXPECT_CALL(nice_foo, DoThis()).Times(0);
|
|
|
|
EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that NiceMock works with a mock class that has a non-default
|
|
|
|
// constructor.
|
|
|
|
TEST(NiceMockTest, NonDefaultConstructor) {
|
|
|
|
NiceMock<MockBar> nice_bar("hi");
|
|
|
|
EXPECT_EQ("hi", nice_bar.str());
|
|
|
|
|
|
|
|
nice_bar.This();
|
|
|
|
nice_bar.That(5, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that NiceMock works with a mock class that has a 10-ary
|
|
|
|
// non-default constructor.
|
|
|
|
TEST(NiceMockTest, NonDefaultConstructor10) {
|
2022-03-15 13:41:41 -07:00
|
|
|
NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f', "g", "h", true,
|
|
|
|
false);
|
2008-12-10 05:08:54 +00:00
|
|
|
EXPECT_EQ("abcdefghTF", nice_bar.str());
|
|
|
|
|
|
|
|
nice_bar.This();
|
|
|
|
nice_bar.That(5, true);
|
|
|
|
}
|
|
|
|
|
Remove multiple inheritance from "unintesting call" mock classes.
Internal CL 156157936, which was published in commit
fe402c27790ff1cc9a7e17c5d0aea4ebe7fd8a71, introduced undefined behavior
by casting a base class (internal::{Naggy,Nice,Strict}Base<MockClass>,
using the curiously recurring template pattern) pointer to a derived
class ({Naggy,Nice,Strict}Mock<MockClass>), in the base class'
constructor. At that point, the object isn't guaranteed to have taken on
the shape of the derived class, and casting is undefined behavior.
The undefined behavior was caught by Chrome's CFI build bot [1], and
prevents rolling googletest past that commit / CL.
This commit simplifies the {Naggy,Nice,Strict}Mock class hierarchy in
a way that removes the undefined behavior.
[1] https://www.chromium.org/developers/testing/control-flow-integrity
2018-04-09 21:57:54 -07:00
|
|
|
TEST(NiceMockTest, AllowLeak) {
|
|
|
|
NiceMock<MockFoo>* leaked = new NiceMock<MockFoo>;
|
|
|
|
Mock::AllowLeak(leaked);
|
|
|
|
EXPECT_CALL(*leaked, DoThis());
|
|
|
|
leaked->DoThis();
|
|
|
|
}
|
|
|
|
|
2018-04-12 14:00:38 -04:00
|
|
|
TEST(NiceMockTest, MoveOnlyConstructor) {
|
2018-12-17 18:59:00 -05:00
|
|
|
NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly{});
|
2018-04-12 14:00:38 -04:00
|
|
|
}
|
|
|
|
|
2009-09-16 17:38:08 +00:00
|
|
|
// Tests that NiceMock<Mock> compiles where Mock is a user-defined
|
2018-12-28 06:03:51 -05:00
|
|
|
// class (as opposed to ::testing::Mock).
|
2009-09-16 17:38:08 +00:00
|
|
|
TEST(NiceMockTest, AcceptsClassNamedMock) {
|
|
|
|
NiceMock< ::Mock> nice;
|
|
|
|
EXPECT_CALL(nice, DoThis());
|
|
|
|
nice.DoThis();
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:41:17 -05:00
|
|
|
TEST(NiceMockTest, IsNiceInDestructor) {
|
|
|
|
{
|
|
|
|
NiceMock<CallsMockMethodInDestructor> nice_on_destroy;
|
|
|
|
// Don't add an expectation for the call before the mock goes out of scope.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 08:07:15 +02:00
|
|
|
TEST(NiceMockTest, IsNaggy_IsNice_IsStrict) {
|
|
|
|
NiceMock<MockFoo> nice_foo;
|
|
|
|
EXPECT_FALSE(Mock::IsNaggy(&nice_foo));
|
2018-10-23 15:13:41 -04:00
|
|
|
EXPECT_TRUE(Mock::IsNice(&nice_foo));
|
2018-10-16 08:07:15 +02:00
|
|
|
EXPECT_FALSE(Mock::IsStrict(&nice_foo));
|
|
|
|
}
|
|
|
|
|
2013-03-01 01:54:22 +00:00
|
|
|
#if GTEST_HAS_STREAM_REDIRECTION
|
|
|
|
|
|
|
|
// Tests that a naggy mock generates warnings for uninteresting calls.
|
|
|
|
TEST(NaggyMockTest, WarningForUninterestingCall) {
|
2021-09-14 14:59:42 -04:00
|
|
|
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
|
|
|
GMOCK_FLAG_SET(verbose, "warning");
|
2013-03-01 01:54:22 +00:00
|
|
|
|
|
|
|
NaggyMock<MockFoo> naggy_foo;
|
|
|
|
|
|
|
|
CaptureStdout();
|
|
|
|
naggy_foo.DoThis();
|
|
|
|
naggy_foo.DoThat(true);
|
|
|
|
EXPECT_THAT(GetCapturedStdout(),
|
|
|
|
HasSubstr("Uninteresting mock function call"));
|
|
|
|
|
2021-09-14 14:59:42 -04:00
|
|
|
GMOCK_FLAG_SET(verbose, saved_flag);
|
2013-03-01 01:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that a naggy mock generates a warning for an uninteresting call
|
|
|
|
// that deletes the mock object.
|
|
|
|
TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
|
2021-09-14 14:59:42 -04:00
|
|
|
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
|
|
|
GMOCK_FLAG_SET(verbose, "warning");
|
2013-03-01 01:54:22 +00:00
|
|
|
|
|
|
|
NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
|
|
|
|
|
|
|
|
ON_CALL(*naggy_foo, DoThis())
|
|
|
|
.WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
|
|
|
|
|
|
|
|
CaptureStdout();
|
|
|
|
naggy_foo->DoThis();
|
|
|
|
EXPECT_THAT(GetCapturedStdout(),
|
|
|
|
HasSubstr("Uninteresting mock function call"));
|
|
|
|
|
2021-09-14 14:59:42 -04:00
|
|
|
GMOCK_FLAG_SET(verbose, saved_flag);
|
2013-03-01 01:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // GTEST_HAS_STREAM_REDIRECTION
|
|
|
|
|
|
|
|
// Tests that a naggy mock allows expected calls.
|
|
|
|
TEST(NaggyMockTest, AllowsExpectedCall) {
|
|
|
|
NaggyMock<MockFoo> naggy_foo;
|
|
|
|
|
|
|
|
EXPECT_CALL(naggy_foo, DoThis());
|
|
|
|
naggy_foo.DoThis();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that an unexpected call on a naggy mock fails.
|
|
|
|
TEST(NaggyMockTest, UnexpectedCallFails) {
|
|
|
|
NaggyMock<MockFoo> naggy_foo;
|
|
|
|
|
|
|
|
EXPECT_CALL(naggy_foo, DoThis()).Times(0);
|
|
|
|
EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
|
|
|
|
"called more times than expected");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that NaggyMock works with a mock class that has a non-default
|
|
|
|
// constructor.
|
|
|
|
TEST(NaggyMockTest, NonDefaultConstructor) {
|
|
|
|
NaggyMock<MockBar> naggy_bar("hi");
|
|
|
|
EXPECT_EQ("hi", naggy_bar.str());
|
|
|
|
|
|
|
|
naggy_bar.This();
|
|
|
|
naggy_bar.That(5, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that NaggyMock works with a mock class that has a 10-ary
|
|
|
|
// non-default constructor.
|
|
|
|
TEST(NaggyMockTest, NonDefaultConstructor10) {
|
2022-03-15 13:41:41 -07:00
|
|
|
NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5', "6", "7", true,
|
|
|
|
false);
|
2013-03-01 01:54:22 +00:00
|
|
|
EXPECT_EQ("01234567TF", naggy_bar.str());
|
|
|
|
|
|
|
|
naggy_bar.This();
|
|
|
|
naggy_bar.That(5, true);
|
|
|
|
}
|
|
|
|
|
Remove multiple inheritance from "unintesting call" mock classes.
Internal CL 156157936, which was published in commit
fe402c27790ff1cc9a7e17c5d0aea4ebe7fd8a71, introduced undefined behavior
by casting a base class (internal::{Naggy,Nice,Strict}Base<MockClass>,
using the curiously recurring template pattern) pointer to a derived
class ({Naggy,Nice,Strict}Mock<MockClass>), in the base class'
constructor. At that point, the object isn't guaranteed to have taken on
the shape of the derived class, and casting is undefined behavior.
The undefined behavior was caught by Chrome's CFI build bot [1], and
prevents rolling googletest past that commit / CL.
This commit simplifies the {Naggy,Nice,Strict}Mock class hierarchy in
a way that removes the undefined behavior.
[1] https://www.chromium.org/developers/testing/control-flow-integrity
2018-04-09 21:57:54 -07:00
|
|
|
TEST(NaggyMockTest, AllowLeak) {
|
|
|
|
NaggyMock<MockFoo>* leaked = new NaggyMock<MockFoo>;
|
|
|
|
Mock::AllowLeak(leaked);
|
|
|
|
EXPECT_CALL(*leaked, DoThis());
|
|
|
|
leaked->DoThis();
|
|
|
|
}
|
|
|
|
|
2018-04-12 14:00:38 -04:00
|
|
|
TEST(NaggyMockTest, MoveOnlyConstructor) {
|
2018-12-17 18:59:00 -05:00
|
|
|
NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly{});
|
2018-04-12 14:00:38 -04:00
|
|
|
}
|
|
|
|
|
2013-03-01 01:54:22 +00:00
|
|
|
// Tests that NaggyMock<Mock> compiles where Mock is a user-defined
|
2018-12-28 06:03:51 -05:00
|
|
|
// class (as opposed to ::testing::Mock).
|
2013-03-01 01:54:22 +00:00
|
|
|
TEST(NaggyMockTest, AcceptsClassNamedMock) {
|
|
|
|
NaggyMock< ::Mock> naggy;
|
|
|
|
EXPECT_CALL(naggy, DoThis());
|
|
|
|
naggy.DoThis();
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:41:17 -05:00
|
|
|
TEST(NaggyMockTest, IsNaggyInDestructor) {
|
2021-09-14 14:59:42 -04:00
|
|
|
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
|
|
|
GMOCK_FLAG_SET(verbose, "warning");
|
2020-12-21 15:41:17 -05:00
|
|
|
CaptureStdout();
|
|
|
|
|
|
|
|
{
|
|
|
|
NaggyMock<CallsMockMethodInDestructor> naggy_on_destroy;
|
|
|
|
// Don't add an expectation for the call before the mock goes out of scope.
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_THAT(GetCapturedStdout(),
|
|
|
|
HasSubstr("Uninteresting mock function call"));
|
|
|
|
|
2021-09-14 14:59:42 -04:00
|
|
|
GMOCK_FLAG_SET(verbose, saved_flag);
|
2020-12-21 15:41:17 -05:00
|
|
|
}
|
|
|
|
|
2018-10-16 08:07:15 +02:00
|
|
|
TEST(NaggyMockTest, IsNaggy_IsNice_IsStrict) {
|
|
|
|
NaggyMock<MockFoo> naggy_foo;
|
2018-10-23 15:13:41 -04:00
|
|
|
EXPECT_TRUE(Mock::IsNaggy(&naggy_foo));
|
2018-10-16 08:07:15 +02:00
|
|
|
EXPECT_FALSE(Mock::IsNice(&naggy_foo));
|
|
|
|
EXPECT_FALSE(Mock::IsStrict(&naggy_foo));
|
|
|
|
}
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Tests that a strict mock allows expected calls.
|
|
|
|
TEST(StrictMockTest, AllowsExpectedCall) {
|
|
|
|
StrictMock<MockFoo> strict_foo;
|
|
|
|
|
|
|
|
EXPECT_CALL(strict_foo, DoThis());
|
|
|
|
strict_foo.DoThis();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that an unexpected call on a strict mock fails.
|
|
|
|
TEST(StrictMockTest, UnexpectedCallFails) {
|
|
|
|
StrictMock<MockFoo> strict_foo;
|
|
|
|
|
|
|
|
EXPECT_CALL(strict_foo, DoThis()).Times(0);
|
|
|
|
EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
|
|
|
|
"called more times than expected");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that an uninteresting call on a strict mock fails.
|
|
|
|
TEST(StrictMockTest, UninterestingCallFails) {
|
|
|
|
StrictMock<MockFoo> strict_foo;
|
|
|
|
|
|
|
|
EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
|
|
|
|
"Uninteresting mock function call");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that an uninteresting call on a strict mock fails, even if
|
|
|
|
// the call deletes the mock object.
|
|
|
|
TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
|
|
|
|
StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
|
|
|
|
|
|
|
|
ON_CALL(*strict_foo, DoThis())
|
|
|
|
.WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
|
|
|
|
|
|
|
|
EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
|
|
|
|
"Uninteresting mock function call");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that StrictMock works with a mock class that has a
|
|
|
|
// non-default constructor.
|
|
|
|
TEST(StrictMockTest, NonDefaultConstructor) {
|
|
|
|
StrictMock<MockBar> strict_bar("hi");
|
|
|
|
EXPECT_EQ("hi", strict_bar.str());
|
|
|
|
|
|
|
|
EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
|
|
|
|
"Uninteresting mock function call");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that StrictMock works with a mock class that has a 10-ary
|
|
|
|
// non-default constructor.
|
|
|
|
TEST(StrictMockTest, NonDefaultConstructor10) {
|
2022-03-15 13:41:41 -07:00
|
|
|
StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f', "g", "h", true,
|
|
|
|
false);
|
2008-12-10 05:08:54 +00:00
|
|
|
EXPECT_EQ("abcdefghTF", strict_bar.str());
|
|
|
|
|
|
|
|
EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
|
|
|
|
"Uninteresting mock function call");
|
|
|
|
}
|
|
|
|
|
Remove multiple inheritance from "unintesting call" mock classes.
Internal CL 156157936, which was published in commit
fe402c27790ff1cc9a7e17c5d0aea4ebe7fd8a71, introduced undefined behavior
by casting a base class (internal::{Naggy,Nice,Strict}Base<MockClass>,
using the curiously recurring template pattern) pointer to a derived
class ({Naggy,Nice,Strict}Mock<MockClass>), in the base class'
constructor. At that point, the object isn't guaranteed to have taken on
the shape of the derived class, and casting is undefined behavior.
The undefined behavior was caught by Chrome's CFI build bot [1], and
prevents rolling googletest past that commit / CL.
This commit simplifies the {Naggy,Nice,Strict}Mock class hierarchy in
a way that removes the undefined behavior.
[1] https://www.chromium.org/developers/testing/control-flow-integrity
2018-04-09 21:57:54 -07:00
|
|
|
TEST(StrictMockTest, AllowLeak) {
|
|
|
|
StrictMock<MockFoo>* leaked = new StrictMock<MockFoo>;
|
|
|
|
Mock::AllowLeak(leaked);
|
|
|
|
EXPECT_CALL(*leaked, DoThis());
|
|
|
|
leaked->DoThis();
|
|
|
|
}
|
|
|
|
|
2018-04-12 14:00:38 -04:00
|
|
|
TEST(StrictMockTest, MoveOnlyConstructor) {
|
2018-12-17 18:59:00 -05:00
|
|
|
StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly{});
|
2018-04-12 14:00:38 -04:00
|
|
|
}
|
|
|
|
|
2009-09-16 17:38:08 +00:00
|
|
|
// Tests that StrictMock<Mock> compiles where Mock is a user-defined
|
2018-12-28 06:03:51 -05:00
|
|
|
// class (as opposed to ::testing::Mock).
|
2009-09-16 17:38:08 +00:00
|
|
|
TEST(StrictMockTest, AcceptsClassNamedMock) {
|
2009-09-17 19:11:00 +00:00
|
|
|
StrictMock< ::Mock> strict;
|
|
|
|
EXPECT_CALL(strict, DoThis());
|
|
|
|
strict.DoThis();
|
2009-09-16 17:38:08 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:41:17 -05:00
|
|
|
TEST(StrictMockTest, IsStrictInDestructor) {
|
|
|
|
EXPECT_NONFATAL_FAILURE(
|
|
|
|
{
|
|
|
|
StrictMock<CallsMockMethodInDestructor> strict_on_destroy;
|
|
|
|
// Don't add an expectation for the call before the mock goes out of
|
|
|
|
// scope.
|
|
|
|
},
|
|
|
|
"Uninteresting mock function call");
|
|
|
|
}
|
|
|
|
|
2018-10-16 08:07:15 +02:00
|
|
|
TEST(StrictMockTest, IsNaggy_IsNice_IsStrict) {
|
|
|
|
StrictMock<MockFoo> strict_foo;
|
|
|
|
EXPECT_FALSE(Mock::IsNaggy(&strict_foo));
|
|
|
|
EXPECT_FALSE(Mock::IsNice(&strict_foo));
|
2018-10-23 15:13:41 -04:00
|
|
|
EXPECT_TRUE(Mock::IsStrict(&strict_foo));
|
2018-10-16 08:07:15 +02:00
|
|
|
}
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
} // namespace gmock_nice_strict_test
|
|
|
|
} // namespace testing
|