Source release 17.1.0
This commit is contained in:
@@ -26,15 +26,14 @@
|
||||
// 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.
|
||||
//
|
||||
// Author: wan@google.com (Zhanyong Wan)
|
||||
|
||||
#include "gmock/gmock-generated-nice-strict.h"
|
||||
#include "gmock/gmock-nice-strict.h"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "gtest/gtest-spi.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
// This must not be defined inside the ::testing namespace, or it will
|
||||
// clash with ::testing::Mock.
|
||||
@@ -51,8 +50,6 @@ class Mock {
|
||||
namespace testing {
|
||||
namespace gmock_nice_strict_test {
|
||||
|
||||
using testing::internal::string;
|
||||
using testing::GMOCK_FLAG(verbose);
|
||||
using testing::HasSubstr;
|
||||
using testing::NaggyMock;
|
||||
using testing::NiceMock;
|
||||
@@ -63,6 +60,18 @@ using testing::internal::CaptureStdout;
|
||||
using testing::internal::GetCapturedStdout;
|
||||
#endif
|
||||
|
||||
// Class without default constructor.
|
||||
class NotDefaultConstructible {
|
||||
public:
|
||||
explicit NotDefaultConstructible(int) {}
|
||||
};
|
||||
|
||||
class CallsMockMethodInDestructor {
|
||||
public:
|
||||
~CallsMockMethodInDestructor() { OnDestroy(); }
|
||||
MOCK_METHOD(void, OnDestroy, ());
|
||||
};
|
||||
|
||||
// Defines some mock classes needed by the tests.
|
||||
|
||||
class Foo {
|
||||
@@ -80,6 +89,7 @@ class MockFoo : public Foo {
|
||||
|
||||
MOCK_METHOD0(DoThis, void());
|
||||
MOCK_METHOD1(DoThat, int(bool flag));
|
||||
MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible());
|
||||
|
||||
private:
|
||||
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
|
||||
@@ -87,33 +97,50 @@ class MockFoo : public Foo {
|
||||
|
||||
class MockBar {
|
||||
public:
|
||||
explicit MockBar(const string& s) : str_(s) {}
|
||||
explicit MockBar(const std::string& s) : str_(s) {}
|
||||
|
||||
MockBar(char a1, char a2, string a3, string a4, int a5, int a6,
|
||||
const string& a7, const string& a8, bool a9, bool a10) {
|
||||
str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
|
||||
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) +
|
||||
static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
|
||||
}
|
||||
|
||||
virtual ~MockBar() {}
|
||||
|
||||
const string& str() const { return str_; }
|
||||
const std::string& str() const { return str_; }
|
||||
|
||||
MOCK_METHOD0(This, int());
|
||||
MOCK_METHOD2(That, string(int, bool));
|
||||
MOCK_METHOD2(That, std::string(int, bool));
|
||||
|
||||
private:
|
||||
string str_;
|
||||
std::string str_;
|
||||
|
||||
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
|
||||
};
|
||||
|
||||
|
||||
class MockBaz {
|
||||
public:
|
||||
class MoveOnly {
|
||||
public:
|
||||
MoveOnly() = default;
|
||||
|
||||
MoveOnly(const MoveOnly&) = delete;
|
||||
MoveOnly& operator=(const MoveOnly&) = delete;
|
||||
|
||||
MoveOnly(MoveOnly&&) = default;
|
||||
MoveOnly& operator=(MoveOnly&&) = default;
|
||||
};
|
||||
|
||||
MockBaz(MoveOnly) {}
|
||||
};
|
||||
|
||||
#if GTEST_HAS_STREAM_REDIRECTION
|
||||
|
||||
// Tests that a raw mock generates warnings for uninteresting calls.
|
||||
TEST(RawMockTest, WarningForUninterestingCall) {
|
||||
const string saved_flag = GMOCK_FLAG(verbose);
|
||||
GMOCK_FLAG(verbose) = "warning";
|
||||
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
||||
GMOCK_FLAG_SET(verbose, "warning");
|
||||
|
||||
MockFoo raw_foo;
|
||||
|
||||
@@ -123,14 +150,14 @@ TEST(RawMockTest, WarningForUninterestingCall) {
|
||||
EXPECT_THAT(GetCapturedStdout(),
|
||||
HasSubstr("Uninteresting mock function call"));
|
||||
|
||||
GMOCK_FLAG(verbose) = saved_flag;
|
||||
GMOCK_FLAG_SET(verbose, saved_flag);
|
||||
}
|
||||
|
||||
// Tests that a raw mock generates warnings for uninteresting calls
|
||||
// that delete the mock object.
|
||||
TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
|
||||
const string saved_flag = GMOCK_FLAG(verbose);
|
||||
GMOCK_FLAG(verbose) = "warning";
|
||||
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
||||
GMOCK_FLAG_SET(verbose, "warning");
|
||||
|
||||
MockFoo* const raw_foo = new MockFoo;
|
||||
|
||||
@@ -142,7 +169,7 @@ TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
|
||||
EXPECT_THAT(GetCapturedStdout(),
|
||||
HasSubstr("Uninteresting mock function call"));
|
||||
|
||||
GMOCK_FLAG(verbose) = saved_flag;
|
||||
GMOCK_FLAG_SET(verbose, saved_flag);
|
||||
}
|
||||
|
||||
// Tests that a raw mock generates informational logs for
|
||||
@@ -150,14 +177,21 @@ TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
|
||||
TEST(RawMockTest, InfoForUninterestingCall) {
|
||||
MockFoo raw_foo;
|
||||
|
||||
const string saved_flag = GMOCK_FLAG(verbose);
|
||||
GMOCK_FLAG(verbose) = "info";
|
||||
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
||||
GMOCK_FLAG_SET(verbose, "info");
|
||||
CaptureStdout();
|
||||
raw_foo.DoThis();
|
||||
EXPECT_THAT(GetCapturedStdout(),
|
||||
HasSubstr("Uninteresting mock function call"));
|
||||
|
||||
GMOCK_FLAG(verbose) = saved_flag;
|
||||
GMOCK_FLAG_SET(verbose, saved_flag);
|
||||
}
|
||||
|
||||
TEST(RawMockTest, IsNaggy_IsNice_IsStrict) {
|
||||
MockFoo raw_foo;
|
||||
EXPECT_TRUE(Mock::IsNaggy(&raw_foo));
|
||||
EXPECT_FALSE(Mock::IsNice(&raw_foo));
|
||||
EXPECT_FALSE(Mock::IsStrict(&raw_foo));
|
||||
}
|
||||
|
||||
// Tests that a nice mock generates no warning for uninteresting calls.
|
||||
@@ -188,14 +222,14 @@ TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
|
||||
TEST(NiceMockTest, InfoForUninterestingCall) {
|
||||
NiceMock<MockFoo> nice_foo;
|
||||
|
||||
const string saved_flag = GMOCK_FLAG(verbose);
|
||||
GMOCK_FLAG(verbose) = "info";
|
||||
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
||||
GMOCK_FLAG_SET(verbose, "info");
|
||||
CaptureStdout();
|
||||
nice_foo.DoThis();
|
||||
EXPECT_THAT(GetCapturedStdout(),
|
||||
HasSubstr("Uninteresting mock function call"));
|
||||
|
||||
GMOCK_FLAG(verbose) = saved_flag;
|
||||
GMOCK_FLAG_SET(verbose, saved_flag);
|
||||
}
|
||||
|
||||
#endif // GTEST_HAS_STREAM_REDIRECTION
|
||||
@@ -208,6 +242,23 @@ TEST(NiceMockTest, AllowsExpectedCall) {
|
||||
nice_foo.DoThis();
|
||||
}
|
||||
|
||||
// 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<MockFoo> nice_foo;
|
||||
#if GTEST_HAS_EXCEPTIONS
|
||||
try {
|
||||
nice_foo.ReturnNonDefaultConstructible();
|
||||
FAIL();
|
||||
} catch (const std::runtime_error& ex) {
|
||||
EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible"));
|
||||
}
|
||||
#else
|
||||
EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, "");
|
||||
#endif
|
||||
}
|
||||
|
||||
// Tests that an unexpected call on a nice mock fails.
|
||||
TEST(NiceMockTest, UnexpectedCallFails) {
|
||||
NiceMock<MockFoo> nice_foo;
|
||||
@@ -237,28 +288,45 @@ TEST(NiceMockTest, NonDefaultConstructor10) {
|
||||
nice_bar.That(5, true);
|
||||
}
|
||||
|
||||
#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
|
||||
TEST(NiceMockTest, AllowLeak) {
|
||||
NiceMock<MockFoo>* leaked = new NiceMock<MockFoo>;
|
||||
Mock::AllowLeak(leaked);
|
||||
EXPECT_CALL(*leaked, DoThis());
|
||||
leaked->DoThis();
|
||||
}
|
||||
|
||||
TEST(NiceMockTest, MoveOnlyConstructor) {
|
||||
NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly{});
|
||||
}
|
||||
|
||||
// Tests that NiceMock<Mock> compiles where Mock is a user-defined
|
||||
// class (as opposed to ::testing::Mock). We had to work around an
|
||||
// MSVC 8.0 bug that caused the symbol Mock used in the definition of
|
||||
// NiceMock to be looked up in the wrong context, and this test
|
||||
// ensures that our fix works.
|
||||
//
|
||||
// We have to skip this test on Symbian and Windows Mobile, as it
|
||||
// causes the program to crash there, for reasons unclear to us yet.
|
||||
// class (as opposed to ::testing::Mock).
|
||||
TEST(NiceMockTest, AcceptsClassNamedMock) {
|
||||
NiceMock< ::Mock> nice;
|
||||
EXPECT_CALL(nice, DoThis());
|
||||
nice.DoThis();
|
||||
}
|
||||
#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
|
||||
|
||||
TEST(NiceMockTest, IsNiceInDestructor) {
|
||||
{
|
||||
NiceMock<CallsMockMethodInDestructor> nice_on_destroy;
|
||||
// Don't add an expectation for the call before the mock goes out of scope.
|
||||
}
|
||||
}
|
||||
|
||||
TEST(NiceMockTest, IsNaggy_IsNice_IsStrict) {
|
||||
NiceMock<MockFoo> nice_foo;
|
||||
EXPECT_FALSE(Mock::IsNaggy(&nice_foo));
|
||||
EXPECT_TRUE(Mock::IsNice(&nice_foo));
|
||||
EXPECT_FALSE(Mock::IsStrict(&nice_foo));
|
||||
}
|
||||
|
||||
#if GTEST_HAS_STREAM_REDIRECTION
|
||||
|
||||
// Tests that a naggy mock generates warnings for uninteresting calls.
|
||||
TEST(NaggyMockTest, WarningForUninterestingCall) {
|
||||
const string saved_flag = GMOCK_FLAG(verbose);
|
||||
GMOCK_FLAG(verbose) = "warning";
|
||||
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
||||
GMOCK_FLAG_SET(verbose, "warning");
|
||||
|
||||
NaggyMock<MockFoo> naggy_foo;
|
||||
|
||||
@@ -268,14 +336,14 @@ TEST(NaggyMockTest, WarningForUninterestingCall) {
|
||||
EXPECT_THAT(GetCapturedStdout(),
|
||||
HasSubstr("Uninteresting mock function call"));
|
||||
|
||||
GMOCK_FLAG(verbose) = saved_flag;
|
||||
GMOCK_FLAG_SET(verbose, saved_flag);
|
||||
}
|
||||
|
||||
// Tests that a naggy mock generates a warning for an uninteresting call
|
||||
// that deletes the mock object.
|
||||
TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
|
||||
const string saved_flag = GMOCK_FLAG(verbose);
|
||||
GMOCK_FLAG(verbose) = "warning";
|
||||
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
||||
GMOCK_FLAG_SET(verbose, "warning");
|
||||
|
||||
NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
|
||||
|
||||
@@ -287,7 +355,7 @@ TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
|
||||
EXPECT_THAT(GetCapturedStdout(),
|
||||
HasSubstr("Uninteresting mock function call"));
|
||||
|
||||
GMOCK_FLAG(verbose) = saved_flag;
|
||||
GMOCK_FLAG_SET(verbose, saved_flag);
|
||||
}
|
||||
|
||||
#endif // GTEST_HAS_STREAM_REDIRECTION
|
||||
@@ -330,21 +398,47 @@ TEST(NaggyMockTest, NonDefaultConstructor10) {
|
||||
naggy_bar.That(5, true);
|
||||
}
|
||||
|
||||
#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
|
||||
TEST(NaggyMockTest, AllowLeak) {
|
||||
NaggyMock<MockFoo>* leaked = new NaggyMock<MockFoo>;
|
||||
Mock::AllowLeak(leaked);
|
||||
EXPECT_CALL(*leaked, DoThis());
|
||||
leaked->DoThis();
|
||||
}
|
||||
|
||||
TEST(NaggyMockTest, MoveOnlyConstructor) {
|
||||
NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly{});
|
||||
}
|
||||
|
||||
// Tests that NaggyMock<Mock> compiles where Mock is a user-defined
|
||||
// class (as opposed to ::testing::Mock). We had to work around an
|
||||
// MSVC 8.0 bug that caused the symbol Mock used in the definition of
|
||||
// NaggyMock to be looked up in the wrong context, and this test
|
||||
// ensures that our fix works.
|
||||
//
|
||||
// We have to skip this test on Symbian and Windows Mobile, as it
|
||||
// causes the program to crash there, for reasons unclear to us yet.
|
||||
// class (as opposed to ::testing::Mock).
|
||||
TEST(NaggyMockTest, AcceptsClassNamedMock) {
|
||||
NaggyMock< ::Mock> naggy;
|
||||
EXPECT_CALL(naggy, DoThis());
|
||||
naggy.DoThis();
|
||||
}
|
||||
#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
|
||||
|
||||
TEST(NaggyMockTest, IsNaggyInDestructor) {
|
||||
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
|
||||
GMOCK_FLAG_SET(verbose, "warning");
|
||||
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"));
|
||||
|
||||
GMOCK_FLAG_SET(verbose, saved_flag);
|
||||
}
|
||||
|
||||
TEST(NaggyMockTest, IsNaggy_IsNice_IsStrict) {
|
||||
NaggyMock<MockFoo> naggy_foo;
|
||||
EXPECT_TRUE(Mock::IsNaggy(&naggy_foo));
|
||||
EXPECT_FALSE(Mock::IsNice(&naggy_foo));
|
||||
EXPECT_FALSE(Mock::IsStrict(&naggy_foo));
|
||||
}
|
||||
|
||||
// Tests that a strict mock allows expected calls.
|
||||
TEST(StrictMockTest, AllowsExpectedCall) {
|
||||
@@ -404,21 +498,41 @@ TEST(StrictMockTest, NonDefaultConstructor10) {
|
||||
"Uninteresting mock function call");
|
||||
}
|
||||
|
||||
#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
|
||||
TEST(StrictMockTest, AllowLeak) {
|
||||
StrictMock<MockFoo>* leaked = new StrictMock<MockFoo>;
|
||||
Mock::AllowLeak(leaked);
|
||||
EXPECT_CALL(*leaked, DoThis());
|
||||
leaked->DoThis();
|
||||
}
|
||||
|
||||
TEST(StrictMockTest, MoveOnlyConstructor) {
|
||||
StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly{});
|
||||
}
|
||||
|
||||
// Tests that StrictMock<Mock> compiles where Mock is a user-defined
|
||||
// class (as opposed to ::testing::Mock). We had to work around an
|
||||
// MSVC 8.0 bug that caused the symbol Mock used in the definition of
|
||||
// StrictMock to be looked up in the wrong context, and this test
|
||||
// ensures that our fix works.
|
||||
//
|
||||
// We have to skip this test on Symbian and Windows Mobile, as it
|
||||
// causes the program to crash there, for reasons unclear to us yet.
|
||||
// class (as opposed to ::testing::Mock).
|
||||
TEST(StrictMockTest, AcceptsClassNamedMock) {
|
||||
StrictMock< ::Mock> strict;
|
||||
EXPECT_CALL(strict, DoThis());
|
||||
strict.DoThis();
|
||||
}
|
||||
#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
TEST(StrictMockTest, IsNaggy_IsNice_IsStrict) {
|
||||
StrictMock<MockFoo> strict_foo;
|
||||
EXPECT_FALSE(Mock::IsNaggy(&strict_foo));
|
||||
EXPECT_FALSE(Mock::IsNice(&strict_foo));
|
||||
EXPECT_TRUE(Mock::IsStrict(&strict_foo));
|
||||
}
|
||||
|
||||
} // namespace gmock_nice_strict_test
|
||||
} // namespace testing
|
||||
|
||||
Reference in New Issue
Block a user