00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 #ifndef GMOCK_TEST_GMOCK_LINK_TEST_H_
00115 #define GMOCK_TEST_GMOCK_LINK_TEST_H_
00116
00117 #include "gmock/gmock.h"
00118
00119 #if !GTEST_OS_WINDOWS_MOBILE
00120 # include <errno.h>
00121 #endif
00122
00123 #include "gmock/internal/gmock-port.h"
00124 #include "gtest/gtest.h"
00125 #include <iostream>
00126 #include <vector>
00127
00128 using testing::_;
00129 using testing::A;
00130 using testing::AllOf;
00131 using testing::AnyOf;
00132 using testing::Assign;
00133 using testing::ContainerEq;
00134 using testing::DoAll;
00135 using testing::DoDefault;
00136 using testing::DoubleEq;
00137 using testing::ElementsAre;
00138 using testing::ElementsAreArray;
00139 using testing::EndsWith;
00140 using testing::Eq;
00141 using testing::Field;
00142 using testing::FloatEq;
00143 using testing::Ge;
00144 using testing::Gt;
00145 using testing::HasSubstr;
00146 using testing::IgnoreResult;
00147 using testing::Invoke;
00148 using testing::InvokeArgument;
00149 using testing::InvokeWithoutArgs;
00150 using testing::IsNull;
00151 using testing::Le;
00152 using testing::Lt;
00153 using testing::Matcher;
00154 using testing::MatcherCast;
00155 using testing::NanSensitiveDoubleEq;
00156 using testing::NanSensitiveFloatEq;
00157 using testing::Ne;
00158 using testing::Not;
00159 using testing::NotNull;
00160 using testing::Pointee;
00161 using testing::Property;
00162 using testing::Ref;
00163 using testing::ResultOf;
00164 using testing::Return;
00165 using testing::ReturnNull;
00166 using testing::ReturnRef;
00167 using testing::SetArgPointee;
00168 using testing::SetArrayArgument;
00169 using testing::StartsWith;
00170 using testing::StrCaseEq;
00171 using testing::StrCaseNe;
00172 using testing::StrEq;
00173 using testing::StrNe;
00174 using testing::Truly;
00175 using testing::TypedEq;
00176 using testing::WithArg;
00177 using testing::WithArgs;
00178 using testing::WithoutArgs;
00179
00180 #if !GTEST_OS_WINDOWS_MOBILE
00181 using testing::SetErrnoAndReturn;
00182 #endif
00183
00184 #if GTEST_HAS_EXCEPTIONS
00185 using testing::Throw;
00186 #endif
00187
00188 using testing::ContainsRegex;
00189 using testing::MatchesRegex;
00190
00191 class Interface {
00192 public:
00193 virtual ~Interface() {}
00194 virtual void VoidFromString(char* str) = 0;
00195 virtual char* StringFromString(char* str) = 0;
00196 virtual int IntFromString(char* str) = 0;
00197 virtual int& IntRefFromString(char* str) = 0;
00198 virtual void VoidFromFunc(void(*func)(char* str)) = 0;
00199 virtual void VoidFromIntRef(int& n) = 0;
00200 virtual void VoidFromFloat(float n) = 0;
00201 virtual void VoidFromDouble(double n) = 0;
00202 virtual void VoidFromVector(const std::vector<int>& v) = 0;
00203 };
00204
00205 class Mock: public Interface {
00206 public:
00207 Mock() {}
00208
00209 MOCK_METHOD1(VoidFromString, void(char* str));
00210 MOCK_METHOD1(StringFromString, char*(char* str));
00211 MOCK_METHOD1(IntFromString, int(char* str));
00212 MOCK_METHOD1(IntRefFromString, int&(char* str));
00213 MOCK_METHOD1(VoidFromFunc, void(void(*func)(char* str)));
00214 MOCK_METHOD1(VoidFromIntRef, void(int& n));
00215 MOCK_METHOD1(VoidFromFloat, void(float n));
00216 MOCK_METHOD1(VoidFromDouble, void(double n));
00217 MOCK_METHOD1(VoidFromVector, void(const std::vector<int>& v));
00218
00219 private:
00220 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock);
00221 };
00222
00223 class InvokeHelper {
00224 public:
00225 static void StaticVoidFromVoid() {}
00226 void VoidFromVoid() {}
00227 static void StaticVoidFromString(char* ) {}
00228 void VoidFromString(char* ) {}
00229 static int StaticIntFromString(char* ) { return 1; }
00230 static bool StaticBoolFromString(const char* ) { return true; }
00231 };
00232
00233 class FieldHelper {
00234 public:
00235 explicit FieldHelper(int a_field) : field_(a_field) {}
00236 int field() const { return field_; }
00237 int field_;
00238
00239 };
00240
00241
00242 TEST(LinkTest, TestReturnVoid) {
00243 Mock mock;
00244
00245 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return());
00246 mock.VoidFromString(NULL);
00247 }
00248
00249
00250 TEST(LinkTest, TestReturn) {
00251 Mock mock;
00252 char ch = 'x';
00253
00254 EXPECT_CALL(mock, StringFromString(_)).WillOnce(Return(&ch));
00255 mock.StringFromString(NULL);
00256 }
00257
00258
00259 TEST(LinkTest, TestReturnNull) {
00260 Mock mock;
00261
00262 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return());
00263 mock.VoidFromString(NULL);
00264 }
00265
00266
00267 TEST(LinkTest, TestReturnRef) {
00268 Mock mock;
00269 int n = 42;
00270
00271 EXPECT_CALL(mock, IntRefFromString(_)).WillOnce(ReturnRef(n));
00272 mock.IntRefFromString(NULL);
00273 }
00274
00275
00276 TEST(LinkTest, TestAssign) {
00277 Mock mock;
00278 char ch = 'x';
00279
00280 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Assign(&ch, 'y'));
00281 mock.VoidFromString(NULL);
00282 }
00283
00284
00285 TEST(LinkTest, TestSetArgPointee) {
00286 Mock mock;
00287 char ch = 'x';
00288
00289 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArgPointee<0>('y'));
00290 mock.VoidFromString(&ch);
00291 }
00292
00293
00294 TEST(LinkTest, TestSetArrayArgument) {
00295 Mock mock;
00296 char ch = 'x';
00297 char ch2 = 'y';
00298
00299 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArrayArgument<0>(&ch2,
00300 &ch2 + 1));
00301 mock.VoidFromString(&ch);
00302 }
00303
00304 #if !GTEST_OS_WINDOWS_MOBILE
00305
00306
00307 TEST(LinkTest, TestSetErrnoAndReturn) {
00308 Mock mock;
00309
00310 int saved_errno = errno;
00311 EXPECT_CALL(mock, IntFromString(_)).WillOnce(SetErrnoAndReturn(1, -1));
00312 mock.IntFromString(NULL);
00313 errno = saved_errno;
00314 }
00315
00316 #endif // !GTEST_OS_WINDOWS_MOBILE
00317
00318
00319 TEST(LinkTest, TestInvoke) {
00320 Mock mock;
00321 InvokeHelper test_invoke_helper;
00322
00323 EXPECT_CALL(mock, VoidFromString(_))
00324 .WillOnce(Invoke(&InvokeHelper::StaticVoidFromString))
00325 .WillOnce(Invoke(&test_invoke_helper, &InvokeHelper::VoidFromString));
00326 mock.VoidFromString(NULL);
00327 mock.VoidFromString(NULL);
00328 }
00329
00330
00331 TEST(LinkTest, TestInvokeWithoutArgs) {
00332 Mock mock;
00333 InvokeHelper test_invoke_helper;
00334
00335 EXPECT_CALL(mock, VoidFromString(_))
00336 .WillOnce(InvokeWithoutArgs(&InvokeHelper::StaticVoidFromVoid))
00337 .WillOnce(InvokeWithoutArgs(&test_invoke_helper,
00338 &InvokeHelper::VoidFromVoid));
00339 mock.VoidFromString(NULL);
00340 mock.VoidFromString(NULL);
00341 }
00342
00343
00344 TEST(LinkTest, TestInvokeArgument) {
00345 Mock mock;
00346 char ch = 'x';
00347
00348 EXPECT_CALL(mock, VoidFromFunc(_)).WillOnce(InvokeArgument<0>(&ch));
00349 mock.VoidFromFunc(InvokeHelper::StaticVoidFromString);
00350 }
00351
00352
00353 TEST(LinkTest, TestWithArg) {
00354 Mock mock;
00355
00356 EXPECT_CALL(mock, VoidFromString(_))
00357 .WillOnce(WithArg<0>(Invoke(&InvokeHelper::StaticVoidFromString)));
00358 mock.VoidFromString(NULL);
00359 }
00360
00361
00362 TEST(LinkTest, TestWithArgs) {
00363 Mock mock;
00364
00365 EXPECT_CALL(mock, VoidFromString(_))
00366 .WillOnce(WithArgs<0>(Invoke(&InvokeHelper::StaticVoidFromString)));
00367 mock.VoidFromString(NULL);
00368 }
00369
00370
00371 TEST(LinkTest, TestWithoutArgs) {
00372 Mock mock;
00373
00374 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(WithoutArgs(Return()));
00375 mock.VoidFromString(NULL);
00376 }
00377
00378
00379 TEST(LinkTest, TestDoAll) {
00380 Mock mock;
00381 char ch = 'x';
00382
00383 EXPECT_CALL(mock, VoidFromString(_))
00384 .WillOnce(DoAll(SetArgPointee<0>('y'), Return()));
00385 mock.VoidFromString(&ch);
00386 }
00387
00388
00389 TEST(LinkTest, TestDoDefault) {
00390 Mock mock;
00391 char ch = 'x';
00392
00393 ON_CALL(mock, VoidFromString(_)).WillByDefault(Return());
00394 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(DoDefault());
00395 mock.VoidFromString(&ch);
00396 }
00397
00398
00399 TEST(LinkTest, TestIgnoreResult) {
00400 Mock mock;
00401
00402 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(IgnoreResult(Return(42)));
00403 mock.VoidFromString(NULL);
00404 }
00405
00406 #if GTEST_HAS_EXCEPTIONS
00407
00408 TEST(LinkTest, TestThrow) {
00409 Mock mock;
00410
00411 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Throw(42));
00412 EXPECT_THROW(mock.VoidFromString(NULL), int);
00413 }
00414 #endif // GTEST_HAS_EXCEPTIONS
00415
00416
00417
00418
00419
00420
00421 #ifdef _MSC_VER
00422 # pragma warning(push)
00423 # pragma warning(disable:4100)
00424 #endif
00425
00426
00427 namespace {
00428 ACTION(Return1) { return 1; }
00429 }
00430
00431 TEST(LinkTest, TestActionMacro) {
00432 Mock mock;
00433
00434 EXPECT_CALL(mock, IntFromString(_)).WillOnce(Return1());
00435 mock.IntFromString(NULL);
00436 }
00437
00438
00439 namespace {
00440 ACTION_P(ReturnArgument, ret_value) { return ret_value; }
00441 }
00442
00443 TEST(LinkTest, TestActionPMacro) {
00444 Mock mock;
00445
00446 EXPECT_CALL(mock, IntFromString(_)).WillOnce(ReturnArgument(42));
00447 mock.IntFromString(NULL);
00448 }
00449
00450
00451 namespace {
00452 ACTION_P2(ReturnEqualsEitherOf, first, second) {
00453 return arg0 == first || arg0 == second;
00454 }
00455 }
00456
00457 #ifdef _MSC_VER
00458 # pragma warning(pop)
00459 #endif
00460
00461 TEST(LinkTest, TestActionP2Macro) {
00462 Mock mock;
00463 char ch = 'x';
00464
00465 EXPECT_CALL(mock, IntFromString(_))
00466 .WillOnce(ReturnEqualsEitherOf("one", "two"));
00467 mock.IntFromString(&ch);
00468 }
00469
00470
00471 TEST(LinkTest, TestMatcherAnything) {
00472 Mock mock;
00473
00474 ON_CALL(mock, VoidFromString(_)).WillByDefault(Return());
00475 }
00476
00477
00478 TEST(LinkTest, TestMatcherA) {
00479 Mock mock;
00480
00481 ON_CALL(mock, VoidFromString(A<char*>())).WillByDefault(Return());
00482 }
00483
00484
00485 TEST(LinkTest, TestMatchersEq) {
00486 Mock mock;
00487 const char* p = "x";
00488
00489 ON_CALL(mock, VoidFromString(Eq(p))).WillByDefault(Return());
00490 ON_CALL(mock, VoidFromString(const_cast<char*>("y")))
00491 .WillByDefault(Return());
00492 }
00493
00494
00495 TEST(LinkTest, TestMatchersRelations) {
00496 Mock mock;
00497
00498 ON_CALL(mock, VoidFromFloat(Lt(1.0f))).WillByDefault(Return());
00499 ON_CALL(mock, VoidFromFloat(Gt(1.0f))).WillByDefault(Return());
00500 ON_CALL(mock, VoidFromFloat(Le(1.0f))).WillByDefault(Return());
00501 ON_CALL(mock, VoidFromFloat(Ge(1.0f))).WillByDefault(Return());
00502 ON_CALL(mock, VoidFromFloat(Ne(1.0f))).WillByDefault(Return());
00503 }
00504
00505
00506 TEST(LinkTest, TestMatcherNotNull) {
00507 Mock mock;
00508
00509 ON_CALL(mock, VoidFromString(NotNull())).WillByDefault(Return());
00510 }
00511
00512
00513 TEST(LinkTest, TestMatcherIsNull) {
00514 Mock mock;
00515
00516 ON_CALL(mock, VoidFromString(IsNull())).WillByDefault(Return());
00517 }
00518
00519
00520 TEST(LinkTest, TestMatcherRef) {
00521 Mock mock;
00522 int a = 0;
00523
00524 ON_CALL(mock, VoidFromIntRef(Ref(a))).WillByDefault(Return());
00525 }
00526
00527
00528 TEST(LinkTest, TestMatcherTypedEq) {
00529 Mock mock;
00530 long a = 0;
00531
00532 ON_CALL(mock, VoidFromIntRef(TypedEq<int&>(a))).WillByDefault(Return());
00533 }
00534
00535
00536
00537 TEST(LinkTest, TestMatchersFloatingPoint) {
00538 Mock mock;
00539 float a = 0;
00540
00541 ON_CALL(mock, VoidFromFloat(FloatEq(a))).WillByDefault(Return());
00542 ON_CALL(mock, VoidFromDouble(DoubleEq(a))).WillByDefault(Return());
00543 ON_CALL(mock, VoidFromFloat(NanSensitiveFloatEq(a))).WillByDefault(Return());
00544 ON_CALL(mock, VoidFromDouble(NanSensitiveDoubleEq(a)))
00545 .WillByDefault(Return());
00546 }
00547
00548
00549 TEST(LinkTest, TestMatcherContainsRegex) {
00550 Mock mock;
00551
00552 ON_CALL(mock, VoidFromString(ContainsRegex(".*"))).WillByDefault(Return());
00553 }
00554
00555
00556 TEST(LinkTest, TestMatcherMatchesRegex) {
00557 Mock mock;
00558
00559 ON_CALL(mock, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return());
00560 }
00561
00562
00563 TEST(LinkTest, TestMatchersSubstrings) {
00564 Mock mock;
00565
00566 ON_CALL(mock, VoidFromString(StartsWith("a"))).WillByDefault(Return());
00567 ON_CALL(mock, VoidFromString(EndsWith("c"))).WillByDefault(Return());
00568 ON_CALL(mock, VoidFromString(HasSubstr("b"))).WillByDefault(Return());
00569 }
00570
00571
00572 TEST(LinkTest, TestMatchersStringEquality) {
00573 Mock mock;
00574 ON_CALL(mock, VoidFromString(StrEq("a"))).WillByDefault(Return());
00575 ON_CALL(mock, VoidFromString(StrNe("a"))).WillByDefault(Return());
00576 ON_CALL(mock, VoidFromString(StrCaseEq("a"))).WillByDefault(Return());
00577 ON_CALL(mock, VoidFromString(StrCaseNe("a"))).WillByDefault(Return());
00578 }
00579
00580
00581 TEST(LinkTest, TestMatcherElementsAre) {
00582 Mock mock;
00583
00584 ON_CALL(mock, VoidFromVector(ElementsAre('a', _))).WillByDefault(Return());
00585 }
00586
00587
00588 TEST(LinkTest, TestMatcherElementsAreArray) {
00589 Mock mock;
00590 char arr[] = { 'a', 'b' };
00591
00592 ON_CALL(mock, VoidFromVector(ElementsAreArray(arr))).WillByDefault(Return());
00593 }
00594
00595
00596 TEST(LinkTest, TestMatcherContainerEq) {
00597 Mock mock;
00598 std::vector<int> v;
00599
00600 ON_CALL(mock, VoidFromVector(ContainerEq(v))).WillByDefault(Return());
00601 }
00602
00603
00604 TEST(LinkTest, TestMatcherField) {
00605 FieldHelper helper(0);
00606
00607 Matcher<const FieldHelper&> m = Field(&FieldHelper::field_, Eq(0));
00608 EXPECT_TRUE(m.Matches(helper));
00609
00610 Matcher<const FieldHelper*> m2 = Field(&FieldHelper::field_, Eq(0));
00611 EXPECT_TRUE(m2.Matches(&helper));
00612 }
00613
00614
00615 TEST(LinkTest, TestMatcherProperty) {
00616 FieldHelper helper(0);
00617
00618 Matcher<const FieldHelper&> m = Property(&FieldHelper::field, Eq(0));
00619 EXPECT_TRUE(m.Matches(helper));
00620
00621 Matcher<const FieldHelper*> m2 = Property(&FieldHelper::field, Eq(0));
00622 EXPECT_TRUE(m2.Matches(&helper));
00623 }
00624
00625
00626 TEST(LinkTest, TestMatcherResultOf) {
00627 Matcher<char*> m = ResultOf(&InvokeHelper::StaticIntFromString, Eq(1));
00628 EXPECT_TRUE(m.Matches(NULL));
00629 }
00630
00631
00632 TEST(LinkTest, TestMatcherPointee) {
00633 int n = 1;
00634
00635 Matcher<int*> m = Pointee(Eq(1));
00636 EXPECT_TRUE(m.Matches(&n));
00637 }
00638
00639
00640 TEST(LinkTest, TestMatcherTruly) {
00641 Matcher<const char*> m = Truly(&InvokeHelper::StaticBoolFromString);
00642 EXPECT_TRUE(m.Matches(NULL));
00643 }
00644
00645
00646 TEST(LinkTest, TestMatcherAllOf) {
00647 Matcher<int> m = AllOf(_, Eq(1));
00648 EXPECT_TRUE(m.Matches(1));
00649 }
00650
00651
00652 TEST(LinkTest, TestMatcherAnyOf) {
00653 Matcher<int> m = AnyOf(_, Eq(1));
00654 EXPECT_TRUE(m.Matches(1));
00655 }
00656
00657
00658 TEST(LinkTest, TestMatcherNot) {
00659 Matcher<int> m = Not(_);
00660 EXPECT_FALSE(m.Matches(1));
00661 }
00662
00663
00664 TEST(LinkTest, TestMatcherCast) {
00665 Matcher<const char*> m = MatcherCast<const char*>(_);
00666 EXPECT_TRUE(m.Matches(NULL));
00667 }
00668
00669 #endif // GMOCK_TEST_GMOCK_LINK_TEST_H_