shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | // Copyright 2007, 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 | // Google Mock - a framework for writing C++ mock classes. |
| 33 | // |
| 34 | // This file tests the spec builder syntax. |
| 35 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 36 | #include "gmock/gmock-spec-builders.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 37 | |
| 38 | #include <ostream> // NOLINT |
| 39 | #include <sstream> |
| 40 | #include <string> |
| 41 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 42 | #include "gmock/gmock.h" |
| 43 | #include "gmock/internal/gmock-port.h" |
| 44 | #include "gtest/gtest.h" |
| 45 | #include "gtest/gtest-spi.h" |
vladlosev | e5121b5 | 2011-02-11 23:50:38 +0000 | [diff] [blame] | 46 | #include "gtest/internal/gtest-port.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 47 | |
| 48 | namespace testing { |
| 49 | namespace internal { |
| 50 | |
| 51 | // Helper class for testing the Expectation class template. |
| 52 | class ExpectationTester { |
| 53 | public: |
| 54 | // Sets the call count of the given expectation to the given number. |
| 55 | void SetCallCount(int n, ExpectationBase* exp) { |
| 56 | exp->call_count_ = n; |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | } // namespace internal |
| 61 | } // namespace testing |
| 62 | |
| 63 | namespace { |
| 64 | |
| 65 | using testing::_; |
| 66 | using testing::AnyNumber; |
| 67 | using testing::AtLeast; |
| 68 | using testing::AtMost; |
| 69 | using testing::Between; |
| 70 | using testing::Cardinality; |
| 71 | using testing::CardinalityInterface; |
zhanyong.wan | d14aaed | 2010-01-14 05:36:32 +0000 | [diff] [blame] | 72 | using testing::ContainsRegex; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 73 | using testing::Const; |
| 74 | using testing::DoAll; |
| 75 | using testing::DoDefault; |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 76 | using testing::Eq; |
| 77 | using testing::Expectation; |
| 78 | using testing::ExpectationSet; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 79 | using testing::GMOCK_FLAG(verbose); |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 80 | using testing::Gt; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 81 | using testing::InSequence; |
| 82 | using testing::Invoke; |
| 83 | using testing::InvokeWithoutArgs; |
| 84 | using testing::IsSubstring; |
| 85 | using testing::Lt; |
| 86 | using testing::Message; |
| 87 | using testing::Mock; |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 88 | using testing::Ne; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 89 | using testing::Return; |
| 90 | using testing::Sequence; |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 91 | using testing::internal::ExpectationTester; |
vladlosev | e5121b5 | 2011-02-11 23:50:38 +0000 | [diff] [blame] | 92 | using testing::internal::FormatFileLocation; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 93 | using testing::internal::g_gmock_mutex; |
| 94 | using testing::internal::kErrorVerbosity; |
| 95 | using testing::internal::kInfoVerbosity; |
| 96 | using testing::internal::kWarningVerbosity; |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 97 | using testing::internal::String; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 98 | using testing::internal::string; |
| 99 | |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 100 | #if GTEST_HAS_STREAM_REDIRECTION |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 101 | using testing::HasSubstr; |
| 102 | using testing::internal::CaptureStdout; |
| 103 | using testing::internal::GetCapturedStdout; |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 104 | #endif |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 105 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 106 | class Result {}; |
| 107 | |
| 108 | class MockA { |
| 109 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 110 | MockA() {} |
| 111 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 112 | MOCK_METHOD1(DoA, void(int n)); // NOLINT |
| 113 | MOCK_METHOD1(ReturnResult, Result(int n)); // NOLINT |
| 114 | MOCK_METHOD2(Binary, bool(int x, int y)); // NOLINT |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 115 | MOCK_METHOD2(ReturnInt, int(int x, int y)); // NOLINT |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 116 | |
| 117 | private: |
| 118 | GTEST_DISALLOW_COPY_AND_ASSIGN_(MockA); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | class MockB { |
| 122 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 123 | MockB() {} |
| 124 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 125 | MOCK_CONST_METHOD0(DoB, int()); // NOLINT |
| 126 | MOCK_METHOD1(DoB, int(int n)); // NOLINT |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 127 | |
| 128 | private: |
| 129 | GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | // Tests that EXPECT_CALL and ON_CALL compile in a presence of macro |
| 133 | // redefining a mock method name. This could happen, for example, when |
| 134 | // the tested code #includes Win32 API headers which define many APIs |
| 135 | // as macros, e.g. #define TextOut TextOutW. |
| 136 | |
| 137 | #define Method MethodW |
| 138 | |
| 139 | class CC { |
| 140 | public: |
| 141 | virtual ~CC() {} |
| 142 | virtual int Method() = 0; |
| 143 | }; |
| 144 | class MockCC : public CC { |
| 145 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 146 | MockCC() {} |
| 147 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 148 | MOCK_METHOD0(Method, int()); |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 149 | |
| 150 | private: |
| 151 | GTEST_DISALLOW_COPY_AND_ASSIGN_(MockCC); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | // Tests that a method with expanded name compiles. |
| 155 | TEST(OnCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) { |
| 156 | MockCC cc; |
| 157 | ON_CALL(cc, Method()); |
| 158 | } |
| 159 | |
| 160 | // Tests that the method with expanded name not only compiles but runs |
| 161 | // and returns a correct value, too. |
| 162 | TEST(OnCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) { |
| 163 | MockCC cc; |
| 164 | ON_CALL(cc, Method()).WillByDefault(Return(42)); |
| 165 | EXPECT_EQ(42, cc.Method()); |
| 166 | } |
| 167 | |
| 168 | // Tests that a method with expanded name compiles. |
| 169 | TEST(ExpectCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) { |
| 170 | MockCC cc; |
| 171 | EXPECT_CALL(cc, Method()); |
| 172 | cc.Method(); |
| 173 | } |
| 174 | |
| 175 | // Tests that it works, too. |
| 176 | TEST(ExpectCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) { |
| 177 | MockCC cc; |
| 178 | EXPECT_CALL(cc, Method()).WillOnce(Return(42)); |
| 179 | EXPECT_EQ(42, cc.Method()); |
| 180 | } |
| 181 | |
| 182 | #undef Method // Done with macro redefinition tests. |
| 183 | |
| 184 | // Tests that ON_CALL evaluates its arguments exactly once as promised |
| 185 | // by Google Mock. |
| 186 | TEST(OnCallSyntaxTest, EvaluatesFirstArgumentOnce) { |
| 187 | MockA a; |
| 188 | MockA* pa = &a; |
| 189 | |
| 190 | ON_CALL(*pa++, DoA(_)); |
| 191 | EXPECT_EQ(&a + 1, pa); |
| 192 | } |
| 193 | |
| 194 | TEST(OnCallSyntaxTest, EvaluatesSecondArgumentOnce) { |
| 195 | MockA a; |
| 196 | int n = 0; |
| 197 | |
| 198 | ON_CALL(a, DoA(n++)); |
| 199 | EXPECT_EQ(1, n); |
| 200 | } |
| 201 | |
| 202 | // Tests that the syntax of ON_CALL() is enforced at run time. |
| 203 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 204 | TEST(OnCallSyntaxTest, WithIsOptional) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 205 | MockA a; |
| 206 | |
| 207 | ON_CALL(a, DoA(5)) |
| 208 | .WillByDefault(Return()); |
| 209 | ON_CALL(a, DoA(_)) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 210 | .With(_) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 211 | .WillByDefault(Return()); |
| 212 | } |
| 213 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 214 | TEST(OnCallSyntaxTest, WithCanAppearAtMostOnce) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 215 | MockA a; |
| 216 | |
| 217 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 218 | ON_CALL(a, ReturnResult(_)) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 219 | .With(_) |
| 220 | .With(_) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 221 | .WillByDefault(Return(Result())); |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 222 | }, ".With() cannot appear more than once in an ON_CALL()"); |
| 223 | } |
| 224 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 225 | TEST(OnCallSyntaxTest, WillByDefaultIsMandatory) { |
| 226 | MockA a; |
| 227 | |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 228 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 229 | ON_CALL(a, DoA(5)); |
| 230 | a.DoA(5); |
| 231 | }, ""); |
| 232 | } |
| 233 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 234 | TEST(OnCallSyntaxTest, WillByDefaultCanAppearAtMostOnce) { |
| 235 | MockA a; |
| 236 | |
| 237 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 238 | ON_CALL(a, DoA(5)) |
| 239 | .WillByDefault(Return()) |
| 240 | .WillByDefault(Return()); |
| 241 | }, ".WillByDefault() must appear exactly once in an ON_CALL()"); |
| 242 | } |
| 243 | |
| 244 | // Tests that EXPECT_CALL evaluates its arguments exactly once as |
| 245 | // promised by Google Mock. |
| 246 | TEST(ExpectCallSyntaxTest, EvaluatesFirstArgumentOnce) { |
| 247 | MockA a; |
| 248 | MockA* pa = &a; |
| 249 | |
| 250 | EXPECT_CALL(*pa++, DoA(_)); |
| 251 | a.DoA(0); |
| 252 | EXPECT_EQ(&a + 1, pa); |
| 253 | } |
| 254 | |
| 255 | TEST(ExpectCallSyntaxTest, EvaluatesSecondArgumentOnce) { |
| 256 | MockA a; |
| 257 | int n = 0; |
| 258 | |
| 259 | EXPECT_CALL(a, DoA(n++)); |
| 260 | a.DoA(0); |
| 261 | EXPECT_EQ(1, n); |
| 262 | } |
| 263 | |
| 264 | // Tests that the syntax of EXPECT_CALL() is enforced at run time. |
| 265 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 266 | TEST(ExpectCallSyntaxTest, WithIsOptional) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 267 | MockA a; |
| 268 | |
| 269 | EXPECT_CALL(a, DoA(5)) |
| 270 | .Times(0); |
| 271 | EXPECT_CALL(a, DoA(6)) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 272 | .With(_) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 273 | .Times(0); |
| 274 | } |
| 275 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 276 | TEST(ExpectCallSyntaxTest, WithCanAppearAtMostOnce) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 277 | MockA a; |
| 278 | |
| 279 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 280 | EXPECT_CALL(a, DoA(6)) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 281 | .With(_) |
| 282 | .With(_); |
| 283 | }, ".With() cannot appear more than once in an EXPECT_CALL()"); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 284 | |
| 285 | a.DoA(6); |
| 286 | } |
| 287 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 288 | TEST(ExpectCallSyntaxTest, WithMustBeFirstClause) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 289 | MockA a; |
| 290 | |
| 291 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 292 | EXPECT_CALL(a, DoA(1)) |
| 293 | .Times(1) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 294 | .With(_); |
| 295 | }, ".With() must be the first clause in an EXPECT_CALL()"); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 296 | |
| 297 | a.DoA(1); |
| 298 | |
| 299 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 300 | EXPECT_CALL(a, DoA(2)) |
| 301 | .WillOnce(Return()) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 302 | .With(_); |
| 303 | }, ".With() must be the first clause in an EXPECT_CALL()"); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 304 | |
| 305 | a.DoA(2); |
| 306 | } |
| 307 | |
| 308 | TEST(ExpectCallSyntaxTest, TimesCanBeInferred) { |
| 309 | MockA a; |
| 310 | |
| 311 | EXPECT_CALL(a, DoA(1)) |
| 312 | .WillOnce(Return()); |
| 313 | |
| 314 | EXPECT_CALL(a, DoA(2)) |
| 315 | .WillOnce(Return()) |
| 316 | .WillRepeatedly(Return()); |
| 317 | |
| 318 | a.DoA(1); |
| 319 | a.DoA(2); |
| 320 | a.DoA(2); |
| 321 | } |
| 322 | |
| 323 | TEST(ExpectCallSyntaxTest, TimesCanAppearAtMostOnce) { |
| 324 | MockA a; |
| 325 | |
| 326 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 327 | EXPECT_CALL(a, DoA(1)) |
| 328 | .Times(1) |
| 329 | .Times(2); |
| 330 | }, ".Times() cannot appear more than once in an EXPECT_CALL()"); |
| 331 | |
| 332 | a.DoA(1); |
| 333 | a.DoA(1); |
| 334 | } |
| 335 | |
| 336 | TEST(ExpectCallSyntaxTest, TimesMustBeBeforeInSequence) { |
| 337 | MockA a; |
| 338 | Sequence s; |
| 339 | |
| 340 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 341 | EXPECT_CALL(a, DoA(1)) |
| 342 | .InSequence(s) |
| 343 | .Times(1); |
| 344 | }, ".Times() cannot appear after "); |
| 345 | |
| 346 | a.DoA(1); |
| 347 | } |
| 348 | |
| 349 | TEST(ExpectCallSyntaxTest, InSequenceIsOptional) { |
| 350 | MockA a; |
| 351 | Sequence s; |
| 352 | |
| 353 | EXPECT_CALL(a, DoA(1)); |
| 354 | EXPECT_CALL(a, DoA(2)) |
| 355 | .InSequence(s); |
| 356 | |
| 357 | a.DoA(1); |
| 358 | a.DoA(2); |
| 359 | } |
| 360 | |
| 361 | TEST(ExpectCallSyntaxTest, InSequenceCanAppearMultipleTimes) { |
| 362 | MockA a; |
| 363 | Sequence s1, s2; |
| 364 | |
| 365 | EXPECT_CALL(a, DoA(1)) |
| 366 | .InSequence(s1, s2) |
| 367 | .InSequence(s1); |
| 368 | |
| 369 | a.DoA(1); |
| 370 | } |
| 371 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 372 | TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeAfter) { |
| 373 | MockA a; |
| 374 | Sequence s; |
| 375 | |
| 376 | Expectation e = EXPECT_CALL(a, DoA(1)) |
| 377 | .Times(AnyNumber()); |
| 378 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 379 | EXPECT_CALL(a, DoA(2)) |
| 380 | .After(e) |
| 381 | .InSequence(s); |
| 382 | }, ".InSequence() cannot appear after "); |
| 383 | |
| 384 | a.DoA(2); |
| 385 | } |
| 386 | |
| 387 | TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWillOnce) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 388 | MockA a; |
| 389 | Sequence s; |
| 390 | |
| 391 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 392 | EXPECT_CALL(a, DoA(1)) |
| 393 | .WillOnce(Return()) |
| 394 | .InSequence(s); |
| 395 | }, ".InSequence() cannot appear after "); |
| 396 | |
| 397 | a.DoA(1); |
| 398 | } |
| 399 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 400 | TEST(ExpectCallSyntaxTest, AfterMustBeBeforeWillOnce) { |
| 401 | MockA a; |
| 402 | |
| 403 | Expectation e = EXPECT_CALL(a, DoA(1)); |
| 404 | EXPECT_NONFATAL_FAILURE({ |
| 405 | EXPECT_CALL(a, DoA(2)) |
| 406 | .WillOnce(Return()) |
| 407 | .After(e); |
| 408 | }, ".After() cannot appear after "); |
| 409 | |
| 410 | a.DoA(1); |
| 411 | a.DoA(2); |
| 412 | } |
| 413 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 414 | TEST(ExpectCallSyntaxTest, WillIsOptional) { |
| 415 | MockA a; |
| 416 | |
| 417 | EXPECT_CALL(a, DoA(1)); |
| 418 | EXPECT_CALL(a, DoA(2)) |
| 419 | .WillOnce(Return()); |
| 420 | |
| 421 | a.DoA(1); |
| 422 | a.DoA(2); |
| 423 | } |
| 424 | |
| 425 | TEST(ExpectCallSyntaxTest, WillCanAppearMultipleTimes) { |
| 426 | MockA a; |
| 427 | |
| 428 | EXPECT_CALL(a, DoA(1)) |
| 429 | .Times(AnyNumber()) |
| 430 | .WillOnce(Return()) |
| 431 | .WillOnce(Return()) |
| 432 | .WillOnce(Return()); |
| 433 | } |
| 434 | |
| 435 | TEST(ExpectCallSyntaxTest, WillMustBeBeforeWillRepeatedly) { |
| 436 | MockA a; |
| 437 | |
| 438 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 439 | EXPECT_CALL(a, DoA(1)) |
| 440 | .WillRepeatedly(Return()) |
| 441 | .WillOnce(Return()); |
| 442 | }, ".WillOnce() cannot appear after "); |
| 443 | |
| 444 | a.DoA(1); |
| 445 | } |
| 446 | |
| 447 | TEST(ExpectCallSyntaxTest, WillRepeatedlyIsOptional) { |
| 448 | MockA a; |
| 449 | |
| 450 | EXPECT_CALL(a, DoA(1)) |
| 451 | .WillOnce(Return()); |
| 452 | EXPECT_CALL(a, DoA(2)) |
| 453 | .WillOnce(Return()) |
| 454 | .WillRepeatedly(Return()); |
| 455 | |
| 456 | a.DoA(1); |
| 457 | a.DoA(2); |
| 458 | a.DoA(2); |
| 459 | } |
| 460 | |
| 461 | TEST(ExpectCallSyntaxTest, WillRepeatedlyCannotAppearMultipleTimes) { |
| 462 | MockA a; |
| 463 | |
| 464 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 465 | EXPECT_CALL(a, DoA(1)) |
| 466 | .WillRepeatedly(Return()) |
| 467 | .WillRepeatedly(Return()); |
| 468 | }, ".WillRepeatedly() cannot appear more than once in an " |
| 469 | "EXPECT_CALL()"); |
| 470 | } |
| 471 | |
| 472 | TEST(ExpectCallSyntaxTest, WillRepeatedlyMustBeBeforeRetiresOnSaturation) { |
| 473 | MockA a; |
| 474 | |
| 475 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 476 | EXPECT_CALL(a, DoA(1)) |
| 477 | .RetiresOnSaturation() |
| 478 | .WillRepeatedly(Return()); |
| 479 | }, ".WillRepeatedly() cannot appear after "); |
| 480 | } |
| 481 | |
| 482 | TEST(ExpectCallSyntaxTest, RetiresOnSaturationIsOptional) { |
| 483 | MockA a; |
| 484 | |
| 485 | EXPECT_CALL(a, DoA(1)); |
| 486 | EXPECT_CALL(a, DoA(1)) |
| 487 | .RetiresOnSaturation(); |
| 488 | |
| 489 | a.DoA(1); |
| 490 | a.DoA(1); |
| 491 | } |
| 492 | |
| 493 | TEST(ExpectCallSyntaxTest, RetiresOnSaturationCannotAppearMultipleTimes) { |
| 494 | MockA a; |
| 495 | |
| 496 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 497 | EXPECT_CALL(a, DoA(1)) |
| 498 | .RetiresOnSaturation() |
| 499 | .RetiresOnSaturation(); |
| 500 | }, ".RetiresOnSaturation() cannot appear more than once"); |
| 501 | |
| 502 | a.DoA(1); |
| 503 | } |
| 504 | |
| 505 | TEST(ExpectCallSyntaxTest, DefaultCardinalityIsOnce) { |
| 506 | { |
| 507 | MockA a; |
| 508 | EXPECT_CALL(a, DoA(1)); |
| 509 | a.DoA(1); |
| 510 | } |
| 511 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 512 | MockA a; |
| 513 | EXPECT_CALL(a, DoA(1)); |
| 514 | }, "to be called once"); |
| 515 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 516 | MockA a; |
| 517 | EXPECT_CALL(a, DoA(1)); |
| 518 | a.DoA(1); |
| 519 | a.DoA(1); |
| 520 | }, "to be called once"); |
| 521 | } |
| 522 | |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 523 | #if GTEST_HAS_STREAM_REDIRECTION |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 524 | |
| 525 | // Tests that Google Mock doesn't print a warning when the number of |
| 526 | // WillOnce() is adequate. |
| 527 | TEST(ExpectCallSyntaxTest, DoesNotWarnOnAdequateActionCount) { |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 528 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 529 | { |
| 530 | MockB b; |
| 531 | |
| 532 | // It's always fine to omit WillOnce() entirely. |
| 533 | EXPECT_CALL(b, DoB()) |
| 534 | .Times(0); |
| 535 | EXPECT_CALL(b, DoB(1)) |
| 536 | .Times(AtMost(1)); |
| 537 | EXPECT_CALL(b, DoB(2)) |
| 538 | .Times(1) |
| 539 | .WillRepeatedly(Return(1)); |
| 540 | |
| 541 | // It's fine for the number of WillOnce()s to equal the upper bound. |
| 542 | EXPECT_CALL(b, DoB(3)) |
| 543 | .Times(Between(1, 2)) |
| 544 | .WillOnce(Return(1)) |
| 545 | .WillOnce(Return(2)); |
| 546 | |
| 547 | // It's fine for the number of WillOnce()s to be smaller than the |
| 548 | // upper bound when there is a WillRepeatedly(). |
| 549 | EXPECT_CALL(b, DoB(4)) |
| 550 | .Times(AtMost(3)) |
| 551 | .WillOnce(Return(1)) |
| 552 | .WillRepeatedly(Return(2)); |
| 553 | |
| 554 | // Satisfies the above expectations. |
| 555 | b.DoB(2); |
| 556 | b.DoB(3); |
| 557 | } |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 558 | EXPECT_STREQ("", GetCapturedStdout().c_str()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | // Tests that Google Mock warns on having too many actions in an |
| 562 | // expectation compared to its cardinality. |
| 563 | TEST(ExpectCallSyntaxTest, WarnsOnTooManyActions) { |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 564 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 565 | { |
| 566 | MockB b; |
| 567 | |
| 568 | // Warns when the number of WillOnce()s is larger than the upper bound. |
| 569 | EXPECT_CALL(b, DoB()) |
| 570 | .Times(0) |
| 571 | .WillOnce(Return(1)); // #1 |
| 572 | EXPECT_CALL(b, DoB()) |
| 573 | .Times(AtMost(1)) |
| 574 | .WillOnce(Return(1)) |
| 575 | .WillOnce(Return(2)); // #2 |
| 576 | EXPECT_CALL(b, DoB(1)) |
| 577 | .Times(1) |
| 578 | .WillOnce(Return(1)) |
| 579 | .WillOnce(Return(2)) |
| 580 | .RetiresOnSaturation(); // #3 |
| 581 | |
| 582 | // Warns when the number of WillOnce()s equals the upper bound and |
| 583 | // there is a WillRepeatedly(). |
| 584 | EXPECT_CALL(b, DoB()) |
| 585 | .Times(0) |
| 586 | .WillRepeatedly(Return(1)); // #4 |
| 587 | EXPECT_CALL(b, DoB(2)) |
| 588 | .Times(1) |
| 589 | .WillOnce(Return(1)) |
| 590 | .WillRepeatedly(Return(2)); // #5 |
| 591 | |
| 592 | // Satisfies the above expectations. |
| 593 | b.DoB(1); |
| 594 | b.DoB(2); |
| 595 | } |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 596 | const String output = GetCapturedStdout(); |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 597 | EXPECT_PRED_FORMAT2( |
| 598 | IsSubstring, |
| 599 | "Too many actions specified in EXPECT_CALL(b, DoB())...\n" |
| 600 | "Expected to be never called, but has 1 WillOnce().", |
| 601 | output); // #1 |
| 602 | EXPECT_PRED_FORMAT2( |
| 603 | IsSubstring, |
| 604 | "Too many actions specified in EXPECT_CALL(b, DoB())...\n" |
| 605 | "Expected to be called at most once, " |
| 606 | "but has 2 WillOnce()s.", |
| 607 | output); // #2 |
| 608 | EXPECT_PRED_FORMAT2( |
| 609 | IsSubstring, |
| 610 | "Too many actions specified in EXPECT_CALL(b, DoB(1))...\n" |
| 611 | "Expected to be called once, but has 2 WillOnce()s.", |
| 612 | output); // #3 |
| 613 | EXPECT_PRED_FORMAT2( |
| 614 | IsSubstring, |
| 615 | "Too many actions specified in EXPECT_CALL(b, DoB())...\n" |
| 616 | "Expected to be never called, but has 0 WillOnce()s " |
| 617 | "and a WillRepeatedly().", |
| 618 | output); // #4 |
| 619 | EXPECT_PRED_FORMAT2( |
| 620 | IsSubstring, |
| 621 | "Too many actions specified in EXPECT_CALL(b, DoB(2))...\n" |
| 622 | "Expected to be called once, but has 1 WillOnce() " |
| 623 | "and a WillRepeatedly().", |
| 624 | output); // #5 |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | // Tests that Google Mock warns on having too few actions in an |
| 628 | // expectation compared to its cardinality. |
| 629 | TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) { |
| 630 | MockB b; |
| 631 | |
| 632 | EXPECT_CALL(b, DoB()) |
| 633 | .Times(Between(2, 3)) |
| 634 | .WillOnce(Return(1)); |
| 635 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 636 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 637 | b.DoB(); |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 638 | const String output = GetCapturedStdout(); |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 639 | EXPECT_PRED_FORMAT2( |
| 640 | IsSubstring, |
| 641 | "Too few actions specified in EXPECT_CALL(b, DoB())...\n" |
| 642 | "Expected to be called between 2 and 3 times, " |
| 643 | "but has only 1 WillOnce().", |
| 644 | output); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 645 | b.DoB(); |
| 646 | } |
| 647 | |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 648 | #endif // GTEST_HAS_STREAM_REDIRECTION |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 649 | |
| 650 | // Tests the semantics of ON_CALL(). |
| 651 | |
| 652 | // Tests that the built-in default action is taken when no ON_CALL() |
| 653 | // is specified. |
| 654 | TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCall) { |
| 655 | MockB b; |
| 656 | EXPECT_CALL(b, DoB()); |
| 657 | |
| 658 | EXPECT_EQ(0, b.DoB()); |
| 659 | } |
| 660 | |
| 661 | // Tests that the built-in default action is taken when no ON_CALL() |
| 662 | // matches the invocation. |
| 663 | TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCallMatches) { |
| 664 | MockB b; |
| 665 | ON_CALL(b, DoB(1)) |
| 666 | .WillByDefault(Return(1)); |
| 667 | EXPECT_CALL(b, DoB(_)); |
| 668 | |
| 669 | EXPECT_EQ(0, b.DoB(2)); |
| 670 | } |
| 671 | |
| 672 | // Tests that the last matching ON_CALL() action is taken. |
| 673 | TEST(OnCallTest, PicksLastMatchingOnCall) { |
| 674 | MockB b; |
| 675 | ON_CALL(b, DoB(_)) |
| 676 | .WillByDefault(Return(3)); |
| 677 | ON_CALL(b, DoB(2)) |
| 678 | .WillByDefault(Return(2)); |
| 679 | ON_CALL(b, DoB(1)) |
| 680 | .WillByDefault(Return(1)); |
| 681 | EXPECT_CALL(b, DoB(_)); |
| 682 | |
| 683 | EXPECT_EQ(2, b.DoB(2)); |
| 684 | } |
| 685 | |
| 686 | // Tests the semantics of EXPECT_CALL(). |
| 687 | |
| 688 | // Tests that any call is allowed when no EXPECT_CALL() is specified. |
| 689 | TEST(ExpectCallTest, AllowsAnyCallWhenNoSpec) { |
| 690 | MockB b; |
| 691 | EXPECT_CALL(b, DoB()); |
| 692 | // There is no expectation on DoB(int). |
| 693 | |
| 694 | b.DoB(); |
| 695 | |
| 696 | // DoB(int) can be called any number of times. |
| 697 | b.DoB(1); |
| 698 | b.DoB(2); |
| 699 | } |
| 700 | |
| 701 | // Tests that the last matching EXPECT_CALL() fires. |
| 702 | TEST(ExpectCallTest, PicksLastMatchingExpectCall) { |
| 703 | MockB b; |
| 704 | EXPECT_CALL(b, DoB(_)) |
| 705 | .WillRepeatedly(Return(2)); |
| 706 | EXPECT_CALL(b, DoB(1)) |
| 707 | .WillRepeatedly(Return(1)); |
| 708 | |
| 709 | EXPECT_EQ(1, b.DoB(1)); |
| 710 | } |
| 711 | |
| 712 | // Tests lower-bound violation. |
| 713 | TEST(ExpectCallTest, CatchesTooFewCalls) { |
| 714 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 715 | MockB b; |
| 716 | EXPECT_CALL(b, DoB(5)) |
| 717 | .Times(AtLeast(2)); |
| 718 | |
| 719 | b.DoB(5); |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 720 | }, "Actual function call count doesn't match EXPECT_CALL(b, DoB(5))...\n" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 721 | " Expected: to be called at least twice\n" |
| 722 | " Actual: called once - unsatisfied and active"); |
| 723 | } |
| 724 | |
| 725 | // Tests that the cardinality can be inferred when no Times(...) is |
| 726 | // specified. |
| 727 | TEST(ExpectCallTest, InfersCardinalityWhenThereIsNoWillRepeatedly) { |
| 728 | { |
| 729 | MockB b; |
| 730 | EXPECT_CALL(b, DoB()) |
| 731 | .WillOnce(Return(1)) |
| 732 | .WillOnce(Return(2)); |
| 733 | |
| 734 | EXPECT_EQ(1, b.DoB()); |
| 735 | EXPECT_EQ(2, b.DoB()); |
| 736 | } |
| 737 | |
| 738 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 739 | MockB b; |
| 740 | EXPECT_CALL(b, DoB()) |
| 741 | .WillOnce(Return(1)) |
| 742 | .WillOnce(Return(2)); |
| 743 | |
| 744 | EXPECT_EQ(1, b.DoB()); |
| 745 | }, "to be called twice"); |
| 746 | |
| 747 | { // NOLINT |
| 748 | MockB b; |
| 749 | EXPECT_CALL(b, DoB()) |
| 750 | .WillOnce(Return(1)) |
| 751 | .WillOnce(Return(2)); |
| 752 | |
| 753 | EXPECT_EQ(1, b.DoB()); |
| 754 | EXPECT_EQ(2, b.DoB()); |
| 755 | EXPECT_NONFATAL_FAILURE(b.DoB(), "to be called twice"); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | TEST(ExpectCallTest, InfersCardinality1WhenThereIsWillRepeatedly) { |
| 760 | { |
| 761 | MockB b; |
| 762 | EXPECT_CALL(b, DoB()) |
| 763 | .WillOnce(Return(1)) |
| 764 | .WillRepeatedly(Return(2)); |
| 765 | |
| 766 | EXPECT_EQ(1, b.DoB()); |
| 767 | } |
| 768 | |
| 769 | { // NOLINT |
| 770 | MockB b; |
| 771 | EXPECT_CALL(b, DoB()) |
| 772 | .WillOnce(Return(1)) |
| 773 | .WillRepeatedly(Return(2)); |
| 774 | |
| 775 | EXPECT_EQ(1, b.DoB()); |
| 776 | EXPECT_EQ(2, b.DoB()); |
| 777 | EXPECT_EQ(2, b.DoB()); |
| 778 | } |
| 779 | |
| 780 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 781 | MockB b; |
| 782 | EXPECT_CALL(b, DoB()) |
| 783 | .WillOnce(Return(1)) |
| 784 | .WillRepeatedly(Return(2)); |
| 785 | }, "to be called at least once"); |
| 786 | } |
| 787 | |
| 788 | // Tests that the n-th action is taken for the n-th matching |
| 789 | // invocation. |
| 790 | TEST(ExpectCallTest, NthMatchTakesNthAction) { |
| 791 | MockB b; |
| 792 | EXPECT_CALL(b, DoB()) |
| 793 | .WillOnce(Return(1)) |
| 794 | .WillOnce(Return(2)) |
| 795 | .WillOnce(Return(3)); |
| 796 | |
| 797 | EXPECT_EQ(1, b.DoB()); |
| 798 | EXPECT_EQ(2, b.DoB()); |
| 799 | EXPECT_EQ(3, b.DoB()); |
| 800 | } |
| 801 | |
vladlosev | e5121b5 | 2011-02-11 23:50:38 +0000 | [diff] [blame] | 802 | // Tests that the WillRepeatedly() action is taken when the WillOnce(...) |
| 803 | // list is exhausted. |
| 804 | TEST(ExpectCallTest, TakesRepeatedActionWhenWillListIsExhausted) { |
| 805 | MockB b; |
| 806 | EXPECT_CALL(b, DoB()) |
| 807 | .WillOnce(Return(1)) |
| 808 | .WillRepeatedly(Return(2)); |
| 809 | |
| 810 | EXPECT_EQ(1, b.DoB()); |
| 811 | EXPECT_EQ(2, b.DoB()); |
| 812 | EXPECT_EQ(2, b.DoB()); |
| 813 | } |
| 814 | |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 815 | #if GTEST_HAS_STREAM_REDIRECTION |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 816 | |
| 817 | // Tests that the default action is taken when the WillOnce(...) list is |
| 818 | // exhausted and there is no WillRepeatedly(). |
| 819 | TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) { |
| 820 | MockB b; |
| 821 | EXPECT_CALL(b, DoB(_)) |
| 822 | .Times(1); |
| 823 | EXPECT_CALL(b, DoB()) |
| 824 | .Times(AnyNumber()) |
| 825 | .WillOnce(Return(1)) |
| 826 | .WillOnce(Return(2)); |
| 827 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 828 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 829 | EXPECT_EQ(0, b.DoB(1)); // Shouldn't generate a warning as the |
| 830 | // expectation has no action clause at all. |
| 831 | EXPECT_EQ(1, b.DoB()); |
| 832 | EXPECT_EQ(2, b.DoB()); |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 833 | const String output1 = GetCapturedStdout(); |
| 834 | EXPECT_STREQ("", output1.c_str()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 835 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 836 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 837 | EXPECT_EQ(0, b.DoB()); |
| 838 | EXPECT_EQ(0, b.DoB()); |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 839 | const String output2 = GetCapturedStdout(); |
| 840 | EXPECT_THAT(output2.c_str(), |
| 841 | HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n" |
| 842 | "Called 3 times, but only 2 WillOnce()s are specified" |
| 843 | " - returning default value.")); |
| 844 | EXPECT_THAT(output2.c_str(), |
| 845 | HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n" |
| 846 | "Called 4 times, but only 2 WillOnce()s are specified" |
| 847 | " - returning default value.")); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 848 | } |
| 849 | |
vladlosev | e5121b5 | 2011-02-11 23:50:38 +0000 | [diff] [blame] | 850 | TEST(FunctionMockerTest, ReportsExpectCallLocationForExhausedActions) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 851 | MockB b; |
vladlosev | e5121b5 | 2011-02-11 23:50:38 +0000 | [diff] [blame] | 852 | std::string expect_call_location = FormatFileLocation(__FILE__, __LINE__ + 1); |
| 853 | EXPECT_CALL(b, DoB()).Times(AnyNumber()).WillOnce(Return(1)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 854 | |
| 855 | EXPECT_EQ(1, b.DoB()); |
vladlosev | e5121b5 | 2011-02-11 23:50:38 +0000 | [diff] [blame] | 856 | |
| 857 | CaptureStdout(); |
| 858 | EXPECT_EQ(0, b.DoB()); |
| 859 | const String output = GetCapturedStdout(); |
| 860 | // The warning message should contain the call location. |
| 861 | EXPECT_PRED_FORMAT2(IsSubstring, expect_call_location, output); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 862 | } |
| 863 | |
vladlosev | e5121b5 | 2011-02-11 23:50:38 +0000 | [diff] [blame] | 864 | TEST(FunctionMockerTest, ReportsDefaultActionLocationOfUninterestingCalls) { |
| 865 | std::string on_call_location; |
| 866 | CaptureStdout(); |
| 867 | { |
| 868 | MockB b; |
| 869 | on_call_location = FormatFileLocation(__FILE__, __LINE__ + 1); |
| 870 | ON_CALL(b, DoB(_)).WillByDefault(Return(0)); |
| 871 | b.DoB(0); |
| 872 | } |
| 873 | EXPECT_PRED_FORMAT2(IsSubstring, on_call_location, GetCapturedStdout()); |
| 874 | } |
| 875 | |
| 876 | #endif // GTEST_HAS_STREAM_REDIRECTION |
| 877 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 878 | // Tests that an uninteresting call performs the default action. |
| 879 | TEST(UninterestingCallTest, DoesDefaultAction) { |
| 880 | // When there is an ON_CALL() statement, the action specified by it |
| 881 | // should be taken. |
| 882 | MockA a; |
| 883 | ON_CALL(a, Binary(_, _)) |
| 884 | .WillByDefault(Return(true)); |
| 885 | EXPECT_TRUE(a.Binary(1, 2)); |
| 886 | |
| 887 | // When there is no ON_CALL(), the default value for the return type |
| 888 | // should be returned. |
| 889 | MockB b; |
| 890 | EXPECT_EQ(0, b.DoB()); |
| 891 | } |
| 892 | |
| 893 | // Tests that an unexpected call performs the default action. |
| 894 | TEST(UnexpectedCallTest, DoesDefaultAction) { |
| 895 | // When there is an ON_CALL() statement, the action specified by it |
| 896 | // should be taken. |
| 897 | MockA a; |
| 898 | ON_CALL(a, Binary(_, _)) |
| 899 | .WillByDefault(Return(true)); |
| 900 | EXPECT_CALL(a, Binary(0, 0)); |
| 901 | a.Binary(0, 0); |
| 902 | bool result = false; |
| 903 | EXPECT_NONFATAL_FAILURE(result = a.Binary(1, 2), |
| 904 | "Unexpected mock function call"); |
| 905 | EXPECT_TRUE(result); |
| 906 | |
| 907 | // When there is no ON_CALL(), the default value for the return type |
| 908 | // should be returned. |
| 909 | MockB b; |
| 910 | EXPECT_CALL(b, DoB(0)) |
| 911 | .Times(0); |
| 912 | int n = -1; |
| 913 | EXPECT_NONFATAL_FAILURE(n = b.DoB(1), |
| 914 | "Unexpected mock function call"); |
| 915 | EXPECT_EQ(0, n); |
| 916 | } |
| 917 | |
| 918 | // Tests that when an unexpected void function generates the right |
| 919 | // failure message. |
| 920 | TEST(UnexpectedCallTest, GeneratesFailureForVoidFunction) { |
| 921 | // First, tests the message when there is only one EXPECT_CALL(). |
| 922 | MockA a1; |
| 923 | EXPECT_CALL(a1, DoA(1)); |
| 924 | a1.DoA(1); |
| 925 | // Ideally we should match the failure message against a regex, but |
| 926 | // EXPECT_NONFATAL_FAILURE doesn't support that, so we test for |
| 927 | // multiple sub-strings instead. |
| 928 | EXPECT_NONFATAL_FAILURE( |
| 929 | a1.DoA(9), |
| 930 | "Unexpected mock function call - returning directly.\n" |
| 931 | " Function call: DoA(9)\n" |
| 932 | "Google Mock tried the following 1 expectation, but it didn't match:"); |
| 933 | EXPECT_NONFATAL_FAILURE( |
| 934 | a1.DoA(9), |
| 935 | " Expected arg #0: is equal to 1\n" |
| 936 | " Actual: 9\n" |
| 937 | " Expected: to be called once\n" |
| 938 | " Actual: called once - saturated and active"); |
| 939 | |
| 940 | // Next, tests the message when there are more than one EXPECT_CALL(). |
| 941 | MockA a2; |
| 942 | EXPECT_CALL(a2, DoA(1)); |
| 943 | EXPECT_CALL(a2, DoA(3)); |
| 944 | a2.DoA(1); |
| 945 | EXPECT_NONFATAL_FAILURE( |
| 946 | a2.DoA(2), |
| 947 | "Unexpected mock function call - returning directly.\n" |
| 948 | " Function call: DoA(2)\n" |
| 949 | "Google Mock tried the following 2 expectations, but none matched:"); |
| 950 | EXPECT_NONFATAL_FAILURE( |
| 951 | a2.DoA(2), |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 952 | "tried expectation #0: EXPECT_CALL(a2, DoA(1))...\n" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 953 | " Expected arg #0: is equal to 1\n" |
| 954 | " Actual: 2\n" |
| 955 | " Expected: to be called once\n" |
| 956 | " Actual: called once - saturated and active"); |
| 957 | EXPECT_NONFATAL_FAILURE( |
| 958 | a2.DoA(2), |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 959 | "tried expectation #1: EXPECT_CALL(a2, DoA(3))...\n" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 960 | " Expected arg #0: is equal to 3\n" |
| 961 | " Actual: 2\n" |
| 962 | " Expected: to be called once\n" |
| 963 | " Actual: never called - unsatisfied and active"); |
| 964 | a2.DoA(3); |
| 965 | } |
| 966 | |
| 967 | // Tests that an unexpected non-void function generates the right |
| 968 | // failure message. |
| 969 | TEST(UnexpectedCallTest, GeneartesFailureForNonVoidFunction) { |
| 970 | MockB b1; |
| 971 | EXPECT_CALL(b1, DoB(1)); |
| 972 | b1.DoB(1); |
| 973 | EXPECT_NONFATAL_FAILURE( |
| 974 | b1.DoB(2), |
| 975 | "Unexpected mock function call - returning default value.\n" |
| 976 | " Function call: DoB(2)\n" |
| 977 | " Returns: 0\n" |
| 978 | "Google Mock tried the following 1 expectation, but it didn't match:"); |
| 979 | EXPECT_NONFATAL_FAILURE( |
| 980 | b1.DoB(2), |
| 981 | " Expected arg #0: is equal to 1\n" |
| 982 | " Actual: 2\n" |
| 983 | " Expected: to be called once\n" |
| 984 | " Actual: called once - saturated and active"); |
| 985 | } |
| 986 | |
| 987 | // Tests that Google Mock explains that an retired expectation doesn't |
| 988 | // match the call. |
| 989 | TEST(UnexpectedCallTest, RetiredExpectation) { |
| 990 | MockB b; |
| 991 | EXPECT_CALL(b, DoB(1)) |
| 992 | .RetiresOnSaturation(); |
| 993 | |
| 994 | b.DoB(1); |
| 995 | EXPECT_NONFATAL_FAILURE( |
| 996 | b.DoB(1), |
| 997 | " Expected: the expectation is active\n" |
| 998 | " Actual: it is retired"); |
| 999 | } |
| 1000 | |
| 1001 | // Tests that Google Mock explains that an expectation that doesn't |
| 1002 | // match the arguments doesn't match the call. |
| 1003 | TEST(UnexpectedCallTest, UnmatchedArguments) { |
| 1004 | MockB b; |
| 1005 | EXPECT_CALL(b, DoB(1)); |
| 1006 | |
| 1007 | EXPECT_NONFATAL_FAILURE( |
| 1008 | b.DoB(2), |
| 1009 | " Expected arg #0: is equal to 1\n" |
| 1010 | " Actual: 2\n"); |
| 1011 | b.DoB(1); |
| 1012 | } |
| 1013 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1014 | // Tests that Google Mock explains that an expectation with |
| 1015 | // unsatisfied pre-requisites doesn't match the call. |
| 1016 | TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) { |
| 1017 | Sequence s1, s2; |
| 1018 | MockB b; |
| 1019 | EXPECT_CALL(b, DoB(1)) |
| 1020 | .InSequence(s1); |
| 1021 | EXPECT_CALL(b, DoB(2)) |
| 1022 | .Times(AnyNumber()) |
| 1023 | .InSequence(s1); |
| 1024 | EXPECT_CALL(b, DoB(3)) |
| 1025 | .InSequence(s2); |
| 1026 | EXPECT_CALL(b, DoB(4)) |
| 1027 | .InSequence(s1, s2); |
| 1028 | |
| 1029 | ::testing::TestPartResultArray failures; |
| 1030 | { |
| 1031 | ::testing::ScopedFakeTestPartResultReporter reporter(&failures); |
| 1032 | b.DoB(4); |
| 1033 | // Now 'failures' contains the Google Test failures generated by |
| 1034 | // the above statement. |
| 1035 | } |
| 1036 | |
| 1037 | // There should be one non-fatal failure. |
| 1038 | ASSERT_EQ(1, failures.size()); |
| 1039 | const ::testing::TestPartResult& r = failures.GetTestPartResult(0); |
zhanyong.wan | bbd6e10 | 2009-09-18 18:17:19 +0000 | [diff] [blame] | 1040 | EXPECT_EQ(::testing::TestPartResult::kNonFatalFailure, r.type()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1041 | |
| 1042 | // Verifies that the failure message contains the two unsatisfied |
| 1043 | // pre-requisites but not the satisfied one. |
zhanyong.wan | d14aaed | 2010-01-14 05:36:32 +0000 | [diff] [blame] | 1044 | #if GTEST_USES_PCRE |
| 1045 | EXPECT_THAT(r.message(), ContainsRegex( |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1046 | // PCRE has trouble using (.|\n) to match any character, but |
| 1047 | // supports the (?s) prefix for using . to match any character. |
| 1048 | "(?s)the following immediate pre-requisites are not satisfied:\n" |
| 1049 | ".*: pre-requisite #0\n" |
zhanyong.wan | d14aaed | 2010-01-14 05:36:32 +0000 | [diff] [blame] | 1050 | ".*: pre-requisite #1")); |
| 1051 | #elif GTEST_USES_POSIX_RE |
| 1052 | EXPECT_THAT(r.message(), ContainsRegex( |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1053 | // POSIX RE doesn't understand the (?s) prefix, but has no trouble |
| 1054 | // with (.|\n). |
| 1055 | "the following immediate pre-requisites are not satisfied:\n" |
| 1056 | "(.|\n)*: pre-requisite #0\n" |
zhanyong.wan | d14aaed | 2010-01-14 05:36:32 +0000 | [diff] [blame] | 1057 | "(.|\n)*: pre-requisite #1")); |
| 1058 | #else |
| 1059 | // We can only use Google Test's own simple regex. |
| 1060 | EXPECT_THAT(r.message(), ContainsRegex( |
| 1061 | "the following immediate pre-requisites are not satisfied:")); |
| 1062 | EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #0")); |
| 1063 | EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #1")); |
| 1064 | #endif // GTEST_USES_PCRE |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1065 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1066 | b.DoB(1); |
| 1067 | b.DoB(3); |
| 1068 | b.DoB(4); |
| 1069 | } |
| 1070 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 1071 | TEST(UndefinedReturnValueTest, ReturnValueIsMandatory) { |
| 1072 | MockA a; |
| 1073 | // TODO(wan@google.com): We should really verify the output message, |
| 1074 | // but we cannot yet due to that EXPECT_DEATH only captures stderr |
| 1075 | // while Google Mock logs to stdout. |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 1076 | EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(1), ""); |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1079 | // Tests that an excessive call (one whose arguments match the |
| 1080 | // matchers but is called too many times) performs the default action. |
| 1081 | TEST(ExcessiveCallTest, DoesDefaultAction) { |
| 1082 | // When there is an ON_CALL() statement, the action specified by it |
| 1083 | // should be taken. |
| 1084 | MockA a; |
| 1085 | ON_CALL(a, Binary(_, _)) |
| 1086 | .WillByDefault(Return(true)); |
| 1087 | EXPECT_CALL(a, Binary(0, 0)); |
| 1088 | a.Binary(0, 0); |
| 1089 | bool result = false; |
| 1090 | EXPECT_NONFATAL_FAILURE(result = a.Binary(0, 0), |
| 1091 | "Mock function called more times than expected"); |
| 1092 | EXPECT_TRUE(result); |
| 1093 | |
| 1094 | // When there is no ON_CALL(), the default value for the return type |
| 1095 | // should be returned. |
| 1096 | MockB b; |
| 1097 | EXPECT_CALL(b, DoB(0)) |
| 1098 | .Times(0); |
| 1099 | int n = -1; |
| 1100 | EXPECT_NONFATAL_FAILURE(n = b.DoB(0), |
| 1101 | "Mock function called more times than expected"); |
| 1102 | EXPECT_EQ(0, n); |
| 1103 | } |
| 1104 | |
| 1105 | // Tests that when a void function is called too many times, |
| 1106 | // the failure message contains the argument values. |
| 1107 | TEST(ExcessiveCallTest, GeneratesFailureForVoidFunction) { |
| 1108 | MockA a; |
| 1109 | EXPECT_CALL(a, DoA(_)) |
| 1110 | .Times(0); |
| 1111 | EXPECT_NONFATAL_FAILURE( |
| 1112 | a.DoA(9), |
| 1113 | "Mock function called more times than expected - returning directly.\n" |
| 1114 | " Function call: DoA(9)\n" |
| 1115 | " Expected: to be never called\n" |
| 1116 | " Actual: called once - over-saturated and active"); |
| 1117 | } |
| 1118 | |
| 1119 | // Tests that when a non-void function is called too many times, the |
| 1120 | // failure message contains the argument values and the return value. |
| 1121 | TEST(ExcessiveCallTest, GeneratesFailureForNonVoidFunction) { |
| 1122 | MockB b; |
| 1123 | EXPECT_CALL(b, DoB(_)); |
| 1124 | b.DoB(1); |
| 1125 | EXPECT_NONFATAL_FAILURE( |
| 1126 | b.DoB(2), |
| 1127 | "Mock function called more times than expected - " |
| 1128 | "returning default value.\n" |
| 1129 | " Function call: DoB(2)\n" |
| 1130 | " Returns: 0\n" |
| 1131 | " Expected: to be called once\n" |
| 1132 | " Actual: called twice - over-saturated and active"); |
| 1133 | } |
| 1134 | |
| 1135 | // Tests using sequences. |
| 1136 | |
| 1137 | TEST(InSequenceTest, AllExpectationInScopeAreInSequence) { |
| 1138 | MockA a; |
| 1139 | { |
| 1140 | InSequence dummy; |
| 1141 | |
| 1142 | EXPECT_CALL(a, DoA(1)); |
| 1143 | EXPECT_CALL(a, DoA(2)); |
| 1144 | } |
| 1145 | |
| 1146 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 1147 | a.DoA(2); |
| 1148 | }, "Unexpected mock function call"); |
| 1149 | |
| 1150 | a.DoA(1); |
| 1151 | a.DoA(2); |
| 1152 | } |
| 1153 | |
| 1154 | TEST(InSequenceTest, NestedInSequence) { |
| 1155 | MockA a; |
| 1156 | { |
| 1157 | InSequence dummy; |
| 1158 | |
| 1159 | EXPECT_CALL(a, DoA(1)); |
| 1160 | { |
| 1161 | InSequence dummy2; |
| 1162 | |
| 1163 | EXPECT_CALL(a, DoA(2)); |
| 1164 | EXPECT_CALL(a, DoA(3)); |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 1169 | a.DoA(1); |
| 1170 | a.DoA(3); |
| 1171 | }, "Unexpected mock function call"); |
| 1172 | |
| 1173 | a.DoA(2); |
| 1174 | a.DoA(3); |
| 1175 | } |
| 1176 | |
| 1177 | TEST(InSequenceTest, ExpectationsOutOfScopeAreNotAffected) { |
| 1178 | MockA a; |
| 1179 | { |
| 1180 | InSequence dummy; |
| 1181 | |
| 1182 | EXPECT_CALL(a, DoA(1)); |
| 1183 | EXPECT_CALL(a, DoA(2)); |
| 1184 | } |
| 1185 | EXPECT_CALL(a, DoA(3)); |
| 1186 | |
| 1187 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 1188 | a.DoA(2); |
| 1189 | }, "Unexpected mock function call"); |
| 1190 | |
| 1191 | a.DoA(3); |
| 1192 | a.DoA(1); |
| 1193 | a.DoA(2); |
| 1194 | } |
| 1195 | |
| 1196 | // Tests that any order is allowed when no sequence is used. |
| 1197 | TEST(SequenceTest, AnyOrderIsOkByDefault) { |
| 1198 | { |
| 1199 | MockA a; |
| 1200 | MockB b; |
| 1201 | |
| 1202 | EXPECT_CALL(a, DoA(1)); |
| 1203 | EXPECT_CALL(b, DoB()) |
| 1204 | .Times(AnyNumber()); |
| 1205 | |
| 1206 | a.DoA(1); |
| 1207 | b.DoB(); |
| 1208 | } |
| 1209 | |
| 1210 | { // NOLINT |
| 1211 | MockA a; |
| 1212 | MockB b; |
| 1213 | |
| 1214 | EXPECT_CALL(a, DoA(1)); |
| 1215 | EXPECT_CALL(b, DoB()) |
| 1216 | .Times(AnyNumber()); |
| 1217 | |
| 1218 | b.DoB(); |
| 1219 | a.DoA(1); |
| 1220 | } |
| 1221 | } |
| 1222 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1223 | // Tests that the calls must be in strict order when a complete order |
| 1224 | // is specified. |
| 1225 | TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo) { |
| 1226 | MockA a; |
| 1227 | Sequence s; |
| 1228 | |
| 1229 | EXPECT_CALL(a, ReturnResult(1)) |
| 1230 | .InSequence(s) |
| 1231 | .WillOnce(Return(Result())); |
| 1232 | |
| 1233 | EXPECT_CALL(a, ReturnResult(2)) |
| 1234 | .InSequence(s) |
| 1235 | .WillOnce(Return(Result())); |
| 1236 | |
| 1237 | EXPECT_CALL(a, ReturnResult(3)) |
| 1238 | .InSequence(s) |
| 1239 | .WillOnce(Return(Result())); |
| 1240 | |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 1241 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1242 | a.ReturnResult(1); |
| 1243 | a.ReturnResult(3); |
| 1244 | a.ReturnResult(2); |
| 1245 | }, ""); |
| 1246 | |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 1247 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1248 | a.ReturnResult(2); |
| 1249 | a.ReturnResult(1); |
| 1250 | a.ReturnResult(3); |
| 1251 | }, ""); |
| 1252 | |
| 1253 | a.ReturnResult(1); |
| 1254 | a.ReturnResult(2); |
| 1255 | a.ReturnResult(3); |
| 1256 | } |
| 1257 | |
| 1258 | // Tests specifying a DAG using multiple sequences. |
| 1259 | TEST(SequenceTest, CallsMustConformToSpecifiedDag) { |
| 1260 | MockA a; |
| 1261 | MockB b; |
| 1262 | Sequence x, y; |
| 1263 | |
| 1264 | EXPECT_CALL(a, ReturnResult(1)) |
| 1265 | .InSequence(x) |
| 1266 | .WillOnce(Return(Result())); |
| 1267 | |
| 1268 | EXPECT_CALL(b, DoB()) |
| 1269 | .Times(2) |
| 1270 | .InSequence(y); |
| 1271 | |
| 1272 | EXPECT_CALL(a, ReturnResult(2)) |
| 1273 | .InSequence(x, y) |
| 1274 | .WillRepeatedly(Return(Result())); |
| 1275 | |
| 1276 | EXPECT_CALL(a, ReturnResult(3)) |
| 1277 | .InSequence(x) |
| 1278 | .WillOnce(Return(Result())); |
| 1279 | |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 1280 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1281 | a.ReturnResult(1); |
| 1282 | b.DoB(); |
| 1283 | a.ReturnResult(2); |
| 1284 | }, ""); |
| 1285 | |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 1286 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1287 | a.ReturnResult(2); |
| 1288 | }, ""); |
| 1289 | |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 1290 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1291 | a.ReturnResult(3); |
| 1292 | }, ""); |
| 1293 | |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 1294 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1295 | a.ReturnResult(1); |
| 1296 | b.DoB(); |
| 1297 | b.DoB(); |
| 1298 | a.ReturnResult(3); |
| 1299 | a.ReturnResult(2); |
| 1300 | }, ""); |
| 1301 | |
| 1302 | b.DoB(); |
| 1303 | a.ReturnResult(1); |
| 1304 | b.DoB(); |
| 1305 | a.ReturnResult(3); |
| 1306 | } |
| 1307 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1308 | TEST(SequenceTest, Retirement) { |
| 1309 | MockA a; |
| 1310 | Sequence s; |
| 1311 | |
| 1312 | EXPECT_CALL(a, DoA(1)) |
| 1313 | .InSequence(s); |
| 1314 | EXPECT_CALL(a, DoA(_)) |
| 1315 | .InSequence(s) |
| 1316 | .RetiresOnSaturation(); |
| 1317 | EXPECT_CALL(a, DoA(1)) |
| 1318 | .InSequence(s); |
| 1319 | |
| 1320 | a.DoA(1); |
| 1321 | a.DoA(2); |
| 1322 | a.DoA(1); |
| 1323 | } |
| 1324 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1325 | // Tests Expectation. |
| 1326 | |
| 1327 | TEST(ExpectationTest, ConstrutorsWork) { |
| 1328 | MockA a; |
| 1329 | Expectation e1; // Default ctor. |
| 1330 | Expectation e2 = EXPECT_CALL(a, DoA(1)); // Ctor from EXPECT_CALL. |
| 1331 | Expectation e3 = e2; // Copy ctor. |
| 1332 | |
| 1333 | EXPECT_THAT(e1, Ne(e2)); |
| 1334 | EXPECT_THAT(e2, Eq(e3)); |
| 1335 | a.DoA(1); |
| 1336 | } |
| 1337 | |
| 1338 | TEST(ExpectationTest, AssignmentWorks) { |
| 1339 | MockA a; |
| 1340 | Expectation e1; |
| 1341 | Expectation e2 = EXPECT_CALL(a, DoA(1)); |
| 1342 | |
| 1343 | EXPECT_THAT(e1, Ne(e2)); |
| 1344 | |
| 1345 | e1 = e2; |
| 1346 | EXPECT_THAT(e1, Eq(e2)); |
| 1347 | |
| 1348 | a.DoA(1); |
| 1349 | } |
| 1350 | |
| 1351 | // Tests ExpectationSet. |
| 1352 | |
| 1353 | TEST(ExpectationSetTest, MemberTypesAreCorrect) { |
| 1354 | ::testing::StaticAssertTypeEq<Expectation, ExpectationSet::value_type>(); |
| 1355 | } |
| 1356 | |
| 1357 | TEST(ExpectationSetTest, ConstructorsWork) { |
| 1358 | MockA a; |
| 1359 | |
| 1360 | Expectation e1; |
| 1361 | const Expectation e2; |
| 1362 | ExpectationSet es1; // Default ctor. |
| 1363 | ExpectationSet es2 = EXPECT_CALL(a, DoA(1)); // Ctor from EXPECT_CALL. |
| 1364 | ExpectationSet es3 = e1; // Ctor from Expectation. |
| 1365 | ExpectationSet es4(e1); // Ctor from Expectation; alternative syntax. |
| 1366 | ExpectationSet es5 = e2; // Ctor from const Expectation. |
| 1367 | ExpectationSet es6(e2); // Ctor from const Expectation; alternative syntax. |
| 1368 | ExpectationSet es7 = es2; // Copy ctor. |
| 1369 | |
| 1370 | EXPECT_EQ(0, es1.size()); |
| 1371 | EXPECT_EQ(1, es2.size()); |
| 1372 | EXPECT_EQ(1, es3.size()); |
| 1373 | EXPECT_EQ(1, es4.size()); |
| 1374 | EXPECT_EQ(1, es5.size()); |
| 1375 | EXPECT_EQ(1, es6.size()); |
| 1376 | EXPECT_EQ(1, es7.size()); |
| 1377 | |
| 1378 | EXPECT_THAT(es3, Ne(es2)); |
| 1379 | EXPECT_THAT(es4, Eq(es3)); |
| 1380 | EXPECT_THAT(es5, Eq(es4)); |
| 1381 | EXPECT_THAT(es6, Eq(es5)); |
| 1382 | EXPECT_THAT(es7, Eq(es2)); |
| 1383 | a.DoA(1); |
| 1384 | } |
| 1385 | |
| 1386 | TEST(ExpectationSetTest, AssignmentWorks) { |
| 1387 | ExpectationSet es1; |
| 1388 | ExpectationSet es2 = Expectation(); |
| 1389 | |
| 1390 | es1 = es2; |
| 1391 | EXPECT_EQ(1, es1.size()); |
| 1392 | EXPECT_THAT(*(es1.begin()), Eq(Expectation())); |
| 1393 | EXPECT_THAT(es1, Eq(es2)); |
| 1394 | } |
| 1395 | |
| 1396 | TEST(ExpectationSetTest, InsertionWorks) { |
| 1397 | ExpectationSet es1; |
| 1398 | Expectation e1; |
| 1399 | es1 += e1; |
| 1400 | EXPECT_EQ(1, es1.size()); |
| 1401 | EXPECT_THAT(*(es1.begin()), Eq(e1)); |
| 1402 | |
| 1403 | MockA a; |
| 1404 | Expectation e2 = EXPECT_CALL(a, DoA(1)); |
| 1405 | es1 += e2; |
| 1406 | EXPECT_EQ(2, es1.size()); |
| 1407 | |
| 1408 | ExpectationSet::const_iterator it1 = es1.begin(); |
| 1409 | ExpectationSet::const_iterator it2 = it1; |
| 1410 | ++it2; |
| 1411 | EXPECT_TRUE(*it1 == e1 || *it2 == e1); // e1 must be in the set. |
| 1412 | EXPECT_TRUE(*it1 == e2 || *it2 == e2); // e2 must be in the set too. |
| 1413 | a.DoA(1); |
| 1414 | } |
| 1415 | |
| 1416 | TEST(ExpectationSetTest, SizeWorks) { |
| 1417 | ExpectationSet es; |
| 1418 | EXPECT_EQ(0, es.size()); |
| 1419 | |
| 1420 | es += Expectation(); |
| 1421 | EXPECT_EQ(1, es.size()); |
| 1422 | |
| 1423 | MockA a; |
| 1424 | es += EXPECT_CALL(a, DoA(1)); |
| 1425 | EXPECT_EQ(2, es.size()); |
| 1426 | |
| 1427 | a.DoA(1); |
| 1428 | } |
| 1429 | |
| 1430 | TEST(ExpectationSetTest, IsEnumerable) { |
| 1431 | ExpectationSet es; |
| 1432 | EXPECT_THAT(es.begin(), Eq(es.end())); |
| 1433 | |
| 1434 | es += Expectation(); |
| 1435 | ExpectationSet::const_iterator it = es.begin(); |
| 1436 | EXPECT_THAT(it, Ne(es.end())); |
| 1437 | EXPECT_THAT(*it, Eq(Expectation())); |
| 1438 | ++it; |
| 1439 | EXPECT_THAT(it, Eq(es.end())); |
| 1440 | } |
| 1441 | |
| 1442 | // Tests the .After() clause. |
| 1443 | |
| 1444 | TEST(AfterTest, SucceedsWhenPartialOrderIsSatisfied) { |
| 1445 | MockA a; |
| 1446 | ExpectationSet es; |
| 1447 | es += EXPECT_CALL(a, DoA(1)); |
| 1448 | es += EXPECT_CALL(a, DoA(2)); |
| 1449 | EXPECT_CALL(a, DoA(3)) |
| 1450 | .After(es); |
| 1451 | |
| 1452 | a.DoA(1); |
| 1453 | a.DoA(2); |
| 1454 | a.DoA(3); |
| 1455 | } |
| 1456 | |
| 1457 | TEST(AfterTest, SucceedsWhenTotalOrderIsSatisfied) { |
| 1458 | MockA a; |
| 1459 | MockB b; |
| 1460 | // The following also verifies that const Expectation objects work |
| 1461 | // too. Do not remove the const modifiers. |
| 1462 | const Expectation e1 = EXPECT_CALL(a, DoA(1)); |
| 1463 | const Expectation e2 = EXPECT_CALL(b, DoB()) |
| 1464 | .Times(2) |
| 1465 | .After(e1); |
| 1466 | EXPECT_CALL(a, DoA(2)).After(e2); |
| 1467 | |
| 1468 | a.DoA(1); |
| 1469 | b.DoB(); |
| 1470 | b.DoB(); |
| 1471 | a.DoA(2); |
| 1472 | } |
| 1473 | |
| 1474 | // Calls must be in strict order when specified so. |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 1475 | TEST(AfterDeathTest, CallsMustBeInStrictOrderWhenSpecifiedSo) { |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1476 | MockA a; |
| 1477 | MockB b; |
| 1478 | Expectation e1 = EXPECT_CALL(a, DoA(1)); |
| 1479 | Expectation e2 = EXPECT_CALL(b, DoB()) |
| 1480 | .Times(2) |
| 1481 | .After(e1); |
| 1482 | EXPECT_CALL(a, ReturnResult(2)) |
| 1483 | .After(e2) |
| 1484 | .WillOnce(Return(Result())); |
| 1485 | |
| 1486 | a.DoA(1); |
| 1487 | // If a call to ReturnResult() violates the specified order, no |
| 1488 | // matching expectation will be found, and thus the default action |
| 1489 | // will be done. Since the return type of ReturnResult() is not a |
| 1490 | // built-in type, gmock won't know what to return and will thus |
| 1491 | // abort the program. Therefore a death test can tell us whether |
| 1492 | // gmock catches the order violation correctly. |
| 1493 | // |
| 1494 | // gtest and gmock print messages to stdout, which isn't captured by |
| 1495 | // death tests. Therefore we have to match with an empty regular |
| 1496 | // expression in all the EXPECT_DEATH()s. |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 1497 | EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(2), ""); |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1498 | |
| 1499 | b.DoB(); |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 1500 | EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(2), ""); |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1501 | |
| 1502 | b.DoB(); |
| 1503 | a.ReturnResult(2); |
| 1504 | } |
| 1505 | |
| 1506 | // Calls must satisfy the partial order when specified so. |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 1507 | TEST(AfterDeathTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo) { |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1508 | MockA a; |
| 1509 | Expectation e = EXPECT_CALL(a, DoA(1)); |
| 1510 | const ExpectationSet es = EXPECT_CALL(a, DoA(2)); |
| 1511 | EXPECT_CALL(a, ReturnResult(3)) |
| 1512 | .After(e, es) |
| 1513 | .WillOnce(Return(Result())); |
| 1514 | |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 1515 | EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), ""); |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1516 | |
| 1517 | a.DoA(2); |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 1518 | EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), ""); |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1519 | |
| 1520 | a.DoA(1); |
| 1521 | a.ReturnResult(3); |
| 1522 | } |
| 1523 | |
| 1524 | // .After() can be combined with .InSequence(). |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 1525 | TEST(AfterDeathTest, CanBeUsedWithInSequence) { |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1526 | MockA a; |
| 1527 | Sequence s; |
| 1528 | Expectation e = EXPECT_CALL(a, DoA(1)); |
| 1529 | EXPECT_CALL(a, DoA(2)).InSequence(s); |
| 1530 | EXPECT_CALL(a, ReturnResult(3)) |
| 1531 | .InSequence(s).After(e) |
| 1532 | .WillOnce(Return(Result())); |
| 1533 | |
| 1534 | a.DoA(1); |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 1535 | EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), ""); |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1536 | |
| 1537 | a.DoA(2); |
| 1538 | a.ReturnResult(3); |
| 1539 | } |
| 1540 | |
| 1541 | // .After() can be called multiple times. |
| 1542 | TEST(AfterTest, CanBeCalledManyTimes) { |
| 1543 | MockA a; |
| 1544 | Expectation e1 = EXPECT_CALL(a, DoA(1)); |
| 1545 | Expectation e2 = EXPECT_CALL(a, DoA(2)); |
| 1546 | Expectation e3 = EXPECT_CALL(a, DoA(3)); |
| 1547 | EXPECT_CALL(a, DoA(4)) |
| 1548 | .After(e1) |
| 1549 | .After(e2) |
| 1550 | .After(e3); |
| 1551 | |
| 1552 | a.DoA(3); |
| 1553 | a.DoA(1); |
| 1554 | a.DoA(2); |
| 1555 | a.DoA(4); |
| 1556 | } |
| 1557 | |
| 1558 | // .After() accepts up to 5 arguments. |
| 1559 | TEST(AfterTest, AcceptsUpToFiveArguments) { |
| 1560 | MockA a; |
| 1561 | Expectation e1 = EXPECT_CALL(a, DoA(1)); |
| 1562 | Expectation e2 = EXPECT_CALL(a, DoA(2)); |
| 1563 | Expectation e3 = EXPECT_CALL(a, DoA(3)); |
| 1564 | ExpectationSet es1 = EXPECT_CALL(a, DoA(4)); |
| 1565 | ExpectationSet es2 = EXPECT_CALL(a, DoA(5)); |
| 1566 | EXPECT_CALL(a, DoA(6)) |
| 1567 | .After(e1, e2, e3, es1, es2); |
| 1568 | |
| 1569 | a.DoA(5); |
| 1570 | a.DoA(2); |
| 1571 | a.DoA(4); |
| 1572 | a.DoA(1); |
| 1573 | a.DoA(3); |
| 1574 | a.DoA(6); |
| 1575 | } |
| 1576 | |
| 1577 | // .After() allows input to contain duplicated Expectations. |
| 1578 | TEST(AfterTest, AcceptsDuplicatedInput) { |
| 1579 | MockA a; |
| 1580 | Expectation e1 = EXPECT_CALL(a, DoA(1)); |
| 1581 | Expectation e2 = EXPECT_CALL(a, DoA(2)); |
| 1582 | ExpectationSet es; |
| 1583 | es += e1; |
| 1584 | es += e2; |
| 1585 | EXPECT_CALL(a, ReturnResult(3)) |
| 1586 | .After(e1, e2, es, e1) |
| 1587 | .WillOnce(Return(Result())); |
| 1588 | |
| 1589 | a.DoA(1); |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 1590 | EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), ""); |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1591 | |
| 1592 | a.DoA(2); |
| 1593 | a.ReturnResult(3); |
| 1594 | } |
| 1595 | |
| 1596 | // An Expectation added to an ExpectationSet after it has been used in |
| 1597 | // an .After() has no effect. |
| 1598 | TEST(AfterTest, ChangesToExpectationSetHaveNoEffectAfterwards) { |
| 1599 | MockA a; |
| 1600 | ExpectationSet es1 = EXPECT_CALL(a, DoA(1)); |
| 1601 | Expectation e2 = EXPECT_CALL(a, DoA(2)); |
| 1602 | EXPECT_CALL(a, DoA(3)) |
| 1603 | .After(es1); |
| 1604 | es1 += e2; |
| 1605 | |
| 1606 | a.DoA(1); |
| 1607 | a.DoA(3); |
| 1608 | a.DoA(2); |
| 1609 | } |
| 1610 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1611 | // Tests that Google Mock correctly handles calls to mock functions |
| 1612 | // after a mock object owning one of their pre-requisites has died. |
| 1613 | |
| 1614 | // Tests that calls that satisfy the original spec are successful. |
| 1615 | TEST(DeletingMockEarlyTest, Success1) { |
| 1616 | MockB* const b1 = new MockB; |
| 1617 | MockA* const a = new MockA; |
| 1618 | MockB* const b2 = new MockB; |
| 1619 | |
| 1620 | { |
| 1621 | InSequence dummy; |
| 1622 | EXPECT_CALL(*b1, DoB(_)) |
| 1623 | .WillOnce(Return(1)); |
| 1624 | EXPECT_CALL(*a, Binary(_, _)) |
| 1625 | .Times(AnyNumber()) |
| 1626 | .WillRepeatedly(Return(true)); |
| 1627 | EXPECT_CALL(*b2, DoB(_)) |
| 1628 | .Times(AnyNumber()) |
| 1629 | .WillRepeatedly(Return(2)); |
| 1630 | } |
| 1631 | |
| 1632 | EXPECT_EQ(1, b1->DoB(1)); |
| 1633 | delete b1; |
| 1634 | // a's pre-requisite has died. |
| 1635 | EXPECT_TRUE(a->Binary(0, 1)); |
| 1636 | delete b2; |
| 1637 | // a's successor has died. |
| 1638 | EXPECT_TRUE(a->Binary(1, 2)); |
| 1639 | delete a; |
| 1640 | } |
| 1641 | |
| 1642 | // Tests that calls that satisfy the original spec are successful. |
| 1643 | TEST(DeletingMockEarlyTest, Success2) { |
| 1644 | MockB* const b1 = new MockB; |
| 1645 | MockA* const a = new MockA; |
| 1646 | MockB* const b2 = new MockB; |
| 1647 | |
| 1648 | { |
| 1649 | InSequence dummy; |
| 1650 | EXPECT_CALL(*b1, DoB(_)) |
| 1651 | .WillOnce(Return(1)); |
| 1652 | EXPECT_CALL(*a, Binary(_, _)) |
| 1653 | .Times(AnyNumber()); |
| 1654 | EXPECT_CALL(*b2, DoB(_)) |
| 1655 | .Times(AnyNumber()) |
| 1656 | .WillRepeatedly(Return(2)); |
| 1657 | } |
| 1658 | |
| 1659 | delete a; // a is trivially satisfied. |
| 1660 | EXPECT_EQ(1, b1->DoB(1)); |
| 1661 | EXPECT_EQ(2, b2->DoB(2)); |
| 1662 | delete b1; |
| 1663 | delete b2; |
| 1664 | } |
| 1665 | |
zhanyong.wan | 6f14769 | 2009-03-03 06:44:08 +0000 | [diff] [blame] | 1666 | // Tests that it's OK to delete a mock object itself in its action. |
| 1667 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1668 | // Suppresses warning on unreferenced formal parameter in MSVC with |
| 1669 | // -W4. |
| 1670 | #ifdef _MSC_VER |
| 1671 | #pragma warning(push) |
| 1672 | #pragma warning(disable:4100) |
| 1673 | #endif |
| 1674 | |
zhanyong.wan | 6f14769 | 2009-03-03 06:44:08 +0000 | [diff] [blame] | 1675 | ACTION_P(Delete, ptr) { delete ptr; } |
| 1676 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1677 | #ifdef _MSC_VER |
| 1678 | #pragma warning(pop) |
| 1679 | #endif |
| 1680 | |
zhanyong.wan | 6f14769 | 2009-03-03 06:44:08 +0000 | [diff] [blame] | 1681 | TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningVoid) { |
| 1682 | MockA* const a = new MockA; |
| 1683 | EXPECT_CALL(*a, DoA(_)).WillOnce(Delete(a)); |
| 1684 | a->DoA(42); // This will cause a to be deleted. |
| 1685 | } |
| 1686 | |
| 1687 | TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningValue) { |
| 1688 | MockA* const a = new MockA; |
| 1689 | EXPECT_CALL(*a, ReturnResult(_)) |
| 1690 | .WillOnce(DoAll(Delete(a), Return(Result()))); |
| 1691 | a->ReturnResult(42); // This will cause a to be deleted. |
| 1692 | } |
| 1693 | |
| 1694 | // Tests that calls that violate the original spec yield failures. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1695 | TEST(DeletingMockEarlyTest, Failure1) { |
| 1696 | MockB* const b1 = new MockB; |
| 1697 | MockA* const a = new MockA; |
| 1698 | MockB* const b2 = new MockB; |
| 1699 | |
| 1700 | { |
| 1701 | InSequence dummy; |
| 1702 | EXPECT_CALL(*b1, DoB(_)) |
| 1703 | .WillOnce(Return(1)); |
| 1704 | EXPECT_CALL(*a, Binary(_, _)) |
| 1705 | .Times(AnyNumber()); |
| 1706 | EXPECT_CALL(*b2, DoB(_)) |
| 1707 | .Times(AnyNumber()) |
| 1708 | .WillRepeatedly(Return(2)); |
| 1709 | } |
| 1710 | |
| 1711 | delete a; // a is trivially satisfied. |
| 1712 | EXPECT_NONFATAL_FAILURE({ |
| 1713 | b2->DoB(2); |
| 1714 | }, "Unexpected mock function call"); |
| 1715 | EXPECT_EQ(1, b1->DoB(1)); |
| 1716 | delete b1; |
| 1717 | delete b2; |
| 1718 | } |
| 1719 | |
zhanyong.wan | 6f14769 | 2009-03-03 06:44:08 +0000 | [diff] [blame] | 1720 | // Tests that calls that violate the original spec yield failures. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1721 | TEST(DeletingMockEarlyTest, Failure2) { |
| 1722 | MockB* const b1 = new MockB; |
| 1723 | MockA* const a = new MockA; |
| 1724 | MockB* const b2 = new MockB; |
| 1725 | |
| 1726 | { |
| 1727 | InSequence dummy; |
| 1728 | EXPECT_CALL(*b1, DoB(_)); |
| 1729 | EXPECT_CALL(*a, Binary(_, _)) |
| 1730 | .Times(AnyNumber()); |
| 1731 | EXPECT_CALL(*b2, DoB(_)) |
| 1732 | .Times(AnyNumber()); |
| 1733 | } |
| 1734 | |
| 1735 | EXPECT_NONFATAL_FAILURE(delete b1, |
| 1736 | "Actual: never called"); |
| 1737 | EXPECT_NONFATAL_FAILURE(a->Binary(0, 1), |
| 1738 | "Unexpected mock function call"); |
| 1739 | EXPECT_NONFATAL_FAILURE(b2->DoB(1), |
| 1740 | "Unexpected mock function call"); |
| 1741 | delete a; |
| 1742 | delete b2; |
| 1743 | } |
| 1744 | |
| 1745 | class EvenNumberCardinality : public CardinalityInterface { |
| 1746 | public: |
| 1747 | // Returns true iff call_count calls will satisfy this cardinality. |
| 1748 | virtual bool IsSatisfiedByCallCount(int call_count) const { |
| 1749 | return call_count % 2 == 0; |
| 1750 | } |
| 1751 | |
| 1752 | // Returns true iff call_count calls will saturate this cardinality. |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1753 | virtual bool IsSaturatedByCallCount(int /* call_count */) const { |
| 1754 | return false; |
| 1755 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1756 | |
| 1757 | // Describes self to an ostream. |
| 1758 | virtual void DescribeTo(::std::ostream* os) const { |
| 1759 | *os << "called even number of times"; |
| 1760 | } |
| 1761 | }; |
| 1762 | |
| 1763 | Cardinality EvenNumber() { |
| 1764 | return Cardinality(new EvenNumberCardinality); |
| 1765 | } |
| 1766 | |
| 1767 | TEST(ExpectationBaseTest, |
| 1768 | AllPrerequisitesAreSatisfiedWorksForNonMonotonicCardinality) { |
| 1769 | MockA* a = new MockA; |
| 1770 | Sequence s; |
| 1771 | |
| 1772 | EXPECT_CALL(*a, DoA(1)) |
| 1773 | .Times(EvenNumber()) |
| 1774 | .InSequence(s); |
| 1775 | EXPECT_CALL(*a, DoA(2)) |
| 1776 | .Times(AnyNumber()) |
| 1777 | .InSequence(s); |
| 1778 | EXPECT_CALL(*a, DoA(3)) |
| 1779 | .Times(AnyNumber()); |
| 1780 | |
| 1781 | a->DoA(3); |
| 1782 | a->DoA(1); |
| 1783 | EXPECT_NONFATAL_FAILURE(a->DoA(2), "Unexpected mock function call"); |
| 1784 | EXPECT_NONFATAL_FAILURE(delete a, "to be called even number of times"); |
| 1785 | } |
| 1786 | |
| 1787 | // The following tests verify the message generated when a mock |
| 1788 | // function is called. |
| 1789 | |
| 1790 | struct Printable { |
| 1791 | }; |
| 1792 | |
| 1793 | inline void operator<<(::std::ostream& os, const Printable&) { |
| 1794 | os << "Printable"; |
| 1795 | } |
| 1796 | |
| 1797 | struct Unprintable { |
| 1798 | Unprintable() : value(0) {} |
| 1799 | int value; |
| 1800 | }; |
| 1801 | |
| 1802 | class MockC { |
| 1803 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1804 | MockC() {} |
| 1805 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1806 | MOCK_METHOD6(VoidMethod, void(bool cond, int n, string s, void* p, |
| 1807 | const Printable& x, Unprintable y)); |
| 1808 | MOCK_METHOD0(NonVoidMethod, int()); // NOLINT |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1809 | |
| 1810 | private: |
| 1811 | GTEST_DISALLOW_COPY_AND_ASSIGN_(MockC); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1812 | }; |
| 1813 | |
vladlosev | 76c1c61 | 2010-05-05 19:47:46 +0000 | [diff] [blame] | 1814 | class VerboseFlagPreservingFixture : public testing::Test { |
| 1815 | protected: |
| 1816 | // The code needs to work when both ::string and ::std::string are defined |
| 1817 | // and the flag is implemented as a testing::internal::String. In this |
| 1818 | // case, without the call to c_str(), the compiler will complain that it |
| 1819 | // cannot figure out what overload of string constructor to use. |
| 1820 | // TODO(vladl@google.com): Use internal::string instead of String for |
| 1821 | // string flags in Google Test. |
| 1822 | VerboseFlagPreservingFixture() |
| 1823 | : saved_verbose_flag_(GMOCK_FLAG(verbose).c_str()) {} |
| 1824 | |
| 1825 | ~VerboseFlagPreservingFixture() { GMOCK_FLAG(verbose) = saved_verbose_flag_; } |
| 1826 | |
| 1827 | private: |
| 1828 | const string saved_verbose_flag_; |
| 1829 | |
| 1830 | GTEST_DISALLOW_COPY_AND_ASSIGN_(VerboseFlagPreservingFixture); |
| 1831 | }; |
| 1832 | |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 1833 | #if GTEST_HAS_STREAM_REDIRECTION |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1834 | |
| 1835 | // Tests that an uninteresting mock function call generates a warning |
| 1836 | // containing the stack trace. |
| 1837 | TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) { |
| 1838 | MockC c; |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1839 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1840 | c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1841 | const String output = GetCapturedStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1842 | EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output); |
| 1843 | EXPECT_PRED_FORMAT2(IsSubstring, "Stack trace:", output); |
| 1844 | #ifndef NDEBUG |
| 1845 | // We check the stack trace content in dbg-mode only, as opt-mode |
| 1846 | // may inline the call we are interested in seeing. |
| 1847 | |
| 1848 | // Verifies that a void mock function's name appears in the stack |
| 1849 | // trace. |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1850 | EXPECT_PRED_FORMAT2(IsSubstring, "VoidMethod(", output); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1851 | |
| 1852 | // Verifies that a non-void mock function's name appears in the |
| 1853 | // stack trace. |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1854 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1855 | c.NonVoidMethod(); |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1856 | const String output2 = GetCapturedStdout(); |
| 1857 | EXPECT_PRED_FORMAT2(IsSubstring, "NonVoidMethod(", output2); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1858 | #endif // NDEBUG |
| 1859 | } |
| 1860 | |
| 1861 | // Tests that an uninteresting mock function call causes the function |
| 1862 | // arguments and return value to be printed. |
| 1863 | TEST(FunctionCallMessageTest, UninterestingCallPrintsArgumentsAndReturnValue) { |
| 1864 | // A non-void mock function. |
| 1865 | MockB b; |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1866 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1867 | b.DoB(); |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1868 | const String output1 = GetCapturedStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1869 | EXPECT_PRED_FORMAT2( |
| 1870 | IsSubstring, |
| 1871 | "Uninteresting mock function call - returning default value.\n" |
| 1872 | " Function call: DoB()\n" |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1873 | " Returns: 0\n", output1.c_str()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1874 | // Makes sure the return value is printed. |
| 1875 | |
| 1876 | // A void mock function. |
| 1877 | MockC c; |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1878 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1879 | c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1880 | const String output2 = GetCapturedStdout(); |
| 1881 | EXPECT_THAT(output2.c_str(), |
| 1882 | ContainsRegex( |
| 1883 | "Uninteresting mock function call - returning directly\\.\n" |
| 1884 | " Function call: VoidMethod" |
| 1885 | "\\(false, 5, \"Hi\", NULL, @.+ " |
zhanyong.wan | c6333dc | 2010-08-09 18:20:45 +0000 | [diff] [blame] | 1886 | "Printable, 4-byte object <00-00 00-00>\\)")); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1887 | // A void function has no return value to print. |
| 1888 | } |
| 1889 | |
| 1890 | // Tests how the --gmock_verbose flag affects Google Mock's output. |
| 1891 | |
vladlosev | 76c1c61 | 2010-05-05 19:47:46 +0000 | [diff] [blame] | 1892 | class GMockVerboseFlagTest : public VerboseFlagPreservingFixture { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1893 | public: |
| 1894 | // Verifies that the given Google Mock output is correct. (When |
| 1895 | // should_print is true, the output should match the given regex and |
| 1896 | // contain the given function name in the stack trace. When it's |
| 1897 | // false, the output should be empty.) |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1898 | void VerifyOutput(const String& output, bool should_print, |
| 1899 | const string& expected_substring, |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1900 | const string& function_name) { |
| 1901 | if (should_print) { |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1902 | EXPECT_THAT(output.c_str(), HasSubstr(expected_substring)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1903 | #ifndef NDEBUG |
| 1904 | // We check the stack trace content in dbg-mode only, as opt-mode |
| 1905 | // may inline the call we are interested in seeing. |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1906 | EXPECT_THAT(output.c_str(), HasSubstr(function_name)); |
| 1907 | #else |
| 1908 | // Suppresses 'unused function parameter' warnings. |
| 1909 | static_cast<void>(function_name); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1910 | #endif // NDEBUG |
| 1911 | } else { |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1912 | EXPECT_STREQ("", output.c_str()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | // Tests how the flag affects expected calls. |
| 1917 | void TestExpectedCall(bool should_print) { |
| 1918 | MockA a; |
| 1919 | EXPECT_CALL(a, DoA(5)); |
| 1920 | EXPECT_CALL(a, Binary(_, 1)) |
| 1921 | .WillOnce(Return(true)); |
| 1922 | |
| 1923 | // A void-returning function. |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1924 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1925 | a.DoA(5); |
| 1926 | VerifyOutput( |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1927 | GetCapturedStdout(), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1928 | should_print, |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1929 | "Mock function call matches EXPECT_CALL(a, DoA(5))...\n" |
| 1930 | " Function call: DoA(5)\n" |
| 1931 | "Stack trace:\n", |
| 1932 | "DoA"); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1933 | |
| 1934 | // A non-void-returning function. |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1935 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1936 | a.Binary(2, 1); |
| 1937 | VerifyOutput( |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1938 | GetCapturedStdout(), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1939 | should_print, |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1940 | "Mock function call matches EXPECT_CALL(a, Binary(_, 1))...\n" |
| 1941 | " Function call: Binary(2, 1)\n" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1942 | " Returns: true\n" |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1943 | "Stack trace:\n", |
| 1944 | "Binary"); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | // Tests how the flag affects uninteresting calls. |
| 1948 | void TestUninterestingCall(bool should_print) { |
| 1949 | MockA a; |
| 1950 | |
| 1951 | // A void-returning function. |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1952 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1953 | a.DoA(5); |
| 1954 | VerifyOutput( |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1955 | GetCapturedStdout(), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1956 | should_print, |
| 1957 | "\nGMOCK WARNING:\n" |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1958 | "Uninteresting mock function call - returning directly.\n" |
| 1959 | " Function call: DoA(5)\n" |
| 1960 | "Stack trace:\n", |
| 1961 | "DoA"); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1962 | |
| 1963 | // A non-void-returning function. |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1964 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1965 | a.Binary(2, 1); |
| 1966 | VerifyOutput( |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1967 | GetCapturedStdout(), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1968 | should_print, |
| 1969 | "\nGMOCK WARNING:\n" |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1970 | "Uninteresting mock function call - returning default value.\n" |
| 1971 | " Function call: Binary(2, 1)\n" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1972 | " Returns: false\n" |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 1973 | "Stack trace:\n", |
| 1974 | "Binary"); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1975 | } |
| 1976 | }; |
| 1977 | |
| 1978 | // Tests that --gmock_verbose=info causes both expected and |
| 1979 | // uninteresting calls to be reported. |
| 1980 | TEST_F(GMockVerboseFlagTest, Info) { |
| 1981 | GMOCK_FLAG(verbose) = kInfoVerbosity; |
| 1982 | TestExpectedCall(true); |
| 1983 | TestUninterestingCall(true); |
| 1984 | } |
| 1985 | |
| 1986 | // Tests that --gmock_verbose=warning causes uninteresting calls to be |
| 1987 | // reported. |
| 1988 | TEST_F(GMockVerboseFlagTest, Warning) { |
| 1989 | GMOCK_FLAG(verbose) = kWarningVerbosity; |
| 1990 | TestExpectedCall(false); |
| 1991 | TestUninterestingCall(true); |
| 1992 | } |
| 1993 | |
| 1994 | // Tests that --gmock_verbose=warning causes neither expected nor |
| 1995 | // uninteresting calls to be reported. |
| 1996 | TEST_F(GMockVerboseFlagTest, Error) { |
| 1997 | GMOCK_FLAG(verbose) = kErrorVerbosity; |
| 1998 | TestExpectedCall(false); |
| 1999 | TestUninterestingCall(false); |
| 2000 | } |
| 2001 | |
| 2002 | // Tests that --gmock_verbose=SOME_INVALID_VALUE has the same effect |
| 2003 | // as --gmock_verbose=warning. |
| 2004 | TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) { |
| 2005 | GMOCK_FLAG(verbose) = "invalid"; // Treated as "warning". |
| 2006 | TestExpectedCall(false); |
| 2007 | TestUninterestingCall(true); |
| 2008 | } |
| 2009 | |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 2010 | #endif // GTEST_HAS_STREAM_REDIRECTION |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2011 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 2012 | // A helper class that generates a failure when printed. We use it to |
| 2013 | // ensure that Google Mock doesn't print a value (even to an internal |
| 2014 | // buffer) when it is not supposed to do so. |
| 2015 | class PrintMeNot {}; |
| 2016 | |
| 2017 | void PrintTo(PrintMeNot /* dummy */, ::std::ostream* /* os */) { |
| 2018 | ADD_FAILURE() << "Google Mock is printing a value that shouldn't be " |
| 2019 | << "printed even to an internal buffer."; |
| 2020 | } |
| 2021 | |
| 2022 | class LogTestHelper { |
| 2023 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2024 | LogTestHelper() {} |
| 2025 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 2026 | MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot)); |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2027 | |
| 2028 | private: |
| 2029 | GTEST_DISALLOW_COPY_AND_ASSIGN_(LogTestHelper); |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 2030 | }; |
| 2031 | |
vladlosev | 76c1c61 | 2010-05-05 19:47:46 +0000 | [diff] [blame] | 2032 | class GMockLogTest : public VerboseFlagPreservingFixture { |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 2033 | protected: |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 2034 | LogTestHelper helper_; |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 2035 | }; |
| 2036 | |
| 2037 | TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsWarning) { |
| 2038 | GMOCK_FLAG(verbose) = kWarningVerbosity; |
| 2039 | EXPECT_CALL(helper_, Foo(_)) |
| 2040 | .WillOnce(Return(PrintMeNot())); |
| 2041 | helper_.Foo(PrintMeNot()); // This is an expected call. |
| 2042 | } |
| 2043 | |
| 2044 | TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsError) { |
| 2045 | GMOCK_FLAG(verbose) = kErrorVerbosity; |
| 2046 | EXPECT_CALL(helper_, Foo(_)) |
| 2047 | .WillOnce(Return(PrintMeNot())); |
| 2048 | helper_.Foo(PrintMeNot()); // This is an expected call. |
| 2049 | } |
| 2050 | |
| 2051 | TEST_F(GMockLogTest, DoesNotPrintWarningInternallyIfVerbosityIsError) { |
| 2052 | GMOCK_FLAG(verbose) = kErrorVerbosity; |
| 2053 | ON_CALL(helper_, Foo(_)) |
| 2054 | .WillByDefault(Return(PrintMeNot())); |
| 2055 | helper_.Foo(PrintMeNot()); // This should generate a warning. |
| 2056 | } |
| 2057 | |
| 2058 | // Tests Mock::AllowLeak(). |
| 2059 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 2060 | TEST(AllowLeakTest, AllowsLeakingUnusedMockObject) { |
| 2061 | MockA* a = new MockA; |
| 2062 | Mock::AllowLeak(a); |
| 2063 | } |
| 2064 | |
| 2065 | TEST(AllowLeakTest, CanBeCalledBeforeOnCall) { |
| 2066 | MockA* a = new MockA; |
| 2067 | Mock::AllowLeak(a); |
| 2068 | ON_CALL(*a, DoA(_)).WillByDefault(Return()); |
| 2069 | a->DoA(0); |
| 2070 | } |
| 2071 | |
| 2072 | TEST(AllowLeakTest, CanBeCalledAfterOnCall) { |
| 2073 | MockA* a = new MockA; |
| 2074 | ON_CALL(*a, DoA(_)).WillByDefault(Return()); |
| 2075 | Mock::AllowLeak(a); |
| 2076 | } |
| 2077 | |
| 2078 | TEST(AllowLeakTest, CanBeCalledBeforeExpectCall) { |
| 2079 | MockA* a = new MockA; |
| 2080 | Mock::AllowLeak(a); |
| 2081 | EXPECT_CALL(*a, DoA(_)); |
| 2082 | a->DoA(0); |
| 2083 | } |
| 2084 | |
| 2085 | TEST(AllowLeakTest, CanBeCalledAfterExpectCall) { |
| 2086 | MockA* a = new MockA; |
| 2087 | EXPECT_CALL(*a, DoA(_)).Times(AnyNumber()); |
| 2088 | Mock::AllowLeak(a); |
| 2089 | } |
| 2090 | |
| 2091 | TEST(AllowLeakTest, WorksWhenBothOnCallAndExpectCallArePresent) { |
| 2092 | MockA* a = new MockA; |
| 2093 | ON_CALL(*a, DoA(_)).WillByDefault(Return()); |
| 2094 | EXPECT_CALL(*a, DoA(_)).Times(AnyNumber()); |
| 2095 | Mock::AllowLeak(a); |
| 2096 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2097 | |
| 2098 | // Tests that we can verify and clear a mock object's expectations |
| 2099 | // when none of its methods has expectations. |
| 2100 | TEST(VerifyAndClearExpectationsTest, NoMethodHasExpectations) { |
| 2101 | MockB b; |
| 2102 | ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b)); |
| 2103 | |
| 2104 | // There should be no expectations on the methods now, so we can |
| 2105 | // freely call them. |
| 2106 | EXPECT_EQ(0, b.DoB()); |
| 2107 | EXPECT_EQ(0, b.DoB(1)); |
| 2108 | } |
| 2109 | |
| 2110 | // Tests that we can verify and clear a mock object's expectations |
| 2111 | // when some, but not all, of its methods have expectations *and* the |
| 2112 | // verification succeeds. |
| 2113 | TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndSucceed) { |
| 2114 | MockB b; |
| 2115 | EXPECT_CALL(b, DoB()) |
| 2116 | .WillOnce(Return(1)); |
| 2117 | b.DoB(); |
| 2118 | ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b)); |
| 2119 | |
| 2120 | // There should be no expectations on the methods now, so we can |
| 2121 | // freely call them. |
| 2122 | EXPECT_EQ(0, b.DoB()); |
| 2123 | EXPECT_EQ(0, b.DoB(1)); |
| 2124 | } |
| 2125 | |
| 2126 | // Tests that we can verify and clear a mock object's expectations |
| 2127 | // when some, but not all, of its methods have expectations *and* the |
| 2128 | // verification fails. |
| 2129 | TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndFail) { |
| 2130 | MockB b; |
| 2131 | EXPECT_CALL(b, DoB()) |
| 2132 | .WillOnce(Return(1)); |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 2133 | bool result = true; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2134 | EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b), |
| 2135 | "Actual: never called"); |
| 2136 | ASSERT_FALSE(result); |
| 2137 | |
| 2138 | // There should be no expectations on the methods now, so we can |
| 2139 | // freely call them. |
| 2140 | EXPECT_EQ(0, b.DoB()); |
| 2141 | EXPECT_EQ(0, b.DoB(1)); |
| 2142 | } |
| 2143 | |
| 2144 | // Tests that we can verify and clear a mock object's expectations |
| 2145 | // when all of its methods have expectations. |
| 2146 | TEST(VerifyAndClearExpectationsTest, AllMethodsHaveExpectations) { |
| 2147 | MockB b; |
| 2148 | EXPECT_CALL(b, DoB()) |
| 2149 | .WillOnce(Return(1)); |
| 2150 | EXPECT_CALL(b, DoB(_)) |
| 2151 | .WillOnce(Return(2)); |
| 2152 | b.DoB(); |
| 2153 | b.DoB(1); |
| 2154 | ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b)); |
| 2155 | |
| 2156 | // There should be no expectations on the methods now, so we can |
| 2157 | // freely call them. |
| 2158 | EXPECT_EQ(0, b.DoB()); |
| 2159 | EXPECT_EQ(0, b.DoB(1)); |
| 2160 | } |
| 2161 | |
| 2162 | // Tests that we can verify and clear a mock object's expectations |
| 2163 | // when a method has more than one expectation. |
| 2164 | TEST(VerifyAndClearExpectationsTest, AMethodHasManyExpectations) { |
| 2165 | MockB b; |
| 2166 | EXPECT_CALL(b, DoB(0)) |
| 2167 | .WillOnce(Return(1)); |
| 2168 | EXPECT_CALL(b, DoB(_)) |
| 2169 | .WillOnce(Return(2)); |
| 2170 | b.DoB(1); |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 2171 | bool result = true; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2172 | EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b), |
| 2173 | "Actual: never called"); |
| 2174 | ASSERT_FALSE(result); |
| 2175 | |
| 2176 | // There should be no expectations on the methods now, so we can |
| 2177 | // freely call them. |
| 2178 | EXPECT_EQ(0, b.DoB()); |
| 2179 | EXPECT_EQ(0, b.DoB(1)); |
| 2180 | } |
| 2181 | |
| 2182 | // Tests that we can call VerifyAndClearExpectations() on the same |
| 2183 | // mock object multiple times. |
| 2184 | TEST(VerifyAndClearExpectationsTest, CanCallManyTimes) { |
| 2185 | MockB b; |
| 2186 | EXPECT_CALL(b, DoB()); |
| 2187 | b.DoB(); |
| 2188 | Mock::VerifyAndClearExpectations(&b); |
| 2189 | |
| 2190 | EXPECT_CALL(b, DoB(_)) |
| 2191 | .WillOnce(Return(1)); |
| 2192 | b.DoB(1); |
| 2193 | Mock::VerifyAndClearExpectations(&b); |
| 2194 | Mock::VerifyAndClearExpectations(&b); |
| 2195 | |
| 2196 | // There should be no expectations on the methods now, so we can |
| 2197 | // freely call them. |
| 2198 | EXPECT_EQ(0, b.DoB()); |
| 2199 | EXPECT_EQ(0, b.DoB(1)); |
| 2200 | } |
| 2201 | |
| 2202 | // Tests that we can clear a mock object's default actions when none |
| 2203 | // of its methods has default actions. |
| 2204 | TEST(VerifyAndClearTest, NoMethodHasDefaultActions) { |
| 2205 | MockB b; |
| 2206 | // If this crashes or generates a failure, the test will catch it. |
| 2207 | Mock::VerifyAndClear(&b); |
| 2208 | EXPECT_EQ(0, b.DoB()); |
| 2209 | } |
| 2210 | |
| 2211 | // Tests that we can clear a mock object's default actions when some, |
| 2212 | // but not all of its methods have default actions. |
| 2213 | TEST(VerifyAndClearTest, SomeMethodsHaveDefaultActions) { |
| 2214 | MockB b; |
| 2215 | ON_CALL(b, DoB()) |
| 2216 | .WillByDefault(Return(1)); |
| 2217 | |
| 2218 | Mock::VerifyAndClear(&b); |
| 2219 | |
| 2220 | // Verifies that the default action of int DoB() was removed. |
| 2221 | EXPECT_EQ(0, b.DoB()); |
| 2222 | } |
| 2223 | |
| 2224 | // Tests that we can clear a mock object's default actions when all of |
| 2225 | // its methods have default actions. |
| 2226 | TEST(VerifyAndClearTest, AllMethodsHaveDefaultActions) { |
| 2227 | MockB b; |
| 2228 | ON_CALL(b, DoB()) |
| 2229 | .WillByDefault(Return(1)); |
| 2230 | ON_CALL(b, DoB(_)) |
| 2231 | .WillByDefault(Return(2)); |
| 2232 | |
| 2233 | Mock::VerifyAndClear(&b); |
| 2234 | |
| 2235 | // Verifies that the default action of int DoB() was removed. |
| 2236 | EXPECT_EQ(0, b.DoB()); |
| 2237 | |
| 2238 | // Verifies that the default action of int DoB(int) was removed. |
| 2239 | EXPECT_EQ(0, b.DoB(0)); |
| 2240 | } |
| 2241 | |
| 2242 | // Tests that we can clear a mock object's default actions when a |
| 2243 | // method has more than one ON_CALL() set on it. |
| 2244 | TEST(VerifyAndClearTest, AMethodHasManyDefaultActions) { |
| 2245 | MockB b; |
| 2246 | ON_CALL(b, DoB(0)) |
| 2247 | .WillByDefault(Return(1)); |
| 2248 | ON_CALL(b, DoB(_)) |
| 2249 | .WillByDefault(Return(2)); |
| 2250 | |
| 2251 | Mock::VerifyAndClear(&b); |
| 2252 | |
| 2253 | // Verifies that the default actions (there are two) of int DoB(int) |
| 2254 | // were removed. |
| 2255 | EXPECT_EQ(0, b.DoB(0)); |
| 2256 | EXPECT_EQ(0, b.DoB(1)); |
| 2257 | } |
| 2258 | |
| 2259 | // Tests that we can call VerifyAndClear() on a mock object multiple |
| 2260 | // times. |
| 2261 | TEST(VerifyAndClearTest, CanCallManyTimes) { |
| 2262 | MockB b; |
| 2263 | ON_CALL(b, DoB()) |
| 2264 | .WillByDefault(Return(1)); |
| 2265 | Mock::VerifyAndClear(&b); |
| 2266 | Mock::VerifyAndClear(&b); |
| 2267 | |
| 2268 | ON_CALL(b, DoB(_)) |
| 2269 | .WillByDefault(Return(1)); |
| 2270 | Mock::VerifyAndClear(&b); |
| 2271 | |
| 2272 | EXPECT_EQ(0, b.DoB()); |
| 2273 | EXPECT_EQ(0, b.DoB(1)); |
| 2274 | } |
| 2275 | |
| 2276 | // Tests that VerifyAndClear() works when the verification succeeds. |
| 2277 | TEST(VerifyAndClearTest, Success) { |
| 2278 | MockB b; |
| 2279 | ON_CALL(b, DoB()) |
| 2280 | .WillByDefault(Return(1)); |
| 2281 | EXPECT_CALL(b, DoB(1)) |
| 2282 | .WillOnce(Return(2)); |
| 2283 | |
| 2284 | b.DoB(); |
| 2285 | b.DoB(1); |
| 2286 | ASSERT_TRUE(Mock::VerifyAndClear(&b)); |
| 2287 | |
| 2288 | // There should be no expectations on the methods now, so we can |
| 2289 | // freely call them. |
| 2290 | EXPECT_EQ(0, b.DoB()); |
| 2291 | EXPECT_EQ(0, b.DoB(1)); |
| 2292 | } |
| 2293 | |
| 2294 | // Tests that VerifyAndClear() works when the verification fails. |
| 2295 | TEST(VerifyAndClearTest, Failure) { |
| 2296 | MockB b; |
| 2297 | ON_CALL(b, DoB(_)) |
| 2298 | .WillByDefault(Return(1)); |
| 2299 | EXPECT_CALL(b, DoB()) |
| 2300 | .WillOnce(Return(2)); |
| 2301 | |
| 2302 | b.DoB(1); |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 2303 | bool result = true; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2304 | EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClear(&b), |
| 2305 | "Actual: never called"); |
| 2306 | ASSERT_FALSE(result); |
| 2307 | |
| 2308 | // There should be no expectations on the methods now, so we can |
| 2309 | // freely call them. |
| 2310 | EXPECT_EQ(0, b.DoB()); |
| 2311 | EXPECT_EQ(0, b.DoB(1)); |
| 2312 | } |
| 2313 | |
| 2314 | // Tests that VerifyAndClear() works when the default actions and |
| 2315 | // expectations are set on a const mock object. |
| 2316 | TEST(VerifyAndClearTest, Const) { |
| 2317 | MockB b; |
| 2318 | ON_CALL(Const(b), DoB()) |
| 2319 | .WillByDefault(Return(1)); |
| 2320 | |
| 2321 | EXPECT_CALL(Const(b), DoB()) |
| 2322 | .WillOnce(DoDefault()) |
| 2323 | .WillOnce(Return(2)); |
| 2324 | |
| 2325 | b.DoB(); |
| 2326 | b.DoB(); |
| 2327 | ASSERT_TRUE(Mock::VerifyAndClear(&b)); |
| 2328 | |
| 2329 | // There should be no expectations on the methods now, so we can |
| 2330 | // freely call them. |
| 2331 | EXPECT_EQ(0, b.DoB()); |
| 2332 | EXPECT_EQ(0, b.DoB(1)); |
| 2333 | } |
| 2334 | |
| 2335 | // Tests that we can set default actions and expectations on a mock |
| 2336 | // object after VerifyAndClear() has been called on it. |
| 2337 | TEST(VerifyAndClearTest, CanSetDefaultActionsAndExpectationsAfterwards) { |
| 2338 | MockB b; |
| 2339 | ON_CALL(b, DoB()) |
| 2340 | .WillByDefault(Return(1)); |
| 2341 | EXPECT_CALL(b, DoB(_)) |
| 2342 | .WillOnce(Return(2)); |
| 2343 | b.DoB(1); |
| 2344 | |
| 2345 | Mock::VerifyAndClear(&b); |
| 2346 | |
| 2347 | EXPECT_CALL(b, DoB()) |
| 2348 | .WillOnce(Return(3)); |
| 2349 | ON_CALL(b, DoB(_)) |
| 2350 | .WillByDefault(Return(4)); |
| 2351 | |
| 2352 | EXPECT_EQ(3, b.DoB()); |
| 2353 | EXPECT_EQ(4, b.DoB(1)); |
| 2354 | } |
| 2355 | |
| 2356 | // Tests that calling VerifyAndClear() on one mock object does not |
| 2357 | // affect other mock objects (either of the same type or not). |
| 2358 | TEST(VerifyAndClearTest, DoesNotAffectOtherMockObjects) { |
| 2359 | MockA a; |
| 2360 | MockB b1; |
| 2361 | MockB b2; |
| 2362 | |
| 2363 | ON_CALL(a, Binary(_, _)) |
| 2364 | .WillByDefault(Return(true)); |
| 2365 | EXPECT_CALL(a, Binary(_, _)) |
| 2366 | .WillOnce(DoDefault()) |
| 2367 | .WillOnce(Return(false)); |
| 2368 | |
| 2369 | ON_CALL(b1, DoB()) |
| 2370 | .WillByDefault(Return(1)); |
| 2371 | EXPECT_CALL(b1, DoB(_)) |
| 2372 | .WillOnce(Return(2)); |
| 2373 | |
| 2374 | ON_CALL(b2, DoB()) |
| 2375 | .WillByDefault(Return(3)); |
| 2376 | EXPECT_CALL(b2, DoB(_)); |
| 2377 | |
| 2378 | b2.DoB(0); |
| 2379 | Mock::VerifyAndClear(&b2); |
| 2380 | |
| 2381 | // Verifies that the default actions and expectations of a and b1 |
| 2382 | // are still in effect. |
| 2383 | EXPECT_TRUE(a.Binary(0, 0)); |
| 2384 | EXPECT_FALSE(a.Binary(0, 0)); |
| 2385 | |
| 2386 | EXPECT_EQ(1, b1.DoB()); |
| 2387 | EXPECT_EQ(2, b1.DoB(0)); |
| 2388 | } |
| 2389 | |
| 2390 | // Tests that a mock function's action can call a mock function |
| 2391 | // (either the same function or a different one) either as an explicit |
| 2392 | // action or as a default action without causing a dead lock. It |
| 2393 | // verifies that the action is not performed inside the critical |
| 2394 | // section. |
vladlosev | 54af9ba | 2010-05-04 16:05:11 +0000 | [diff] [blame] | 2395 | TEST(SynchronizationTest, CanCallMockMethodInAction) { |
| 2396 | MockA a; |
| 2397 | MockC c; |
| 2398 | ON_CALL(a, DoA(_)) |
| 2399 | .WillByDefault(IgnoreResult(InvokeWithoutArgs(&c, |
| 2400 | &MockC::NonVoidMethod))); |
| 2401 | EXPECT_CALL(a, DoA(1)); |
| 2402 | EXPECT_CALL(a, DoA(1)) |
| 2403 | .WillOnce(Invoke(&a, &MockA::DoA)) |
| 2404 | .RetiresOnSaturation(); |
| 2405 | EXPECT_CALL(c, NonVoidMethod()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2406 | |
vladlosev | 54af9ba | 2010-05-04 16:05:11 +0000 | [diff] [blame] | 2407 | a.DoA(1); |
| 2408 | // This will match the second EXPECT_CALL() and trigger another a.DoA(1), |
| 2409 | // which will in turn match the first EXPECT_CALL() and trigger a call to |
| 2410 | // c.NonVoidMethod() that was specified by the ON_CALL() since the first |
| 2411 | // EXPECT_CALL() did not specify an action. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | } // namespace |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 2415 | |
zhanyong.wan | 9571b28 | 2009-08-07 07:15:56 +0000 | [diff] [blame] | 2416 | // Allows the user to define his own main and then invoke gmock_main |
| 2417 | // from it. This might be necessary on some platforms which require |
| 2418 | // specific setup and teardown. |
| 2419 | #if GMOCK_RENAME_MAIN |
| 2420 | int gmock_main(int argc, char **argv) { |
| 2421 | #else |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 2422 | int main(int argc, char **argv) { |
zhanyong.wan | 9571b28 | 2009-08-07 07:15:56 +0000 | [diff] [blame] | 2423 | #endif // GMOCK_RENAME_MAIN |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 2424 | testing::InitGoogleMock(&argc, argv); |
| 2425 | |
| 2426 | // Ensures that the tests pass no matter what value of |
| 2427 | // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. |
| 2428 | testing::GMOCK_FLAG(catch_leaked_mocks) = true; |
| 2429 | testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity; |
| 2430 | |
| 2431 | return RUN_ALL_TESTS(); |
| 2432 | } |