gmock_output_test_.cc revision 13481
15148SN/A// Copyright 2008, Google Inc. 25148SN/A// All rights reserved. 35148SN/A// 410036SAli.Saidi@ARM.com// Redistribution and use in source and binary forms, with or without 58835SAli.Saidi@ARM.com// modification, are permitted provided that the following conditions are 610036SAli.Saidi@ARM.com// met: 77873SN/A// 87873SN/A// * Redistributions of source code must retain the above copyright 97873SN/A// notice, this list of conditions and the following disclaimer. 105148SN/A// * Redistributions in binary form must reproduce the above 115148SN/A// copyright notice, this list of conditions and the following disclaimer 125148SN/A// in the documentation and/or other materials provided with the 1310315Snilay@cs.wisc.edu// distribution. 148835SAli.Saidi@ARM.com// * Neither the name of Google Inc. nor the names of its 159885Sstever@gmail.com// contributors may be used to endorse or promote products derived from 169885Sstever@gmail.com// this software without specific prior written permission. 1711570SCurtis.Dunham@arm.com// 1810036SAli.Saidi@ARM.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1911312Santhony.gutierrez@amd.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 208835SAli.Saidi@ARM.com// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 218835SAli.Saidi@ARM.com// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2210315Snilay@cs.wisc.edu// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 238835SAli.Saidi@ARM.com// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2410315Snilay@cs.wisc.edu// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 255148SN/A// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 269481Snilay@cs.wisc.edu// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 278673SN/A// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2810736Snilay@cs.wisc.edu// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2911219Snilay@cs.wisc.edu// 308721SN/A// Author: wan@google.com (Zhanyong Wan) 3111570SCurtis.Dunham@arm.com 3211570SCurtis.Dunham@arm.com// Tests Google Mock's output in various scenarios. This ensures that 3311570SCurtis.Dunham@arm.com// Google Mock's messages are readable and useful. 3411570SCurtis.Dunham@arm.com 358835SAli.Saidi@ARM.com#include "gmock/gmock.h" 368835SAli.Saidi@ARM.com 3711440SCurtis.Dunham@arm.com#include <stdio.h> 3811440SCurtis.Dunham@arm.com#include <string> 397935SN/A 407935SN/A#include "gtest/gtest.h" 417935SN/A 427935SN/Ausing testing::_; 437935SN/Ausing testing::AnyNumber; 447935SN/Ausing testing::Ge; 457935SN/Ausing testing::InSequence; 468983Snate@binkert.orgusing testing::NaggyMock; 475148SN/Ausing testing::Ref; 489885Sstever@gmail.comusing testing::Return; 499885Sstever@gmail.comusing testing::Sequence; 509885Sstever@gmail.com 5110315Snilay@cs.wisc.educlass MockFoo { 5210036SAli.Saidi@ARM.com public: 5310315Snilay@cs.wisc.edu MockFoo() {} 549885Sstever@gmail.com 559885Sstever@gmail.com MOCK_METHOD3(Bar, char(const std::string& s, int i, double x)); 565148SN/A MOCK_METHOD2(Bar2, bool(int x, int y)); 575148SN/A MOCK_METHOD2(Bar3, void(int x, int y)); 589885Sstever@gmail.com 5910315Snilay@cs.wisc.edu private: 605876SN/A GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo); 619885Sstever@gmail.com}; 625148SN/A 6311570SCurtis.Dunham@arm.comclass GMockOutputTest : public testing::Test { 645876SN/A protected: 658835SAli.Saidi@ARM.com NaggyMock<MockFoo> foo_; 665876SN/A}; 675148SN/A 6810036SAli.Saidi@ARM.comTEST_F(GMockOutputTest, ExpectedCall) { 698983Snate@binkert.org testing::GMOCK_FLAG(verbose) = "info"; 705148SN/A 715148SN/A EXPECT_CALL(foo_, Bar2(0, _)); 728835SAli.Saidi@ARM.com foo_.Bar2(0, 0); // Expected call 739481Snilay@cs.wisc.edu 745148SN/A testing::GMOCK_FLAG(verbose) = "warning"; 755148SN/A} 765148SN/A 775148SN/ATEST_F(GMockOutputTest, ExpectedCallToVoidFunction) { 785148SN/A testing::GMOCK_FLAG(verbose) = "info"; 795540SN/A 8011570SCurtis.Dunham@arm.com EXPECT_CALL(foo_, Bar3(0, _)); 8111570SCurtis.Dunham@arm.com foo_.Bar3(0, 0); // Expected call 8211570SCurtis.Dunham@arm.com 8311570SCurtis.Dunham@arm.com testing::GMOCK_FLAG(verbose) = "warning"; 848835SAli.Saidi@ARM.com} 855148SN/A 869885Sstever@gmail.comTEST_F(GMockOutputTest, ExplicitActionsRunOut) { 875509SN/A EXPECT_CALL(foo_, Bar2(_, _)) 885509SN/A .Times(2) 8910315Snilay@cs.wisc.edu .WillOnce(Return(false)); 909481Snilay@cs.wisc.edu foo_.Bar2(2, 2); 915148SN/A foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out. 925148SN/A} 935148SN/A 945148SN/ATEST_F(GMockOutputTest, UnexpectedCall) { 958983Snate@binkert.org EXPECT_CALL(foo_, Bar2(0, _)); 968983Snate@binkert.org 975148SN/A foo_.Bar2(1, 0); // Unexpected call 989885Sstever@gmail.com foo_.Bar2(0, 0); // Expected call 999885Sstever@gmail.com} 1009885Sstever@gmail.com 1019885Sstever@gmail.comTEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) { 10210036SAli.Saidi@ARM.com EXPECT_CALL(foo_, Bar3(0, _)); 1039885Sstever@gmail.com 1045148SN/A foo_.Bar3(1, 0); // Unexpected call 1056024SN/A foo_.Bar3(0, 0); // Expected call 1068835SAli.Saidi@ARM.com} 10710036SAli.Saidi@ARM.com 1085148SN/ATEST_F(GMockOutputTest, ExcessiveCall) { 1098835SAli.Saidi@ARM.com EXPECT_CALL(foo_, Bar2(0, _)); 1108835SAli.Saidi@ARM.com 1118835SAli.Saidi@ARM.com foo_.Bar2(0, 0); // Expected call 1128835SAli.Saidi@ARM.com foo_.Bar2(0, 1); // Excessive call 1139885Sstever@gmail.com} 11411570SCurtis.Dunham@arm.com 11510036SAli.Saidi@ARM.comTEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) { 1169885Sstever@gmail.com EXPECT_CALL(foo_, Bar3(0, _)); 11711570SCurtis.Dunham@arm.com 11811570SCurtis.Dunham@arm.com foo_.Bar3(0, 0); // Expected call 11911570SCurtis.Dunham@arm.com foo_.Bar3(0, 1); // Excessive call 12011570SCurtis.Dunham@arm.com} 1218835SAli.Saidi@ARM.com 1228983Snate@binkert.orgTEST_F(GMockOutputTest, UninterestingCall) { 1238835SAli.Saidi@ARM.com foo_.Bar2(0, 1); // Uninteresting call 1248835SAli.Saidi@ARM.com} 1258835SAli.Saidi@ARM.com 1269885Sstever@gmail.comTEST_F(GMockOutputTest, UninterestingCallToVoidFunction) { 12711570SCurtis.Dunham@arm.com foo_.Bar3(0, 1); // Uninteresting call 12810036SAli.Saidi@ARM.com} 1298835SAli.Saidi@ARM.com 13011570SCurtis.Dunham@arm.comTEST_F(GMockOutputTest, RetiredExpectation) { 13111570SCurtis.Dunham@arm.com EXPECT_CALL(foo_, Bar2(_, _)) 13211570SCurtis.Dunham@arm.com .RetiresOnSaturation(); 1338835SAli.Saidi@ARM.com EXPECT_CALL(foo_, Bar2(0, 0)); 1349213Snilay@cs.wisc.edu 13511570SCurtis.Dunham@arm.com foo_.Bar2(1, 1); 1368835SAli.Saidi@ARM.com foo_.Bar2(1, 1); // Matches a retired expectation 1378983Snate@binkert.org foo_.Bar2(0, 0); 1388983Snate@binkert.org} 1398983Snate@binkert.org 1405148SN/ATEST_F(GMockOutputTest, UnsatisfiedPrerequisite) { 1419481Snilay@cs.wisc.edu { 1429481Snilay@cs.wisc.edu InSequence s; 14310036SAli.Saidi@ARM.com EXPECT_CALL(foo_, Bar(_, 0, _)); 1449481Snilay@cs.wisc.edu EXPECT_CALL(foo_, Bar2(0, 0)); 1455148SN/A EXPECT_CALL(foo_, Bar2(1, _)); 1466024SN/A } 1478835SAli.Saidi@ARM.com 14810036SAli.Saidi@ARM.com foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite 1495148SN/A foo_.Bar("Hi", 0, 0); 1508835SAli.Saidi@ARM.com foo_.Bar2(0, 0); 1518835SAli.Saidi@ARM.com foo_.Bar2(1, 0); 1528835SAli.Saidi@ARM.com} 1538835SAli.Saidi@ARM.com 1549885Sstever@gmail.comTEST_F(GMockOutputTest, UnsatisfiedPrerequisites) { 15511570SCurtis.Dunham@arm.com Sequence s1, s2; 15610036SAli.Saidi@ARM.com 1579885Sstever@gmail.com EXPECT_CALL(foo_, Bar(_, 0, _)) 15811570SCurtis.Dunham@arm.com .InSequence(s1); 15911570SCurtis.Dunham@arm.com EXPECT_CALL(foo_, Bar2(0, 0)) 16011570SCurtis.Dunham@arm.com .InSequence(s2); 16111570SCurtis.Dunham@arm.com EXPECT_CALL(foo_, Bar2(1, _)) 1628835SAli.Saidi@ARM.com .InSequence(s1, s2); 1638983Snate@binkert.org 1645148SN/A foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites 1655148SN/A foo_.Bar("Hi", 0, 0); 1665148SN/A foo_.Bar2(0, 0); 16710036SAli.Saidi@ARM.com foo_.Bar2(1, 0); 1685148SN/A} 1695148SN/A 1705148SN/ATEST_F(GMockOutputTest, UnsatisfiedWith) { 1715148SN/A EXPECT_CALL(foo_, Bar2(_, _)).With(Ge()); 1725148SN/A} 17310645Snilay@cs.wisc.edu 1745148SN/ATEST_F(GMockOutputTest, UnsatisfiedExpectation) { 1755148SN/A EXPECT_CALL(foo_, Bar(_, _, _)); 1765516SN/A EXPECT_CALL(foo_, Bar2(0, _)) 1775148SN/A .Times(2); 17810036SAli.Saidi@ARM.com 17911570SCurtis.Dunham@arm.com foo_.Bar2(0, 1); 1805148SN/A} 1815148SN/A 18210645Snilay@cs.wisc.eduTEST_F(GMockOutputTest, MismatchArguments) { 1835176SN/A const std::string s = "Hi"; 1845148SN/A EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0))); 1855148SN/A 1865148SN/A foo_.Bar("Ho", 0, -0.1); // Mismatch arguments 1875410SN/A foo_.Bar(s, 0, 0); 1885148SN/A} 1895148SN/A 19010451Snilay@cs.wisc.eduTEST_F(GMockOutputTest, MismatchWith) { 1915148SN/A EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))) 1929885Sstever@gmail.com .With(Ge()); 1939885Sstever@gmail.com 1949885Sstever@gmail.com foo_.Bar2(2, 3); // Mismatch With() 19510315Snilay@cs.wisc.edu foo_.Bar2(2, 1); 19610036SAli.Saidi@ARM.com} 19710315Snilay@cs.wisc.edu 1989885Sstever@gmail.comTEST_F(GMockOutputTest, MismatchArgumentsAndWith) { 1999885Sstever@gmail.com EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))) 20010315Snilay@cs.wisc.edu .With(Ge()); 20110315Snilay@cs.wisc.edu 20210315Snilay@cs.wisc.edu foo_.Bar2(1, 3); // Mismatch arguments and mismatch With() 20310315Snilay@cs.wisc.edu foo_.Bar2(2, 1); 20410315Snilay@cs.wisc.edu} 20510315Snilay@cs.wisc.edu 20610315Snilay@cs.wisc.eduTEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) { 20710315Snilay@cs.wisc.edu ON_CALL(foo_, Bar2(_, _)) 2085148SN/A .WillByDefault(Return(true)); // Default action #1 20910451Snilay@cs.wisc.edu ON_CALL(foo_, Bar2(1, _)) 2109885Sstever@gmail.com .WillByDefault(Return(false)); // Default action #2 21111570SCurtis.Dunham@arm.com 21210036SAli.Saidi@ARM.com EXPECT_CALL(foo_, Bar2(2, 2)); 21310736Snilay@cs.wisc.edu foo_.Bar2(1, 0); // Unexpected call, takes default action #2. 21410736Snilay@cs.wisc.edu foo_.Bar2(0, 0); // Unexpected call, takes default action #1. 21511570SCurtis.Dunham@arm.com foo_.Bar2(2, 2); // Expected call. 21611570SCurtis.Dunham@arm.com} 21711570SCurtis.Dunham@arm.com 21811440SCurtis.Dunham@arm.comTEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) { 21911570SCurtis.Dunham@arm.com ON_CALL(foo_, Bar2(_, _)) 22010736Snilay@cs.wisc.edu .WillByDefault(Return(true)); // Default action #1 22110451Snilay@cs.wisc.edu ON_CALL(foo_, Bar2(1, _)) 22210736Snilay@cs.wisc.edu .WillByDefault(Return(false)); // Default action #2 2239583Snilay@cs.wisc.edu 2247524SN/A EXPECT_CALL(foo_, Bar2(2, 2)); 22510736Snilay@cs.wisc.edu EXPECT_CALL(foo_, Bar2(1, 1)); 2269150SAli.Saidi@ARM.com 2278983Snate@binkert.org foo_.Bar2(2, 2); // Expected call. 2285148SN/A foo_.Bar2(2, 2); // Excessive call, takes default action #1. 2295148SN/A foo_.Bar2(1, 1); // Expected call. 2308983Snate@binkert.org foo_.Bar2(1, 1); // Excessive call, takes default action #2. 2319373Snilay@cs.wisc.edu} 2329885Sstever@gmail.com 2339885Sstever@gmail.comTEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) { 23411570SCurtis.Dunham@arm.com ON_CALL(foo_, Bar2(_, _)) 23510036SAli.Saidi@ARM.com .WillByDefault(Return(true)); // Default action #1 2368983Snate@binkert.org ON_CALL(foo_, Bar2(1, _)) 2375540SN/A .WillByDefault(Return(false)); // Default action #2 2385410SN/A 2395509SN/A foo_.Bar2(2, 2); // Uninteresting call, takes default action #1. 24011570SCurtis.Dunham@arm.com foo_.Bar2(1, 1); // Uninteresting call, takes default action #2. 24111570SCurtis.Dunham@arm.com} 24211570SCurtis.Dunham@arm.com 24311570SCurtis.Dunham@arm.comTEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) { 2445148SN/A ON_CALL(foo_, Bar2(_, _)) 2458983Snate@binkert.org .WillByDefault(Return(true)); // Default action #1 2465148SN/A 2479885Sstever@gmail.com EXPECT_CALL(foo_, Bar2(_, _)) 2489885Sstever@gmail.com .Times(2) 24910036SAli.Saidi@ARM.com .WillOnce(Return(false)); 2509885Sstever@gmail.com foo_.Bar2(2, 2); 2519885Sstever@gmail.com foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out. 252} 253 254TEST_F(GMockOutputTest, CatchesLeakedMocks) { 255 MockFoo* foo1 = new MockFoo; 256 MockFoo* foo2 = new MockFoo; 257 258 // Invokes ON_CALL on foo1. 259 ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a')); 260 261 // Invokes EXPECT_CALL on foo2. 262 EXPECT_CALL(*foo2, Bar2(_, _)); 263 EXPECT_CALL(*foo2, Bar2(1, _)); 264 EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber()); 265 foo2->Bar2(2, 1); 266 foo2->Bar2(1, 1); 267 268 // Both foo1 and foo2 are deliberately leaked. 269} 270 271void TestCatchesLeakedMocksInAdHocTests() { 272 MockFoo* foo = new MockFoo; 273 274 // Invokes EXPECT_CALL on foo. 275 EXPECT_CALL(*foo, Bar2(_, _)); 276 foo->Bar2(2, 1); 277 278 // foo is deliberately leaked. 279} 280 281int main(int argc, char **argv) { 282 testing::InitGoogleMock(&argc, argv); 283 284 // Ensures that the tests pass no matter what value of 285 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. 286 testing::GMOCK_FLAG(catch_leaked_mocks) = true; 287 testing::GMOCK_FLAG(verbose) = "warning"; 288 289 TestCatchesLeakedMocksInAdHocTests(); 290 return RUN_ALL_TESTS(); 291} 292