Blame googlemock/test/gmock_output_test_.cc

Packit bd1cd8
// Copyright 2008, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
Packit bd1cd8
// Tests Google Mock's output in various scenarios.  This ensures that
Packit bd1cd8
// Google Mock's messages are readable and useful.
Packit bd1cd8
Packit bd1cd8
#include "gmock/gmock.h"
Packit bd1cd8
Packit bd1cd8
#include <stdio.h>
Packit bd1cd8
#include <string>
Packit bd1cd8
Packit bd1cd8
#include "gtest/gtest.h"
Packit bd1cd8
Packit bd1cd8
using testing::_;
Packit bd1cd8
using testing::AnyNumber;
Packit bd1cd8
using testing::Ge;
Packit bd1cd8
using testing::InSequence;
Packit bd1cd8
using testing::NaggyMock;
Packit bd1cd8
using testing::Ref;
Packit bd1cd8
using testing::Return;
Packit bd1cd8
using testing::Sequence;
Packit bd1cd8
Packit bd1cd8
class MockFoo {
Packit bd1cd8
 public:
Packit bd1cd8
  MockFoo() {}
Packit bd1cd8
Packit bd1cd8
  MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
Packit bd1cd8
  MOCK_METHOD2(Bar2, bool(int x, int y));
Packit bd1cd8
  MOCK_METHOD2(Bar3, void(int x, int y));
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
class GMockOutputTest : public testing::Test {
Packit bd1cd8
 protected:
Packit bd1cd8
  NaggyMock<MockFoo> foo_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, ExpectedCall) {
Packit bd1cd8
  testing::GMOCK_FLAG(verbose) = "info";
Packit bd1cd8
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(0, _));
Packit bd1cd8
  foo_.Bar2(0, 0);  // Expected call
Packit bd1cd8
Packit bd1cd8
  testing::GMOCK_FLAG(verbose) = "warning";
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
Packit bd1cd8
  testing::GMOCK_FLAG(verbose) = "info";
Packit bd1cd8
Packit bd1cd8
  EXPECT_CALL(foo_, Bar3(0, _));
Packit bd1cd8
  foo_.Bar3(0, 0);  // Expected call
Packit bd1cd8
Packit bd1cd8
  testing::GMOCK_FLAG(verbose) = "warning";
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(_, _))
Packit bd1cd8
      .Times(2)
Packit bd1cd8
      .WillOnce(Return(false));
Packit bd1cd8
  foo_.Bar2(2, 2);
Packit bd1cd8
  foo_.Bar2(1, 1);  // Explicit actions in EXPECT_CALL run out.
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, UnexpectedCall) {
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(0, _));
Packit bd1cd8
Packit bd1cd8
  foo_.Bar2(1, 0);  // Unexpected call
Packit bd1cd8
  foo_.Bar2(0, 0);  // Expected call
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
Packit bd1cd8
  EXPECT_CALL(foo_, Bar3(0, _));
Packit bd1cd8
Packit bd1cd8
  foo_.Bar3(1, 0);  // Unexpected call
Packit bd1cd8
  foo_.Bar3(0, 0);  // Expected call
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, ExcessiveCall) {
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(0, _));
Packit bd1cd8
Packit bd1cd8
  foo_.Bar2(0, 0);  // Expected call
Packit bd1cd8
  foo_.Bar2(0, 1);  // Excessive call
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
Packit bd1cd8
  EXPECT_CALL(foo_, Bar3(0, _));
Packit bd1cd8
Packit bd1cd8
  foo_.Bar3(0, 0);  // Expected call
Packit bd1cd8
  foo_.Bar3(0, 1);  // Excessive call
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, UninterestingCall) {
Packit bd1cd8
  foo_.Bar2(0, 1);  // Uninteresting call
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
Packit bd1cd8
  foo_.Bar3(0, 1);  // Uninteresting call
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, RetiredExpectation) {
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(_, _))
Packit bd1cd8
      .RetiresOnSaturation();
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(0, 0));
Packit bd1cd8
Packit bd1cd8
  foo_.Bar2(1, 1);
Packit bd1cd8
  foo_.Bar2(1, 1);  // Matches a retired expectation
Packit bd1cd8
  foo_.Bar2(0, 0);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
Packit bd1cd8
  {
Packit bd1cd8
    InSequence s;
Packit bd1cd8
    EXPECT_CALL(foo_, Bar(_, 0, _));
Packit bd1cd8
    EXPECT_CALL(foo_, Bar2(0, 0));
Packit bd1cd8
    EXPECT_CALL(foo_, Bar2(1, _));
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  foo_.Bar2(1, 0);  // Has one immediate unsatisfied pre-requisite
Packit bd1cd8
  foo_.Bar("Hi", 0, 0);
Packit bd1cd8
  foo_.Bar2(0, 0);
Packit bd1cd8
  foo_.Bar2(1, 0);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
Packit bd1cd8
  Sequence s1, s2;
Packit bd1cd8
Packit bd1cd8
  EXPECT_CALL(foo_, Bar(_, 0, _))
Packit bd1cd8
      .InSequence(s1);
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(0, 0))
Packit bd1cd8
      .InSequence(s2);
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(1, _))
Packit bd1cd8
      .InSequence(s1, s2);
Packit bd1cd8
Packit bd1cd8
  foo_.Bar2(1, 0);  // Has two immediate unsatisfied pre-requisites
Packit bd1cd8
  foo_.Bar("Hi", 0, 0);
Packit bd1cd8
  foo_.Bar2(0, 0);
Packit bd1cd8
  foo_.Bar2(1, 0);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, UnsatisfiedWith) {
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
Packit bd1cd8
  EXPECT_CALL(foo_, Bar(_, _, _));
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(0, _))
Packit bd1cd8
      .Times(2);
Packit bd1cd8
Packit bd1cd8
  foo_.Bar2(0, 1);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, MismatchArguments) {
Packit bd1cd8
  const std::string s = "Hi";
Packit bd1cd8
  EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
Packit bd1cd8
Packit bd1cd8
  foo_.Bar("Ho", 0, -0.1);  // Mismatch arguments
Packit bd1cd8
  foo_.Bar(s, 0, 0);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, MismatchWith) {
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
Packit bd1cd8
      .With(Ge());
Packit bd1cd8
Packit bd1cd8
  foo_.Bar2(2, 3);  // Mismatch With()
Packit bd1cd8
  foo_.Bar2(2, 1);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
Packit bd1cd8
      .With(Ge());
Packit bd1cd8
Packit bd1cd8
  foo_.Bar2(1, 3);  // Mismatch arguments and mismatch With()
Packit bd1cd8
  foo_.Bar2(2, 1);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
Packit bd1cd8
  ON_CALL(foo_, Bar2(_, _))
Packit bd1cd8
      .WillByDefault(Return(true));   // Default action #1
Packit bd1cd8
  ON_CALL(foo_, Bar2(1, _))
Packit bd1cd8
      .WillByDefault(Return(false));  // Default action #2
Packit bd1cd8
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(2, 2));
Packit bd1cd8
  foo_.Bar2(1, 0);  // Unexpected call, takes default action #2.
Packit bd1cd8
  foo_.Bar2(0, 0);  // Unexpected call, takes default action #1.
Packit bd1cd8
  foo_.Bar2(2, 2);  // Expected call.
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
Packit bd1cd8
  ON_CALL(foo_, Bar2(_, _))
Packit bd1cd8
      .WillByDefault(Return(true));   // Default action #1
Packit bd1cd8
  ON_CALL(foo_, Bar2(1, _))
Packit bd1cd8
      .WillByDefault(Return(false));  // Default action #2
Packit bd1cd8
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(2, 2));
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(1, 1));
Packit bd1cd8
Packit bd1cd8
  foo_.Bar2(2, 2);  // Expected call.
Packit bd1cd8
  foo_.Bar2(2, 2);  // Excessive call, takes default action #1.
Packit bd1cd8
  foo_.Bar2(1, 1);  // Expected call.
Packit bd1cd8
  foo_.Bar2(1, 1);  // Excessive call, takes default action #2.
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
Packit bd1cd8
  ON_CALL(foo_, Bar2(_, _))
Packit bd1cd8
      .WillByDefault(Return(true));   // Default action #1
Packit bd1cd8
  ON_CALL(foo_, Bar2(1, _))
Packit bd1cd8
      .WillByDefault(Return(false));  // Default action #2
Packit bd1cd8
Packit bd1cd8
  foo_.Bar2(2, 2);  // Uninteresting call, takes default action #1.
Packit bd1cd8
  foo_.Bar2(1, 1);  // Uninteresting call, takes default action #2.
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
Packit bd1cd8
  ON_CALL(foo_, Bar2(_, _))
Packit bd1cd8
      .WillByDefault(Return(true));   // Default action #1
Packit bd1cd8
Packit bd1cd8
  EXPECT_CALL(foo_, Bar2(_, _))
Packit bd1cd8
      .Times(2)
Packit bd1cd8
      .WillOnce(Return(false));
Packit bd1cd8
  foo_.Bar2(2, 2);
Packit bd1cd8
  foo_.Bar2(1, 1);  // Explicit actions in EXPECT_CALL run out.
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(GMockOutputTest, CatchesLeakedMocks) {
Packit bd1cd8
  MockFoo* foo1 = new MockFoo;
Packit bd1cd8
  MockFoo* foo2 = new MockFoo;
Packit bd1cd8
Packit bd1cd8
  // Invokes ON_CALL on foo1.
Packit bd1cd8
  ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
Packit bd1cd8
Packit bd1cd8
  // Invokes EXPECT_CALL on foo2.
Packit bd1cd8
  EXPECT_CALL(*foo2, Bar2(_, _));
Packit bd1cd8
  EXPECT_CALL(*foo2, Bar2(1, _));
Packit bd1cd8
  EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
Packit bd1cd8
  foo2->Bar2(2, 1);
Packit bd1cd8
  foo2->Bar2(1, 1);
Packit bd1cd8
Packit bd1cd8
  // Both foo1 and foo2 are deliberately leaked.
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
void TestCatchesLeakedMocksInAdHocTests() {
Packit bd1cd8
  MockFoo* foo = new MockFoo;
Packit bd1cd8
Packit bd1cd8
  // Invokes EXPECT_CALL on foo.
Packit bd1cd8
  EXPECT_CALL(*foo, Bar2(_, _));
Packit bd1cd8
  foo->Bar2(2, 1);
Packit bd1cd8
Packit bd1cd8
  // foo is deliberately leaked.
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
int main(int argc, char **argv) {
Packit bd1cd8
  testing::InitGoogleMock(&argc, argv);
Packit bd1cd8
Packit bd1cd8
  // Ensures that the tests pass no matter what value of
Packit bd1cd8
  // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
Packit bd1cd8
  testing::GMOCK_FLAG(catch_leaked_mocks) = true;
Packit bd1cd8
  testing::GMOCK_FLAG(verbose) = "warning";
Packit bd1cd8
Packit bd1cd8
  TestCatchesLeakedMocksInAdHocTests();
Packit bd1cd8
  return RUN_ALL_TESTS();
Packit bd1cd8
}