shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | |
| 32 | // Tests Google Mock's output in various scenarios. This ensures that |
| 33 | // Google Mock's messages are readable and useful. |
| 34 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame^] | 35 | #include "gmock/gmock.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 36 | |
| 37 | #include <stdio.h> |
| 38 | #include <string> |
| 39 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame^] | 40 | #include "gtest/gtest.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 41 | |
| 42 | using testing::_; |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 43 | using testing::AnyNumber; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 44 | using testing::Ge; |
| 45 | using testing::InSequence; |
| 46 | using testing::Ref; |
| 47 | using testing::Return; |
| 48 | using testing::Sequence; |
| 49 | |
| 50 | class MockFoo { |
| 51 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 52 | MockFoo() {} |
| 53 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 54 | MOCK_METHOD3(Bar, char(const std::string& s, int i, double x)); |
| 55 | MOCK_METHOD2(Bar2, bool(int x, int y)); |
| 56 | MOCK_METHOD2(Bar3, void(int x, int y)); |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 57 | |
| 58 | private: |
| 59 | GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | class GMockOutputTest : public testing::Test { |
| 63 | protected: |
| 64 | MockFoo foo_; |
| 65 | }; |
| 66 | |
| 67 | TEST_F(GMockOutputTest, ExpectedCall) { |
| 68 | testing::GMOCK_FLAG(verbose) = "info"; |
| 69 | |
| 70 | EXPECT_CALL(foo_, Bar2(0, _)); |
| 71 | foo_.Bar2(0, 0); // Expected call |
| 72 | |
| 73 | testing::GMOCK_FLAG(verbose) = "warning"; |
| 74 | } |
| 75 | |
| 76 | TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) { |
| 77 | testing::GMOCK_FLAG(verbose) = "info"; |
| 78 | |
| 79 | EXPECT_CALL(foo_, Bar3(0, _)); |
| 80 | foo_.Bar3(0, 0); // Expected call |
| 81 | |
| 82 | testing::GMOCK_FLAG(verbose) = "warning"; |
| 83 | } |
| 84 | |
| 85 | TEST_F(GMockOutputTest, ExplicitActionsRunOut) { |
| 86 | EXPECT_CALL(foo_, Bar2(_, _)) |
| 87 | .Times(2) |
| 88 | .WillOnce(Return(false)); |
| 89 | foo_.Bar2(2, 2); |
| 90 | foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out. |
| 91 | } |
| 92 | |
| 93 | TEST_F(GMockOutputTest, UnexpectedCall) { |
| 94 | EXPECT_CALL(foo_, Bar2(0, _)); |
| 95 | |
| 96 | foo_.Bar2(1, 0); // Unexpected call |
| 97 | foo_.Bar2(0, 0); // Expected call |
| 98 | } |
| 99 | |
| 100 | TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) { |
| 101 | EXPECT_CALL(foo_, Bar3(0, _)); |
| 102 | |
| 103 | foo_.Bar3(1, 0); // Unexpected call |
| 104 | foo_.Bar3(0, 0); // Expected call |
| 105 | } |
| 106 | |
| 107 | TEST_F(GMockOutputTest, ExcessiveCall) { |
| 108 | EXPECT_CALL(foo_, Bar2(0, _)); |
| 109 | |
| 110 | foo_.Bar2(0, 0); // Expected call |
| 111 | foo_.Bar2(0, 1); // Excessive call |
| 112 | } |
| 113 | |
| 114 | TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) { |
| 115 | EXPECT_CALL(foo_, Bar3(0, _)); |
| 116 | |
| 117 | foo_.Bar3(0, 0); // Expected call |
| 118 | foo_.Bar3(0, 1); // Excessive call |
| 119 | } |
| 120 | |
| 121 | TEST_F(GMockOutputTest, UninterestingCall) { |
| 122 | foo_.Bar2(0, 1); // Uninteresting call |
| 123 | } |
| 124 | |
| 125 | TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) { |
| 126 | foo_.Bar3(0, 1); // Uninteresting call |
| 127 | } |
| 128 | |
| 129 | TEST_F(GMockOutputTest, RetiredExpectation) { |
| 130 | EXPECT_CALL(foo_, Bar2(_, _)) |
| 131 | .RetiresOnSaturation(); |
| 132 | EXPECT_CALL(foo_, Bar2(0, 0)); |
| 133 | |
| 134 | foo_.Bar2(1, 1); |
| 135 | foo_.Bar2(1, 1); // Matches a retired expectation |
| 136 | foo_.Bar2(0, 0); |
| 137 | } |
| 138 | |
| 139 | TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) { |
| 140 | { |
| 141 | InSequence s; |
| 142 | EXPECT_CALL(foo_, Bar(_, 0, _)); |
| 143 | EXPECT_CALL(foo_, Bar2(0, 0)); |
| 144 | EXPECT_CALL(foo_, Bar2(1, _)); |
| 145 | } |
| 146 | |
| 147 | foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite |
| 148 | foo_.Bar("Hi", 0, 0); |
| 149 | foo_.Bar2(0, 0); |
| 150 | foo_.Bar2(1, 0); |
| 151 | } |
| 152 | |
| 153 | TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) { |
| 154 | Sequence s1, s2; |
| 155 | |
| 156 | EXPECT_CALL(foo_, Bar(_, 0, _)) |
| 157 | .InSequence(s1); |
| 158 | EXPECT_CALL(foo_, Bar2(0, 0)) |
| 159 | .InSequence(s2); |
| 160 | EXPECT_CALL(foo_, Bar2(1, _)) |
| 161 | .InSequence(s1, s2); |
| 162 | |
| 163 | foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites |
| 164 | foo_.Bar("Hi", 0, 0); |
| 165 | foo_.Bar2(0, 0); |
| 166 | foo_.Bar2(1, 0); |
| 167 | } |
| 168 | |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 169 | TEST_F(GMockOutputTest, UnsatisfiedWith) { |
| 170 | EXPECT_CALL(foo_, Bar2(_, _)).With(Ge()); |
| 171 | } |
| 172 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 173 | TEST_F(GMockOutputTest, UnsatisfiedExpectation) { |
| 174 | EXPECT_CALL(foo_, Bar(_, _, _)); |
| 175 | EXPECT_CALL(foo_, Bar2(0, _)) |
| 176 | .Times(2); |
| 177 | |
| 178 | foo_.Bar2(0, 1); |
| 179 | } |
| 180 | |
| 181 | TEST_F(GMockOutputTest, MismatchArguments) { |
| 182 | const std::string s = "Hi"; |
| 183 | EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0))); |
| 184 | |
| 185 | foo_.Bar("Ho", 0, -0.1); // Mismatch arguments |
| 186 | foo_.Bar(s, 0, 0); |
| 187 | } |
| 188 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 189 | TEST_F(GMockOutputTest, MismatchWith) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 190 | EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 191 | .With(Ge()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 192 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 193 | foo_.Bar2(2, 3); // Mismatch With() |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 194 | foo_.Bar2(2, 1); |
| 195 | } |
| 196 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 197 | TEST_F(GMockOutputTest, MismatchArgumentsAndWith) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 198 | EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 199 | .With(Ge()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 200 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 201 | foo_.Bar2(1, 3); // Mismatch arguments and mismatch With() |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 202 | foo_.Bar2(2, 1); |
| 203 | } |
| 204 | |
| 205 | TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) { |
| 206 | ON_CALL(foo_, Bar2(_, _)) |
| 207 | .WillByDefault(Return(true)); // Default action #1 |
| 208 | ON_CALL(foo_, Bar2(1, _)) |
| 209 | .WillByDefault(Return(false)); // Default action #2 |
| 210 | |
| 211 | EXPECT_CALL(foo_, Bar2(2, 2)); |
| 212 | foo_.Bar2(1, 0); // Unexpected call, takes default action #2. |
| 213 | foo_.Bar2(0, 0); // Unexpected call, takes default action #1. |
| 214 | foo_.Bar2(2, 2); // Expected call. |
| 215 | } |
| 216 | |
| 217 | TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) { |
| 218 | ON_CALL(foo_, Bar2(_, _)) |
| 219 | .WillByDefault(Return(true)); // Default action #1 |
| 220 | ON_CALL(foo_, Bar2(1, _)) |
| 221 | .WillByDefault(Return(false)); // Default action #2 |
| 222 | |
| 223 | EXPECT_CALL(foo_, Bar2(2, 2)); |
| 224 | EXPECT_CALL(foo_, Bar2(1, 1)); |
| 225 | |
| 226 | foo_.Bar2(2, 2); // Expected call. |
| 227 | foo_.Bar2(2, 2); // Excessive call, takes default action #1. |
| 228 | foo_.Bar2(1, 1); // Expected call. |
| 229 | foo_.Bar2(1, 1); // Excessive call, takes default action #2. |
| 230 | } |
| 231 | |
| 232 | TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) { |
| 233 | ON_CALL(foo_, Bar2(_, _)) |
| 234 | .WillByDefault(Return(true)); // Default action #1 |
| 235 | ON_CALL(foo_, Bar2(1, _)) |
| 236 | .WillByDefault(Return(false)); // Default action #2 |
| 237 | |
| 238 | foo_.Bar2(2, 2); // Uninteresting call, takes default action #1. |
| 239 | foo_.Bar2(1, 1); // Uninteresting call, takes default action #2. |
| 240 | } |
| 241 | |
| 242 | TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) { |
| 243 | ON_CALL(foo_, Bar2(_, _)) |
| 244 | .WillByDefault(Return(true)); // Default action #1 |
| 245 | |
| 246 | EXPECT_CALL(foo_, Bar2(_, _)) |
| 247 | .Times(2) |
| 248 | .WillOnce(Return(false)); |
| 249 | foo_.Bar2(2, 2); |
| 250 | foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out. |
| 251 | } |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 252 | |
| 253 | TEST_F(GMockOutputTest, CatchesLeakedMocks) { |
| 254 | MockFoo* foo1 = new MockFoo; |
| 255 | MockFoo* foo2 = new MockFoo; |
| 256 | |
| 257 | // Invokes ON_CALL on foo1. |
| 258 | ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a')); |
| 259 | |
| 260 | // Invokes EXPECT_CALL on foo2. |
| 261 | EXPECT_CALL(*foo2, Bar2(_, _)); |
| 262 | EXPECT_CALL(*foo2, Bar2(1, _)); |
| 263 | EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber()); |
| 264 | foo2->Bar2(2, 1); |
| 265 | foo2->Bar2(1, 1); |
| 266 | |
| 267 | // Both foo1 and foo2 are deliberately leaked. |
| 268 | } |
| 269 | |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 270 | void TestCatchesLeakedMocksInAdHocTests() { |
| 271 | MockFoo* foo = new MockFoo; |
| 272 | |
| 273 | // Invokes EXPECT_CALL on foo. |
| 274 | EXPECT_CALL(*foo, Bar2(_, _)); |
| 275 | foo->Bar2(2, 1); |
| 276 | |
| 277 | // foo is deliberately leaked. |
| 278 | } |
| 279 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 280 | int main(int argc, char **argv) { |
| 281 | testing::InitGoogleMock(&argc, argv); |
| 282 | |
| 283 | // Ensures that the tests pass no matter what value of |
| 284 | // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. |
| 285 | testing::GMOCK_FLAG(catch_leaked_mocks) = true; |
| 286 | testing::GMOCK_FLAG(verbose) = "warning"; |
| 287 | |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 288 | TestCatchesLeakedMocksInAdHocTests(); |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 289 | return RUN_ALL_TESTS(); |
| 290 | } |