zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 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 |
zhanyong.wan | 5921483 | 2010-10-05 05:58:51 +0000 | [diff] [blame] | 47 | // SetArgPointee |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 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 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 117 | #include "gmock/gmock.h" |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 118 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 119 | #if !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 658ac0b | 2011-02-24 07:29:13 +0000 | [diff] [blame] | 120 | # include <errno.h> |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 121 | #endif |
| 122 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 123 | #include "gmock/internal/gmock-port.h" |
| 124 | #include "gtest/gtest.h" |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 125 | #include <iostream> |
| 126 | #include <vector> |
| 127 | |
| 128 | using testing::_; |
| 129 | using testing::A; |
| 130 | using testing::AllOf; |
| 131 | using testing::AnyOf; |
| 132 | using testing::Assign; |
| 133 | using testing::ContainerEq; |
| 134 | using testing::DoAll; |
| 135 | using testing::DoDefault; |
| 136 | using testing::DoubleEq; |
| 137 | using testing::ElementsAre; |
| 138 | using testing::ElementsAreArray; |
| 139 | using testing::EndsWith; |
| 140 | using testing::Eq; |
| 141 | using testing::Field; |
| 142 | using testing::FloatEq; |
| 143 | using testing::Ge; |
| 144 | using testing::Gt; |
| 145 | using testing::HasSubstr; |
| 146 | using testing::IgnoreResult; |
| 147 | using testing::Invoke; |
| 148 | using testing::InvokeArgument; |
| 149 | using testing::InvokeWithoutArgs; |
zhanyong.wan | 2d970ee | 2009-09-24 21:41:36 +0000 | [diff] [blame] | 150 | using testing::IsNull; |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 151 | using testing::Le; |
| 152 | using testing::Lt; |
| 153 | using testing::Matcher; |
| 154 | using testing::MatcherCast; |
| 155 | using testing::NanSensitiveDoubleEq; |
| 156 | using testing::NanSensitiveFloatEq; |
| 157 | using testing::Ne; |
| 158 | using testing::Not; |
| 159 | using testing::NotNull; |
| 160 | using testing::Pointee; |
| 161 | using testing::Property; |
| 162 | using testing::Ref; |
| 163 | using testing::ResultOf; |
| 164 | using testing::Return; |
| 165 | using testing::ReturnNull; |
| 166 | using testing::ReturnRef; |
zhanyong.wan | 5921483 | 2010-10-05 05:58:51 +0000 | [diff] [blame] | 167 | using testing::SetArgPointee; |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 168 | using testing::SetArrayArgument; |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 169 | using testing::StartsWith; |
| 170 | using testing::StrCaseEq; |
| 171 | using testing::StrCaseNe; |
| 172 | using testing::StrEq; |
| 173 | using testing::StrNe; |
| 174 | using testing::Truly; |
| 175 | using testing::TypedEq; |
| 176 | using testing::WithArg; |
| 177 | using testing::WithArgs; |
| 178 | using testing::WithoutArgs; |
| 179 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 180 | #if !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 181 | using testing::SetErrnoAndReturn; |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 182 | #endif |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 183 | |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 184 | #if GTEST_HAS_EXCEPTIONS |
| 185 | using testing::Throw; |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 186 | #endif |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 187 | |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 188 | using testing::ContainsRegex; |
| 189 | using testing::MatchesRegex; |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 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: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 207 | Mock() {} |
| 208 | |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 209 | MOCK_METHOD1(VoidFromString, void(char* str)); |
| 210 | MOCK_METHOD1(StringFromString, char*(char* str)); |
| 211 | MOCK_METHOD1(IntFromString, int(char* str)); |
| 212 | MOCK_METHOD1(IntRefFromString, int&(char* str)); |
| 213 | MOCK_METHOD1(VoidFromFunc, void(void(*func)(char* str))); |
| 214 | MOCK_METHOD1(VoidFromIntRef, void(int& n)); |
| 215 | MOCK_METHOD1(VoidFromFloat, void(float n)); |
| 216 | MOCK_METHOD1(VoidFromDouble, void(double n)); |
| 217 | MOCK_METHOD1(VoidFromVector, void(const std::vector<int>& v)); |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 218 | |
| 219 | private: |
| 220 | GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock); |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | class InvokeHelper { |
| 224 | public: |
| 225 | static void StaticVoidFromVoid() {} |
| 226 | void VoidFromVoid() {} |
| 227 | static void StaticVoidFromString(char*) {} |
| 228 | void VoidFromString(char*) {} |
| 229 | static int StaticIntFromString(char*) { return 1; } |
| 230 | static bool StaticBoolFromString(const char*) { return true; } |
| 231 | }; |
| 232 | |
| 233 | class FieldHelper { |
| 234 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 235 | FieldHelper(int a_field) : field_(a_field) {} |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 236 | int field() const { return field_; } |
| 237 | int field_; // NOLINT -- need external access to field_ to test |
| 238 | // the Field matcher. |
| 239 | }; |
| 240 | |
| 241 | // Tests the linkage of the ReturnVoid action. |
| 242 | TEST(LinkTest, TestReturnVoid) { |
| 243 | Mock mock; |
| 244 | |
| 245 | EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return()); |
| 246 | mock.VoidFromString(NULL); |
| 247 | } |
| 248 | |
| 249 | // Tests the linkage of the Return action. |
| 250 | TEST(LinkTest, TestReturn) { |
| 251 | Mock mock; |
| 252 | char ch = 'x'; |
| 253 | |
| 254 | EXPECT_CALL(mock, StringFromString(_)).WillOnce(Return(&ch)); |
| 255 | mock.StringFromString(NULL); |
| 256 | } |
| 257 | |
| 258 | // Tests the linkage of the ReturnNull action. |
| 259 | TEST(LinkTest, TestReturnNull) { |
| 260 | Mock mock; |
| 261 | |
| 262 | EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return()); |
| 263 | mock.VoidFromString(NULL); |
| 264 | } |
| 265 | |
| 266 | // Tests the linkage of the ReturnRef action. |
| 267 | TEST(LinkTest, TestReturnRef) { |
| 268 | Mock mock; |
| 269 | int n = 42; |
| 270 | |
| 271 | EXPECT_CALL(mock, IntRefFromString(_)).WillOnce(ReturnRef(n)); |
| 272 | mock.IntRefFromString(NULL); |
| 273 | } |
| 274 | |
| 275 | // Tests the linkage of the Assign action. |
| 276 | TEST(LinkTest, TestAssign) { |
| 277 | Mock mock; |
| 278 | char ch = 'x'; |
| 279 | |
| 280 | EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Assign(&ch, 'y')); |
| 281 | mock.VoidFromString(NULL); |
| 282 | } |
| 283 | |
zhanyong.wan | 5921483 | 2010-10-05 05:58:51 +0000 | [diff] [blame] | 284 | // Tests the linkage of the SetArgPointee action. |
| 285 | TEST(LinkTest, TestSetArgPointee) { |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 286 | Mock mock; |
| 287 | char ch = 'x'; |
| 288 | |
zhanyong.wan | 5921483 | 2010-10-05 05:58:51 +0000 | [diff] [blame] | 289 | EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArgPointee<0>('y')); |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 290 | mock.VoidFromString(&ch); |
| 291 | } |
| 292 | |
| 293 | // Tests the linkage of the SetArrayArgument action. |
| 294 | TEST(LinkTest, TestSetArrayArgument) { |
| 295 | Mock mock; |
| 296 | char ch = 'x'; |
| 297 | char ch2 = 'y'; |
| 298 | |
| 299 | EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArrayArgument<0>(&ch2, |
| 300 | &ch2 + 1)); |
| 301 | mock.VoidFromString(&ch); |
| 302 | } |
| 303 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 304 | #if !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 305 | |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 306 | // Tests the linkage of the SetErrnoAndReturn action. |
| 307 | TEST(LinkTest, TestSetErrnoAndReturn) { |
| 308 | Mock mock; |
| 309 | |
| 310 | int saved_errno = errno; |
| 311 | EXPECT_CALL(mock, IntFromString(_)).WillOnce(SetErrnoAndReturn(1, -1)); |
| 312 | mock.IntFromString(NULL); |
| 313 | errno = saved_errno; |
| 314 | } |
| 315 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 316 | #endif // !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 317 | |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 318 | // Tests the linkage of the Invoke(function) and Invoke(object, method) actions. |
| 319 | TEST(LinkTest, TestInvoke) { |
| 320 | Mock mock; |
| 321 | InvokeHelper test_invoke_helper; |
| 322 | |
| 323 | EXPECT_CALL(mock, VoidFromString(_)) |
| 324 | .WillOnce(Invoke(&InvokeHelper::StaticVoidFromString)) |
| 325 | .WillOnce(Invoke(&test_invoke_helper, &InvokeHelper::VoidFromString)); |
| 326 | mock.VoidFromString(NULL); |
| 327 | mock.VoidFromString(NULL); |
| 328 | } |
| 329 | |
| 330 | // Tests the linkage of the InvokeWithoutArgs action. |
| 331 | TEST(LinkTest, TestInvokeWithoutArgs) { |
| 332 | Mock mock; |
| 333 | InvokeHelper test_invoke_helper; |
| 334 | |
| 335 | EXPECT_CALL(mock, VoidFromString(_)) |
| 336 | .WillOnce(InvokeWithoutArgs(&InvokeHelper::StaticVoidFromVoid)) |
| 337 | .WillOnce(InvokeWithoutArgs(&test_invoke_helper, |
| 338 | &InvokeHelper::VoidFromVoid)); |
| 339 | mock.VoidFromString(NULL); |
| 340 | mock.VoidFromString(NULL); |
| 341 | } |
| 342 | |
| 343 | // Tests the linkage of the InvokeArgument action. |
| 344 | TEST(LinkTest, TestInvokeArgument) { |
| 345 | Mock mock; |
| 346 | char ch = 'x'; |
| 347 | |
| 348 | EXPECT_CALL(mock, VoidFromFunc(_)).WillOnce(InvokeArgument<0>(&ch)); |
| 349 | mock.VoidFromFunc(InvokeHelper::StaticVoidFromString); |
| 350 | } |
| 351 | |
| 352 | // Tests the linkage of the WithArg action. |
| 353 | TEST(LinkTest, TestWithArg) { |
| 354 | Mock mock; |
| 355 | |
| 356 | EXPECT_CALL(mock, VoidFromString(_)) |
| 357 | .WillOnce(WithArg<0>(Invoke(&InvokeHelper::StaticVoidFromString))); |
| 358 | mock.VoidFromString(NULL); |
| 359 | } |
| 360 | |
| 361 | // Tests the linkage of the WithArgs action. |
| 362 | TEST(LinkTest, TestWithArgs) { |
| 363 | Mock mock; |
| 364 | |
| 365 | EXPECT_CALL(mock, VoidFromString(_)) |
| 366 | .WillOnce(WithArgs<0>(Invoke(&InvokeHelper::StaticVoidFromString))); |
| 367 | mock.VoidFromString(NULL); |
| 368 | } |
| 369 | |
| 370 | // Tests the linkage of the WithoutArgs action. |
| 371 | TEST(LinkTest, TestWithoutArgs) { |
| 372 | Mock mock; |
| 373 | |
| 374 | EXPECT_CALL(mock, VoidFromString(_)).WillOnce(WithoutArgs(Return())); |
| 375 | mock.VoidFromString(NULL); |
| 376 | } |
| 377 | |
| 378 | // Tests the linkage of the DoAll action. |
| 379 | TEST(LinkTest, TestDoAll) { |
| 380 | Mock mock; |
| 381 | char ch = 'x'; |
| 382 | |
| 383 | EXPECT_CALL(mock, VoidFromString(_)) |
zhanyong.wan | 5921483 | 2010-10-05 05:58:51 +0000 | [diff] [blame] | 384 | .WillOnce(DoAll(SetArgPointee<0>('y'), Return())); |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 385 | mock.VoidFromString(&ch); |
| 386 | } |
| 387 | |
| 388 | // Tests the linkage of the DoDefault action. |
| 389 | TEST(LinkTest, TestDoDefault) { |
| 390 | Mock mock; |
| 391 | char ch = 'x'; |
| 392 | |
| 393 | ON_CALL(mock, VoidFromString(_)).WillByDefault(Return()); |
| 394 | EXPECT_CALL(mock, VoidFromString(_)).WillOnce(DoDefault()); |
| 395 | mock.VoidFromString(&ch); |
| 396 | } |
| 397 | |
| 398 | // Tests the linkage of the IgnoreResult action. |
| 399 | TEST(LinkTest, TestIgnoreResult) { |
| 400 | Mock mock; |
| 401 | |
| 402 | EXPECT_CALL(mock, VoidFromString(_)).WillOnce(IgnoreResult(Return(42))); |
| 403 | mock.VoidFromString(NULL); |
| 404 | } |
| 405 | |
| 406 | #if GTEST_HAS_EXCEPTIONS |
| 407 | // Tests the linkage of the Throw action. |
| 408 | TEST(LinkTest, TestThrow) { |
| 409 | Mock mock; |
| 410 | |
| 411 | EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Throw(42)); |
| 412 | EXPECT_THROW(mock.VoidFromString(NULL), int); |
| 413 | } |
| 414 | #endif // GTEST_HAS_EXCEPTIONS |
| 415 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 416 | // The ACTION*() macros trigger warning C4100 (unreferenced formal |
| 417 | // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in |
| 418 | // the macro definition, as the warnings are generated when the macro |
| 419 | // is expanded and macro expansion cannot contain #pragma. Therefore |
| 420 | // we suppress them here. |
| 421 | #ifdef _MSC_VER |
zhanyong.wan | 658ac0b | 2011-02-24 07:29:13 +0000 | [diff] [blame] | 422 | # pragma warning(push) |
| 423 | # pragma warning(disable:4100) |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 424 | #endif |
| 425 | |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 426 | // Tests the linkage of actions created using ACTION macro. |
| 427 | namespace { |
| 428 | ACTION(Return1) { return 1; } |
| 429 | } |
| 430 | |
| 431 | TEST(LinkTest, TestActionMacro) { |
| 432 | Mock mock; |
| 433 | |
| 434 | EXPECT_CALL(mock, IntFromString(_)).WillOnce(Return1()); |
| 435 | mock.IntFromString(NULL); |
| 436 | } |
| 437 | |
| 438 | // Tests the linkage of actions created using ACTION_P macro. |
| 439 | namespace { |
| 440 | ACTION_P(ReturnArgument, ret_value) { return ret_value; } |
| 441 | } |
| 442 | |
| 443 | TEST(LinkTest, TestActionPMacro) { |
| 444 | Mock mock; |
| 445 | |
| 446 | EXPECT_CALL(mock, IntFromString(_)).WillOnce(ReturnArgument(42)); |
| 447 | mock.IntFromString(NULL); |
| 448 | } |
| 449 | |
| 450 | // Tests the linkage of actions created using ACTION_P2 macro. |
| 451 | namespace { |
| 452 | ACTION_P2(ReturnEqualsEitherOf, first, second) { |
| 453 | return arg0 == first || arg0 == second; |
| 454 | } |
| 455 | } |
| 456 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 457 | #ifdef _MSC_VER |
zhanyong.wan | 658ac0b | 2011-02-24 07:29:13 +0000 | [diff] [blame] | 458 | # pragma warning(pop) |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 459 | #endif |
| 460 | |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 461 | TEST(LinkTest, TestActionP2Macro) { |
| 462 | Mock mock; |
| 463 | char ch = 'x'; |
| 464 | |
| 465 | EXPECT_CALL(mock, IntFromString(_)) |
| 466 | .WillOnce(ReturnEqualsEitherOf("one", "two")); |
| 467 | mock.IntFromString(&ch); |
| 468 | } |
| 469 | |
| 470 | // Tests the linkage of the "_" matcher. |
| 471 | TEST(LinkTest, TestMatcherAnything) { |
| 472 | Mock mock; |
| 473 | |
| 474 | ON_CALL(mock, VoidFromString(_)).WillByDefault(Return()); |
| 475 | } |
| 476 | |
| 477 | // Tests the linkage of the A matcher. |
| 478 | TEST(LinkTest, TestMatcherA) { |
| 479 | Mock mock; |
| 480 | |
| 481 | ON_CALL(mock, VoidFromString(A<char*>())).WillByDefault(Return()); |
| 482 | } |
| 483 | |
| 484 | // Tests the linkage of the Eq and the "bare value" matcher. |
| 485 | TEST(LinkTest, TestMatchersEq) { |
| 486 | Mock mock; |
| 487 | const char* p = "x"; |
| 488 | |
| 489 | ON_CALL(mock, VoidFromString(Eq(p))).WillByDefault(Return()); |
| 490 | ON_CALL(mock, VoidFromString(const_cast<char*>("y"))) |
| 491 | .WillByDefault(Return()); |
| 492 | } |
| 493 | |
| 494 | // Tests the linkage of the Lt, Gt, Le, Ge, and Ne matchers. |
| 495 | TEST(LinkTest, TestMatchersRelations) { |
| 496 | Mock mock; |
| 497 | |
| 498 | ON_CALL(mock, VoidFromFloat(Lt(1.0f))).WillByDefault(Return()); |
| 499 | ON_CALL(mock, VoidFromFloat(Gt(1.0f))).WillByDefault(Return()); |
| 500 | ON_CALL(mock, VoidFromFloat(Le(1.0f))).WillByDefault(Return()); |
| 501 | ON_CALL(mock, VoidFromFloat(Ge(1.0f))).WillByDefault(Return()); |
| 502 | ON_CALL(mock, VoidFromFloat(Ne(1.0f))).WillByDefault(Return()); |
| 503 | } |
| 504 | |
| 505 | // Tests the linkage of the NotNull matcher. |
| 506 | TEST(LinkTest, TestMatcherNotNull) { |
| 507 | Mock mock; |
| 508 | |
| 509 | ON_CALL(mock, VoidFromString(NotNull())).WillByDefault(Return()); |
| 510 | } |
| 511 | |
zhanyong.wan | 2d970ee | 2009-09-24 21:41:36 +0000 | [diff] [blame] | 512 | // Tests the linkage of the IsNull matcher. |
| 513 | TEST(LinkTest, TestMatcherIsNull) { |
| 514 | Mock mock; |
| 515 | |
| 516 | ON_CALL(mock, VoidFromString(IsNull())).WillByDefault(Return()); |
| 517 | } |
| 518 | |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 519 | // Tests the linkage of the Ref matcher. |
| 520 | TEST(LinkTest, TestMatcherRef) { |
| 521 | Mock mock; |
| 522 | int a = 0; |
| 523 | |
| 524 | ON_CALL(mock, VoidFromIntRef(Ref(a))).WillByDefault(Return()); |
| 525 | } |
| 526 | |
| 527 | // Tests the linkage of the TypedEq matcher. |
| 528 | TEST(LinkTest, TestMatcherTypedEq) { |
| 529 | Mock mock; |
zhanyong.wan | 3fbd2dd | 2009-03-26 19:06:45 +0000 | [diff] [blame] | 530 | long a = 0; |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 531 | |
| 532 | ON_CALL(mock, VoidFromIntRef(TypedEq<int&>(a))).WillByDefault(Return()); |
| 533 | } |
| 534 | |
| 535 | // Tests the linkage of the FloatEq, DoubleEq, NanSensitiveFloatEq and |
| 536 | // NanSensitiveDoubleEq matchers. |
| 537 | TEST(LinkTest, TestMatchersFloatingPoint) { |
| 538 | Mock mock; |
| 539 | float a = 0; |
| 540 | |
| 541 | ON_CALL(mock, VoidFromFloat(FloatEq(a))).WillByDefault(Return()); |
| 542 | ON_CALL(mock, VoidFromDouble(DoubleEq(a))).WillByDefault(Return()); |
| 543 | ON_CALL(mock, VoidFromFloat(NanSensitiveFloatEq(a))).WillByDefault(Return()); |
| 544 | ON_CALL(mock, VoidFromDouble(NanSensitiveDoubleEq(a))) |
| 545 | .WillByDefault(Return()); |
| 546 | } |
| 547 | |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 548 | // Tests the linkage of the ContainsRegex matcher. |
| 549 | TEST(LinkTest, TestMatcherContainsRegex) { |
| 550 | Mock mock; |
| 551 | |
| 552 | ON_CALL(mock, VoidFromString(ContainsRegex(".*"))).WillByDefault(Return()); |
| 553 | } |
| 554 | |
| 555 | // Tests the linkage of the MatchesRegex matcher. |
| 556 | TEST(LinkTest, TestMatcherMatchesRegex) { |
| 557 | Mock mock; |
| 558 | |
| 559 | ON_CALL(mock, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return()); |
| 560 | } |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 561 | |
| 562 | // Tests the linkage of the StartsWith, EndsWith, and HasSubstr matchers. |
| 563 | TEST(LinkTest, TestMatchersSubstrings) { |
| 564 | Mock mock; |
| 565 | |
| 566 | ON_CALL(mock, VoidFromString(StartsWith("a"))).WillByDefault(Return()); |
| 567 | ON_CALL(mock, VoidFromString(EndsWith("c"))).WillByDefault(Return()); |
| 568 | ON_CALL(mock, VoidFromString(HasSubstr("b"))).WillByDefault(Return()); |
| 569 | } |
| 570 | |
| 571 | // Tests the linkage of the StrEq, StrNe, StrCaseEq, and StrCaseNe matchers. |
| 572 | TEST(LinkTest, TestMatchersStringEquality) { |
| 573 | Mock mock; |
| 574 | ON_CALL(mock, VoidFromString(StrEq("a"))).WillByDefault(Return()); |
| 575 | ON_CALL(mock, VoidFromString(StrNe("a"))).WillByDefault(Return()); |
| 576 | ON_CALL(mock, VoidFromString(StrCaseEq("a"))).WillByDefault(Return()); |
| 577 | ON_CALL(mock, VoidFromString(StrCaseNe("a"))).WillByDefault(Return()); |
| 578 | } |
| 579 | |
| 580 | // Tests the linkage of the ElementsAre matcher. |
| 581 | TEST(LinkTest, TestMatcherElementsAre) { |
| 582 | Mock mock; |
| 583 | |
| 584 | ON_CALL(mock, VoidFromVector(ElementsAre('a', _))).WillByDefault(Return()); |
| 585 | } |
| 586 | |
| 587 | // Tests the linkage of the ElementsAreArray matcher. |
| 588 | TEST(LinkTest, TestMatcherElementsAreArray) { |
| 589 | Mock mock; |
| 590 | char arr[] = { 'a', 'b' }; |
| 591 | |
| 592 | ON_CALL(mock, VoidFromVector(ElementsAreArray(arr))).WillByDefault(Return()); |
| 593 | } |
| 594 | |
| 595 | // Tests the linkage of the ContainerEq matcher. |
| 596 | TEST(LinkTest, TestMatcherContainerEq) { |
| 597 | Mock mock; |
| 598 | std::vector<int> v; |
| 599 | |
| 600 | ON_CALL(mock, VoidFromVector(ContainerEq(v))).WillByDefault(Return()); |
| 601 | } |
| 602 | |
| 603 | // Tests the linkage of the Field matcher. |
| 604 | TEST(LinkTest, TestMatcherField) { |
| 605 | FieldHelper helper(0); |
| 606 | |
| 607 | Matcher<const FieldHelper&> m = Field(&FieldHelper::field_, Eq(0)); |
| 608 | EXPECT_TRUE(m.Matches(helper)); |
| 609 | |
| 610 | Matcher<const FieldHelper*> m2 = Field(&FieldHelper::field_, Eq(0)); |
| 611 | EXPECT_TRUE(m2.Matches(&helper)); |
| 612 | } |
| 613 | |
| 614 | // Tests the linkage of the Property matcher. |
| 615 | TEST(LinkTest, TestMatcherProperty) { |
| 616 | FieldHelper helper(0); |
| 617 | |
| 618 | Matcher<const FieldHelper&> m = Property(&FieldHelper::field, Eq(0)); |
| 619 | EXPECT_TRUE(m.Matches(helper)); |
| 620 | |
| 621 | Matcher<const FieldHelper*> m2 = Property(&FieldHelper::field, Eq(0)); |
| 622 | EXPECT_TRUE(m2.Matches(&helper)); |
| 623 | } |
| 624 | |
| 625 | // Tests the linkage of the ResultOf matcher. |
| 626 | TEST(LinkTest, TestMatcherResultOf) { |
| 627 | Matcher<char*> m = ResultOf(&InvokeHelper::StaticIntFromString, Eq(1)); |
| 628 | EXPECT_TRUE(m.Matches(NULL)); |
| 629 | } |
| 630 | |
| 631 | // Tests the linkage of the ResultOf matcher. |
| 632 | TEST(LinkTest, TestMatcherPointee) { |
| 633 | int n = 1; |
| 634 | |
| 635 | Matcher<int*> m = Pointee(Eq(1)); |
| 636 | EXPECT_TRUE(m.Matches(&n)); |
| 637 | } |
| 638 | |
| 639 | // Tests the linkage of the Truly matcher. |
| 640 | TEST(LinkTest, TestMatcherTruly) { |
| 641 | Matcher<const char*> m = Truly(&InvokeHelper::StaticBoolFromString); |
| 642 | EXPECT_TRUE(m.Matches(NULL)); |
| 643 | } |
| 644 | |
| 645 | // Tests the linkage of the AllOf matcher. |
| 646 | TEST(LinkTest, TestMatcherAllOf) { |
| 647 | Matcher<int> m = AllOf(_, Eq(1)); |
| 648 | EXPECT_TRUE(m.Matches(1)); |
| 649 | } |
| 650 | |
| 651 | // Tests the linkage of the AnyOf matcher. |
| 652 | TEST(LinkTest, TestMatcherAnyOf) { |
| 653 | Matcher<int> m = AnyOf(_, Eq(1)); |
| 654 | EXPECT_TRUE(m.Matches(1)); |
| 655 | } |
| 656 | |
| 657 | // Tests the linkage of the Not matcher. |
| 658 | TEST(LinkTest, TestMatcherNot) { |
| 659 | Matcher<int> m = Not(_); |
| 660 | EXPECT_FALSE(m.Matches(1)); |
| 661 | } |
| 662 | |
| 663 | // Tests the linkage of the MatcherCast<T>() function. |
| 664 | TEST(LinkTest, TestMatcherCast) { |
| 665 | Matcher<const char*> m = MatcherCast<const char*>(_); |
| 666 | EXPECT_TRUE(m.Matches(NULL)); |
| 667 | } |
| 668 | |
| 669 | #endif // GMOCK_TEST_GMOCK_LINK_TEST_H_ |