gmock_output_test_.cc (googletest-release-1.11.0) | : | gmock_output_test_.cc (googletest-release-1.12.0) | ||
---|---|---|---|---|
skipping to change at line 33 | skipping to change at line 33 | |||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
// Tests Google Mock's output in various scenarios. This ensures that | // Tests Google Mock's output in various scenarios. This ensures that | |||
// Google Mock's messages are readable and useful. | // Google Mock's messages are readable and useful. | |||
#include "gmock/gmock.h" | ||||
#include <stdio.h> | #include <stdio.h> | |||
#include <string> | #include <string> | |||
#include "gmock/gmock.h" | ||||
#include "gtest/gtest.h" | #include "gtest/gtest.h" | |||
// Silence C4100 (unreferenced formal parameter) | // Silence C4100 (unreferenced formal parameter) | |||
#ifdef _MSC_VER | #ifdef _MSC_VER | |||
# pragma warning(push) | #pragma warning(push) | |||
# pragma warning(disable:4100) | #pragma warning(disable : 4100) | |||
#endif | #endif | |||
using testing::_; | using testing::_; | |||
using testing::AnyNumber; | using testing::AnyNumber; | |||
using testing::Ge; | using testing::Ge; | |||
using testing::InSequence; | using testing::InSequence; | |||
using testing::NaggyMock; | using testing::NaggyMock; | |||
using testing::Ref; | using testing::Ref; | |||
using testing::Return; | using testing::Return; | |||
using testing::Sequence; | using testing::Sequence; | |||
skipping to change at line 65 | skipping to change at line 65 | |||
class MockFoo { | class MockFoo { | |||
public: | public: | |||
MockFoo() {} | MockFoo() {} | |||
MOCK_METHOD3(Bar, char(const std::string& s, int i, double x)); | MOCK_METHOD3(Bar, char(const std::string& s, int i, double x)); | |||
MOCK_METHOD2(Bar2, bool(int x, int y)); | MOCK_METHOD2(Bar2, bool(int x, int y)); | |||
MOCK_METHOD2(Bar3, void(int x, int y)); | MOCK_METHOD2(Bar3, void(int x, int y)); | |||
private: | private: | |||
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo); | MockFoo(const MockFoo&) = delete; | |||
MockFoo& operator=(const MockFoo&) = delete; | ||||
}; | }; | |||
class GMockOutputTest : public testing::Test { | class GMockOutputTest : public testing::Test { | |||
protected: | protected: | |||
NaggyMock<MockFoo> foo_; | NaggyMock<MockFoo> foo_; | |||
}; | }; | |||
TEST_F(GMockOutputTest, ExpectedCall) { | TEST_F(GMockOutputTest, ExpectedCall) { | |||
testing::GMOCK_FLAG(verbose) = "info"; | GMOCK_FLAG_SET(verbose, "info"); | |||
EXPECT_CALL(foo_, Bar2(0, _)); | EXPECT_CALL(foo_, Bar2(0, _)); | |||
foo_.Bar2(0, 0); // Expected call | foo_.Bar2(0, 0); // Expected call | |||
testing::GMOCK_FLAG(verbose) = "warning"; | GMOCK_FLAG_SET(verbose, "warning"); | |||
} | } | |||
TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) { | TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) { | |||
testing::GMOCK_FLAG(verbose) = "info"; | GMOCK_FLAG_SET(verbose, "info"); | |||
EXPECT_CALL(foo_, Bar3(0, _)); | EXPECT_CALL(foo_, Bar3(0, _)); | |||
foo_.Bar3(0, 0); // Expected call | foo_.Bar3(0, 0); // Expected call | |||
testing::GMOCK_FLAG(verbose) = "warning"; | GMOCK_FLAG_SET(verbose, "warning"); | |||
} | } | |||
TEST_F(GMockOutputTest, ExplicitActionsRunOut) { | TEST_F(GMockOutputTest, ExplicitActionsRunOut) { | |||
EXPECT_CALL(foo_, Bar2(_, _)) | EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false)); | |||
.Times(2) | ||||
.WillOnce(Return(false)); | ||||
foo_.Bar2(2, 2); | foo_.Bar2(2, 2); | |||
foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out. | foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out. | |||
} | } | |||
TEST_F(GMockOutputTest, UnexpectedCall) { | TEST_F(GMockOutputTest, UnexpectedCall) { | |||
EXPECT_CALL(foo_, Bar2(0, _)); | EXPECT_CALL(foo_, Bar2(0, _)); | |||
foo_.Bar2(1, 0); // Unexpected call | foo_.Bar2(1, 0); // Unexpected call | |||
foo_.Bar2(0, 0); // Expected call | foo_.Bar2(0, 0); // Expected call | |||
} | } | |||
skipping to change at line 136 | skipping to change at line 135 | |||
TEST_F(GMockOutputTest, UninterestingCall) { | TEST_F(GMockOutputTest, UninterestingCall) { | |||
foo_.Bar2(0, 1); // Uninteresting call | foo_.Bar2(0, 1); // Uninteresting call | |||
} | } | |||
TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) { | TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) { | |||
foo_.Bar3(0, 1); // Uninteresting call | foo_.Bar3(0, 1); // Uninteresting call | |||
} | } | |||
TEST_F(GMockOutputTest, RetiredExpectation) { | TEST_F(GMockOutputTest, RetiredExpectation) { | |||
EXPECT_CALL(foo_, Bar2(_, _)) | EXPECT_CALL(foo_, Bar2(_, _)).RetiresOnSaturation(); | |||
.RetiresOnSaturation(); | ||||
EXPECT_CALL(foo_, Bar2(0, 0)); | EXPECT_CALL(foo_, Bar2(0, 0)); | |||
foo_.Bar2(1, 1); | foo_.Bar2(1, 1); | |||
foo_.Bar2(1, 1); // Matches a retired expectation | foo_.Bar2(1, 1); // Matches a retired expectation | |||
foo_.Bar2(0, 0); | foo_.Bar2(0, 0); | |||
} | } | |||
TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) { | TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) { | |||
{ | { | |||
InSequence s; | InSequence s; | |||
skipping to change at line 162 | skipping to change at line 160 | |||
foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite | foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite | |||
foo_.Bar("Hi", 0, 0); | foo_.Bar("Hi", 0, 0); | |||
foo_.Bar2(0, 0); | foo_.Bar2(0, 0); | |||
foo_.Bar2(1, 0); | foo_.Bar2(1, 0); | |||
} | } | |||
TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) { | TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) { | |||
Sequence s1, s2; | Sequence s1, s2; | |||
EXPECT_CALL(foo_, Bar(_, 0, _)) | EXPECT_CALL(foo_, Bar(_, 0, _)).InSequence(s1); | |||
.InSequence(s1); | EXPECT_CALL(foo_, Bar2(0, 0)).InSequence(s2); | |||
EXPECT_CALL(foo_, Bar2(0, 0)) | EXPECT_CALL(foo_, Bar2(1, _)).InSequence(s1, s2); | |||
.InSequence(s2); | ||||
EXPECT_CALL(foo_, Bar2(1, _)) | ||||
.InSequence(s1, s2); | ||||
foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites | foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites | |||
foo_.Bar("Hi", 0, 0); | foo_.Bar("Hi", 0, 0); | |||
foo_.Bar2(0, 0); | foo_.Bar2(0, 0); | |||
foo_.Bar2(1, 0); | foo_.Bar2(1, 0); | |||
} | } | |||
TEST_F(GMockOutputTest, UnsatisfiedWith) { | TEST_F(GMockOutputTest, UnsatisfiedWith) { | |||
EXPECT_CALL(foo_, Bar2(_, _)).With(Ge()); | EXPECT_CALL(foo_, Bar2(_, _)).With(Ge()); | |||
} | } | |||
TEST_F(GMockOutputTest, UnsatisfiedExpectation) { | TEST_F(GMockOutputTest, UnsatisfiedExpectation) { | |||
EXPECT_CALL(foo_, Bar(_, _, _)); | EXPECT_CALL(foo_, Bar(_, _, _)); | |||
EXPECT_CALL(foo_, Bar2(0, _)) | EXPECT_CALL(foo_, Bar2(0, _)).Times(2); | |||
.Times(2); | ||||
foo_.Bar2(0, 1); | foo_.Bar2(0, 1); | |||
} | } | |||
TEST_F(GMockOutputTest, MismatchArguments) { | TEST_F(GMockOutputTest, MismatchArguments) { | |||
const std::string s = "Hi"; | const std::string s = "Hi"; | |||
EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0))); | EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0))); | |||
foo_.Bar("Ho", 0, -0.1); // Mismatch arguments | foo_.Bar("Ho", 0, -0.1); // Mismatch arguments | |||
foo_.Bar(s, 0, 0); | foo_.Bar(s, 0, 0); | |||
} | } | |||
TEST_F(GMockOutputTest, MismatchWith) { | TEST_F(GMockOutputTest, MismatchWith) { | |||
EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))) | EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge()); | |||
.With(Ge()); | ||||
foo_.Bar2(2, 3); // Mismatch With() | foo_.Bar2(2, 3); // Mismatch With() | |||
foo_.Bar2(2, 1); | foo_.Bar2(2, 1); | |||
} | } | |||
TEST_F(GMockOutputTest, MismatchArgumentsAndWith) { | TEST_F(GMockOutputTest, MismatchArgumentsAndWith) { | |||
EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))) | EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge()); | |||
.With(Ge()); | ||||
foo_.Bar2(1, 3); // Mismatch arguments and mismatch With() | foo_.Bar2(1, 3); // Mismatch arguments and mismatch With() | |||
foo_.Bar2(2, 1); | foo_.Bar2(2, 1); | |||
} | } | |||
TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) { | TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) { | |||
ON_CALL(foo_, Bar2(_, _)) | ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1 | |||
.WillByDefault(Return(true)); // Default action #1 | ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2 | |||
ON_CALL(foo_, Bar2(1, _)) | ||||
.WillByDefault(Return(false)); // Default action #2 | ||||
EXPECT_CALL(foo_, Bar2(2, 2)); | EXPECT_CALL(foo_, Bar2(2, 2)); | |||
foo_.Bar2(1, 0); // Unexpected call, takes default action #2. | foo_.Bar2(1, 0); // Unexpected call, takes default action #2. | |||
foo_.Bar2(0, 0); // Unexpected call, takes default action #1. | foo_.Bar2(0, 0); // Unexpected call, takes default action #1. | |||
foo_.Bar2(2, 2); // Expected call. | foo_.Bar2(2, 2); // Expected call. | |||
} | } | |||
TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) { | TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) { | |||
ON_CALL(foo_, Bar2(_, _)) | ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1 | |||
.WillByDefault(Return(true)); // Default action #1 | ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2 | |||
ON_CALL(foo_, Bar2(1, _)) | ||||
.WillByDefault(Return(false)); // Default action #2 | ||||
EXPECT_CALL(foo_, Bar2(2, 2)); | EXPECT_CALL(foo_, Bar2(2, 2)); | |||
EXPECT_CALL(foo_, Bar2(1, 1)); | EXPECT_CALL(foo_, Bar2(1, 1)); | |||
foo_.Bar2(2, 2); // Expected call. | foo_.Bar2(2, 2); // Expected call. | |||
foo_.Bar2(2, 2); // Excessive call, takes default action #1. | foo_.Bar2(2, 2); // Excessive call, takes default action #1. | |||
foo_.Bar2(1, 1); // Expected call. | foo_.Bar2(1, 1); // Expected call. | |||
foo_.Bar2(1, 1); // Excessive call, takes default action #2. | foo_.Bar2(1, 1); // Excessive call, takes default action #2. | |||
} | } | |||
TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) { | TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) { | |||
ON_CALL(foo_, Bar2(_, _)) | ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1 | |||
.WillByDefault(Return(true)); // Default action #1 | ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2 | |||
ON_CALL(foo_, Bar2(1, _)) | ||||
.WillByDefault(Return(false)); // Default action #2 | ||||
foo_.Bar2(2, 2); // Uninteresting call, takes default action #1. | foo_.Bar2(2, 2); // Uninteresting call, takes default action #1. | |||
foo_.Bar2(1, 1); // Uninteresting call, takes default action #2. | foo_.Bar2(1, 1); // Uninteresting call, takes default action #2. | |||
} | } | |||
TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) { | TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) { | |||
ON_CALL(foo_, Bar2(_, _)) | ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1 | |||
.WillByDefault(Return(true)); // Default action #1 | ||||
EXPECT_CALL(foo_, Bar2(_, _)) | EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false)); | |||
.Times(2) | ||||
.WillOnce(Return(false)); | ||||
foo_.Bar2(2, 2); | foo_.Bar2(2, 2); | |||
foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out. | foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out. | |||
} | } | |||
TEST_F(GMockOutputTest, CatchesLeakedMocks) { | TEST_F(GMockOutputTest, CatchesLeakedMocks) { | |||
MockFoo* foo1 = new MockFoo; | MockFoo* foo1 = new MockFoo; | |||
MockFoo* foo2 = new MockFoo; | MockFoo* foo2 = new MockFoo; | |||
// Invokes ON_CALL on foo1. | // Invokes ON_CALL on foo1. | |||
ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a')); | ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a')); | |||
skipping to change at line 295 | skipping to change at line 278 | |||
void TestCatchesLeakedMocksInAdHocTests() { | void TestCatchesLeakedMocksInAdHocTests() { | |||
MockFoo* foo = new MockFoo; | MockFoo* foo = new MockFoo; | |||
// Invokes EXPECT_CALL on foo. | // Invokes EXPECT_CALL on foo. | |||
EXPECT_CALL(*foo, Bar2(_, _)); | EXPECT_CALL(*foo, Bar2(_, _)); | |||
foo->Bar2(2, 1); | foo->Bar2(2, 1); | |||
// foo is deliberately leaked. | // foo is deliberately leaked. | |||
} | } | |||
int main(int argc, char **argv) { | int main(int argc, char** argv) { | |||
testing::InitGoogleMock(&argc, argv); | testing::InitGoogleMock(&argc, argv); | |||
// Ensures that the tests pass no matter what value of | // Ensures that the tests pass no matter what value of | |||
// --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. | // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. | |||
testing::GMOCK_FLAG(catch_leaked_mocks) = true; | GMOCK_FLAG_SET(catch_leaked_mocks, true); | |||
testing::GMOCK_FLAG(verbose) = "warning"; | GMOCK_FLAG_SET(verbose, "warning"); | |||
TestCatchesLeakedMocksInAdHocTests(); | TestCatchesLeakedMocksInAdHocTests(); | |||
return RUN_ALL_TESTS(); | return RUN_ALL_TESTS(); | |||
} | } | |||
#ifdef _MSC_VER | #ifdef _MSC_VER | |||
# pragma warning(pop) | #pragma warning(pop) | |||
#endif | #endif | |||
End of changes. 23 change blocks. | ||||
47 lines changed or deleted | 30 lines changed or added |