OLD | NEW |
(Empty) | |
| 1 // Copyright 2009, 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: vladl@google.com (Vlad Losev) |
| 31 |
| 32 // Google Mock - a framework for writing C++ mock classes. |
| 33 // |
| 34 // This file tests that: |
| 35 // a. A header file defining a mock class can be included in multiple |
| 36 // translation units without causing a link error. |
| 37 // b. Actions and matchers can be instantiated with identical template |
| 38 // arguments in different translation units without causing link |
| 39 // errors. |
| 40 // The following constructs are currently tested: |
| 41 // Actions: |
| 42 // Return() |
| 43 // Return(value) |
| 44 // ReturnNull |
| 45 // ReturnRef |
| 46 // Assign |
| 47 // SetArgumentPointee |
| 48 // SetArrayArgument |
| 49 // SetErrnoAndReturn |
| 50 // Invoke(function) |
| 51 // Invoke(object, method) |
| 52 // InvokeWithoutArgs(function) |
| 53 // InvokeWithoutArgs(object, method) |
| 54 // InvokeArgument |
| 55 // WithArg |
| 56 // WithArgs |
| 57 // WithoutArgs |
| 58 // DoAll |
| 59 // DoDefault |
| 60 // IgnoreResult |
| 61 // Throw |
| 62 // ACTION()-generated |
| 63 // ACTION_P()-generated |
| 64 // ACTION_P2()-generated |
| 65 // Matchers: |
| 66 // _ |
| 67 // A |
| 68 // An |
| 69 // Eq |
| 70 // Gt, Lt, Ge, Le, Ne |
| 71 // NotNull |
| 72 // Ref |
| 73 // TypedEq |
| 74 // DoubleEq |
| 75 // FloatEq |
| 76 // NanSensitiveDoubleEq |
| 77 // NanSensitiveFloatEq |
| 78 // ContainsRegex |
| 79 // MatchesRegex |
| 80 // EndsWith |
| 81 // HasSubstr |
| 82 // StartsWith |
| 83 // StrCaseEq |
| 84 // StrCaseNe |
| 85 // StrEq |
| 86 // StrNe |
| 87 // ElementsAre |
| 88 // ElementsAreArray |
| 89 // ContainerEq |
| 90 // Field |
| 91 // Property |
| 92 // ResultOf(function) |
| 93 // Pointee |
| 94 // Truly(predicate) |
| 95 // AllOf |
| 96 // AnyOf |
| 97 // Not |
| 98 // MatcherCast<T> |
| 99 // |
| 100 // Please note: this test does not verify the functioning of these |
| 101 // constructs, only that the programs using them will link successfully. |
| 102 // |
| 103 // Implementation note: |
| 104 // This test requires identical definitions of Interface and Mock to be |
| 105 // included in different translation units. We achieve this by writing |
| 106 // them in this header and #including it in gmock_link_test.cc and |
| 107 // gmock_link2_test.cc. Because the symbols generated by the compiler for |
| 108 // those constructs must be identical in both translation units, |
| 109 // definitions of Interface and Mock tests MUST be kept in the SAME |
| 110 // NON-ANONYMOUS namespace in this file. The test fixture class LinkTest |
| 111 // is defined as LinkTest1 in gmock_link_test.cc and as LinkTest2 in |
| 112 // gmock_link2_test.cc to avoid producing linker errors. |
| 113 |
| 114 #ifndef GMOCK_TEST_GMOCK_LINK_TEST_H_ |
| 115 #define GMOCK_TEST_GMOCK_LINK_TEST_H_ |
| 116 |
| 117 #include <gmock/gmock.h> |
| 118 |
| 119 #ifndef _WIN32_WCE |
| 120 #include <errno.h> |
| 121 #endif |
| 122 |
| 123 #include <gtest/gtest.h> |
| 124 #include <iostream> |
| 125 #include <vector> |
| 126 |
| 127 using testing::_; |
| 128 using testing::A; |
| 129 using testing::AllOf; |
| 130 using testing::AnyOf; |
| 131 using testing::Assign; |
| 132 using testing::ContainerEq; |
| 133 using testing::DoAll; |
| 134 using testing::DoDefault; |
| 135 using testing::DoubleEq; |
| 136 using testing::ElementsAre; |
| 137 using testing::ElementsAreArray; |
| 138 using testing::EndsWith; |
| 139 using testing::Eq; |
| 140 using testing::Field; |
| 141 using testing::FloatEq; |
| 142 using testing::Ge; |
| 143 using testing::Gt; |
| 144 using testing::HasSubstr; |
| 145 using testing::IgnoreResult; |
| 146 using testing::Invoke; |
| 147 using testing::InvokeArgument; |
| 148 using testing::InvokeWithoutArgs; |
| 149 using testing::Le; |
| 150 using testing::Lt; |
| 151 using testing::Matcher; |
| 152 using testing::MatcherCast; |
| 153 using testing::NanSensitiveDoubleEq; |
| 154 using testing::NanSensitiveFloatEq; |
| 155 using testing::Ne; |
| 156 using testing::Not; |
| 157 using testing::NotNull; |
| 158 using testing::Pointee; |
| 159 using testing::Property; |
| 160 using testing::Ref; |
| 161 using testing::ResultOf; |
| 162 using testing::Return; |
| 163 using testing::ReturnNull; |
| 164 using testing::ReturnRef; |
| 165 using testing::SetArgumentPointee; |
| 166 using testing::SetArrayArgument; |
| 167 using testing::StartsWith; |
| 168 using testing::StrCaseEq; |
| 169 using testing::StrCaseNe; |
| 170 using testing::StrEq; |
| 171 using testing::StrNe; |
| 172 using testing::Truly; |
| 173 using testing::TypedEq; |
| 174 using testing::WithArg; |
| 175 using testing::WithArgs; |
| 176 using testing::WithoutArgs; |
| 177 |
| 178 #ifndef _WIN32_WCE |
| 179 using testing::SetErrnoAndReturn; |
| 180 #endif // _WIN32_WCE |
| 181 |
| 182 #if GTEST_HAS_EXCEPTIONS |
| 183 using testing::Throw; |
| 184 #endif // GTEST_HAS_EXCEPTIONS |
| 185 |
| 186 #if GMOCK_HAS_REGEX |
| 187 using testing::ContainsRegex; |
| 188 using testing::MatchesRegex; |
| 189 #endif // GMOCK_HAS_REGEX |
| 190 |
| 191 class Interface { |
| 192 public: |
| 193 virtual ~Interface() {} |
| 194 virtual void VoidFromString(char* str) = 0; |
| 195 virtual char* StringFromString(char* str) = 0; |
| 196 virtual int IntFromString(char* str) = 0; |
| 197 virtual int& IntRefFromString(char* str) = 0; |
| 198 virtual void VoidFromFunc(void(*)(char*)) = 0; |
| 199 virtual void VoidFromIntRef(int& n) = 0; |
| 200 virtual void VoidFromFloat(float n) = 0; |
| 201 virtual void VoidFromDouble(double n) = 0; |
| 202 virtual void VoidFromVector(const std::vector<int>& v) = 0; |
| 203 }; |
| 204 |
| 205 class Mock: public Interface { |
| 206 public: |
| 207 MOCK_METHOD1(VoidFromString, void(char* str)); |
| 208 MOCK_METHOD1(StringFromString, char*(char* str)); |
| 209 MOCK_METHOD1(IntFromString, int(char* str)); |
| 210 MOCK_METHOD1(IntRefFromString, int&(char* str)); |
| 211 MOCK_METHOD1(VoidFromFunc, void(void(*func)(char* str))); |
| 212 MOCK_METHOD1(VoidFromIntRef, void(int& n)); |
| 213 MOCK_METHOD1(VoidFromFloat, void(float n)); |
| 214 MOCK_METHOD1(VoidFromDouble, void(double n)); |
| 215 MOCK_METHOD1(VoidFromVector, void(const std::vector<int>& v)); |
| 216 }; |
| 217 |
| 218 class InvokeHelper { |
| 219 public: |
| 220 static void StaticVoidFromVoid() {} |
| 221 void VoidFromVoid() {} |
| 222 static void StaticVoidFromString(char*) {} |
| 223 void VoidFromString(char*) {} |
| 224 static int StaticIntFromString(char*) { return 1; } |
| 225 static bool StaticBoolFromString(const char*) { return true; } |
| 226 }; |
| 227 |
| 228 class FieldHelper { |
| 229 public: |
| 230 FieldHelper(int field) : field_(field) {} |
| 231 int field() const { return field_; } |
| 232 int field_; // NOLINT -- need external access to field_ to test |
| 233 // the Field matcher. |
| 234 }; |
| 235 |
| 236 // Tests the linkage of the ReturnVoid action. |
| 237 TEST(LinkTest, TestReturnVoid) { |
| 238 Mock mock; |
| 239 |
| 240 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return()); |
| 241 mock.VoidFromString(NULL); |
| 242 } |
| 243 |
| 244 // Tests the linkage of the Return action. |
| 245 TEST(LinkTest, TestReturn) { |
| 246 Mock mock; |
| 247 char ch = 'x'; |
| 248 |
| 249 EXPECT_CALL(mock, StringFromString(_)).WillOnce(Return(&ch)); |
| 250 mock.StringFromString(NULL); |
| 251 } |
| 252 |
| 253 // Tests the linkage of the ReturnNull action. |
| 254 TEST(LinkTest, TestReturnNull) { |
| 255 Mock mock; |
| 256 |
| 257 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return()); |
| 258 mock.VoidFromString(NULL); |
| 259 } |
| 260 |
| 261 // Tests the linkage of the ReturnRef action. |
| 262 TEST(LinkTest, TestReturnRef) { |
| 263 Mock mock; |
| 264 int n = 42; |
| 265 |
| 266 EXPECT_CALL(mock, IntRefFromString(_)).WillOnce(ReturnRef(n)); |
| 267 mock.IntRefFromString(NULL); |
| 268 } |
| 269 |
| 270 // Tests the linkage of the Assign action. |
| 271 TEST(LinkTest, TestAssign) { |
| 272 Mock mock; |
| 273 char ch = 'x'; |
| 274 |
| 275 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Assign(&ch, 'y')); |
| 276 mock.VoidFromString(NULL); |
| 277 } |
| 278 |
| 279 // Tests the linkage of the SetArgumentPointee action. |
| 280 TEST(LinkTest, TestSetArgumentPointee) { |
| 281 Mock mock; |
| 282 char ch = 'x'; |
| 283 |
| 284 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArgumentPointee<0>('y')); |
| 285 mock.VoidFromString(&ch); |
| 286 } |
| 287 |
| 288 // Tests the linkage of the SetArrayArgument action. |
| 289 TEST(LinkTest, TestSetArrayArgument) { |
| 290 Mock mock; |
| 291 char ch = 'x'; |
| 292 char ch2 = 'y'; |
| 293 |
| 294 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArrayArgument<0>(&ch2, |
| 295 &ch2 + 1)); |
| 296 mock.VoidFromString(&ch); |
| 297 } |
| 298 |
| 299 #ifndef _WIN32_WCE |
| 300 |
| 301 // Tests the linkage of the SetErrnoAndReturn action. |
| 302 TEST(LinkTest, TestSetErrnoAndReturn) { |
| 303 Mock mock; |
| 304 |
| 305 int saved_errno = errno; |
| 306 EXPECT_CALL(mock, IntFromString(_)).WillOnce(SetErrnoAndReturn(1, -1)); |
| 307 mock.IntFromString(NULL); |
| 308 errno = saved_errno; |
| 309 } |
| 310 |
| 311 #endif // _WIN32_WCE |
| 312 |
| 313 // Tests the linkage of the Invoke(function) and Invoke(object, method) actions. |
| 314 TEST(LinkTest, TestInvoke) { |
| 315 Mock mock; |
| 316 InvokeHelper test_invoke_helper; |
| 317 |
| 318 EXPECT_CALL(mock, VoidFromString(_)) |
| 319 .WillOnce(Invoke(&InvokeHelper::StaticVoidFromString)) |
| 320 .WillOnce(Invoke(&test_invoke_helper, &InvokeHelper::VoidFromString)); |
| 321 mock.VoidFromString(NULL); |
| 322 mock.VoidFromString(NULL); |
| 323 } |
| 324 |
| 325 // Tests the linkage of the InvokeWithoutArgs action. |
| 326 TEST(LinkTest, TestInvokeWithoutArgs) { |
| 327 Mock mock; |
| 328 InvokeHelper test_invoke_helper; |
| 329 |
| 330 EXPECT_CALL(mock, VoidFromString(_)) |
| 331 .WillOnce(InvokeWithoutArgs(&InvokeHelper::StaticVoidFromVoid)) |
| 332 .WillOnce(InvokeWithoutArgs(&test_invoke_helper, |
| 333 &InvokeHelper::VoidFromVoid)); |
| 334 mock.VoidFromString(NULL); |
| 335 mock.VoidFromString(NULL); |
| 336 } |
| 337 |
| 338 // Tests the linkage of the InvokeArgument action. |
| 339 TEST(LinkTest, TestInvokeArgument) { |
| 340 Mock mock; |
| 341 char ch = 'x'; |
| 342 |
| 343 EXPECT_CALL(mock, VoidFromFunc(_)).WillOnce(InvokeArgument<0>(&ch)); |
| 344 mock.VoidFromFunc(InvokeHelper::StaticVoidFromString); |
| 345 } |
| 346 |
| 347 // Tests the linkage of the WithArg action. |
| 348 TEST(LinkTest, TestWithArg) { |
| 349 Mock mock; |
| 350 |
| 351 EXPECT_CALL(mock, VoidFromString(_)) |
| 352 .WillOnce(WithArg<0>(Invoke(&InvokeHelper::StaticVoidFromString))); |
| 353 mock.VoidFromString(NULL); |
| 354 } |
| 355 |
| 356 // Tests the linkage of the WithArgs action. |
| 357 TEST(LinkTest, TestWithArgs) { |
| 358 Mock mock; |
| 359 |
| 360 EXPECT_CALL(mock, VoidFromString(_)) |
| 361 .WillOnce(WithArgs<0>(Invoke(&InvokeHelper::StaticVoidFromString))); |
| 362 mock.VoidFromString(NULL); |
| 363 } |
| 364 |
| 365 // Tests the linkage of the WithoutArgs action. |
| 366 TEST(LinkTest, TestWithoutArgs) { |
| 367 Mock mock; |
| 368 |
| 369 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(WithoutArgs(Return())); |
| 370 mock.VoidFromString(NULL); |
| 371 } |
| 372 |
| 373 // Tests the linkage of the DoAll action. |
| 374 TEST(LinkTest, TestDoAll) { |
| 375 Mock mock; |
| 376 char ch = 'x'; |
| 377 |
| 378 EXPECT_CALL(mock, VoidFromString(_)) |
| 379 .WillOnce(DoAll(SetArgumentPointee<0>('y'), Return())); |
| 380 mock.VoidFromString(&ch); |
| 381 } |
| 382 |
| 383 // Tests the linkage of the DoDefault action. |
| 384 TEST(LinkTest, TestDoDefault) { |
| 385 Mock mock; |
| 386 char ch = 'x'; |
| 387 |
| 388 ON_CALL(mock, VoidFromString(_)).WillByDefault(Return()); |
| 389 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(DoDefault()); |
| 390 mock.VoidFromString(&ch); |
| 391 } |
| 392 |
| 393 // Tests the linkage of the IgnoreResult action. |
| 394 TEST(LinkTest, TestIgnoreResult) { |
| 395 Mock mock; |
| 396 |
| 397 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(IgnoreResult(Return(42))); |
| 398 mock.VoidFromString(NULL); |
| 399 } |
| 400 |
| 401 #if GTEST_HAS_EXCEPTIONS |
| 402 // Tests the linkage of the Throw action. |
| 403 TEST(LinkTest, TestThrow) { |
| 404 Mock mock; |
| 405 |
| 406 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Throw(42)); |
| 407 EXPECT_THROW(mock.VoidFromString(NULL), int); |
| 408 } |
| 409 #endif // GTEST_HAS_EXCEPTIONS |
| 410 |
| 411 // Tests the linkage of actions created using ACTION macro. |
| 412 namespace { |
| 413 ACTION(Return1) { return 1; } |
| 414 } |
| 415 |
| 416 TEST(LinkTest, TestActionMacro) { |
| 417 Mock mock; |
| 418 |
| 419 EXPECT_CALL(mock, IntFromString(_)).WillOnce(Return1()); |
| 420 mock.IntFromString(NULL); |
| 421 } |
| 422 |
| 423 // Tests the linkage of actions created using ACTION_P macro. |
| 424 namespace { |
| 425 ACTION_P(ReturnArgument, ret_value) { return ret_value; } |
| 426 } |
| 427 |
| 428 TEST(LinkTest, TestActionPMacro) { |
| 429 Mock mock; |
| 430 |
| 431 EXPECT_CALL(mock, IntFromString(_)).WillOnce(ReturnArgument(42)); |
| 432 mock.IntFromString(NULL); |
| 433 } |
| 434 |
| 435 // Tests the linkage of actions created using ACTION_P2 macro. |
| 436 namespace { |
| 437 ACTION_P2(ReturnEqualsEitherOf, first, second) { |
| 438 return arg0 == first || arg0 == second; |
| 439 } |
| 440 } |
| 441 |
| 442 TEST(LinkTest, TestActionP2Macro) { |
| 443 Mock mock; |
| 444 char ch = 'x'; |
| 445 |
| 446 EXPECT_CALL(mock, IntFromString(_)) |
| 447 .WillOnce(ReturnEqualsEitherOf("one", "two")); |
| 448 mock.IntFromString(&ch); |
| 449 } |
| 450 |
| 451 // Tests the linkage of the "_" matcher. |
| 452 TEST(LinkTest, TestMatcherAnything) { |
| 453 Mock mock; |
| 454 |
| 455 ON_CALL(mock, VoidFromString(_)).WillByDefault(Return()); |
| 456 } |
| 457 |
| 458 // Tests the linkage of the A matcher. |
| 459 TEST(LinkTest, TestMatcherA) { |
| 460 Mock mock; |
| 461 |
| 462 ON_CALL(mock, VoidFromString(A<char*>())).WillByDefault(Return()); |
| 463 } |
| 464 |
| 465 // Tests the linkage of the Eq and the "bare value" matcher. |
| 466 TEST(LinkTest, TestMatchersEq) { |
| 467 Mock mock; |
| 468 const char* p = "x"; |
| 469 |
| 470 ON_CALL(mock, VoidFromString(Eq(p))).WillByDefault(Return()); |
| 471 ON_CALL(mock, VoidFromString(const_cast<char*>("y"))) |
| 472 .WillByDefault(Return()); |
| 473 } |
| 474 |
| 475 // Tests the linkage of the Lt, Gt, Le, Ge, and Ne matchers. |
| 476 TEST(LinkTest, TestMatchersRelations) { |
| 477 Mock mock; |
| 478 |
| 479 ON_CALL(mock, VoidFromFloat(Lt(1.0f))).WillByDefault(Return()); |
| 480 ON_CALL(mock, VoidFromFloat(Gt(1.0f))).WillByDefault(Return()); |
| 481 ON_CALL(mock, VoidFromFloat(Le(1.0f))).WillByDefault(Return()); |
| 482 ON_CALL(mock, VoidFromFloat(Ge(1.0f))).WillByDefault(Return()); |
| 483 ON_CALL(mock, VoidFromFloat(Ne(1.0f))).WillByDefault(Return()); |
| 484 } |
| 485 |
| 486 // Tests the linkage of the NotNull matcher. |
| 487 TEST(LinkTest, TestMatcherNotNull) { |
| 488 Mock mock; |
| 489 |
| 490 ON_CALL(mock, VoidFromString(NotNull())).WillByDefault(Return()); |
| 491 } |
| 492 |
| 493 // Tests the linkage of the Ref matcher. |
| 494 TEST(LinkTest, TestMatcherRef) { |
| 495 Mock mock; |
| 496 int a = 0; |
| 497 |
| 498 ON_CALL(mock, VoidFromIntRef(Ref(a))).WillByDefault(Return()); |
| 499 } |
| 500 |
| 501 // Tests the linkage of the TypedEq matcher. |
| 502 TEST(LinkTest, TestMatcherTypedEq) { |
| 503 Mock mock; |
| 504 long a = 0; |
| 505 |
| 506 ON_CALL(mock, VoidFromIntRef(TypedEq<int&>(a))).WillByDefault(Return()); |
| 507 } |
| 508 |
| 509 // Tests the linkage of the FloatEq, DoubleEq, NanSensitiveFloatEq and |
| 510 // NanSensitiveDoubleEq matchers. |
| 511 TEST(LinkTest, TestMatchersFloatingPoint) { |
| 512 Mock mock; |
| 513 float a = 0; |
| 514 |
| 515 ON_CALL(mock, VoidFromFloat(FloatEq(a))).WillByDefault(Return()); |
| 516 ON_CALL(mock, VoidFromDouble(DoubleEq(a))).WillByDefault(Return()); |
| 517 ON_CALL(mock, VoidFromFloat(NanSensitiveFloatEq(a))).WillByDefault(Return()); |
| 518 ON_CALL(mock, VoidFromDouble(NanSensitiveDoubleEq(a))) |
| 519 .WillByDefault(Return()); |
| 520 } |
| 521 |
| 522 #if GMOCK_HAS_REGEX |
| 523 // Tests the linkage of the ContainsRegex matcher. |
| 524 TEST(LinkTest, TestMatcherContainsRegex) { |
| 525 Mock mock; |
| 526 |
| 527 ON_CALL(mock, VoidFromString(ContainsRegex(".*"))).WillByDefault(Return()); |
| 528 } |
| 529 |
| 530 // Tests the linkage of the MatchesRegex matcher. |
| 531 TEST(LinkTest, TestMatcherMatchesRegex) { |
| 532 Mock mock; |
| 533 |
| 534 ON_CALL(mock, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return()); |
| 535 } |
| 536 #endif // GMOCK_HAS_REGEX |
| 537 |
| 538 // Tests the linkage of the StartsWith, EndsWith, and HasSubstr matchers. |
| 539 TEST(LinkTest, TestMatchersSubstrings) { |
| 540 Mock mock; |
| 541 |
| 542 ON_CALL(mock, VoidFromString(StartsWith("a"))).WillByDefault(Return()); |
| 543 ON_CALL(mock, VoidFromString(EndsWith("c"))).WillByDefault(Return()); |
| 544 ON_CALL(mock, VoidFromString(HasSubstr("b"))).WillByDefault(Return()); |
| 545 } |
| 546 |
| 547 // Tests the linkage of the StrEq, StrNe, StrCaseEq, and StrCaseNe matchers. |
| 548 TEST(LinkTest, TestMatchersStringEquality) { |
| 549 Mock mock; |
| 550 ON_CALL(mock, VoidFromString(StrEq("a"))).WillByDefault(Return()); |
| 551 ON_CALL(mock, VoidFromString(StrNe("a"))).WillByDefault(Return()); |
| 552 ON_CALL(mock, VoidFromString(StrCaseEq("a"))).WillByDefault(Return()); |
| 553 ON_CALL(mock, VoidFromString(StrCaseNe("a"))).WillByDefault(Return()); |
| 554 } |
| 555 |
| 556 // Tests the linkage of the ElementsAre matcher. |
| 557 TEST(LinkTest, TestMatcherElementsAre) { |
| 558 Mock mock; |
| 559 |
| 560 ON_CALL(mock, VoidFromVector(ElementsAre('a', _))).WillByDefault(Return()); |
| 561 } |
| 562 |
| 563 // Tests the linkage of the ElementsAreArray matcher. |
| 564 TEST(LinkTest, TestMatcherElementsAreArray) { |
| 565 Mock mock; |
| 566 char arr[] = { 'a', 'b' }; |
| 567 |
| 568 ON_CALL(mock, VoidFromVector(ElementsAreArray(arr))).WillByDefault(Return()); |
| 569 } |
| 570 |
| 571 // Tests the linkage of the ContainerEq matcher. |
| 572 TEST(LinkTest, TestMatcherContainerEq) { |
| 573 Mock mock; |
| 574 std::vector<int> v; |
| 575 |
| 576 ON_CALL(mock, VoidFromVector(ContainerEq(v))).WillByDefault(Return()); |
| 577 } |
| 578 |
| 579 // Tests the linkage of the Field matcher. |
| 580 TEST(LinkTest, TestMatcherField) { |
| 581 FieldHelper helper(0); |
| 582 |
| 583 Matcher<const FieldHelper&> m = Field(&FieldHelper::field_, Eq(0)); |
| 584 EXPECT_TRUE(m.Matches(helper)); |
| 585 |
| 586 Matcher<const FieldHelper*> m2 = Field(&FieldHelper::field_, Eq(0)); |
| 587 EXPECT_TRUE(m2.Matches(&helper)); |
| 588 } |
| 589 |
| 590 // Tests the linkage of the Property matcher. |
| 591 TEST(LinkTest, TestMatcherProperty) { |
| 592 FieldHelper helper(0); |
| 593 |
| 594 Matcher<const FieldHelper&> m = Property(&FieldHelper::field, Eq(0)); |
| 595 EXPECT_TRUE(m.Matches(helper)); |
| 596 |
| 597 Matcher<const FieldHelper*> m2 = Property(&FieldHelper::field, Eq(0)); |
| 598 EXPECT_TRUE(m2.Matches(&helper)); |
| 599 } |
| 600 |
| 601 // Tests the linkage of the ResultOf matcher. |
| 602 TEST(LinkTest, TestMatcherResultOf) { |
| 603 Matcher<char*> m = ResultOf(&InvokeHelper::StaticIntFromString, Eq(1)); |
| 604 EXPECT_TRUE(m.Matches(NULL)); |
| 605 } |
| 606 |
| 607 // Tests the linkage of the ResultOf matcher. |
| 608 TEST(LinkTest, TestMatcherPointee) { |
| 609 int n = 1; |
| 610 |
| 611 Matcher<int*> m = Pointee(Eq(1)); |
| 612 EXPECT_TRUE(m.Matches(&n)); |
| 613 } |
| 614 |
| 615 // Tests the linkage of the Truly matcher. |
| 616 TEST(LinkTest, TestMatcherTruly) { |
| 617 Matcher<const char*> m = Truly(&InvokeHelper::StaticBoolFromString); |
| 618 EXPECT_TRUE(m.Matches(NULL)); |
| 619 } |
| 620 |
| 621 // Tests the linkage of the AllOf matcher. |
| 622 TEST(LinkTest, TestMatcherAllOf) { |
| 623 Matcher<int> m = AllOf(_, Eq(1)); |
| 624 EXPECT_TRUE(m.Matches(1)); |
| 625 } |
| 626 |
| 627 // Tests the linkage of the AnyOf matcher. |
| 628 TEST(LinkTest, TestMatcherAnyOf) { |
| 629 Matcher<int> m = AnyOf(_, Eq(1)); |
| 630 EXPECT_TRUE(m.Matches(1)); |
| 631 } |
| 632 |
| 633 // Tests the linkage of the Not matcher. |
| 634 TEST(LinkTest, TestMatcherNot) { |
| 635 Matcher<int> m = Not(_); |
| 636 EXPECT_FALSE(m.Matches(1)); |
| 637 } |
| 638 |
| 639 // Tests the linkage of the MatcherCast<T>() function. |
| 640 TEST(LinkTest, TestMatcherCast) { |
| 641 Matcher<const char*> m = MatcherCast<const char*>(_); |
| 642 EXPECT_TRUE(m.Matches(NULL)); |
| 643 } |
| 644 |
| 645 #endif // GMOCK_TEST_GMOCK_LINK_TEST_H_ |
OLD | NEW |