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