| OLD | NEW |
| 1 // Copyright 2007, Google Inc. | 1 // Copyright 2007, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT | 262 Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT |
| 263 EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); | 263 EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); |
| 264 } | 264 } |
| 265 | 265 |
| 266 // Tests using Invoke() with a 6-argument function. | 266 // Tests using Invoke() with a 6-argument function. |
| 267 TEST(InvokeTest, FunctionThatTakes6Arguments) { | 267 TEST(InvokeTest, FunctionThatTakes6Arguments) { |
| 268 Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT | 268 Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT |
| 269 EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); | 269 EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); |
| 270 } | 270 } |
| 271 | 271 |
| 272 // A helper that turns the type of a C-string literal from const |
| 273 // char[N] to const char*. |
| 274 inline const char* CharPtr(const char* s) { return s; } |
| 275 |
| 272 // Tests using Invoke() with a 7-argument function. | 276 // Tests using Invoke() with a 7-argument function. |
| 273 TEST(InvokeTest, FunctionThatTakes7Arguments) { | 277 TEST(InvokeTest, FunctionThatTakes7Arguments) { |
| 274 Action<string(const char*, const char*, const char*, const char*, | 278 Action<string(const char*, const char*, const char*, const char*, |
| 275 const char*, const char*, const char*)> a = | 279 const char*, const char*, const char*)> a = |
| 276 Invoke(Concat7); | 280 Invoke(Concat7); |
| 277 EXPECT_EQ("1234567", | 281 EXPECT_EQ("1234567", |
| 278 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7"))); | 282 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 283 CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 284 CharPtr("7")))); |
| 279 } | 285 } |
| 280 | 286 |
| 281 // Tests using Invoke() with a 8-argument function. | 287 // Tests using Invoke() with a 8-argument function. |
| 282 TEST(InvokeTest, FunctionThatTakes8Arguments) { | 288 TEST(InvokeTest, FunctionThatTakes8Arguments) { |
| 283 Action<string(const char*, const char*, const char*, const char*, | 289 Action<string(const char*, const char*, const char*, const char*, |
| 284 const char*, const char*, const char*, const char*)> a = | 290 const char*, const char*, const char*, const char*)> a = |
| 285 Invoke(Concat8); | 291 Invoke(Concat8); |
| 286 EXPECT_EQ("12345678", | 292 EXPECT_EQ("12345678", |
| 287 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8"))); | 293 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 294 CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 295 CharPtr("7"), CharPtr("8")))); |
| 288 } | 296 } |
| 289 | 297 |
| 290 // Tests using Invoke() with a 9-argument function. | 298 // Tests using Invoke() with a 9-argument function. |
| 291 TEST(InvokeTest, FunctionThatTakes9Arguments) { | 299 TEST(InvokeTest, FunctionThatTakes9Arguments) { |
| 292 Action<string(const char*, const char*, const char*, const char*, | 300 Action<string(const char*, const char*, const char*, const char*, |
| 293 const char*, const char*, const char*, const char*, | 301 const char*, const char*, const char*, const char*, |
| 294 const char*)> a = Invoke(Concat9); | 302 const char*)> a = Invoke(Concat9); |
| 295 EXPECT_EQ("123456789", | 303 EXPECT_EQ("123456789", |
| 296 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8", "9"))); | 304 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 305 CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 306 CharPtr("7"), CharPtr("8"), CharPtr("9")))); |
| 297 } | 307 } |
| 298 | 308 |
| 299 // Tests using Invoke() with a 10-argument function. | 309 // Tests using Invoke() with a 10-argument function. |
| 300 TEST(InvokeTest, FunctionThatTakes10Arguments) { | 310 TEST(InvokeTest, FunctionThatTakes10Arguments) { |
| 301 Action<string(const char*, const char*, const char*, const char*, | 311 Action<string(const char*, const char*, const char*, const char*, |
| 302 const char*, const char*, const char*, const char*, | 312 const char*, const char*, const char*, const char*, |
| 303 const char*, const char*)> a = Invoke(Concat10); | 313 const char*, const char*)> a = Invoke(Concat10); |
| 304 EXPECT_EQ("1234567890", a.Perform(make_tuple("1", "2", "3", "4", "5", "6", | 314 EXPECT_EQ("1234567890", |
| 305 "7", "8", "9", "0"))); | 315 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 316 CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 317 CharPtr("7"), CharPtr("8"), CharPtr("9"), |
| 318 CharPtr("0")))); |
| 306 } | 319 } |
| 307 | 320 |
| 308 // Tests using Invoke() with functions with parameters declared as Unused. | 321 // Tests using Invoke() with functions with parameters declared as Unused. |
| 309 TEST(InvokeTest, FunctionWithUnusedParameters) { | 322 TEST(InvokeTest, FunctionWithUnusedParameters) { |
| 310 Action<int(int, int, double, const string&)> a1 = | 323 Action<int(int, int, double, const string&)> a1 = |
| 311 Invoke(SumOfFirst2); | 324 Invoke(SumOfFirst2); |
| 312 EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, "hi"))); | 325 EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, CharPtr("hi")))); |
| 313 | 326 |
| 314 Action<int(int, int, bool, int*)> a2 = | 327 Action<int(int, int, bool, int*)> a2 = |
| 315 Invoke(SumOfFirst2); | 328 Invoke(SumOfFirst2); |
| 316 EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL)))); | 329 EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL)))); |
| 317 } | 330 } |
| 318 | 331 |
| 319 // Tests using Invoke() with methods with parameters declared as Unused. | 332 // Tests using Invoke() with methods with parameters declared as Unused. |
| 320 TEST(InvokeTest, MethodWithUnusedParameters) { | 333 TEST(InvokeTest, MethodWithUnusedParameters) { |
| 321 Foo foo; | 334 Foo foo; |
| 322 Action<int(string, bool, int, int)> a1 = | 335 Action<int(string, bool, int, int)> a1 = |
| 323 Invoke(&foo, &Foo::SumOfLast2); | 336 Invoke(&foo, &Foo::SumOfLast2); |
| 324 EXPECT_EQ(12, a1.Perform(make_tuple("hi", true, 10, 2))); | 337 EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2))); |
| 325 | 338 |
| 326 Action<int(char, double, int, int)> a2 = | 339 Action<int(char, double, int, int)> a2 = |
| 327 Invoke(&foo, &Foo::SumOfLast2); | 340 Invoke(&foo, &Foo::SumOfLast2); |
| 328 EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3))); | 341 EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3))); |
| 329 } | 342 } |
| 330 | 343 |
| 331 // Tests using Invoke() with a functor. | 344 // Tests using Invoke() with a functor. |
| 332 TEST(InvokeTest, Functor) { | 345 TEST(InvokeTest, Functor) { |
| 333 Action<int(short, char)> a = Invoke(plus<short>()); // NOLINT | 346 Action<int(short, char)> a = Invoke(plus<short>()); // NOLINT |
| 334 EXPECT_EQ(3, a.Perform(make_tuple(1, 2))); | 347 EXPECT_EQ(3, a.Perform(make_tuple(1, 2))); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); | 406 EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); |
| 394 } | 407 } |
| 395 | 408 |
| 396 // Tests using Invoke() with a 7-argument method. | 409 // Tests using Invoke() with a 7-argument method. |
| 397 TEST(InvokeMethodTest, MethodThatTakes7Arguments) { | 410 TEST(InvokeMethodTest, MethodThatTakes7Arguments) { |
| 398 Foo foo; | 411 Foo foo; |
| 399 Action<string(const char*, const char*, const char*, const char*, | 412 Action<string(const char*, const char*, const char*, const char*, |
| 400 const char*, const char*, const char*)> a = | 413 const char*, const char*, const char*)> a = |
| 401 Invoke(&foo, &Foo::Concat7); | 414 Invoke(&foo, &Foo::Concat7); |
| 402 EXPECT_EQ("1234567", | 415 EXPECT_EQ("1234567", |
| 403 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7"))); | 416 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 417 CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 418 CharPtr("7")))); |
| 404 } | 419 } |
| 405 | 420 |
| 406 // Tests using Invoke() with a 8-argument method. | 421 // Tests using Invoke() with a 8-argument method. |
| 407 TEST(InvokeMethodTest, MethodThatTakes8Arguments) { | 422 TEST(InvokeMethodTest, MethodThatTakes8Arguments) { |
| 408 Foo foo; | 423 Foo foo; |
| 409 Action<string(const char*, const char*, const char*, const char*, | 424 Action<string(const char*, const char*, const char*, const char*, |
| 410 const char*, const char*, const char*, const char*)> a = | 425 const char*, const char*, const char*, const char*)> a = |
| 411 Invoke(&foo, &Foo::Concat8); | 426 Invoke(&foo, &Foo::Concat8); |
| 412 EXPECT_EQ("12345678", | 427 EXPECT_EQ("12345678", |
| 413 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8"))); | 428 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 429 CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 430 CharPtr("7"), CharPtr("8")))); |
| 414 } | 431 } |
| 415 | 432 |
| 416 // Tests using Invoke() with a 9-argument method. | 433 // Tests using Invoke() with a 9-argument method. |
| 417 TEST(InvokeMethodTest, MethodThatTakes9Arguments) { | 434 TEST(InvokeMethodTest, MethodThatTakes9Arguments) { |
| 418 Foo foo; | 435 Foo foo; |
| 419 Action<string(const char*, const char*, const char*, const char*, | 436 Action<string(const char*, const char*, const char*, const char*, |
| 420 const char*, const char*, const char*, const char*, | 437 const char*, const char*, const char*, const char*, |
| 421 const char*)> a = Invoke(&foo, &Foo::Concat9); | 438 const char*)> a = Invoke(&foo, &Foo::Concat9); |
| 422 EXPECT_EQ("123456789", | 439 EXPECT_EQ("123456789", |
| 423 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8", "9"))); | 440 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 441 CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 442 CharPtr("7"), CharPtr("8"), CharPtr("9")))); |
| 424 } | 443 } |
| 425 | 444 |
| 426 // Tests using Invoke() with a 10-argument method. | 445 // Tests using Invoke() with a 10-argument method. |
| 427 TEST(InvokeMethodTest, MethodThatTakes10Arguments) { | 446 TEST(InvokeMethodTest, MethodThatTakes10Arguments) { |
| 428 Foo foo; | 447 Foo foo; |
| 429 Action<string(const char*, const char*, const char*, const char*, | 448 Action<string(const char*, const char*, const char*, const char*, |
| 430 const char*, const char*, const char*, const char*, | 449 const char*, const char*, const char*, const char*, |
| 431 const char*, const char*)> a = Invoke(&foo, &Foo::Concat10); | 450 const char*, const char*)> a = Invoke(&foo, &Foo::Concat10); |
| 432 EXPECT_EQ("1234567890", a.Perform(make_tuple("1", "2", "3", "4", "5", "6", | 451 EXPECT_EQ("1234567890", |
| 433 "7", "8", "9", "0"))); | 452 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 453 CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 454 CharPtr("7"), CharPtr("8"), CharPtr("9"), |
| 455 CharPtr("0")))); |
| 434 } | 456 } |
| 435 | 457 |
| 436 // Tests using Invoke(f) as an action of a compatible type. | 458 // Tests using Invoke(f) as an action of a compatible type. |
| 437 TEST(InvokeMethodTest, MethodWithCompatibleType) { | 459 TEST(InvokeMethodTest, MethodWithCompatibleType) { |
| 438 Foo foo; | 460 Foo foo; |
| 439 Action<long(int, short, char, bool)> a = // NOLINT | 461 Action<long(int, short, char, bool)> a = // NOLINT |
| 440 Invoke(&foo, &Foo::SumOf4); | 462 Invoke(&foo, &Foo::SumOf4); |
| 441 EXPECT_EQ(4444, a.Perform(make_tuple(4000, 300, 20, true))); | 463 EXPECT_EQ(4444, a.Perform(make_tuple(4000, 300, 20, true))); |
| 442 } | 464 } |
| 443 | 465 |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 EXPECT_TRUE(a.Perform(make_tuple(1.5, -1))); | 680 EXPECT_TRUE(a.Perform(make_tuple(1.5, -1))); |
| 659 EXPECT_FALSE(a.Perform(make_tuple(1.5, 1))); | 681 EXPECT_FALSE(a.Perform(make_tuple(1.5, 1))); |
| 660 | 682 |
| 661 } | 683 } |
| 662 | 684 |
| 663 // Tests using WithArgs with an action that takes 2 arguments. | 685 // Tests using WithArgs with an action that takes 2 arguments. |
| 664 TEST(WithArgsTest, TwoArgs) { | 686 TEST(WithArgsTest, TwoArgs) { |
| 665 Action<const char*(const char* s, double x, int n)> a = | 687 Action<const char*(const char* s, double x, int n)> a = |
| 666 WithArgs<0, 2>(Invoke(Binary)); | 688 WithArgs<0, 2>(Invoke(Binary)); |
| 667 const char s[] = "Hello"; | 689 const char s[] = "Hello"; |
| 668 EXPECT_EQ(s + 2, a.Perform(make_tuple(s, 0.5, 2))); | 690 EXPECT_EQ(s + 2, a.Perform(make_tuple(CharPtr(s), 0.5, 2))); |
| 669 } | 691 } |
| 670 | 692 |
| 671 // Tests using WithArgs with an action that takes 3 arguments. | 693 // Tests using WithArgs with an action that takes 3 arguments. |
| 672 TEST(WithArgsTest, ThreeArgs) { | 694 TEST(WithArgsTest, ThreeArgs) { |
| 673 Action<int(int, double, char, short)> a = // NOLINT | 695 Action<int(int, double, char, short)> a = // NOLINT |
| 674 WithArgs<0, 2, 3>(Invoke(Ternary)); | 696 WithArgs<0, 2, 3>(Invoke(Ternary)); |
| 675 EXPECT_EQ(123, a.Perform(make_tuple(100, 6.5, 20, 3))); | 697 EXPECT_EQ(123, a.Perform(make_tuple(100, 6.5, 20, 3))); |
| 676 } | 698 } |
| 677 | 699 |
| 678 // Tests using WithArgs with an action that takes 4 arguments. | 700 // Tests using WithArgs with an action that takes 4 arguments. |
| 679 TEST(WithArgsTest, FourArgs) { | 701 TEST(WithArgsTest, FourArgs) { |
| 680 Action<string(const char*, const char*, double, const char*, const char*)> a = | 702 Action<string(const char*, const char*, double, const char*, const char*)> a = |
| 681 WithArgs<4, 3, 1, 0>(Invoke(Concat4)); | 703 WithArgs<4, 3, 1, 0>(Invoke(Concat4)); |
| 682 EXPECT_EQ("4310", a.Perform(make_tuple("0", "1", 2.5, "3", "4"))); | 704 EXPECT_EQ("4310", a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), 2.5, |
| 705 CharPtr("3"), CharPtr("4")))); |
| 683 } | 706 } |
| 684 | 707 |
| 685 // Tests using WithArgs with an action that takes 5 arguments. | 708 // Tests using WithArgs with an action that takes 5 arguments. |
| 686 TEST(WithArgsTest, FiveArgs) { | 709 TEST(WithArgsTest, FiveArgs) { |
| 687 Action<string(const char*, const char*, const char*, | 710 Action<string(const char*, const char*, const char*, |
| 688 const char*, const char*)> a = | 711 const char*, const char*)> a = |
| 689 WithArgs<4, 3, 2, 1, 0>(Invoke(Concat5)); | 712 WithArgs<4, 3, 2, 1, 0>(Invoke(Concat5)); |
| 690 EXPECT_EQ("43210", a.Perform(make_tuple("0", "1", "2", "3", "4"))); | 713 EXPECT_EQ("43210", |
| 714 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), |
| 715 CharPtr("3"), CharPtr("4")))); |
| 691 } | 716 } |
| 692 | 717 |
| 693 // Tests using WithArgs with an action that takes 6 arguments. | 718 // Tests using WithArgs with an action that takes 6 arguments. |
| 694 TEST(WithArgsTest, SixArgs) { | 719 TEST(WithArgsTest, SixArgs) { |
| 695 Action<string(const char*, const char*, const char*)> a = | 720 Action<string(const char*, const char*, const char*)> a = |
| 696 WithArgs<0, 1, 2, 2, 1, 0>(Invoke(Concat6)); | 721 WithArgs<0, 1, 2, 2, 1, 0>(Invoke(Concat6)); |
| 697 EXPECT_EQ("012210", a.Perform(make_tuple("0", "1", "2"))); | 722 EXPECT_EQ("012210", |
| 723 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2")))); |
| 698 } | 724 } |
| 699 | 725 |
| 700 // Tests using WithArgs with an action that takes 7 arguments. | 726 // Tests using WithArgs with an action that takes 7 arguments. |
| 701 TEST(WithArgsTest, SevenArgs) { | 727 TEST(WithArgsTest, SevenArgs) { |
| 702 Action<string(const char*, const char*, const char*, const char*)> a = | 728 Action<string(const char*, const char*, const char*, const char*)> a = |
| 703 WithArgs<0, 1, 2, 3, 2, 1, 0>(Invoke(Concat7)); | 729 WithArgs<0, 1, 2, 3, 2, 1, 0>(Invoke(Concat7)); |
| 704 EXPECT_EQ("0123210", a.Perform(make_tuple("0", "1", "2", "3"))); | 730 EXPECT_EQ("0123210", |
| 731 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), |
| 732 CharPtr("3")))); |
| 705 } | 733 } |
| 706 | 734 |
| 707 // Tests using WithArgs with an action that takes 8 arguments. | 735 // Tests using WithArgs with an action that takes 8 arguments. |
| 708 TEST(WithArgsTest, EightArgs) { | 736 TEST(WithArgsTest, EightArgs) { |
| 709 Action<string(const char*, const char*, const char*, const char*)> a = | 737 Action<string(const char*, const char*, const char*, const char*)> a = |
| 710 WithArgs<0, 1, 2, 3, 0, 1, 2, 3>(Invoke(Concat8)); | 738 WithArgs<0, 1, 2, 3, 0, 1, 2, 3>(Invoke(Concat8)); |
| 711 EXPECT_EQ("01230123", a.Perform(make_tuple("0", "1", "2", "3"))); | 739 EXPECT_EQ("01230123", |
| 740 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), |
| 741 CharPtr("3")))); |
| 712 } | 742 } |
| 713 | 743 |
| 714 // Tests using WithArgs with an action that takes 9 arguments. | 744 // Tests using WithArgs with an action that takes 9 arguments. |
| 715 TEST(WithArgsTest, NineArgs) { | 745 TEST(WithArgsTest, NineArgs) { |
| 716 Action<string(const char*, const char*, const char*, const char*)> a = | 746 Action<string(const char*, const char*, const char*, const char*)> a = |
| 717 WithArgs<0, 1, 2, 3, 1, 2, 3, 2, 3>(Invoke(Concat9)); | 747 WithArgs<0, 1, 2, 3, 1, 2, 3, 2, 3>(Invoke(Concat9)); |
| 718 EXPECT_EQ("012312323", a.Perform(make_tuple("0", "1", "2", "3"))); | 748 EXPECT_EQ("012312323", |
| 749 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), |
| 750 CharPtr("3")))); |
| 719 } | 751 } |
| 720 | 752 |
| 721 // Tests using WithArgs with an action that takes 10 arguments. | 753 // Tests using WithArgs with an action that takes 10 arguments. |
| 722 TEST(WithArgsTest, TenArgs) { | 754 TEST(WithArgsTest, TenArgs) { |
| 723 Action<string(const char*, const char*, const char*, const char*)> a = | 755 Action<string(const char*, const char*, const char*, const char*)> a = |
| 724 WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(Concat10)); | 756 WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(Concat10)); |
| 725 EXPECT_EQ("0123210123", a.Perform(make_tuple("0", "1", "2", "3"))); | 757 EXPECT_EQ("0123210123", |
| 758 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), |
| 759 CharPtr("3")))); |
| 726 } | 760 } |
| 727 | 761 |
| 728 // Tests using WithArgs with an action that is not Invoke(). | 762 // Tests using WithArgs with an action that is not Invoke(). |
| 729 class SubstractAction : public ActionInterface<int(int, int)> { // NOLINT | 763 class SubstractAction : public ActionInterface<int(int, int)> { // NOLINT |
| 730 public: | 764 public: |
| 731 virtual int Perform(const tuple<int, int>& args) { | 765 virtual int Perform(const tuple<int, int>& args) { |
| 732 return get<0>(args) - get<1>(args); | 766 return get<0>(args) - get<1>(args); |
| 733 } | 767 } |
| 734 }; | 768 }; |
| 735 | 769 |
| 736 TEST(WithArgsTest, NonInvokeAction) { | 770 TEST(WithArgsTest, NonInvokeAction) { |
| 737 Action<int(const string&, int, int)> a = // NOLINT | 771 Action<int(const string&, int, int)> a = // NOLINT |
| 738 WithArgs<2, 1>(MakeAction(new SubstractAction)); | 772 WithArgs<2, 1>(MakeAction(new SubstractAction)); |
| 739 EXPECT_EQ(8, a.Perform(make_tuple("hi", 2, 10))); | 773 EXPECT_EQ(8, a.Perform(make_tuple(CharPtr("hi"), 2, 10))); |
| 740 } | 774 } |
| 741 | 775 |
| 742 // Tests using WithArgs to pass all original arguments in the original order. | 776 // Tests using WithArgs to pass all original arguments in the original order. |
| 743 TEST(WithArgsTest, Identity) { | 777 TEST(WithArgsTest, Identity) { |
| 744 Action<int(int x, char y, short z)> a = // NOLINT | 778 Action<int(int x, char y, short z)> a = // NOLINT |
| 745 WithArgs<0, 1, 2>(Invoke(Ternary)); | 779 WithArgs<0, 1, 2>(Invoke(Ternary)); |
| 746 EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 3))); | 780 EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 3))); |
| 747 } | 781 } |
| 748 | 782 |
| 749 // Tests using WithArgs with repeated arguments. | 783 // Tests using WithArgs with repeated arguments. |
| 750 TEST(WithArgsTest, RepeatedArguments) { | 784 TEST(WithArgsTest, RepeatedArguments) { |
| 751 Action<int(bool, int m, int n)> a = // NOLINT | 785 Action<int(bool, int m, int n)> a = // NOLINT |
| 752 WithArgs<1, 1, 1, 1>(Invoke(SumOf4)); | 786 WithArgs<1, 1, 1, 1>(Invoke(SumOf4)); |
| 753 EXPECT_EQ(4, a.Perform(make_tuple(false, 1, 10))); | 787 EXPECT_EQ(4, a.Perform(make_tuple(false, 1, 10))); |
| 754 } | 788 } |
| 755 | 789 |
| 756 // Tests using WithArgs with reversed argument order. | 790 // Tests using WithArgs with reversed argument order. |
| 757 TEST(WithArgsTest, ReversedArgumentOrder) { | 791 TEST(WithArgsTest, ReversedArgumentOrder) { |
| 758 Action<const char*(short n, const char* input)> a = // NOLINT | 792 Action<const char*(short n, const char* input)> a = // NOLINT |
| 759 WithArgs<1, 0>(Invoke(Binary)); | 793 WithArgs<1, 0>(Invoke(Binary)); |
| 760 const char s[] = "Hello"; | 794 const char s[] = "Hello"; |
| 761 EXPECT_EQ(s + 2, a.Perform(make_tuple(2, s))); | 795 EXPECT_EQ(s + 2, a.Perform(make_tuple(2, CharPtr(s)))); |
| 762 } | 796 } |
| 763 | 797 |
| 764 // Tests using WithArgs with compatible, but not identical, argument types. | 798 // Tests using WithArgs with compatible, but not identical, argument types. |
| 765 TEST(WithArgsTest, ArgsOfCompatibleTypes) { | 799 TEST(WithArgsTest, ArgsOfCompatibleTypes) { |
| 766 Action<long(short x, int y, double z, char c)> a = // NOLINT | 800 Action<long(short x, int y, double z, char c)> a = // NOLINT |
| 767 WithArgs<0, 1, 3>(Invoke(Ternary)); | 801 WithArgs<0, 1, 3>(Invoke(Ternary)); |
| 768 EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 5.6, 3))); | 802 EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 5.6, 3))); |
| 769 } | 803 } |
| 770 | 804 |
| 771 // Tests using WithArgs with an action that returns void. | 805 // Tests using WithArgs with an action that returns void. |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1116 } | 1150 } |
| 1117 | 1151 |
| 1118 ACTION_P2(OverloadedAction, true_value, false_value) { | 1152 ACTION_P2(OverloadedAction, true_value, false_value) { |
| 1119 return arg0 ? true_value : false_value; | 1153 return arg0 ? true_value : false_value; |
| 1120 } | 1154 } |
| 1121 | 1155 |
| 1122 TEST(ActionMacroTest, CanDefineOverloadedActions) { | 1156 TEST(ActionMacroTest, CanDefineOverloadedActions) { |
| 1123 typedef Action<const char*(bool, const char*)> MyAction; | 1157 typedef Action<const char*(bool, const char*)> MyAction; |
| 1124 | 1158 |
| 1125 const MyAction a1 = OverloadedAction(); | 1159 const MyAction a1 = OverloadedAction(); |
| 1126 EXPECT_STREQ("hello", a1.Perform(make_tuple(false, "world"))); | 1160 EXPECT_STREQ("hello", a1.Perform(make_tuple(false, CharPtr("world")))); |
| 1127 EXPECT_STREQ("world", a1.Perform(make_tuple(true, "world"))); | 1161 EXPECT_STREQ("world", a1.Perform(make_tuple(true, CharPtr("world")))); |
| 1128 | 1162 |
| 1129 const MyAction a2 = OverloadedAction("hi"); | 1163 const MyAction a2 = OverloadedAction("hi"); |
| 1130 EXPECT_STREQ("hi", a2.Perform(make_tuple(false, "world"))); | 1164 EXPECT_STREQ("hi", a2.Perform(make_tuple(false, CharPtr("world")))); |
| 1131 EXPECT_STREQ("world", a2.Perform(make_tuple(true, "world"))); | 1165 EXPECT_STREQ("world", a2.Perform(make_tuple(true, CharPtr("world")))); |
| 1132 | 1166 |
| 1133 const MyAction a3 = OverloadedAction("hi", "you"); | 1167 const MyAction a3 = OverloadedAction("hi", "you"); |
| 1134 EXPECT_STREQ("hi", a3.Perform(make_tuple(true, "world"))); | 1168 EXPECT_STREQ("hi", a3.Perform(make_tuple(true, CharPtr("world")))); |
| 1135 EXPECT_STREQ("you", a3.Perform(make_tuple(false, "world"))); | 1169 EXPECT_STREQ("you", a3.Perform(make_tuple(false, CharPtr("world")))); |
| 1136 } | 1170 } |
| 1137 | 1171 |
| 1138 // Tests ACTION_Pn where n >= 3. | 1172 // Tests ACTION_Pn where n >= 3. |
| 1139 | 1173 |
| 1140 ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; } | 1174 ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; } |
| 1141 | 1175 |
| 1142 TEST(ActionPnMacroTest, WorksFor3Parameters) { | 1176 TEST(ActionPnMacroTest, WorksFor3Parameters) { |
| 1143 Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4); | 1177 Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4); |
| 1144 EXPECT_DOUBLE_EQ(3123.4, a1.Perform(make_tuple(3000, true))); | 1178 EXPECT_DOUBLE_EQ(3123.4, a1.Perform(make_tuple(3000, true))); |
| 1145 | 1179 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1217 std::string prefix_str(prefix); | 1251 std::string prefix_str(prefix); |
| 1218 char suffix_char(suffix); | 1252 char suffix_char(suffix); |
| 1219 return prefix_str + arg0 + suffix_char; | 1253 return prefix_str + arg0 + suffix_char; |
| 1220 } | 1254 } |
| 1221 | 1255 |
| 1222 TEST(ActionPnMacroTest, SimpleTypePromotion) { | 1256 TEST(ActionPnMacroTest, SimpleTypePromotion) { |
| 1223 Action<std::string(const char*)> no_promo = | 1257 Action<std::string(const char*)> no_promo = |
| 1224 PadArgument(std::string("foo"), 'r'); | 1258 PadArgument(std::string("foo"), 'r'); |
| 1225 Action<std::string(const char*)> promo = | 1259 Action<std::string(const char*)> promo = |
| 1226 PadArgument("foo", static_cast<int>('r')); | 1260 PadArgument("foo", static_cast<int>('r')); |
| 1227 EXPECT_EQ("foobar", no_promo.Perform(make_tuple("ba"))); | 1261 EXPECT_EQ("foobar", no_promo.Perform(make_tuple(CharPtr("ba")))); |
| 1228 EXPECT_EQ("foobar", promo.Perform(make_tuple("ba"))); | 1262 EXPECT_EQ("foobar", promo.Perform(make_tuple(CharPtr("ba")))); |
| 1229 } | 1263 } |
| 1230 | 1264 |
| 1231 // Tests that we can partially restrict parameter types using a | 1265 // Tests that we can partially restrict parameter types using a |
| 1232 // straight-forward pattern. | 1266 // straight-forward pattern. |
| 1233 | 1267 |
| 1234 // Defines a generic action that doesn't restrict the types of its | 1268 // Defines a generic action that doesn't restrict the types of its |
| 1235 // parameters. | 1269 // parameters. |
| 1236 ACTION_P3(ConcatImpl, a, b, c) { | 1270 ACTION_P3(ConcatImpl, a, b, c) { |
| 1237 std::stringstream ss; | 1271 std::stringstream ss; |
| 1238 ss << a << b << c; | 1272 ss << a << b << c; |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1463 a1.Perform(make_tuple(t)); | 1497 a1.Perform(make_tuple(t)); |
| 1464 EXPECT_TRUE(is_deleted); | 1498 EXPECT_TRUE(is_deleted); |
| 1465 } | 1499 } |
| 1466 | 1500 |
| 1467 TEST(DeleteArgActionTest, TenArgs) { | 1501 TEST(DeleteArgActionTest, TenArgs) { |
| 1468 bool is_deleted = false; | 1502 bool is_deleted = false; |
| 1469 DeletionTester* t = new DeletionTester(&is_deleted); | 1503 DeletionTester* t = new DeletionTester(&is_deleted); |
| 1470 const Action<void(bool, int, int, const char*, bool, | 1504 const Action<void(bool, int, int, const char*, bool, |
| 1471 int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>(); | 1505 int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>(); |
| 1472 EXPECT_FALSE(is_deleted); | 1506 EXPECT_FALSE(is_deleted); |
| 1473 a1.Perform(make_tuple(true, 5, 6, "hi", false, 7, 8, 9, 10, t)); | 1507 a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t)); |
| 1474 EXPECT_TRUE(is_deleted); | 1508 EXPECT_TRUE(is_deleted); |
| 1475 } | 1509 } |
| 1476 | 1510 |
| 1477 #if GTEST_HAS_EXCEPTIONS | 1511 #if GTEST_HAS_EXCEPTIONS |
| 1478 | 1512 |
| 1479 TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) { | 1513 TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) { |
| 1480 const Action<void(int n)> a = Throw('a'); | 1514 const Action<void(int n)> a = Throw('a'); |
| 1481 EXPECT_THROW(a.Perform(make_tuple(0)), char); | 1515 EXPECT_THROW(a.Perform(make_tuple(0)), char); |
| 1482 } | 1516 } |
| 1483 | 1517 |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1642 const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5); | 1676 const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5); |
| 1643 EXPECT_EQ(0, a0.Perform(make_tuple())); | 1677 EXPECT_EQ(0, a0.Perform(make_tuple())); |
| 1644 EXPECT_EQ(1, a1.Perform(make_tuple())); | 1678 EXPECT_EQ(1, a1.Perform(make_tuple())); |
| 1645 EXPECT_EQ(3, a2.Perform(make_tuple())); | 1679 EXPECT_EQ(3, a2.Perform(make_tuple())); |
| 1646 EXPECT_EQ(6, a3.Perform(make_tuple())); | 1680 EXPECT_EQ(6, a3.Perform(make_tuple())); |
| 1647 EXPECT_EQ(12345, a4.Perform(make_tuple())); | 1681 EXPECT_EQ(12345, a4.Perform(make_tuple())); |
| 1648 } | 1682 } |
| 1649 | 1683 |
| 1650 } // namespace gmock_generated_actions_test | 1684 } // namespace gmock_generated_actions_test |
| 1651 } // namespace testing | 1685 } // namespace testing |
| OLD | NEW |