compiling_tests_11.cpp (131080B)
1 /* 2 * Trompeloeil C++ mocking framework 3 * 4 * Copyright Björn Fahller 2014-2019 5 * Copyright (C) 2017 Andrew Paxie 6 * 7 * Use, modification and distribution is subject to the 8 * Boost Software License, Version 1.0. (See accompanying 9 * file LICENSE_1_0.txt or copy at 10 * http://www.boost.org/LICENSE_1_0.txt) 11 * 12 * Project home: https://github.com/rollbear/trompeloeil 13 */ 14 15 #include "compiling_tests.hpp" 16 17 #include <catch.hpp> 18 19 #include <algorithm> 20 #include <cstddef> 21 #include <iostream> 22 #include <map> 23 #include <memory> 24 #include <regex> 25 #include <string> 26 #include <type_traits> 27 #include <utility> 28 #include <vector> 29 30 31 /* 32 * Used in combination with escape_parens() (below) to work around 33 * macro expansion occurring before stringizing in C++11 expectation 34 * macros, which results in different matching requirements for 35 * messages reported from the Trompeloeil runtime. 36 */ 37 #define CXX11_AS_STRING_IMPL(x) #x 38 #define CXX11_AS_STRING(x) CXX11_AS_STRING_IMPL(x) 39 40 // Exercise C++11/14 Trompeloeil interface 41 42 using trompeloeil::_; 43 44 namespace 45 { 46 /* These types are local to this file as their definition depends on 47 * macros whose names are particular to each API version. 48 */ 49 50 template <typename T> 51 class tmock 52 { 53 public: 54 MAKE_MOCK1(func, void(int)); 55 MAKE_MOCK1(tfunc, void(T)); 56 57 tmock() : m(NAMED_FORBID_CALL_V(*this, func(_))) {} 58 59 private: 60 std::unique_ptr<trompeloeil::expectation> m; 61 }; 62 63 // multiple inheritance 64 65 struct combined 66 : mock_c 67 , tmock<int> 68 { 69 }; 70 71 class self_ref_mock 72 { 73 public: 74 void expect_self() 75 { 76 exp = NAMED_REQUIRE_CALL_V(*this, mfunc()); 77 } 78 MAKE_MOCK0(mfunc, void()); 79 std::unique_ptr<trompeloeil::expectation> exp; 80 }; 81 82 #define MANY_REQS(obj) \ 83 REQUIRE_CALL_V(obj, f0()); \ 84 REQUIRE_CALL_V(obj, f1(0)); \ 85 REQUIRE_CALL_V(obj, f2(0,1)) 86 87 std::string escape_parens(const std::string& s) 88 { 89 constexpr auto backslash = '\\'; 90 91 std::string tmp; 92 93 for (auto& c : s) 94 { 95 if (c == '(' || c == ')') 96 { 97 tmp += backslash; 98 } 99 100 tmp += c; 101 } 102 103 return tmp; 104 } 105 106 #if TROMPELOEIL_TEST_REGEX_FAILURES 107 108 bool is_match(const std::string& msg, const std::string& re) 109 { 110 return std::regex_search(msg, std::regex(re)); 111 } 112 113 #else /* TROMPELOEIL_TEST_REGEX_FAILURES */ 114 115 /* 116 * g++-4.8 -std=c++11: If std::regex_search is used, 117 * the test cases fail with exception std::regex_error instead of reporting. 118 * In this case replace a regex match with less precise string match. 119 */ 120 bool is_match(const std::string& msg, const std::string& re) 121 { 122 return msg.c_str() && re.c_str(); // true. Enhancement: match by string. 123 } 124 125 #endif /* !TROMPELOEIL_TEST_REGEX_FAILURES */ 126 127 } /* unnamed namespace */ 128 129 // mock_interface<> tests 130 131 TEST_CASE_METHOD( 132 Fixture, 133 "C++11: mock from interface is callable like any other", 134 "[C++11][mock_interface]") 135 { 136 mi imock; 137 REQUIRE_CALL_V(imock, func(3), 138 .RETURN(4)); 139 REQUIRE_CALL_V(imock, cfunc(3), 140 .RETURN(5)); 141 REQUIRE_CALL_V(imock, func3(1,2,"three"), 142 .RETURN(6)); 143 const mi& cimock = imock; 144 REQUIRE_CALL_V(cimock, func3(2,3,"four"), 145 .RETURN(7)); 146 REQUIRE(imock.func(3) == 4); 147 REQUIRE(imock.cfunc(3) == 5); 148 REQUIRE(imock.func3(1,2,"three") == 6); 149 REQUIRE(cimock.func3(2,3,"four") == 7); 150 151 } 152 153 // IN_SEQUENCE tests 154 155 TEST_CASE_METHOD( 156 Fixture, 157 "C++11: follow single sequence gives no reports", 158 "[C++11][C++14][sequences]") 159 { 160 { // Compile-time tests 161 static_assert(std::is_default_constructible<trompeloeil::sequence>::value, "Should be default constructible"); 162 static_assert(std::is_nothrow_move_constructible<trompeloeil::sequence>::value, "Should be move constructible"); 163 static_assert(std::is_nothrow_move_assignable<trompeloeil::sequence>::value, "Should be move assignable"); 164 static_assert(!std::is_copy_constructible<trompeloeil::sequence>::value, "Should NOT be copy constructible"); 165 static_assert(!std::is_copy_assignable<trompeloeil::sequence>::value, "Should NOT be copy assignable"); 166 } 167 168 { 169 mock_c obj1(1), obj2("apa"); 170 171 auto seq = trompeloeil::sequence{}; // Use "almost always auto" style 172 173 REQUIRE_CALL_V(obj1, count(), 174 .IN_SEQUENCE(seq) 175 .RETURN(1)); 176 177 REQUIRE_CALL_V(obj2, func(_, _), 178 .IN_SEQUENCE(seq)); 179 180 REQUIRE_CALL_V(obj2, count(), 181 .IN_SEQUENCE(seq) 182 .RETURN(3)); 183 184 std::string str = "apa"; 185 obj1.count(); 186 obj2.func(3, str); 187 obj2.count(); 188 } 189 REQUIRE(reports.empty()); 190 } 191 192 TEST_CASE_METHOD( 193 Fixture, 194 "C++11: correct sequence on moved seq object is not reported", 195 "[C++11][C++14][sequences]") 196 { 197 { 198 mock_c obj; 199 auto seq1 = trompeloeil::sequence{}; 200 201 REQUIRE_CALL_V(obj, count(), 202 .IN_SEQUENCE(seq1) 203 .RETURN(1)); 204 205 auto seq2 = std::move(seq1); 206 207 REQUIRE_CALL_V(obj, func(_,_), 208 .IN_SEQUENCE(seq2)); 209 210 obj.count(); 211 std::string foo = "foo"; 212 obj.func(3, foo); 213 } 214 REQUIRE(reports.empty()); 215 } 216 217 TEST_CASE_METHOD( 218 Fixture, 219 "C++11: incorrect sequence on moved seq object is reported", 220 "[C++11][C++14][sequences]") 221 { 222 try 223 { 224 mock_c obj; 225 auto seq1 = trompeloeil::sequence{}; 226 227 REQUIRE_CALL_V(obj, count(), 228 .IN_SEQUENCE(seq1) 229 .RETURN(1)); 230 231 auto seq2 = std::move(seq1); 232 233 REQUIRE_CALL_V(obj, func(_,_), 234 .IN_SEQUENCE(seq2)); 235 236 std::string foo = "foo"; 237 obj.func(3, foo); 238 FAIL("did not report"); 239 } 240 catch (reported) 241 { 242 auto re = R":(Sequence mismatch.*\"seq2\".*matching.*obj.func\(_,_\).*has obj.count\(\) at.*first):"; 243 auto& msg = reports.front().msg; 244 INFO("msg=" << msg); 245 REQUIRE(is_match(msg, re)); 246 } 247 } 248 249 TEST_CASE_METHOD( 250 Fixture, 251 "C++11: join two sequences gives no report", 252 "[C++11][C++14][sequences]") 253 { 254 { 255 mock_c obj1, obj2; 256 257 trompeloeil::sequence seq1, seq2; 258 259 REQUIRE_CALL_V(obj1, count(), 260 .IN_SEQUENCE(seq1) 261 .RETURN(1)); 262 263 REQUIRE_CALL_V(obj2, func(_, _), 264 .IN_SEQUENCE(seq2)); 265 266 REQUIRE_CALL_V(obj2, count(), 267 .IN_SEQUENCE(seq2, seq1) 268 .RETURN(3)); 269 270 std::string str = "apa"; 271 obj2.func(3, str); 272 obj1.count(); 273 obj2.count(); 274 } 275 REQUIRE(reports.empty()); 276 } 277 278 TEST_CASE_METHOD( 279 Fixture, 280 "C++11: violating single sequence reports first violation as fatal", 281 "[C++11][C++14][sequences]") 282 { 283 try { 284 mock_c obj1, obj2; 285 286 trompeloeil::sequence seq; 287 288 REQUIRE_CALL_V(obj1, count(), 289 .IN_SEQUENCE(seq) 290 .RETURN(1)); 291 292 REQUIRE_CALL_V(obj2, func(_,_), 293 .IN_SEQUENCE(seq)); 294 295 REQUIRE_CALL_V(obj2, count(), 296 .IN_SEQUENCE(seq) 297 .RETURN(3)); 298 299 std::string str = "apa"; 300 obj1.count(); 301 obj2.count(); 302 obj2.func(3, str); 303 FAIL("didn't throw!"); 304 } 305 catch (reported) 306 { 307 REQUIRE(!reports.empty()); 308 auto re = R":(Sequence mismatch.*\"seq\".*matching.*obj2.count\(\).*has obj2\.func\(_,_\) at.*first):"; 309 REQUIRE(is_match(reports.front().msg, re)); 310 } 311 } 312 313 TEST_CASE_METHOD( 314 Fixture, 315 "C++11: violating parallel sequences reports first violation as fatal", 316 "[C++11][C++14][sequences]") 317 { 318 try { 319 mock_c obj1, obj2; 320 321 trompeloeil::sequence seq1, seq2; 322 323 REQUIRE_CALL_V(obj1, count(), 324 .IN_SEQUENCE(seq1) 325 .RETURN(1)); 326 327 REQUIRE_CALL_V(obj2, func(_, _), 328 .IN_SEQUENCE(seq2, seq1)); 329 330 REQUIRE_CALL_V(obj1, count(), 331 .IN_SEQUENCE(seq2) 332 .RETURN(3)); 333 334 REQUIRE_CALL_V(obj2, count(), 335 .IN_SEQUENCE(seq2) 336 .RETURN(3)); 337 338 std::string str = "apa"; 339 obj1.count(); 340 obj2.func(3, str); 341 obj2.count(); 342 FAIL("didn't throw!"); 343 } 344 catch (reported) 345 { 346 REQUIRE(!reports.empty()); 347 auto re = R":(Sequence mismatch.*seq2.*of obj2\.count\(\).*has obj1.count\(\).*first):"; 348 REQUIRE(is_match(reports.front().msg, re)); 349 } 350 } 351 352 TEST_CASE_METHOD( 353 Fixture, 354 "C++11: a sequence retires after min calls", 355 "[C++11][C++14][sequences]") 356 { 357 { 358 int count = 0; 359 360 mock_c obj1; 361 trompeloeil::sequence seq1; 362 363 REQUIRE_CALL_V(obj1, count(), 364 .IN_SEQUENCE(seq1) 365 .TIMES(AT_LEAST(3)) 366 .RETURN(1)); 367 368 REQUIRE_CALL_V(obj1, func(_, _), 369 .IN_SEQUENCE(seq1)); 370 371 count += obj1.count(); 372 count += obj1.count(); 373 count += obj1.count(); 374 std::string s = "apa"; 375 obj1.func(count, s); 376 } 377 REQUIRE(reports.empty()); 378 } 379 380 TEST_CASE_METHOD( 381 Fixture, 382 "C++11: ALLOW_CALL in sequence may be skipped", 383 "[C++11][C++14][sequences]") 384 { 385 { 386 int count = 0; 387 388 mock_c obj1; 389 trompeloeil::sequence seq1; 390 391 ALLOW_CALL_V(obj1, count(), 392 .IN_SEQUENCE(seq1) 393 .RETURN(1)); 394 395 REQUIRE_CALL_V(obj1, func(_, _), 396 .IN_SEQUENCE(seq1)); 397 398 std::string s = "apa"; 399 obj1.func(count, s); 400 } 401 REQUIRE(reports.empty()); 402 } 403 404 TEST_CASE_METHOD( 405 Fixture, 406 "C++11: ALLOW_CALL in sequence may be called", 407 "[C++11][C++14][sequences]") 408 { 409 { 410 int count = 0; 411 412 mock_c obj1; 413 trompeloeil::sequence seq1; 414 415 ALLOW_CALL_V(obj1, count(), 416 .IN_SEQUENCE(seq1) 417 .RETURN(1)); 418 419 REQUIRE_CALL_V(obj1, func(_, _), 420 .IN_SEQUENCE(seq1)); 421 422 obj1.count(); 423 std::string s = "apa"; 424 obj1.func(count, s); 425 } 426 REQUIRE(reports.empty()); 427 } 428 429 TEST_CASE_METHOD( 430 Fixture, 431 "C++11: ALLOW_CALL sequenced after REQUIRE_CALL is reported if require is not satisfied", 432 "[C++11][C++14][sequences]") 433 { 434 try { 435 mock_c obj; 436 trompeloeil::sequence seq; 437 438 REQUIRE_CALL_V(obj, func(_, _), 439 .IN_SEQUENCE(seq)); 440 441 ALLOW_CALL_V(obj, count(), 442 .IN_SEQUENCE(seq) 443 .RETURN(1)); 444 445 obj.count(); 446 FAIL("did not report out of sequence call"); 447 } 448 catch (reported) 449 { 450 REQUIRE(reports.size() >= 1U); 451 auto re = R":(Sequence mismatch.*seq.*of obj\.count\(\).*has obj\.func\(_, _\).*first):"; 452 INFO("report=" << reports.front().msg); 453 REQUIRE(is_match(reports.front().msg, re)); 454 } 455 } 456 457 TEST_CASE_METHOD( 458 Fixture, 459 "C++11: Several ALLOW_CALL and REQUIRE_CALL can be interleaved in a sequence", 460 "[C++11][C++14][sequences]") 461 { 462 { 463 mock_c obj; 464 trompeloeil::sequence seq; 465 466 REQUIRE_CALL_V(obj, func(1, _), 467 .IN_SEQUENCE(seq)); 468 469 ALLOW_CALL_V(obj, count(), 470 .IN_SEQUENCE(seq) 471 .RETURN(1)); 472 473 REQUIRE_CALL_V(obj, func(2, _), 474 .IN_SEQUENCE(seq)); 475 476 ALLOW_CALL_V(obj, count(), 477 .IN_SEQUENCE(seq) 478 .RETURN(2)); 479 480 std::string foo = "foo"; 481 482 obj.func(1, foo); 483 REQUIRE(obj.count() == 1); 484 REQUIRE(obj.count() == 1); 485 obj.func(2, foo); 486 REQUIRE(obj.count() == 2); 487 REQUIRE(obj.count() == 2); 488 } 489 REQUIRE(reports.empty()); 490 } 491 492 TEST_CASE_METHOD( 493 Fixture, 494 "C++11: Calling 3 sequenced allow call in seq is allowed" 495 "[C++11][C++14][sequences]") 496 { 497 { 498 mock_c obj; 499 trompeloeil::sequence seq; 500 501 ALLOW_CALL_V(obj, getter(1), 502 .IN_SEQUENCE(seq) 503 .RETURN(1)); 504 505 ALLOW_CALL_V(obj, getter(2), 506 .IN_SEQUENCE(seq) 507 .RETURN(2)); 508 509 ALLOW_CALL_V(obj, getter(3), 510 .IN_SEQUENCE(seq) 511 .RETURN(3)); 512 513 REQUIRE(obj.getter(1) == 1); 514 REQUIRE(obj.getter(1) == 1); 515 REQUIRE(obj.getter(2) == 2); 516 REQUIRE(obj.getter(2) == 2); 517 REQUIRE(obj.getter(3) == 3); 518 REQUIRE(obj.getter(3) == 3); 519 } 520 REQUIRE(reports.empty()); 521 } 522 523 TEST_CASE_METHOD( 524 Fixture, 525 "C++11: Calling the 1st and 3rd sequenced allow call in seq is allowed" 526 "[C++11][C++14][sequences]") 527 { 528 { 529 mock_c obj; 530 trompeloeil::sequence seq; 531 532 ALLOW_CALL_V(obj, getter(1), 533 .IN_SEQUENCE(seq) 534 .RETURN(1)); 535 536 ALLOW_CALL_V(obj, getter(2), 537 .IN_SEQUENCE(seq) 538 .RETURN(2)); 539 540 ALLOW_CALL_V(obj, getter(3), 541 .IN_SEQUENCE(seq) 542 .RETURN(3)); 543 544 REQUIRE(obj.getter(1) == 1); 545 REQUIRE(obj.getter(1) == 1); 546 REQUIRE(obj.getter(3) == 3); 547 REQUIRE(obj.getter(3) == 3); 548 } 549 REQUIRE(reports.empty()); 550 } 551 552 TEST_CASE_METHOD( 553 Fixture, 554 "C++11: Calling the 3nd after 1st and 3rd sequenced allow call in seq is illegal", 555 "[C++11][C++14][sequences]") 556 { 557 mock_c obj; 558 trompeloeil::sequence seq; 559 560 ALLOW_CALL_V(obj, getter(1), 561 .IN_SEQUENCE(seq) 562 .RETURN(1)); 563 564 ALLOW_CALL_V(obj, getter(2), 565 .IN_SEQUENCE(seq) 566 .RETURN(2)); 567 568 ALLOW_CALL_V(obj, getter(3), 569 .IN_SEQUENCE(seq) 570 .RETURN(3)); 571 572 REQUIRE(obj.getter(1) == 1); 573 REQUIRE(obj.getter(1) == 1); 574 REQUIRE(obj.getter(3) == 3); 575 REQUIRE(obj.getter(3) == 3); 576 577 try { 578 obj.getter(2); 579 FAIL("didn't throw"); 580 } 581 catch (reported) 582 { 583 auto re = R":(Sequence mismatch.*seq.*of obj\.getter\(2\).*has obj\.getter\(3\).*first):"; 584 INFO("report=" << reports.front().msg); 585 REQUIRE(is_match(reports.front().msg, re)); 586 auto& first = reports.front(); 587 INFO(first.file << ':' << first.line << "\n" << first.msg); 588 } 589 } 590 591 TEST_CASE_METHOD( 592 Fixture, 593 "C++11: Calling the 1nd after 3rd sequenced allow call in seq is illegal", 594 "[C++11][C++14][sequences]") 595 { 596 mock_c obj; 597 trompeloeil::sequence seq; 598 599 ALLOW_CALL_V(obj, getter(1), 600 .IN_SEQUENCE(seq) 601 .RETURN(1)); 602 603 ALLOW_CALL_V(obj, getter(2), 604 .IN_SEQUENCE(seq) 605 .RETURN(2)); 606 607 ALLOW_CALL_V(obj, getter(3), 608 .IN_SEQUENCE(seq) 609 .RETURN(3)); 610 611 REQUIRE(obj.getter(3) == 3); 612 REQUIRE(obj.getter(3) == 3); 613 614 try { 615 obj.getter(1); 616 FAIL("didn't throw"); 617 } 618 catch (reported) 619 { 620 auto re = R":(Sequence mismatch.*seq.*of obj\.getter\(1\).*has obj\.getter\(3\).*first):"; 621 INFO("report=" << reports.front().msg); 622 REQUIRE(is_match(reports.front().msg, re)); 623 auto& first = reports.front(); 624 INFO(first.file << ':' << first.line << "\n" << first.msg); 625 } 626 } 627 628 TEST_CASE_METHOD( 629 Fixture, 630 "C++11: calling a sequenced match after seq retires is allowed", 631 "[C++11][C++14][sequences]") 632 { 633 { 634 int count = 0; 635 636 mock_c obj1; 637 trompeloeil::sequence seq1; 638 639 REQUIRE_CALL_V(obj1, count(), 640 .IN_SEQUENCE(seq1) 641 .TIMES(AT_LEAST(3)) 642 .RETURN(1)); 643 644 REQUIRE_CALL_V(obj1, func(_, _), 645 .IN_SEQUENCE(seq1)); 646 647 count += obj1.count(); 648 count += obj1.count(); 649 count += obj1.count(); 650 count += obj1.count(); 651 count += obj1.count(); 652 std::string s = "apa"; 653 obj1.func(count, s); 654 } 655 REQUIRE(reports.empty()); 656 } 657 658 TEST_CASE_METHOD( 659 Fixture, 660 "C++11: breaking a sequence before retirement is illegal", 661 "[C++11][C++14][sequences]") 662 { 663 int count = 0; 664 665 mock_c obj1; 666 trompeloeil::sequence seq1; 667 668 REQUIRE_CALL_V(obj1, count(), 669 .IN_SEQUENCE(seq1) 670 .TIMES(AT_LEAST(3)) 671 .RETURN(1)); 672 673 REQUIRE_CALL_V(obj1, func(_, _), 674 .IN_SEQUENCE(seq1)); 675 676 count += obj1.count(); 677 count += obj1.count(); 678 679 try { 680 std::string s = "apa"; 681 obj1.func(count, s); 682 FAIL("didn't throw"); 683 } 684 catch (reported) 685 { 686 REQUIRE(reports.size() == 1U); 687 auto re = R":(Sequence mismatch.*seq1.*of obj1\.func\(_, _\).*has obj1\.count\(\).*first):"; 688 INFO("report=" << reports.front().msg); 689 REQUIRE(is_match(reports.front().msg, re)); 690 auto& first = reports.front(); 691 INFO(first.file << ':' << first.line << "\n" << first.msg); 692 } 693 } 694 695 TEST_CASE_METHOD( 696 Fixture, 697 "C++11: sequences impose order between multiple matching expectations", 698 "[C++11][C++14][sequences]") 699 { 700 mock_c obj; 701 702 trompeloeil::sequence seq; 703 704 REQUIRE_CALL_V(obj, getter(ANY(int)), 705 .RETURN(1) 706 .IN_SEQUENCE(seq)); 707 708 REQUIRE_CALL_V(obj, getter(ANY(int)), 709 .RETURN(2) 710 .IN_SEQUENCE(seq)); 711 712 REQUIRE_CALL_V(obj, getter(ANY(int)), 713 .RETURN(3) 714 .IN_SEQUENCE(seq)); 715 716 std::string s; 717 s += std::to_string(obj.getter(1)); 718 s += std::to_string(obj.getter(1)); 719 s += std::to_string(obj.getter(1)); 720 REQUIRE(s == "123"); 721 } 722 723 TEST_CASE_METHOD( 724 Fixture, 725 "C++11: Sequence object destruction with live expectations is reported", 726 "[C++11][C++14][sequences]") 727 { 728 mock_c obj; 729 730 std::unique_ptr<trompeloeil::expectation> e[2]; 731 { 732 733 trompeloeil::sequence s; 734 735 e[0] = NAMED_REQUIRE_CALL_V(obj, getter(ANY(int)), 736 .IN_SEQUENCE(s) 737 .RETURN(0)); 738 e[1] = NAMED_REQUIRE_CALL_V(obj, foo(_), 739 .IN_SEQUENCE(s)); 740 } 741 742 REQUIRE(!reports.empty()); 743 auto& msg = reports.front().msg; 744 INFO("report=" << msg); 745 auto re = R":(Sequence expectations not met at destruction of sequence object "s": 746 missing obj\.getter\():" + 747 escape_parens(CXX11_AS_STRING(ANY(int))) + 748 R":(\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 749 missing obj\.foo\(_\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 750 ):"; 751 REQUIRE(is_match(msg, re)); 752 } 753 754 TEST_CASE_METHOD( 755 Fixture, 756 "C++11: a sequence is completed when no expectations remain on it", 757 "[C++11][C++14][sequences]") 758 { 759 mock_c obj; 760 trompeloeil::sequence seq; 761 762 REQUIRE_CALL_V(obj, getter(ANY(int)), 763 .IN_SEQUENCE(seq) 764 .RETURN(0)); 765 766 REQUIRE_CALL_V(obj, foo(_), 767 .IN_SEQUENCE(seq) 768 .TIMES(2)); 769 770 REQUIRE_CALL_V(obj, getter(ANY(int)), 771 .IN_SEQUENCE(seq) 772 .RETURN(0)); 773 774 REQUIRE(!seq.is_completed()); 775 obj.getter(3); 776 REQUIRE(!seq.is_completed()); 777 obj.foo(""); 778 REQUIRE(!seq.is_completed()); 779 obj.foo("bar"); 780 REQUIRE(!seq.is_completed()); 781 obj.getter(0); 782 REQUIRE(seq.is_completed()); 783 } 784 785 // SIDE_EFFECT and LR_SIDE_EFFECT tests 786 787 TEST_CASE_METHOD( 788 Fixture, 789 "C++11: side effect access copy of local object", 790 "[C++11][C++14][side effects]") 791 { 792 { 793 int n = 1; 794 mock_c obj; 795 REQUIRE_CALL_V(obj, getter(ANY(int)), 796 .SIDE_EFFECT(global_n = n) 797 .RETURN(_1)); 798 n = 2; 799 obj.getter(n); 800 } 801 REQUIRE(global_n == 1); 802 } 803 804 TEST_CASE_METHOD( 805 Fixture, 806 "C++11: lr side effect access reference of local object", 807 "[C++11][C++14][side effects]") 808 { 809 { 810 int n = 1; 811 mock_c obj; 812 REQUIRE_CALL_V(obj, getter(ANY(int)), 813 .LR_SIDE_EFFECT(global_n = n) 814 .RETURN(_1)); 815 n = 2; 816 obj.getter(n); 817 } 818 REQUIRE(reports.empty()); 819 REQUIRE(global_n == 2); 820 } 821 822 TEST_CASE_METHOD( 823 Fixture, 824 "C++11: multiple side effects are executed in the order declared", 825 "[C++11][C++14][side effects]") 826 { 827 std::string s; 828 { 829 mock_c obj; 830 831 REQUIRE_CALL_V(obj, getter(ANY(int)), 832 .LR_SIDE_EFFECT(s = std::to_string(_1)) 833 .LR_SIDE_EFFECT(s += "_") 834 .LR_SIDE_EFFECT(s += s) 835 .RETURN(_1)); 836 837 obj.getter(3); 838 } 839 840 REQUIRE(reports.empty()); 841 REQUIRE(s == "3_3_"); 842 } 843 844 // RETURN and LR_RETURN tests 845 846 TEST_CASE_METHOD( 847 Fixture, 848 "C++11: RETURN access copy of local object", 849 "[C++11][C++14][return values]") 850 { 851 { 852 int n = 1; 853 mock_c obj; 854 855 REQUIRE_CALL_V(obj, getter(ANY(int)), 856 .RETURN(n)); 857 858 n = 2; 859 auto m = obj.getter(n); 860 REQUIRE(m == 1); 861 } 862 REQUIRE(reports.empty()); 863 } 864 865 TEST_CASE_METHOD( 866 Fixture, 867 "C++11: LR_RETURN access the actual local object", 868 "[C++11][C++14][return values]") 869 { 870 { 871 int n = 1; 872 mock_c obj; 873 874 REQUIRE_CALL_V(obj, getter(ANY(int)), 875 .LR_RETURN(n)); 876 877 n = 2; 878 auto m = obj.getter(n); 879 REQUIRE(m == 2); 880 } 881 REQUIRE(reports.empty()); 882 } 883 884 TEST_CASE_METHOD( 885 Fixture, 886 "C++11: RETURN a ref to local obj, std::ref(obj) returns object given", 887 "[C++11][C++14][return values]") 888 { 889 { 890 mock_c obj; 891 unmovable s; 892 REQUIRE_CALL_V(obj, getter(ANY(unmovable&)), 893 .LR_RETURN(std::ref(s))); 894 895 REQUIRE(&obj.getter(s) == &s); 896 } 897 REQUIRE(reports.empty()); 898 } 899 900 TEST_CASE_METHOD( 901 Fixture, 902 "C++11: RETURN a ref to local obj, (obj) returns object given", 903 "[C++11][C++14][return values]") 904 { 905 { 906 mock_c obj; 907 unmovable s; 908 909 REQUIRE_CALL_V(obj, getter(ANY(unmovable&)), 910 .LR_RETURN((s))); 911 912 REQUIRE(&obj.getter(s) == &s); 913 } 914 REQUIRE(reports.empty()); 915 } 916 917 TEST_CASE_METHOD( 918 Fixture, 919 "C++11: RETURN const char* from const char*", 920 "[C++11][C++14][return]") 921 { 922 mstr m; 923 const char* s = "foo"; 924 925 REQUIRE_CALL_V(m, cc_str(), 926 .RETURN(s)); 927 928 REQUIRE(m.cc_str() == std::string("foo")); 929 } 930 931 TEST_CASE_METHOD( 932 Fixture, 933 "C++11: RETURN const char* from string literal", 934 "[C++11][C++14][return]") 935 { 936 mstr m; 937 938 REQUIRE_CALL_V(m, cc_str(), 939 .RETURN("foo")); 940 941 REQUIRE(m.cc_str() == std::string("foo")); 942 } 943 944 TEST_CASE_METHOD( 945 Fixture, 946 "C++11: RETURN const char* from static char array", 947 "[C++11][C++14][return]") 948 { 949 mstr m; 950 951 REQUIRE_CALL_V(m, cc_str(), 952 .RETURN(carr)); 953 954 REQUIRE(m.cc_str() == std::string("foo")); 955 } 956 957 TEST_CASE_METHOD( 958 Fixture, 959 "C++11: RETURN const char* from static const char array", 960 "[C++11][C++14][return]") 961 { 962 mstr m; 963 964 REQUIRE_CALL_V(m, cc_str(), 965 .RETURN(ccarr)); 966 967 REQUIRE(m.cc_str() == std::string("bar")); 968 } 969 970 TEST_CASE_METHOD( 971 Fixture, 972 "C++11: RETURN char* from char*", 973 "[C++11][C++14][return]") 974 { 975 mstr m; 976 char* s = carr; 977 978 REQUIRE_CALL_V(m, cc_str(), 979 .RETURN(s)); 980 981 REQUIRE(m.cc_str() == std::string("foo")); 982 } 983 984 TEST_CASE_METHOD( 985 Fixture, 986 "C++11: RETURN char* from char array", 987 "[C++11][C++14][return]") 988 { 989 mstr m; 990 991 REQUIRE_CALL_V(m, cc_str(), 992 .RETURN(carr)); 993 994 REQUIRE(m.cc_str() == std::string("foo")); 995 } 996 997 TEST_CASE_METHOD( 998 Fixture, 999 "C++11: RETURN string from string literal", 1000 "[C++11][C++14][return]") 1001 { 1002 mstr m; 1003 1004 REQUIRE_CALL_V(m, str(), 1005 .RETURN("foo")); 1006 1007 REQUIRE(m.str() == "foo"); 1008 } 1009 1010 TEST_CASE_METHOD( 1011 Fixture, 1012 "C++11: RETURN string from static char array", 1013 "[C++11][C++14][return]") 1014 { 1015 mstr m; 1016 1017 REQUIRE_CALL_V(m, str(), 1018 .RETURN(carr)); 1019 1020 REQUIRE(m.str() == "foo"); 1021 } 1022 1023 TEST_CASE_METHOD( 1024 Fixture, 1025 "C++11: RETURN string from static const char array", 1026 "[C++11][C++14][return]") 1027 { 1028 mstr m; 1029 1030 REQUIRE_CALL_V(m, str(), 1031 .RETURN(ccarr)); 1032 1033 REQUIRE(m.str() == std::string("bar")); 1034 } 1035 1036 TEST_CASE_METHOD( 1037 Fixture, 1038 "C++11: RETURN const string from string literal", 1039 "[C++11][C++14][return]") 1040 { 1041 mstr m; 1042 1043 REQUIRE_CALL_V(m, cstr(), 1044 .RETURN("foo")); 1045 1046 REQUIRE(m.cstr() == "foo"); 1047 } 1048 1049 TEST_CASE_METHOD( 1050 Fixture, 1051 "C++11: RETURN const string from static char array", 1052 "[C++11][C++14][return]") 1053 { 1054 mstr m; 1055 1056 REQUIRE_CALL_V(m, cstr(), 1057 .RETURN(carr)); 1058 1059 REQUIRE(m.cstr() == "foo"); 1060 } 1061 1062 TEST_CASE_METHOD( 1063 Fixture, 1064 "C++11: RETURN const string from static const char array", 1065 "[C++11][C++14][return]") 1066 { 1067 mstr m; 1068 1069 REQUIRE_CALL_V(m, cstr(), 1070 .RETURN(ccarr)); 1071 1072 REQUIRE(m.cstr() == std::string("bar")); 1073 } 1074 1075 TEST_CASE_METHOD( 1076 Fixture, 1077 "C++11: RETURN ref param returns object given", 1078 "[C++11][C++14][return values]") 1079 { 1080 { 1081 mock_c obj; 1082 unmovable s; 1083 1084 REQUIRE_CALL_V(obj, getter(ANY(unmovable&)), 1085 .RETURN(_1)); 1086 1087 REQUIRE(&obj.getter(s) == &s); 1088 } 1089 REQUIRE(reports.empty()); 1090 } 1091 1092 // THROW and LR_THROW tests 1093 1094 TEST_CASE_METHOD( 1095 Fixture, 1096 "C++11: THROW access copy of local object", 1097 "[C++11][C++14][return values]") 1098 { 1099 int n = 1; 1100 mock_c obj; 1101 1102 REQUIRE_CALL_V(obj, getter(ANY(int)), 1103 .THROW(n)); 1104 1105 n = 2; 1106 try { 1107 obj.getter(n); 1108 FAIL("didn't throw"); 1109 } 1110 catch (int m) 1111 { 1112 REQUIRE(m == 1); 1113 } 1114 } 1115 1116 TEST_CASE_METHOD( 1117 Fixture, 1118 "C++11: LR_THROW access actual local object", 1119 "[C++11][C++14][return values]") 1120 { 1121 int n = 1; 1122 mock_c obj; 1123 1124 REQUIRE_CALL_V(obj, getter(ANY(int)), 1125 .LR_THROW(n)); 1126 1127 n = 2; 1128 try { 1129 obj.getter(n); 1130 FAIL("didn't throw"); 1131 } 1132 catch (int m) 1133 { 1134 REQUIRE(m == 2); 1135 } 1136 } 1137 1138 TEST_CASE_METHOD( 1139 Fixture, 1140 "C++11: THROW throws after side effect when replacing return for non void functions", 1141 "[C++11][C++14][return values]") 1142 { 1143 int thrown = 0; 1144 int global = 0; 1145 try { 1146 mock_c obj; 1147 1148 REQUIRE_CALL_V(obj, getter(ANY(int)), 1149 .THROW(8) 1150 .LR_SIDE_EFFECT(global = _1)); 1151 1152 obj.getter(8); 1153 FAIL("didn't throw"); 1154 } 1155 catch (int n) 1156 { 1157 thrown = n; 1158 } 1159 REQUIRE(thrown == 8); 1160 REQUIRE(global == 8); 1161 } 1162 1163 TEST_CASE_METHOD( 1164 Fixture, 1165 "C++11: THROW throws after side effect in void functions", 1166 "[C++11][C++14][return values]") 1167 { 1168 int thrown = 0; 1169 std::string s; 1170 try { 1171 mock_c obj; 1172 1173 REQUIRE_CALL_V(obj, func(_, _), 1174 .THROW(8) 1175 .SIDE_EFFECT(_2 = std::to_string(_1))); 1176 1177 obj.func(8, s); 1178 FAIL("didn't throw"); 1179 } 1180 catch (int n) 1181 { 1182 thrown = n; 1183 } 1184 REQUIRE(thrown == 8); 1185 REQUIRE(s == "8"); 1186 } 1187 1188 TEST_CASE_METHOD( 1189 Fixture, 1190 "C++11: THROW from a function returning a non-default constructible type", 1191 "[c++11][C++14][return values]") 1192 { 1193 int thrown = 0; 1194 try { 1195 mock_c obj; 1196 REQUIRE_CALL_V(obj, no_default_return(), 1197 .THROW(8)); 1198 obj.no_default_return(); 1199 FAIL("didn't throw"); 1200 } 1201 catch (int n) 1202 { 1203 thrown = n; 1204 } 1205 REQUIRE(thrown == 8); 1206 } 1207 1208 // WITH and LR_WITH tests 1209 1210 TEST_CASE_METHOD( 1211 Fixture, 1212 "C++11: WITH matches copy of local object", 1213 "[C++11][C++14][matching]") 1214 { 1215 { 1216 mock_c obj; 1217 int n = 1; 1218 1219 REQUIRE_CALL_V(obj, getter(ANY(int)), 1220 .WITH(_1 == n) 1221 .RETURN(_1)); 1222 1223 n = 2; 1224 obj.getter(1); 1225 1226 // Read n. 1227 CHECK(n == 2); 1228 } 1229 REQUIRE(reports.empty()); 1230 } 1231 1232 TEST_CASE_METHOD( 1233 Fixture, 1234 "C++11: LR_WITH access actual local object", 1235 "[C++11][C++14][matching]") 1236 { 1237 { 1238 mock_c obj; 1239 int n = 1; 1240 1241 REQUIRE_CALL_V(obj, getter(ANY(int)), 1242 .LR_WITH(_1 == n) 1243 .RETURN(_1)); 1244 1245 n = 2; 1246 obj.getter(2); 1247 } 1248 REQUIRE(reports.empty()); 1249 } 1250 1251 TEST_CASE_METHOD( 1252 Fixture, 1253 "C++11: rvalue reference parameter can be compared with nullptr in WITH", 1254 "[C++11][C++14][matching]") 1255 { 1256 { 1257 mock_c obj; 1258 1259 REQUIRE_CALL_V(obj, ptr(_), 1260 .WITH(_1 != nullptr) 1261 .RETURN(std::move(_1))); 1262 1263 auto p = obj.ptr(std::unique_ptr<int>(new int{3})); 1264 REQUIRE(p); 1265 REQUIRE(*p == 3); 1266 } 1267 REQUIRE(reports.empty()); 1268 } 1269 1270 TEST_CASE_METHOD( 1271 Fixture, 1272 "C++11: rvalue reference parameter can be compared with external value on WITH", 1273 "[C++11][C++14][matching]") 1274 { 1275 { 1276 mock_c obj; 1277 auto pi = new int{3}; 1278 1279 REQUIRE_CALL_V(obj, ptr(_), 1280 .WITH(_1.get() == pi) 1281 .RETURN(std::move(_1))); 1282 1283 auto p = obj.ptr(std::unique_ptr<int>(pi)); 1284 REQUIRE(p); 1285 REQUIRE(p.get() == pi); 1286 } 1287 REQUIRE(reports.empty()); 1288 } 1289 1290 TEST_CASE_METHOD( 1291 Fixture, 1292 "C++11: mocks can be inherited", 1293 "[C++11][C++14][matching]") 1294 { 1295 combined obj; 1296 1297 REQUIRE_CALL_V(obj, getter(3), 1298 .RETURN(2)); 1299 1300 auto n = obj.getter(3); 1301 REQUIRE(n == 2); 1302 } 1303 1304 // tests of direct parameter matching with fixed values and wildcards 1305 1306 TEST_CASE_METHOD( 1307 Fixture, 1308 "C++11: An uncomparable but constructible type by reference matches a call", 1309 "[C++11][C++14][matching]") 1310 { 1311 { 1312 U u; 1313 REQUIRE_CALL_V(u, func_ustr("str")); 1314 u.func_ustr("str"); 1315 } 1316 REQUIRE(reports.empty()); 1317 } 1318 1319 TEST_CASE_METHOD( 1320 Fixture, 1321 "C++11: An uncomparable but constructible type by value matches a call", 1322 "[C++11][C++14][matching]") 1323 { 1324 { 1325 U u; 1326 REQUIRE_CALL_V(u, func_ustrv("str")); 1327 u.func_ustrv("str"); 1328 } 1329 REQUIRE(reports.empty()); 1330 } 1331 1332 TEST_CASE_METHOD( 1333 Fixture, 1334 "C++11: An uncomparable but constructible type by reference mismatch is reported", 1335 "[C++11][C++14][matching]") 1336 { 1337 try 1338 { 1339 U u; 1340 REQUIRE_CALL_V(u, func_ustr("str")); 1341 u.func_ustr("strr"); 1342 FAIL("didn't report"); 1343 } 1344 catch (reported) 1345 { 1346 REQUIRE(reports.size() == 1U); 1347 auto& msg = reports.front().msg; 1348 auto re = R":(No match for call of func_ustr with signature void\(const uncomparable_string&\) with\. 1349 param _1 == strr 1350 1351 Tried u\.func_ustr\("str"\) at [A-Za-z0-9_ ./:\]*:[0-9]*.*):"; 1352 INFO("msg=" << msg); 1353 REQUIRE(is_match(msg, re)); 1354 } 1355 } 1356 1357 TEST_CASE_METHOD( 1358 Fixture, 1359 "C++11: An uncomparable but constructible type by value mismatch is reported", 1360 "[C++11][C++14][matching]") 1361 { 1362 try 1363 { 1364 U u; 1365 REQUIRE_CALL_V(u, func_ustrv("str")); 1366 u.func_ustrv("strr"); 1367 FAIL("didn't report"); 1368 } 1369 catch (reported) 1370 { 1371 REQUIRE(reports.size() == 1U); 1372 auto& msg = reports.front().msg; 1373 auto re = R":(No match for call of func_ustrv with signature void\(uncomparable_string\) with\. 1374 param _1 == strr 1375 1376 Tried u\.func_ustrv\("str"\) at [A-Za-z0-9_ ./:\]*:[0-9]*.*):"; 1377 INFO("msg=" << msg); 1378 REQUIRE(is_match(msg, re)); 1379 } 1380 } 1381 1382 TEST_CASE_METHOD( 1383 Fixture, 1384 "C++11: pointer to function matches wildcard", 1385 "[C++11][C++14][matching]") 1386 { 1387 { 1388 U u; 1389 REQUIRE_CALL_V(u, func_ptr_f(_)); 1390 u.func_ptr_f(intfunc); 1391 } 1392 REQUIRE(reports.empty()); 1393 } 1394 1395 TEST_CASE_METHOD( 1396 Fixture, 1397 "C++11: pointer to member function matches wildcard", 1398 "[C++11][C++14][matching]") 1399 { 1400 { 1401 U u; 1402 REQUIRE_CALL_V(u, func_mptr_f(_)); 1403 u.func_mptr_f(&U::func_cstr); 1404 } 1405 REQUIRE(reports.empty()); 1406 } 1407 1408 TEST_CASE_METHOD( 1409 Fixture, 1410 "C++11: pointer to member data matches wildcard", 1411 "[C++11][C++14][matching]") 1412 { 1413 { 1414 U u; 1415 REQUIRE_CALL_V(u, func_mptr_d(_)); 1416 u.func_mptr_d(&U::m); 1417 } 1418 REQUIRE(reports.empty()); 1419 } 1420 1421 TEST_CASE_METHOD( 1422 Fixture, 1423 "C++11: ostream& matches wildcard", 1424 "[C++11][C++14][matching]") 1425 { 1426 { 1427 U u; 1428 REQUIRE_CALL_V(u, func_streamref(_)); 1429 u.func_streamref(std::cout); 1430 } 1431 REQUIRE(reports.empty()); 1432 } 1433 1434 TEST_CASE_METHOD( 1435 Fixture, 1436 "C++11: uncomparable parameter matches wildcard", 1437 "[C++11][C++14][matching]") 1438 { 1439 { 1440 U u; 1441 REQUIRE_CALL_V(u, func_u(_)); 1442 u.func_u(uncomparable{}); 1443 } 1444 REQUIRE(reports.empty()); 1445 } 1446 1447 TEST_CASE_METHOD( 1448 Fixture, 1449 "C++11: uncomparable parameter matches typed wildcard", 1450 "[C++11][C++14][matching]") 1451 { 1452 { 1453 U u; 1454 REQUIRE_CALL_V(u, func_u(ANY(uncomparable))); 1455 u.func_u(uncomparable{}); 1456 } 1457 REQUIRE(reports.empty()); 1458 } 1459 1460 TEST_CASE_METHOD( 1461 Fixture, 1462 "C++11: wildcard matches parameter value type", 1463 "[C++11][C++14][matching]") 1464 { 1465 { 1466 U u; 1467 REQUIRE_CALL_V(u, func_v(_)); 1468 u.func_v(1); 1469 } 1470 REQUIRE(reports.empty()); 1471 } 1472 1473 TEST_CASE_METHOD( 1474 Fixture, 1475 "C++11: wildcard matches parameter const value type", 1476 "[C++11][C++14][matching]") 1477 { 1478 { 1479 U u; 1480 REQUIRE_CALL_V(u, func_cv(_)); 1481 u.func_cv(1); 1482 } 1483 REQUIRE(reports.empty()); 1484 } 1485 1486 TEST_CASE_METHOD( 1487 Fixture, 1488 "C++11: wildcard matches unique_ptr<> value type", 1489 "[C++11][C++14][matching]") 1490 { 1491 { 1492 U u; 1493 REQUIRE_CALL_V(u, func_uniqv(_)); 1494 u.func_uniqv(detail::make_unique<int>(3)); 1495 } 1496 REQUIRE(reports.empty()); 1497 } 1498 1499 TEST_CASE_METHOD( 1500 Fixture, 1501 "C++11: wildcard matches shared_ptr<> value type", 1502 "[C++11][C++14][matching]") 1503 { 1504 { 1505 U u; 1506 REQUIRE_CALL_V(u, func_sharedv(_)); 1507 u.func_sharedv(std::make_shared<int>(3)); 1508 } 1509 REQUIRE(reports.empty()); 1510 } 1511 1512 TEST_CASE_METHOD( 1513 Fixture, 1514 "C++11: wildcard matches parameter lvalue reference type", 1515 "[C++11][C++14][matching]") 1516 { 1517 { 1518 U u; 1519 REQUIRE_CALL_V(u, func_lr(_)); 1520 int v = 1; 1521 u.func_lr(v); 1522 } 1523 REQUIRE(reports.empty()); 1524 } 1525 1526 TEST_CASE_METHOD( 1527 Fixture, 1528 "C++11: wildcard matches parameter const lvalue reference type", 1529 "[C++11][C++14][matching]") 1530 { 1531 { 1532 U u; 1533 REQUIRE_CALL_V(u, func_clr(_)); 1534 int v = 1; 1535 u.func_clr(v); 1536 } 1537 REQUIRE(reports.empty()); 1538 } 1539 1540 TEST_CASE_METHOD( 1541 Fixture, 1542 "C++11: wildcard matches non-copyable non-const lvalue reference type", 1543 "[C++1][C++14][matching]") 1544 { 1545 { 1546 C_ptr u; 1547 REQUIRE_CALL_V(u, uptrr(_)); 1548 std::unique_ptr<int> p; 1549 u.uptrr(p); 1550 } 1551 REQUIRE(reports.empty()); 1552 } 1553 1554 TEST_CASE_METHOD( 1555 Fixture, 1556 "C++11: wildcard matches non-copyable rvalue reference type", 1557 "[C++1][C++14][matching]") 1558 { 1559 { 1560 C_ptr u; 1561 REQUIRE_CALL_V(u, uptrrr(_)); 1562 std::unique_ptr<int> p; 1563 u.uptrrr(std::move(p)); 1564 } 1565 REQUIRE(reports.empty()); 1566 } 1567 1568 TEST_CASE_METHOD( 1569 Fixture, 1570 "C++11: wildcard matches non-copyable non-const value type", 1571 "[C++1][C++14][matching]") 1572 { 1573 { 1574 C_ptr u; 1575 REQUIRE_CALL_V(u, uptr(_)); 1576 std::unique_ptr<int> p; 1577 u.uptr(std::move(p)); 1578 } 1579 REQUIRE(reports.empty()); 1580 } 1581 TEST_CASE_METHOD( 1582 Fixture, 1583 "C++11: wildcard matches non-copyable const lvalue reference type", 1584 "[C++1][C++14][matching]") 1585 { 1586 { 1587 C_ptr u; 1588 REQUIRE_CALL_V(u, uptrcr(_)); 1589 std::unique_ptr<int> p; 1590 u.uptrcr(p); 1591 } 1592 REQUIRE(reports.empty()); 1593 } 1594 1595 TEST_CASE_METHOD( 1596 Fixture, 1597 "C++11: wildcard matches parameter rvalue reference type", 1598 "[C++11][C++14][matching]") 1599 { 1600 { 1601 U u; 1602 REQUIRE_CALL_V(u, func_rr(_)); 1603 u.func_rr(1); 1604 } 1605 REQUIRE(reports.empty()); 1606 } 1607 1608 TEST_CASE_METHOD( 1609 Fixture, 1610 "C++11: wildcard matches parameter const rvalue reference type", 1611 "[C++11][C++14][matching]") 1612 { 1613 { 1614 U u; 1615 REQUIRE_CALL_V(u, func_crr(_)); 1616 u.func_crr(1); 1617 } 1618 REQUIRE(reports.empty()); 1619 } 1620 1621 TEST_CASE_METHOD( 1622 Fixture, 1623 "C++11: wildcard matches tuple<int> by value", 1624 "[C++11][C++14][matching]") 1625 { 1626 { 1627 U u; 1628 REQUIRE_CALL_V(u, func_tupv(_)); 1629 u.func_tupv(std::tuple<int>{1}); 1630 } 1631 REQUIRE(reports.empty()); 1632 } 1633 1634 TEST_CASE_METHOD( 1635 Fixture, 1636 "C++11: wildcard matches tuple<int> by lvalue reference", 1637 "[C++11][C++14][matching]") 1638 { 1639 { 1640 U u; 1641 std::tuple<int> tup{1}; 1642 REQUIRE_CALL_V(u, func_tupr(_)); 1643 u.func_tupr(tup); 1644 } 1645 REQUIRE(reports.empty()); 1646 } 1647 1648 TEST_CASE_METHOD( 1649 Fixture, 1650 "C++11: wildcard matches tuple<int> by rvalue reference", 1651 "[C++11][C++14][matching]") 1652 { 1653 { 1654 U u; 1655 REQUIRE_CALL_V(u, func_tuprr(_)); 1656 u.func_tuprr(std::tuple<int>{1}); 1657 } 1658 REQUIRE(reports.empty()); 1659 } 1660 1661 TEST_CASE_METHOD( 1662 Fixture, 1663 "C++11: wildcard matches tuple<int> by const lvalue reference", 1664 "[C++11][C++14][matching]") 1665 { 1666 { 1667 U u; 1668 REQUIRE_CALL_V(u, func_tupcr(_)); 1669 u.func_tupcr(std::tuple<int>{1}); 1670 } 1671 REQUIRE(reports.empty()); 1672 } 1673 1674 TEST_CASE_METHOD( 1675 Fixture, 1676 "C++11: wildcard matches tuple<int> by const rvalue reference", 1677 "[C++11][C++14][matching]") 1678 { 1679 { 1680 U u; 1681 REQUIRE_CALL_V(u, func_tupcrr(_)); 1682 u.func_tupcrr(std::tuple<int>{1}); 1683 } 1684 REQUIRE(reports.empty()); 1685 } 1686 1687 TEST_CASE_METHOD( 1688 Fixture, 1689 "C++11: ANY can match unique_ptr<> by value", 1690 "[C++11][C++14][matching]") 1691 { 1692 { 1693 U u; 1694 REQUIRE_CALL_V(u, func_uniqv(ANY(std::unique_ptr<int>))); 1695 u.func_uniqv(detail::make_unique<int>(3)); 1696 } 1697 REQUIRE(reports.empty()); 1698 } 1699 1700 TEST_CASE_METHOD( 1701 Fixture, 1702 "C++11: ANY can match shared_ptr<> by value", 1703 "[C++11][C++14][matching]") 1704 { 1705 { 1706 U u; 1707 REQUIRE_CALL_V(u, func_sharedv(ANY(std::shared_ptr<int>))); 1708 u.func_sharedv(std::make_shared<int>(3)); 1709 } 1710 REQUIRE(reports.empty()); 1711 } 1712 1713 TEST_CASE_METHOD( 1714 Fixture, 1715 "C++11: ANY can select overload on lvalue reference type", 1716 "[C++11][C++14][matching]") 1717 { 1718 { 1719 U u; 1720 REQUIRE_CALL_V(u, func(ANY(int&))); 1721 int i = 1; 1722 u.func(i); 1723 } 1724 REQUIRE(reports.empty()); 1725 } 1726 1727 #if !(TROMPELOEIL_GCC && TROMPELOEIL_GCC_VERSION < 40804) 1728 1729 // g++ prior to 4.8.5 (possibly 4.8.4) cannot handle this test case, 1730 // producing an unhandled exception. 1731 1732 TEST_CASE_METHOD( 1733 Fixture, 1734 "C++11: ANY can select overload on const lvalue reference type", 1735 "[C++11][C++14][matching]") 1736 { 1737 { 1738 U u; 1739 REQUIRE_CALL_V(u, func(ANY(const int&))); 1740 const int i = 1; 1741 u.func(i); 1742 } 1743 REQUIRE(reports.empty()); 1744 } 1745 1746 #endif /* !(TROMPELOEIL_GCC && TROMPELOEIL_GCC_VERSION < 40804) */ 1747 1748 TEST_CASE_METHOD( 1749 Fixture, 1750 "C++11: ANY can select overload on rvalue reference type", 1751 "[C++11][C++14][matching]") 1752 { 1753 { 1754 U u; 1755 REQUIRE_CALL_V(u, func(ANY(int&&))); 1756 u.func(1); 1757 } 1758 REQUIRE(reports.empty()); 1759 } 1760 1761 TEST_CASE_METHOD( 1762 Fixture, 1763 "C++11: Expectation matches a mocked function with param from template", 1764 "[C++11][C++14][matching][templates]") 1765 { 1766 { 1767 tmock<int> m; 1768 REQUIRE_CALL_V(m, tfunc(_)); 1769 m.tfunc(3); 1770 } 1771 REQUIRE(reports.empty()); 1772 } 1773 1774 // tests of overload selection with parameter matching 1775 1776 TEST_CASE_METHOD( 1777 Fixture, 1778 "C++11: wildcards matches overload on type and parameter count", 1779 "[C++11][C++14][matching][overloads]") 1780 { 1781 { 1782 mock_c obj; 1783 1784 REQUIRE_CALL_V(obj, getter(ANY(unmovable&)), 1785 .RETURN(_1)); 1786 1787 FORBID_CALL_V(obj, getter(ANY(int))); 1788 1789 REQUIRE_CALL_V(obj, getter(_, _), 1790 .SIDE_EFFECT(_2 = std::to_string(_1))); 1791 1792 unmovable u; 1793 auto& ur = obj.getter(u); 1794 REQUIRE(&ur == &u); 1795 1796 std::string s; 1797 obj.getter(3, s); 1798 1799 REQUIRE(s == "3"); 1800 } 1801 REQUIRE(reports.empty()); 1802 } 1803 1804 // tests of parameter matching using duck typed matcher eq 1805 1806 TEST_CASE_METHOD( 1807 Fixture, 1808 "C++11: long value matches equal int for duck typed eq", 1809 "[C++11][C++14][matching][matchers][eq]") 1810 { 1811 { 1812 U obj; 1813 REQUIRE_CALL_V(obj, func_v(trompeloeil::eq(3L))); 1814 obj.func_v(3); 1815 } 1816 REQUIRE(reports.empty()); 1817 } 1818 1819 TEST_CASE_METHOD( 1820 Fixture, 1821 "C++11: long value with mismatches int is reported for duck typed eq", 1822 "[C++11][C++14][matching][matchers][eq]") 1823 { 1824 try { 1825 U obj; 1826 REQUIRE_CALL_V(obj, func_v(trompeloeil::eq(3L))); 1827 obj.func_v(0); 1828 FAIL("didn't report"); 1829 } 1830 catch (reported) 1831 { 1832 REQUIRE(reports.size() == 1U); 1833 auto& msg = reports.front().msg; 1834 INFO("report=" << msg); 1835 auto re = R":(No match for call of func_v with signature void\(int\) with\. 1836 param _1 == 0 1837 1838 Tried obj\.func_v\(trompeloeil::eq\(3L\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 1839 Expected _1 == 3):"; 1840 REQUIRE(is_match(msg, re)); 1841 } 1842 } 1843 1844 TEST_CASE_METHOD( 1845 Fixture, 1846 "C++11: std::string value matches equal const char* for duck typed eq", 1847 "[C++11][C++14][matching][matchers][eq]") 1848 { 1849 { 1850 U obj; 1851 REQUIRE_CALL_V(obj, func_cstr(trompeloeil::eq(std::string("foo")))); 1852 obj.func_cstr("foo"); 1853 } 1854 REQUIRE(reports.empty()); 1855 } 1856 1857 TEST_CASE_METHOD( 1858 Fixture, 1859 "C++11: std::string value mismatching const char* is reported for duck typed eq", 1860 "[C++11][C++14][matching][matchers][eq]") 1861 { 1862 try { 1863 U obj; 1864 REQUIRE_CALL_V(obj, func_cstr(trompeloeil::eq(std::string("foo")))); 1865 obj.func_cstr("bar"); 1866 FAIL("didn't report"); 1867 } 1868 catch (reported) 1869 { 1870 REQUIRE(reports.size() == 1U); 1871 auto& msg = reports.front().msg; 1872 INFO("report=" << msg); 1873 auto re = R":(No match for call of func_cstr with signature void\(const char\*\) with\. 1874 param _1 == bar 1875 1876 Tried obj\.func_cstr\(trompeloeil::eq\(std::string\(\"foo\"\)\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 1877 Expected _1 == foo):"; 1878 REQUIRE(is_match(msg, re)); 1879 } 1880 } 1881 1882 // tests of parameter matching using explicitly typed matcher eq 1883 1884 TEST_CASE_METHOD( 1885 Fixture, 1886 "C++11: disambiguated eq<int&> matches equal value", 1887 "[C++11][C++14][matching][matchers][eq]") 1888 { 1889 { 1890 U obj; 1891 REQUIRE_CALL_V(obj, func(trompeloeil::eq<int&>(3))); 1892 int i = 3; 1893 obj.func(i); 1894 } 1895 REQUIRE(reports.empty()); 1896 } 1897 1898 TEST_CASE_METHOD( 1899 Fixture, 1900 "C++11: disambiguated eq<int&> reports mismatching value", 1901 "[C++11][C++14][matching][matchers][eq]") 1902 { 1903 try { 1904 U obj; 1905 REQUIRE_CALL_V(obj, func(trompeloeil::eq<int&>(3))); 1906 int i = 0; 1907 obj.func(i); 1908 FAIL("didn't report"); 1909 } 1910 catch (reported) 1911 { 1912 REQUIRE(reports.size() == 1U); 1913 auto& msg = reports.front().msg; 1914 INFO("report=" << msg); 1915 auto re = R":(No match for call of func with signature void\(int&\) with\. 1916 param _1 == 0 1917 1918 Tried obj\.func\(trompeloeil::eq<int&>\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 1919 Expected _1 == 3):"; 1920 REQUIRE(is_match(msg, re)); 1921 } 1922 } 1923 1924 // tests of parameter matching using duck typed matcher ne 1925 1926 TEST_CASE_METHOD( 1927 Fixture, 1928 "C++11: nullptr mismatching equal function for duck typed ne", 1929 "[C++11][C++14][matching][matchers][ne]") 1930 { 1931 { 1932 U obj; 1933 REQUIRE_CALL_V(obj, func_f(trompeloeil::ne(nullptr))); 1934 obj.func_f([](){}); 1935 } 1936 REQUIRE(reports.empty()); 1937 } 1938 1939 TEST_CASE_METHOD( 1940 Fixture, 1941 "C++11: long value mismatching equal int for duck typed ne", 1942 "[C++11][C++14][matching][matchers][ne]") 1943 { 1944 { 1945 U obj; 1946 REQUIRE_CALL_V(obj, func_v(trompeloeil::ne(3L))); 1947 obj.func_v(0); 1948 } 1949 REQUIRE(reports.empty()); 1950 } 1951 1952 TEST_CASE_METHOD( 1953 Fixture, 1954 "C++11: long value with matching int is reported for duck typed ne", 1955 "[C++11][C++14][matching][matchers][ne]") 1956 { 1957 try { 1958 U obj; 1959 REQUIRE_CALL_V(obj, func_v(trompeloeil::ne(3L))); 1960 obj.func_v(3); 1961 FAIL("didn't report"); 1962 } 1963 catch (reported) 1964 { 1965 REQUIRE(reports.size() == 1U); 1966 auto& msg = reports.front().msg; 1967 INFO("report=" << msg); 1968 auto re = R":(No match for call of func_v with signature void\(int\) with\. 1969 param _1 == 3 1970 1971 Tried obj\.func_v\(trompeloeil::ne\(3L\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 1972 Expected _1 != 3):"; 1973 REQUIRE(is_match(msg, re)); 1974 } 1975 } 1976 1977 TEST_CASE_METHOD( 1978 Fixture, 1979 "C++11: std::string value mismatches inequal const char* for duck typed ne", 1980 "[C++11][C++14][matching][matchers][ne]") 1981 { 1982 { 1983 U obj; 1984 REQUIRE_CALL_V(obj, func_cstr(trompeloeil::ne(std::string("foo")))); 1985 obj.func_cstr("bar"); 1986 } 1987 REQUIRE(reports.empty()); 1988 } 1989 1990 TEST_CASE_METHOD( 1991 Fixture, 1992 "C++11: std::string value matching const char* is reported for duck typed ne", 1993 "[C++11][C++14][matching][matchers][ne]") 1994 { 1995 try { 1996 U obj; 1997 REQUIRE_CALL_V(obj, func_cstr(trompeloeil::ne(std::string("foo")))); 1998 obj.func_cstr("foo"); 1999 FAIL("didn't report"); 2000 } 2001 catch (reported) 2002 { 2003 REQUIRE(reports.size() == 1U); 2004 auto& msg = reports.front().msg; 2005 INFO("report=" << msg); 2006 auto re = R":(No match for call of func_cstr with signature void\(const char\*\) with\. 2007 param _1 == foo 2008 2009 Tried obj\.func_cstr\(trompeloeil::ne\(std::string\(\"foo\"\)\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2010 Expected _1 != foo):"; 2011 REQUIRE(is_match(msg, re)); 2012 } 2013 } 2014 2015 // tests of parameter matching using typed matcher ne 2016 2017 TEST_CASE_METHOD( 2018 Fixture, 2019 "C++11: a non equal value matches ne", 2020 "[C++11][C++14][matching][matchers][ne]") 2021 { 2022 { 2023 mock_c obj; 2024 REQUIRE_CALL_V(obj, foo(trompeloeil::ne<std::string>("bar"))); 2025 obj.foo("baz"); 2026 } 2027 REQUIRE(reports.empty()); 2028 } 2029 2030 TEST_CASE_METHOD( 2031 Fixture, 2032 "C++11: an equal value fails ne with report", 2033 "[C++11][C++14][matching][matchers][ne]") 2034 { 2035 try { 2036 mock_c obj; 2037 REQUIRE_CALL_V(obj, foo(trompeloeil::ne<std::string>("bar"))); 2038 obj.foo("bar"); 2039 FAIL("didn't report"); 2040 } 2041 catch (reported) 2042 { 2043 REQUIRE(reports.size() == 1U); 2044 INFO("report=" << reports.front().msg); 2045 auto re = R":(No match for call of foo with signature void\(std::string\) with\. 2046 param _1 == bar 2047 2048 Tried obj\.foo\(trompeloeil::ne<std::string>\("bar"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2049 Expected _1 != bar):"; 2050 REQUIRE(is_match(reports.front().msg, re)); 2051 } 2052 } 2053 2054 TEST_CASE_METHOD( 2055 Fixture, 2056 "C++11: non-nullptr call matches ne(nullptr)", 2057 "[C++11][C++14][matching][matchers][ne]") 2058 { 2059 { 2060 C_foo1 obj; 2061 REQUIRE_CALL_V(obj, foo(trompeloeil::ne(nullptr))); 2062 int n; 2063 obj.foo(&n); 2064 } 2065 REQUIRE(reports.empty()); 2066 } 2067 2068 TEST_CASE_METHOD( 2069 Fixture, 2070 "C++11: nullptr call with ne(nullptr) is reported", 2071 "[C++11][C++14][matching][matchers][ne]") 2072 { 2073 try { 2074 C_foo1 obj; 2075 REQUIRE_CALL_V(obj, foo(trompeloeil::ne(nullptr))); 2076 obj.foo(nullptr); 2077 FAIL("didn't report"); 2078 } 2079 catch (reported) 2080 { 2081 REQUIRE(reports.size() == 1U); 2082 INFO("report=" << reports.front().msg); 2083 auto re = R":(No match for call of foo with signature void\(int\*\) with\. 2084 param _1 == .* 2085 2086 Tried obj\.foo\(trompeloeil::ne\(nullptr\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2087 Expected _1 != .*):"; 2088 2089 REQUIRE(is_match(reports.front().msg, re)); 2090 } 2091 } 2092 2093 TEST_CASE_METHOD( 2094 Fixture, 2095 "C++11: overloaded non-nullptr call disambiguated with ne<type>(nullptr) is matched", 2096 "[C++11][C++14][matching][matchers][ne]") 2097 { 2098 { 2099 C_foo2 obj; 2100 REQUIRE_CALL_V(obj, foo(trompeloeil::ne<int*>(nullptr))); 2101 int i; 2102 obj.foo(&i); 2103 } 2104 REQUIRE(reports.empty()); 2105 } 2106 2107 TEST_CASE_METHOD( 2108 Fixture, 2109 "C++11: overloaded nullptr call disambiguated with ne<type>(nullptr) is reported", 2110 "[C++11][C++14][matching][matchers][ne]") 2111 { 2112 try { 2113 C_foo2 obj; 2114 REQUIRE_CALL_V(obj, foo(trompeloeil::ne<int*>(nullptr))); 2115 int* i_null = nullptr; 2116 obj.foo(i_null); 2117 FAIL("didn't report"); 2118 } 2119 catch (reported) 2120 { 2121 REQUIRE(reports.size() == 1U); 2122 INFO("report=" << reports.front().msg); 2123 auto re = R":(No match for call of foo with signature void\(int\*\) with\. 2124 param _1 == nullptr 2125 2126 Tried obj\.foo\(trompeloeil::ne<int\*>\(nullptr\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2127 Expected _1 != .*):"; 2128 2129 REQUIRE(is_match(reports.front().msg, re)); 2130 } 2131 } 2132 2133 // 2134 2135 TEST_CASE_METHOD( 2136 Fixture, 2137 "C++11: overloaded nullptr call disambiguated with eq<type>(nullptr) is matched", 2138 "[C++11][C++14][matching][matchers][ne]") 2139 { 2140 { 2141 C_foo2 obj; 2142 REQUIRE_CALL_V(obj, foo(trompeloeil::eq<int*>(nullptr))); 2143 int *i_null = nullptr; 2144 obj.foo(i_null); 2145 } 2146 REQUIRE(reports.empty()); 2147 } 2148 2149 TEST_CASE_METHOD( 2150 Fixture, 2151 "C++11: overloaded non-nullptr call disambiguated with eq<type>(nullptr) is reported", 2152 "[C++11][C++14][matching][matchers][ne]") 2153 { 2154 try { 2155 C_foo2 obj; 2156 REQUIRE_CALL_V(obj, foo(trompeloeil::eq<int*>(nullptr))); 2157 int i; 2158 obj.foo(&i); 2159 FAIL("didn't report"); 2160 } 2161 catch (reported) 2162 { 2163 REQUIRE(reports.size() == 1U); 2164 INFO("report=" << reports.front().msg); 2165 auto re = R":(No match for call of foo with signature void\(int\*\) with\. 2166 param _1 == .* 2167 2168 Tried obj\.foo\(trompeloeil::eq<int\*>\(nullptr\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2169 Expected _1 == nullptr):"; 2170 2171 REQUIRE(is_match(reports.front().msg, re)); 2172 } 2173 } 2174 2175 TEST_CASE_METHOD( 2176 Fixture, 2177 "C++11: pointer to member call with ne(nullptr) matched", 2178 "[C++11][C++14][matching][matchers][ne]") 2179 { 2180 { 2181 C_foo3 obj; 2182 REQUIRE_CALL_V(obj, foo(trompeloeil::ne(nullptr))); 2183 obj.foo(&C_foo3::m); 2184 } 2185 REQUIRE(reports.empty()); 2186 } 2187 2188 TEST_CASE_METHOD( 2189 Fixture, 2190 "C++11: pointer to member ptr call with ne(nullptr) is reported", 2191 "[C++11][C++14][matching][matchers][ne]") 2192 { 2193 try { 2194 C_foo3 obj; 2195 REQUIRE_CALL_V(obj, foo(trompeloeil::ne(nullptr))); 2196 obj.foo(nullptr); 2197 FAIL("didn't report"); 2198 } 2199 catch (reported) 2200 { 2201 REQUIRE(reports.size() == 1U); 2202 INFO("report=" << reports.front().msg); 2203 auto re = R":(No match for call of foo with signature void\(int C_foo3::\*\) with\. 2204 param _1 == .* 2205 2206 Tried obj\.foo\(trompeloeil::ne\(nullptr\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2207 Expected _1 != .*):"; 2208 2209 REQUIRE(is_match(reports.front().msg, re)); 2210 } 2211 } 2212 2213 // 2214 2215 TEST_CASE_METHOD( 2216 Fixture, 2217 "C++11: pointer to member ptr call with eq(nullptr) matched", 2218 "[C++11][C++14][matching][matchers][ne]") 2219 { 2220 { 2221 C_foo3 obj; 2222 REQUIRE_CALL_V(obj, foo(trompeloeil::eq(nullptr))); 2223 obj.foo(nullptr); 2224 } 2225 REQUIRE(reports.empty()); 2226 } 2227 2228 TEST_CASE_METHOD( 2229 Fixture, 2230 "C++11: pointer to member ptr call with eq(nullptr) is reported", 2231 "[C++11][C++14][matching][matchers][ne]") 2232 { 2233 try { 2234 C_foo3 obj; 2235 REQUIRE_CALL_V(obj, foo(trompeloeil::eq(nullptr))); 2236 obj.foo(&C_foo3::m); 2237 FAIL("didn't report"); 2238 } 2239 catch (reported) 2240 { 2241 REQUIRE(reports.size() == 1U); 2242 INFO("report=" << reports.front().msg); 2243 auto re = R":(No match for call of foo with signature void\(int C_foo3::\*\) with\. 2244 param _1 == .* 2245 2246 Tried obj\.foo\(trompeloeil::eq\(nullptr\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2247 Expected _1 == nullptr):"; 2248 2249 REQUIRE(is_match(reports.front().msg, re)); 2250 } 2251 } 2252 2253 TEST_CASE_METHOD( 2254 Fixture, 2255 "C++11: pointer to function call with ne(nullptr) matched", 2256 "[C++11][C++14][matching][matchers][ne]") 2257 { 2258 { 2259 C_foo3 obj; 2260 REQUIRE_CALL_V(obj, bar(trompeloeil::ne(nullptr))); 2261 obj.bar(intfunc); 2262 } 2263 REQUIRE(reports.empty()); 2264 } 2265 2266 TEST_CASE_METHOD( 2267 Fixture, 2268 "C++11: pointer to function call with ne(nullptr) is reported", 2269 "[C++11][C++14][matching][matchers][ne]") 2270 { 2271 try { 2272 C_foo3 obj; 2273 REQUIRE_CALL_V(obj, bar(trompeloeil::ne(nullptr))); 2274 obj.bar(nullptr); 2275 FAIL("didn't report"); 2276 } 2277 catch (reported) 2278 { 2279 REQUIRE(reports.size() == 1U); 2280 INFO("report=" << reports.front().msg); 2281 auto re = R":(No match for call of bar with signature void\(int \(\*\)\(int\)\) with\. 2282 param _1 == .* 2283 2284 Tried obj\.bar\(trompeloeil::ne\(nullptr\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2285 Expected _1 != .*):"; 2286 2287 REQUIRE(is_match(reports.front().msg, re)); 2288 } 2289 } 2290 2291 // 2292 2293 TEST_CASE_METHOD( 2294 Fixture, 2295 "C++11: pointer to function call with eq(nullptr) matched", 2296 "[C++11][C++14][matching][matchers][ne]") 2297 { 2298 { 2299 C_foo3 obj; 2300 REQUIRE_CALL_V(obj, bar(trompeloeil::eq(nullptr))); 2301 obj.bar(nullptr); 2302 } 2303 REQUIRE(reports.empty()); 2304 } 2305 2306 TEST_CASE_METHOD( 2307 Fixture, 2308 "C++11: pointer to function call with eq(nullptr) is reported", 2309 "[C++11][C++14][matching][matchers][ne]") 2310 { 2311 try { 2312 C_foo3 obj; 2313 REQUIRE_CALL_V(obj, bar(trompeloeil::eq(nullptr))); 2314 obj.bar(intfunc); 2315 FAIL("didn't report"); 2316 } 2317 catch (reported) 2318 { 2319 REQUIRE(reports.size() == 1U); 2320 INFO("report=" << reports.front().msg); 2321 auto re = R":(No match for call of bar with signature void\(int \(\*\)\(int\)\) with\. 2322 param _1 == .* 2323 2324 Tried obj\.bar\(trompeloeil::eq\(nullptr\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2325 Expected _1 == nullptr):"; 2326 2327 REQUIRE(is_match(reports.front().msg, re)); 2328 } 2329 } 2330 2331 // 2332 // tests of parameter matching using duck typed matcher ge 2333 2334 TEST_CASE_METHOD( 2335 Fixture, 2336 "C++11: an equal value matches ge", 2337 "[C++11][C++14][matching][matchers][ge]") 2338 { 2339 { 2340 mock_c obj; 2341 2342 REQUIRE_CALL_V(obj, getter(trompeloeil::ge(3)), 2343 .RETURN(0)); 2344 2345 obj.getter(3); 2346 } 2347 REQUIRE(reports.empty()); 2348 } 2349 2350 TEST_CASE_METHOD( 2351 Fixture, 2352 "C++11: a greater value matches ge", 2353 "[C++11][C++14][matching][matchers][ge]") 2354 { 2355 { 2356 mock_c obj; 2357 2358 REQUIRE_CALL_V(obj, getter(trompeloeil::ge(3)), 2359 .RETURN(0)); 2360 2361 obj.getter(4); 2362 } 2363 REQUIRE(reports.empty()); 2364 } 2365 2366 TEST_CASE_METHOD( 2367 Fixture, 2368 "C++11: a lesser value is reported by ge", 2369 "[C++11][C++14][matching][matchers][ge]") 2370 { 2371 try { 2372 mock_c obj; 2373 2374 REQUIRE_CALL_V(obj, getter(trompeloeil::ge(3)), 2375 .RETURN(0)); 2376 2377 obj.getter(2); 2378 FAIL("didn't report"); 2379 } 2380 catch (reported) 2381 { 2382 REQUIRE(reports.size() == 1U); 2383 auto re = R":(No match for call of getter with signature int\(int\) with. 2384 param _1 == 2 2385 2386 Tried obj\.getter\(trompeloeil::ge\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2387 Expected _1 >= 3):"; 2388 REQUIRE(is_match(reports.front().msg, re)); 2389 } 2390 } 2391 2392 // tests of parameter matching using typed matcher ge 2393 2394 TEST_CASE_METHOD( 2395 Fixture, 2396 "C++11: an equal value matches disambiguated ge<int&>", 2397 "[C++11][C++14][matching][matchers][ge]") 2398 { 2399 { 2400 U obj; 2401 REQUIRE_CALL_V(obj, func(trompeloeil::ge<int&>(3))); 2402 int i = 3; 2403 obj.func(i); 2404 } 2405 REQUIRE(reports.empty()); 2406 } 2407 2408 TEST_CASE_METHOD( 2409 Fixture, 2410 "C++11: a greater value matches disambiguated ge<int&>", 2411 "[C++11][C++14][matching][matchers][ge]") 2412 { 2413 { 2414 U obj; 2415 REQUIRE_CALL_V(obj, func(trompeloeil::ge<int&>(3))); 2416 int i = 4; 2417 obj.func(i); 2418 } 2419 REQUIRE(reports.empty()); 2420 } 2421 2422 TEST_CASE_METHOD( 2423 Fixture, 2424 "C++11: a lesser value is reported by disambiguated ge<int&>", 2425 "[C++11][C++14][matching][matchers][ge]") 2426 { 2427 try { 2428 U obj; 2429 REQUIRE_CALL_V(obj, func(trompeloeil::ge<int&>(3))); 2430 int i = 2; 2431 obj.func(i); 2432 FAIL("didn't report"); 2433 } 2434 catch (reported) 2435 { 2436 REQUIRE(reports.size() == 1U); 2437 auto re = R":(No match for call of func with signature void\(int&\) with. 2438 param _1 == 2 2439 2440 Tried obj\.func\(trompeloeil::ge<int&>\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2441 Expected _1 >= 3):"; 2442 REQUIRE(is_match(reports.front().msg, re)); 2443 } 2444 } 2445 2446 // tests of parameter matching using duck typed matcher gt 2447 2448 TEST_CASE_METHOD( 2449 Fixture, 2450 "C++11: an equal value is reported by gt", 2451 "[C++11][C++14][matching][matchers][gt]") 2452 { 2453 try { 2454 mock_c obj; 2455 2456 REQUIRE_CALL_V(obj, getter(trompeloeil::gt(3)), 2457 .RETURN(0)); 2458 2459 obj.getter(3); 2460 FAIL("didn't report"); 2461 } 2462 catch (reported) 2463 { 2464 REQUIRE(reports.size() == 1U); 2465 auto re = R":(No match for call of getter with signature int\(int\) with\. 2466 param _1 == 3 2467 2468 Tried obj\.getter\(trompeloeil::gt\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2469 Expected _1 > 3):"; 2470 REQUIRE(is_match(reports.front().msg, re)); 2471 } 2472 } 2473 2474 TEST_CASE_METHOD( 2475 Fixture, 2476 "C++11: a greater value matches gt", 2477 "[C++11][C++14][matching][matchers][gt]") 2478 { 2479 { 2480 mock_c obj; 2481 2482 REQUIRE_CALL_V(obj, getter(trompeloeil::gt(3)), 2483 .RETURN(0)); 2484 2485 obj.getter(4); 2486 } 2487 REQUIRE(reports.empty()); 2488 } 2489 2490 TEST_CASE_METHOD( 2491 Fixture, 2492 "C++11: a lesser value is reported by gt", 2493 "[C++11][C++14][matching][matchers][gt]") 2494 { 2495 try { 2496 mock_c obj; 2497 2498 REQUIRE_CALL_V(obj, getter(trompeloeil::gt(3)), 2499 .RETURN(0)); 2500 2501 obj.getter(2); 2502 FAIL("didn't report"); 2503 } 2504 catch (reported) 2505 { 2506 REQUIRE(reports.size() == 1U); 2507 auto re = R":(No match for call of getter with signature int\(int\) with\. 2508 param _1 == 2 2509 2510 Tried obj.getter\(trompeloeil::gt\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2511 Expected _1 > 3):"; 2512 REQUIRE(is_match(reports.front().msg, re)); 2513 } 2514 } 2515 2516 // tests of parameter matching using typed matcher gt 2517 2518 TEST_CASE_METHOD( 2519 Fixture, 2520 "C++11: an equal value is reported by disambiguated gt<int&>", 2521 "[C++11][C++14][matching][matchers][gt]") 2522 { 2523 try { 2524 U obj; 2525 REQUIRE_CALL_V(obj, func(trompeloeil::gt<int&>(3))); 2526 int i = 3; 2527 obj.func(i); 2528 FAIL("didn't report"); 2529 } 2530 catch (reported) 2531 { 2532 REQUIRE(reports.size() == 1U); 2533 auto re = R":(No match for call of func with signature void\(int&\) with\. 2534 param _1 == 3 2535 2536 Tried obj\.func\(trompeloeil::gt<int&>\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2537 Expected _1 > 3):"; 2538 REQUIRE(is_match(reports.front().msg, re)); 2539 } 2540 } 2541 2542 TEST_CASE_METHOD( 2543 Fixture, 2544 "C++11: a greater value matches disambiguated gt<int&>", 2545 "[C++11][C++14][matching][matchers][gt]") 2546 { 2547 { 2548 U obj; 2549 REQUIRE_CALL_V(obj, func(trompeloeil::gt<int&>(3))); 2550 int i = 4; 2551 obj.func(i); 2552 } 2553 REQUIRE(reports.empty()); 2554 } 2555 2556 TEST_CASE_METHOD( 2557 Fixture, 2558 "C++11: a lesser value is reported by disambiguated gt<int&>", 2559 "[C++11][C++14][matching][matchers][gt]") 2560 { 2561 try { 2562 U obj; 2563 REQUIRE_CALL_V(obj, func(trompeloeil::gt<int&>(3))); 2564 int i = 2; 2565 obj.func(i); 2566 FAIL("didn't report"); 2567 } 2568 catch (reported) 2569 { 2570 REQUIRE(reports.size() == 1U); 2571 auto re = R":(No match for call of func with signature void\(int&\) with\. 2572 param _1 == 2 2573 2574 Tried obj.func\(trompeloeil::gt<int&>\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2575 Expected _1 > 3):"; 2576 REQUIRE(is_match(reports.front().msg, re)); 2577 } 2578 } 2579 2580 // tests of parameter matching using duck typed matcher lt 2581 2582 TEST_CASE_METHOD( 2583 Fixture, 2584 "C++11: an equal value is reported by lt", 2585 "[C++11][C++14][matching][matchers][lt]") 2586 { 2587 try { 2588 mock_c obj; 2589 2590 REQUIRE_CALL_V(obj, getter(trompeloeil::lt(3)), 2591 .RETURN(0)); 2592 2593 obj.getter(3); 2594 FAIL("didn't report"); 2595 } 2596 catch (reported) 2597 { 2598 REQUIRE(reports.size() == 1U); 2599 auto re = R":(No match for call of getter with signature int\(int\) with\. 2600 param _1 == 3 2601 2602 Tried obj\.getter\(trompeloeil::lt\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2603 Expected _1 < 3):"; 2604 REQUIRE(is_match(reports.front().msg, re)); 2605 } 2606 } 2607 2608 TEST_CASE_METHOD( 2609 Fixture, 2610 "C++11: a greater value is reported by lt", 2611 "[C++11][C++14][matching][matchers][lt]") 2612 { 2613 try { 2614 mock_c obj; 2615 2616 REQUIRE_CALL_V(obj, getter(trompeloeil::lt(3)), 2617 .RETURN(0)); 2618 2619 obj.getter(4); 2620 FAIL("didn't report"); 2621 } 2622 catch (reported) 2623 { 2624 REQUIRE(reports.size() == 1U); 2625 auto re = R":(No match for call of getter with signature int\(int\) with\. 2626 param _1 == 4 2627 2628 Tried obj\.getter\(trompeloeil::lt\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2629 Expected _1 < 3):"; 2630 REQUIRE(is_match(reports.front().msg, re)); 2631 } 2632 } 2633 2634 TEST_CASE_METHOD( 2635 Fixture, 2636 "C++11: a lesser value matches lt", 2637 "[C++11][C++14][matching][matchers][lt]") 2638 { 2639 { 2640 mock_c obj; 2641 2642 REQUIRE_CALL_V(obj, getter(trompeloeil::lt(3)), 2643 .RETURN(0)); 2644 2645 obj.getter(2); 2646 } 2647 REQUIRE(reports.empty()); 2648 } 2649 2650 // tests of parameter matching using typed matcher lt 2651 2652 TEST_CASE_METHOD( 2653 Fixture, 2654 "C++11: an equal value is reported by disambiguated lt<int&>", 2655 "[C++11][C++14][matching][matchers][lt]") 2656 { 2657 try { 2658 U obj; 2659 REQUIRE_CALL_V(obj, func(trompeloeil::lt<int&>(3))); 2660 int i = 3; 2661 obj.func(i); 2662 FAIL("didn't report"); 2663 } 2664 catch (reported) 2665 { 2666 REQUIRE(reports.size() == 1U); 2667 auto re = R":(No match for call of func with signature void\(int&\) with\. 2668 param _1 == 3 2669 2670 Tried obj\.func\(trompeloeil::lt<int&>\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2671 Expected _1 < 3):"; 2672 REQUIRE(is_match(reports.front().msg, re)); 2673 } 2674 } 2675 2676 TEST_CASE_METHOD( 2677 Fixture, 2678 "C++11: a greater value is reported by disambiguated lt<int&>", 2679 "[C++11][C++14][matching][matchers][lt]") 2680 { 2681 try { 2682 U obj; 2683 REQUIRE_CALL_V(obj, func(trompeloeil::lt<int&>(3))); 2684 int i = 4; 2685 obj.func(i); 2686 FAIL("didn't report"); 2687 } 2688 catch (reported) 2689 { 2690 REQUIRE(reports.size() == 1U); 2691 auto re = R":(No match for call of func with signature void\(int&\) with\. 2692 param _1 == 4 2693 2694 Tried obj\.func\(trompeloeil::lt<int&>\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2695 Expected _1 < 3):"; 2696 REQUIRE(is_match(reports.front().msg, re)); 2697 } 2698 } 2699 2700 TEST_CASE_METHOD( 2701 Fixture, 2702 "C++11: a lesser value matches disambiguated lt<int&>", 2703 "[C++11][C++14][matching][matchers][lt]") 2704 { 2705 { 2706 U obj; 2707 REQUIRE_CALL_V(obj, func(trompeloeil::lt<int&>(3))); 2708 int i = 2; 2709 obj.func(i); 2710 } 2711 REQUIRE(reports.empty()); 2712 } 2713 2714 // tests of parameter matching using duck typed matcher le 2715 2716 TEST_CASE_METHOD( 2717 Fixture, 2718 "C++11: an equal value matches le", 2719 "[C++11][C++14][matching][matchers][le]") 2720 { 2721 { 2722 mock_c obj; 2723 2724 REQUIRE_CALL_V(obj, getter(trompeloeil::le(3)), 2725 .RETURN(0)); 2726 2727 obj.getter(3); 2728 } 2729 REQUIRE(reports.empty()); 2730 } 2731 2732 TEST_CASE_METHOD( 2733 Fixture, 2734 "C++11: a greater value is reported by le", 2735 "[C++11][C++14][matching][matchers][le]") 2736 { 2737 try { 2738 mock_c obj; 2739 2740 REQUIRE_CALL_V(obj, getter(trompeloeil::le(3)), 2741 .RETURN(0)); 2742 2743 obj.getter(4); 2744 FAIL("didn't report"); 2745 } 2746 catch (reported) 2747 { 2748 REQUIRE(reports.size() == 1U); 2749 auto re = R":(No match for call of getter with signature int\(int\) with\. 2750 param _1 == 4 2751 2752 Tried obj\.getter\(trompeloeil::le\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2753 Expected _1 <= 3):"; 2754 REQUIRE(is_match(reports.front().msg, re)); 2755 } 2756 } 2757 2758 TEST_CASE_METHOD( 2759 Fixture, 2760 "C++11: a lesser value matches le", 2761 "[C++11][C++14][matching][matchers][le]") 2762 { 2763 { 2764 mock_c obj; 2765 2766 REQUIRE_CALL_V(obj, getter(trompeloeil::le(3)), 2767 .RETURN(0)); 2768 2769 obj.getter(2); 2770 } 2771 REQUIRE(reports.empty()); 2772 } 2773 2774 // tests of parameter matching using typed matcher le 2775 2776 TEST_CASE_METHOD( 2777 Fixture, 2778 "C++11: an equal value matches disambiguated le<int&>", 2779 "[C++11][C++14][matching][matchers][le]") 2780 { 2781 { 2782 U obj; 2783 REQUIRE_CALL_V(obj, func(trompeloeil::le<int&>(3))); 2784 int i = 3; 2785 obj.func(i); 2786 } 2787 REQUIRE(reports.empty()); 2788 } 2789 2790 TEST_CASE_METHOD( 2791 Fixture, 2792 "C++11: a greater value is reported by disambiguated le<int&>", 2793 "[C++11][C++14][matching][matchers][le]") 2794 { 2795 try { 2796 U obj; 2797 REQUIRE_CALL_V(obj, func(trompeloeil::le<int&>(3))); 2798 int i = 4; 2799 obj.func(i); 2800 FAIL("didn't report"); 2801 } 2802 catch (reported) 2803 { 2804 REQUIRE(reports.size() == 1U); 2805 auto re = R":(No match for call of func with signature void\(int&\) with\. 2806 param _1 == 4 2807 2808 Tried obj\.func\(trompeloeil::le<int&>\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2809 Expected _1 <= 3):"; 2810 REQUIRE(is_match(reports.front().msg, re)); 2811 } 2812 } 2813 2814 TEST_CASE_METHOD( 2815 Fixture, 2816 "C++11: a lesser value matches disambiguated le<int&>", 2817 "[C++11][C++14][matching][matchers][le]") 2818 { 2819 { 2820 U obj; 2821 REQUIRE_CALL_V(obj, func(trompeloeil::le<int&>(3))); 2822 int i = 2; 2823 obj.func(i); 2824 } 2825 REQUIRE(reports.empty()); 2826 } 2827 2828 // tests of parameter matching using typed matcher re 2829 2830 #if TROMPELOEIL_TEST_REGEX_FAILURES 2831 2832 TEST_CASE_METHOD( 2833 Fixture, 2834 "C++11: call to const c-string function matching regex is not reported", 2835 "[C++11][C++14][matching][matchers][re]") 2836 { 2837 { 2838 mock_str obj; 2839 REQUIRE_CALL_V(obj, c_c_str(trompeloeil::re("mid"))); 2840 char str[] = "pre mid post"; 2841 obj.c_c_str(str); 2842 } 2843 REQUIRE(reports.empty()); 2844 } 2845 2846 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 2847 2848 #if TROMPELOEIL_TEST_REGEX_FAILURES 2849 2850 TEST_CASE_METHOD( 2851 Fixture, 2852 "C++11: call to const c-string function with nullptr to regex is reported", 2853 "[C++11][C++14][matching][matchers][re]") 2854 { 2855 mock_str obj; 2856 REQUIRE_CALL_V(obj, c_c_str(trompeloeil::re("mid"))); 2857 try 2858 { 2859 obj.c_c_str(nullptr); 2860 FAIL("did not throw"); 2861 } 2862 catch (reported) 2863 { 2864 REQUIRE(reports.size() == 1U); 2865 auto& msg = reports.front().msg; 2866 INFO("msg=" << msg); 2867 auto re = R":(No match for call of c_c_str with signature void\(char const\*\) with. 2868 param _1 == nullptr 2869 2870 Tried obj.c_c_str\(trompeloeil::re\("mid"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2871 Expected _1 matching regular expression /mid/):"; 2872 REQUIRE(is_match(msg, re)); 2873 } 2874 } 2875 2876 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 2877 2878 #if TROMPELOEIL_TEST_REGEX_FAILURES 2879 2880 TEST_CASE_METHOD( 2881 Fixture, 2882 "C++11: call to const c-string function with non-matching string to regex is reported", 2883 "[C++11][C++14][matching][matchers][re]") 2884 { 2885 mock_str obj; 2886 REQUIRE_CALL_V(obj, c_c_str(trompeloeil::re("mid"))); 2887 try 2888 { 2889 char str[] = "abcde"; 2890 obj.c_c_str(str); 2891 FAIL("did not throw"); 2892 } 2893 catch (reported) 2894 { 2895 REQUIRE(reports.size() == 1U); 2896 auto& msg = reports.front().msg; 2897 INFO("msg=" << msg); 2898 auto re = R":(No match for call of c_c_str with signature void\(char const\*\) with. 2899 param _1 == abcde 2900 2901 Tried obj.c_c_str\(trompeloeil::re\("mid"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2902 Expected _1 matching regular expression /mid/):"; 2903 REQUIRE(is_match(msg, re)); 2904 } 2905 } 2906 2907 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 2908 2909 // 2910 2911 #if TROMPELOEIL_TEST_REGEX_FAILURES 2912 2913 TEST_CASE_METHOD( 2914 Fixture, 2915 "C++11: call to non-const c-string function matching regex is not reported", 2916 "[C++11][C++14][matching][matchers][re]") 2917 { 2918 { 2919 mock_str obj; 2920 REQUIRE_CALL_V(obj, c_str(trompeloeil::re("mid"))); 2921 char str[] = "pre mid post"; 2922 obj.c_str(str); 2923 } 2924 REQUIRE(reports.empty()); 2925 } 2926 2927 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 2928 2929 #if TROMPELOEIL_TEST_REGEX_FAILURES 2930 2931 TEST_CASE_METHOD( 2932 Fixture, 2933 "C++11: call to non-const c-string function with nullptr to regex is reported", 2934 "[C++11][C++14][matching][matchers][re]") 2935 { 2936 mock_str obj; 2937 REQUIRE_CALL_V(obj, c_str(trompeloeil::re("mid"))); 2938 try 2939 { 2940 obj.c_str(nullptr); 2941 FAIL("did not throw"); 2942 } 2943 catch (reported) 2944 { 2945 REQUIRE(reports.size() == 1U); 2946 auto& msg = reports.front().msg; 2947 INFO("msg=" << msg); 2948 auto re = R":(No match for call of c_str with signature void\(char\*\) with. 2949 param _1 == nullptr 2950 2951 Tried obj.c_str\(trompeloeil::re\("mid"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2952 Expected _1 matching regular expression /mid/):"; 2953 REQUIRE(is_match(msg, re)); 2954 } 2955 } 2956 2957 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 2958 2959 #if TROMPELOEIL_TEST_REGEX_FAILURES 2960 2961 TEST_CASE_METHOD( 2962 Fixture, 2963 "C++11: call to non-const c-string function with non-matching string to regex is reported", 2964 "[C++11][C++14][matching][matchers][re]") 2965 { 2966 mock_str obj; 2967 REQUIRE_CALL_V(obj, c_str(trompeloeil::re("mid"))); 2968 try 2969 { 2970 char str[] = "abcde"; 2971 obj.c_str(str); 2972 FAIL("did not throw"); 2973 } 2974 catch (reported) 2975 { 2976 REQUIRE(reports.size() == 1U); 2977 auto& msg = reports.front().msg; 2978 INFO("msg=" << msg); 2979 auto re = R":(No match for call of c_str with signature void\(char\*\) with. 2980 param _1 == abcde 2981 2982 Tried obj.c_str\(trompeloeil::re\("mid"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 2983 Expected _1 matching regular expression /mid/):"; 2984 REQUIRE(is_match(msg, re)); 2985 } 2986 } 2987 2988 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 2989 2990 //@@ 2991 2992 #if TROMPELOEIL_TEST_REGEX_FAILURES 2993 2994 TEST_CASE_METHOD( 2995 Fixture, 2996 "C++11: call to const strref function matching regex is not reported", 2997 "[C++11][C++14][matching][matchers][re]") 2998 { 2999 { 3000 mock_str obj; 3001 REQUIRE_CALL_V(obj, strcref(trompeloeil::re("mid"))); 3002 obj.strcref(std::string("pre mid post")); 3003 } 3004 REQUIRE(reports.empty()); 3005 } 3006 3007 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3008 3009 #if TROMPELOEIL_TEST_REGEX_FAILURES 3010 3011 TEST_CASE_METHOD( 3012 Fixture, 3013 "C++11: call to const strref function with non-matching string to regex is reported", 3014 "[C++11][C++14][matching][matchers][re]") 3015 { 3016 mock_str obj; 3017 REQUIRE_CALL_V(obj, strcref(trompeloeil::re("mid"))); 3018 try 3019 { 3020 obj.strcref(std::string("abcde")); 3021 FAIL("did not throw"); 3022 } 3023 catch (reported) 3024 { 3025 REQUIRE(reports.size() == 1U); 3026 auto& msg = reports.front().msg; 3027 INFO("msg=" << msg); 3028 auto re = R":(No match for call of strcref with signature void\(std::string const&\) with\. 3029 param _1 == abcde 3030 3031 Tried obj\.strcref\(trompeloeil::re\("mid"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3032 Expected _1 matching regular expression /mid/):"; 3033 REQUIRE(is_match(msg, re)); 3034 } 3035 } 3036 3037 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3038 3039 // 3040 3041 #if TROMPELOEIL_TEST_REGEX_FAILURES 3042 3043 TEST_CASE_METHOD( 3044 Fixture, 3045 "C++11: call to non-const strref function matching regex is not reported", 3046 "[C++11][C++14][matching][matchers][re]") 3047 { 3048 { 3049 mock_str obj; 3050 REQUIRE_CALL_V(obj, strref(trompeloeil::re("mid"))); 3051 std::string str = "pre mid post"; 3052 obj.strref(str); 3053 } 3054 REQUIRE(reports.empty()); 3055 } 3056 3057 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3058 3059 #if TROMPELOEIL_TEST_REGEX_FAILURES 3060 3061 TEST_CASE_METHOD( 3062 Fixture, 3063 "C++11: call to non-const strref function with non-matching string to regex is reported", 3064 "[C++11][C++14][matching][matchers][re]") 3065 { 3066 mock_str obj; 3067 REQUIRE_CALL_V(obj, strref(trompeloeil::re("mid"))); 3068 try 3069 { 3070 std::string str = "abcde"; 3071 obj.strref(str); 3072 FAIL("did not throw"); 3073 } 3074 catch (reported) 3075 { 3076 REQUIRE(reports.size() == 1U); 3077 auto& msg = reports.front().msg; 3078 INFO("msg=" << msg); 3079 auto re = R":(No match for call of strref with signature void\(std::string&\) with. 3080 param _1 == abcde 3081 3082 Tried obj.strref\(trompeloeil::re\("mid"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3083 Expected _1 matching regular expression /mid/):"; 3084 REQUIRE(is_match(msg, re)); 3085 } 3086 } 3087 3088 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3089 3090 // 3091 3092 #if TROMPELOEIL_TEST_REGEX_FAILURES 3093 3094 TEST_CASE_METHOD( 3095 Fixture, 3096 "C++11: call to non-const strrref function matching regex is not reported", 3097 "[C++11][C++14][matching][matchers][re]") 3098 { 3099 { 3100 mock_str obj; 3101 REQUIRE_CALL_V(obj, strrref(trompeloeil::re("mid"))); 3102 std::string str = "pre mid post"; 3103 obj.strrref(std::move(str)); 3104 } 3105 REQUIRE(reports.empty()); 3106 } 3107 3108 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3109 3110 #if TROMPELOEIL_TEST_REGEX_FAILURES 3111 3112 TEST_CASE_METHOD( 3113 Fixture, 3114 "C++11: call to non-const strrref function with non-matching string to regex is reported", 3115 "[C++11][C++14][matching][matchers][re]") 3116 { 3117 mock_str obj; 3118 REQUIRE_CALL_V(obj, strrref(trompeloeil::re("mid"))); 3119 try 3120 { 3121 std::string str = "abcde"; 3122 obj.strrref(std::move(str)); 3123 FAIL("did not throw"); 3124 } 3125 catch (reported) 3126 { 3127 REQUIRE(reports.size() == 1U); 3128 auto& msg = reports.front().msg; 3129 INFO("msg=" << msg); 3130 auto re = R":(No match for call of strrref with signature void\(std::string&&\) with. 3131 param _1 == abcde 3132 3133 Tried obj.strrref\(trompeloeil::re\("mid"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3134 Expected _1 matching regular expression /mid/):"; 3135 REQUIRE(is_match(msg, re)); 3136 } 3137 } 3138 3139 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3140 3141 // 3142 3143 #if TROMPELOEIL_TEST_REGEX_FAILURES 3144 3145 TEST_CASE_METHOD( 3146 Fixture, 3147 "C++11: call to str val function matching regex is not reported", 3148 "[C++11][C++14][matching][matchers][re]") 3149 { 3150 { 3151 mock_str obj; 3152 REQUIRE_CALL_V(obj, str(trompeloeil::re("mid"))); 3153 std::string str = "pre mid post"; 3154 obj.str(std::move(str)); 3155 } 3156 REQUIRE(reports.empty()); 3157 } 3158 3159 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3160 3161 #if TROMPELOEIL_TEST_REGEX_FAILURES 3162 3163 TEST_CASE_METHOD( 3164 Fixture, 3165 "C++11: call to str val function with non-matching string to regex is reported", 3166 "[C++11][C++14][matching][matchers][re]") 3167 { 3168 mock_str obj; 3169 REQUIRE_CALL_V(obj, str(trompeloeil::re("mid"))); 3170 try 3171 { 3172 std::string str = "abcde"; 3173 obj.str(std::move(str)); 3174 FAIL("did not throw"); 3175 } 3176 catch (reported) 3177 { 3178 REQUIRE(reports.size() == 1U); 3179 auto& msg = reports.front().msg; 3180 INFO("msg=" << msg); 3181 auto re = R":(No match for call of str with signature void\(std::string\) with. 3182 param _1 == abcde 3183 3184 Tried obj.str\(trompeloeil::re\("mid"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3185 Expected _1 matching regular expression /mid/):"; 3186 REQUIRE(is_match(msg, re)); 3187 } 3188 } 3189 3190 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3191 3192 #if TROMPELOEIL_TEST_REGEX_FAILURES 3193 3194 TEST_CASE_METHOD( 3195 Fixture, 3196 "C++11: call to mismatching regex of typed overload is reported", 3197 "[C++11][C++14][matching][matchers][re]") 3198 { 3199 mock_str obj; 3200 REQUIRE_CALL_V(obj, overload(trompeloeil::re<char const*>("mid"))); 3201 try 3202 { 3203 obj.overload("abcde"); 3204 FAIL("did not throw"); 3205 } 3206 catch (reported) 3207 { 3208 REQUIRE(reports.size() == 1U); 3209 auto& msg = reports.front().msg; 3210 INFO("msg=" << msg); 3211 auto re = R":(No match for call of overload with signature void\(char const\*\) with. 3212 param _1 == abcde 3213 3214 Tried obj.overload\(trompeloeil::re<char const\*>\("mid"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3215 Expected _1 matching regular expression /mid/):"; 3216 REQUIRE(is_match(msg, re)); 3217 } 3218 } 3219 3220 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3221 3222 #if TROMPELOEIL_TEST_REGEX_FAILURES 3223 3224 TEST_CASE_METHOD( 3225 Fixture, 3226 "C++11: call to matching regex of typed overload is not reported", 3227 "[C++11][C++14][matching][matchers][re]") 3228 { 3229 { 3230 mock_str obj; 3231 REQUIRE_CALL_V(obj, overload(trompeloeil::re<std::string const&>("mid"))); 3232 std::string str = "pre mid post"; 3233 obj.overload(str); 3234 } 3235 REQUIRE(reports.empty()); 3236 } 3237 3238 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3239 3240 // 3241 3242 #if TROMPELOEIL_TEST_REGEX_FAILURES 3243 3244 TEST_CASE_METHOD( 3245 Fixture, 3246 "C++11: case insensitive regex matches case mismatch without explicit type", 3247 "[C++11][C++14][matching][matchers][re]") 3248 { 3249 { 3250 mock_str obj; 3251 REQUIRE_CALL_V(obj, str(trompeloeil::re("MiXeD", std::regex_constants::icase))); 3252 std::string str = "mIXEd"; 3253 obj.str(str); 3254 } 3255 REQUIRE(reports.empty()); 3256 } 3257 3258 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3259 3260 #if TROMPELOEIL_TEST_REGEX_FAILURES 3261 3262 TEST_CASE_METHOD( 3263 Fixture, 3264 "C++11: case insensitive regex matches case mismatch overload", 3265 "[C++11][C++14][matching][matchers][re]") 3266 { 3267 { 3268 mock_str obj; 3269 REQUIRE_CALL_V(obj, overload(trompeloeil::re<std::string const&>("MiXeD", std::regex_constants::icase))); 3270 std::string str = "mIXEd"; 3271 obj.overload(str); 3272 } 3273 REQUIRE(reports.empty()); 3274 } 3275 3276 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3277 3278 #if TROMPELOEIL_TEST_REGEX_BOL_EOL_FAILURES 3279 3280 TEST_CASE_METHOD( 3281 Fixture, 3282 "C++11: not_eol regex mismatching $ without explicit type is reported", 3283 "[C++11][C++14][matching][matchers][re]") 3284 { 3285 try { 3286 mock_str obj; 3287 REQUIRE_CALL_V(obj, str(trompeloeil::re("end$", std::regex_constants::match_not_eol))); 3288 std::string str = "begin end"; 3289 obj.str(str); 3290 } 3291 catch (reported) 3292 { 3293 REQUIRE(reports.size() == 1U); 3294 auto& msg = reports.front().msg; 3295 INFO("msg=" << msg); 3296 auto re = R":(No match for call of str with signature void\(std::string\) with. 3297 param _1 == begin end 3298 3299 Tried obj.str\(trompeloeil::re\("end\$", std::regex_constants::match_not_eol\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3300 Expected _1 matching regular expression /end\$/):"; 3301 REQUIRE(is_match(msg, re)); 3302 } 3303 } 3304 3305 #endif /* TROMPELOEIL_TEST_REGEX_BOL_EOL_FAILURES */ 3306 3307 #if TROMPELOEIL_TEST_REGEX_BOL_EOL_FAILURES 3308 3309 TEST_CASE_METHOD( 3310 Fixture, 3311 "C++11: not_eol regex mismatching $ overload is reported", 3312 "[C++11][C++14][matching][matchers][re]") 3313 { 3314 try { 3315 mock_str obj; 3316 REQUIRE_CALL_V(obj, overload(trompeloeil::re<std::string const&>("end$", std::regex_constants::match_not_eol))); 3317 std::string str = "begin end"; 3318 obj.overload(str); 3319 FAIL("did not throw"); 3320 } 3321 catch (reported) 3322 { 3323 REQUIRE(reports.size() == 1U); 3324 auto& msg = reports.front().msg; 3325 INFO("msg=" << msg); 3326 auto re = R":(No match for call of overload with signature void\(std::string const&\) with. 3327 param _1 == begin end 3328 3329 Tried obj.overload\(trompeloeil::re<std::string const&>\("end\$", std::regex_constants::match_not_eol\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3330 Expected _1 matching regular expression /end\$/):"; 3331 REQUIRE(is_match(msg, re)); 3332 } 3333 } 3334 3335 #endif /* TROMPELOEIL_TEST_REGEX_BOL_EOL_FAILURES */ 3336 3337 // tests of parameter matching using neg_matcher 3338 3339 3340 TEST_CASE_METHOD( 3341 Fixture, 3342 "C++11: ! to duck typed equal matches inequal string", 3343 "[C++11][C++14][matching][matchers][eq][neg]") 3344 { 3345 { 3346 mock_str obj; 3347 REQUIRE_CALL_V(obj, str(!trompeloeil::eq("foo"))); 3348 obj.str("bar"); 3349 } 3350 REQUIRE(reports.empty()); 3351 } 3352 3353 3354 TEST_CASE_METHOD( 3355 Fixture, 3356 "C++11: ! to duck typed equal reports equal string", 3357 "[C++11][C++14][matching][matchers][eq][neg]") 3358 { 3359 try { 3360 mock_str obj; 3361 REQUIRE_CALL_V(obj, str(!trompeloeil::eq("foo"))); 3362 obj.str("foo"); 3363 FAIL("did not throw"); 3364 } 3365 catch (reported) 3366 { 3367 REQUIRE(reports.size() == 1U); 3368 auto& msg = reports.front().msg; 3369 INFO("msg=" << msg); 3370 auto re= R":(No match for call of str with signature void\(std::string\) with. 3371 param _1 == foo 3372 3373 Tried obj\.str\(!trompeloeil::eq\("foo"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3374 Expected not _1 == foo):"; 3375 REQUIRE(is_match(msg, re)); 3376 } 3377 } 3378 3379 3380 TEST_CASE_METHOD( 3381 Fixture, 3382 "C++11: ! to disambiguated equal matches inequal string", 3383 "[C++11][C++14][matching][matchers][eq][neg]") 3384 { 3385 { 3386 mock_str obj; 3387 REQUIRE_CALL_V(obj, overload(!trompeloeil::eq<std::string>("foo"))); 3388 obj.overload(std::string("bar")); 3389 } 3390 REQUIRE(reports.empty()); 3391 } 3392 3393 TEST_CASE_METHOD( 3394 Fixture, 3395 "C++11: ! to disambiguated equal reports equal string", 3396 "[C++11][C++14][matching][matchers][eq][neg]") 3397 { 3398 try { 3399 mock_str obj; 3400 REQUIRE_CALL_V(obj, overload(!trompeloeil::eq<std::string>("foo"))); 3401 obj.overload(std::string("foo")); 3402 FAIL("did not throw"); 3403 } 3404 catch (reported) 3405 { 3406 REQUIRE(reports.size() == 1U); 3407 auto& msg = reports.front().msg; 3408 INFO("msg=" << msg); 3409 auto re= R":(No match for call of overload with signature void\(std::string const&\) with. 3410 param _1 == foo 3411 3412 Tried obj\.overload\(!trompeloeil::eq<std::string>\("foo"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3413 Expected not _1 == foo):"; 3414 REQUIRE(is_match(msg, re)); 3415 } 3416 } 3417 3418 // tests of parameter matching using ptr deref matcher 3419 3420 #if !(TROMPELOEIL_GCC && TROMPELOEIL_GCC_VERSION < 40804) 3421 3422 // g++ prior to 4.8.5 (possibly 4.8.4) cannot handle this test case, 3423 // reporting "call of overload ... is ambiguous". 3424 3425 TEST_CASE_METHOD( 3426 Fixture, 3427 "C++11: ptr to disambiguated equal const value matches deref", 3428 "[C++11][C++14][matching][matchers][eq]") 3429 { 3430 { 3431 C_ptr obj; 3432 REQUIRE_CALL_V(obj, coverload(*trompeloeil::eq<const int&>(3))); 3433 const int n = 3; 3434 obj.coverload(&n); 3435 } 3436 REQUIRE(reports.empty()); 3437 } 3438 3439 #endif /* !(TROMPELOEIL_GCC && TROMPELOEIL_GCC_VERSION < 40804) */ 3440 3441 TEST_CASE_METHOD( 3442 Fixture, 3443 "C++11: ptr to equal value matches deref", 3444 "[C++11][C++14][matching][matchers][eq]") 3445 { 3446 { 3447 C_ptr obj; 3448 REQUIRE_CALL_V(obj, ptr(*trompeloeil::eq(3))); 3449 int n = 3; 3450 obj.ptr(&n); 3451 } 3452 REQUIRE(reports.empty()); 3453 } 3454 3455 TEST_CASE_METHOD( 3456 Fixture, 3457 "C++11: ptr to equal nullptr matches deref", 3458 "[C++11][C++14][matching][matchers][eq]") 3459 { 3460 { 3461 C_ptr obj; 3462 REQUIRE_CALL_V(obj, pp(*trompeloeil::eq(nullptr))); 3463 int* p = nullptr; 3464 obj.pp(&p); 3465 } 3466 REQUIRE(reports.empty()); 3467 } 3468 3469 TEST_CASE_METHOD( 3470 Fixture, 3471 "C++11: ptr to overloaded ptr matches equal deref", 3472 "[C++11][C++14][matching][matchers][eq]") 3473 { 3474 { 3475 C_ptr obj; 3476 REQUIRE_CALL_V(obj, overloaded(*trompeloeil::eq(nullptr))); 3477 int* p = nullptr; 3478 obj.overloaded(&p); 3479 } 3480 REQUIRE(reports.empty()); 3481 } 3482 3483 TEST_CASE_METHOD( 3484 Fixture, 3485 "C++11: ptr to overloaded string matches equal deref to string literal", 3486 "[C++11][C++14][matching][matchers][eq]") 3487 { 3488 { 3489 C_ptr obj; 3490 REQUIRE_CALL_V(obj, overloaded(*trompeloeil::eq<std::string&>(std::string("apa")))); 3491 std::string s{"apa"}; 3492 obj.overloaded(&s); 3493 } 3494 REQUIRE(reports.empty()); 3495 } 3496 3497 TEST_CASE_METHOD( 3498 Fixture, 3499 "C++11: nullptr when equal ptr deref expected is reported", 3500 "[C++11][C++14][matching][matchers][eq]") 3501 { 3502 try 3503 { 3504 C_ptr obj; 3505 REQUIRE_CALL_V(obj, ptr(*trompeloeil::eq(3))); 3506 obj.ptr(nullptr); 3507 FAIL("didn't throw"); 3508 } 3509 catch (reported) 3510 { 3511 REQUIRE(!reports.empty()); 3512 auto re = R":(No match for call of ptr with signature void\(int\*\) with\. 3513 param _1 == nullptr 3514 3515 Tried obj\.ptr\(\*trompeloeil::eq\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3516 Expected \*_1 == 3):"; 3517 3518 INFO("msg=" << reports.front().msg); 3519 REQUIRE(is_match(reports.front().msg, re)); 3520 } 3521 } 3522 3523 TEST_CASE_METHOD( 3524 Fixture, 3525 "C++11: non-nullptr when equal nullptr ptr deref expected is reported", 3526 "[C++11][C++14][matching][matchers][eq]") 3527 { 3528 try 3529 { 3530 C_ptr obj; 3531 REQUIRE_CALL_V(obj, pp(*trompeloeil::eq(nullptr))); 3532 int i = 3; 3533 auto pi = &i; 3534 obj.pp(&pi); 3535 FAIL("didn't throw"); 3536 } 3537 catch (reported) 3538 { 3539 REQUIRE(!reports.empty()); 3540 auto re = R":(No match for call of pp with signature void\(int\*\*\) with\. 3541 param _1 == .* 3542 3543 Tried obj\.pp\(\*trompeloeil::eq\(nullptr\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3544 Expected \*_1 == nullptr):"; 3545 3546 INFO("msg=" << reports.front().msg); 3547 REQUIRE(is_match(reports.front().msg, re)); 3548 } 3549 } 3550 3551 TEST_CASE_METHOD( 3552 Fixture, 3553 "C++11: ptr to different value when equal ptr deref expected is reported", 3554 "[C++11][C++14][matching][matchers][eq]") 3555 { 3556 try 3557 { 3558 C_ptr obj; 3559 REQUIRE_CALL_V(obj, ptr(*trompeloeil::eq(3))); 3560 int n = 2; 3561 obj.ptr(&n); 3562 FAIL("didn't throw"); 3563 } 3564 catch (reported) 3565 { 3566 REQUIRE(!reports.empty()); 3567 auto re = R":(No match for call of ptr with signature void\(int\*\) with\. 3568 param _1 == .* 3569 3570 Tried obj\.ptr\(\*trompeloeil::eq\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3571 Expected \*_1 == 3):"; 3572 INFO(reports.front().msg); 3573 REQUIRE(is_match(reports.front().msg, re)); 3574 } 3575 } 3576 3577 TEST_CASE_METHOD( 3578 Fixture, 3579 "C++11: ptr to equal value of different size matches deref", 3580 "[C++11][C++14][matching][matchers][eq]") 3581 { 3582 { 3583 C_ptr obj; 3584 REQUIRE_CALL_V(obj, ptr(*trompeloeil::eq(3L))); 3585 int n = 3; 3586 obj.ptr(&n); 3587 } 3588 REQUIRE(reports.empty()); 3589 } 3590 3591 //// 3592 TEST_CASE_METHOD( 3593 Fixture, 3594 "C++11: unique_ptr value to equal value matches deref", 3595 "[C++11][C++14][matching][matchers][eq]") 3596 { 3597 { 3598 C_ptr obj; 3599 REQUIRE_CALL_V(obj, uptr(*trompeloeil::eq(3))); 3600 obj.uptr(detail::make_unique<int>(3)); 3601 } 3602 REQUIRE(reports.empty()); 3603 } 3604 3605 TEST_CASE_METHOD( 3606 Fixture, 3607 "C++11: unique_ptr<>() value when equal ptr deref expected is reported", 3608 "[C++11][C++14][matching][matchers][eq]") 3609 { 3610 try 3611 { 3612 C_ptr obj; 3613 REQUIRE_CALL_V(obj, uptr(*trompeloeil::eq(3))); 3614 obj.uptr(nullptr); 3615 FAIL("didn't throw"); 3616 } 3617 catch (reported) 3618 { 3619 REQUIRE(!reports.empty()); 3620 auto re = R":(No match for call of uptr with signature void\(std::unique_ptr<int>\) with\. 3621 param _1 == nullptr 3622 3623 Tried obj\.uptr\(\*trompeloeil::eq\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3624 Expected \*_1 == 3):"; 3625 INFO("msg=" << reports.front().msg); 3626 REQUIRE(is_match(reports.front().msg, re)); 3627 } 3628 } 3629 3630 TEST_CASE_METHOD( 3631 Fixture, 3632 "C++11: unique ptr value to different value when equal ptr deref expected is reported", 3633 "[C++11][C++14][matching][matchers][eq]") 3634 { 3635 try 3636 { 3637 C_ptr obj; 3638 REQUIRE_CALL_V(obj, uptr(*trompeloeil::eq(3))); 3639 obj.uptr(detail::make_unique<int>(2)); 3640 FAIL("didn't throw"); 3641 } 3642 catch (reported) 3643 { 3644 REQUIRE(!reports.empty()); 3645 auto re = R":(No match for call of uptr with signature void\(std::unique_ptr<int>\) with\. 3646 param _1 == .* 3647 3648 Tried obj\.uptr\(\*trompeloeil::eq\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3649 Expected \*_1 == 3):"; 3650 REQUIRE(is_match(reports.front().msg, re)); 3651 } 3652 } 3653 3654 TEST_CASE_METHOD( 3655 Fixture, 3656 "C++11: unique value ptr to equal value of different size matches deref", 3657 "[C++11][C++14][matching][matchers][eq]") 3658 { 3659 { 3660 C_ptr obj; 3661 REQUIRE_CALL_V(obj, uptr(*trompeloeil::eq(3L))); 3662 obj.uptr(detail::make_unique<int>(3)); 3663 } 3664 REQUIRE(reports.empty()); 3665 } 3666 ////// 3667 3668 TEST_CASE_METHOD( 3669 Fixture, 3670 "C++11: unique_ptr rvalue ref to equal value matches deref", 3671 "[C++11][C++14][matching][matchers][eq]") 3672 { 3673 { 3674 C_ptr obj; 3675 REQUIRE_CALL_V(obj, uptrrr(*trompeloeil::eq(3))); 3676 obj.uptrrr(detail::make_unique<int>(3)); 3677 } 3678 REQUIRE(reports.empty()); 3679 } 3680 3681 TEST_CASE_METHOD( 3682 Fixture, 3683 "C++11: unique_ptr<>() rvalue ref when equal ptr deref expected is reported", 3684 "[C++11][C++14][matching][matchers][eq]") 3685 { 3686 try 3687 { 3688 C_ptr obj; 3689 REQUIRE_CALL_V(obj, uptrrr(*trompeloeil::eq(3))); 3690 obj.uptrrr(nullptr); 3691 FAIL("didn't throw"); 3692 } 3693 catch (reported) 3694 { 3695 REQUIRE(!reports.empty()); 3696 auto re = R":(No match for call of uptrrr with signature void\(std::unique_ptr<int>&&\) with\. 3697 param _1 == nullptr 3698 3699 Tried obj\.uptrrr\(\*trompeloeil::eq\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3700 Expected \*_1 == 3):"; 3701 INFO("msg=" << reports.front().msg); 3702 REQUIRE(is_match(reports.front().msg, re)); 3703 } 3704 } 3705 3706 TEST_CASE_METHOD( 3707 Fixture, 3708 "C++11: unique ptr rvalue ref to different value when equal ptr deref expected is reported", 3709 "[C++11][C++14][matching][matchers][eq]") 3710 { 3711 try 3712 { 3713 C_ptr obj; 3714 REQUIRE_CALL_V(obj, uptrrr(*trompeloeil::eq(3))); 3715 obj.uptrrr(detail::make_unique<int>(2)); 3716 FAIL("didn't throw"); 3717 } 3718 catch (reported) 3719 { 3720 REQUIRE(!reports.empty()); 3721 auto re = R":(No match for call of uptrrr with signature void\(std::unique_ptr<int>&&\) with\. 3722 param _1 == .* 3723 3724 Tried obj\.uptrrr\(\*trompeloeil::eq\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3725 Expected \*_1 == 3):"; 3726 REQUIRE(is_match(reports.front().msg, re)); 3727 } 3728 } 3729 3730 TEST_CASE_METHOD( 3731 Fixture, 3732 "C++11: unique ptr rvalue ref to equal value of different size matches deref", 3733 "[C++11][C++14][matching][matchers][eq]") 3734 { 3735 { 3736 C_ptr obj; 3737 REQUIRE_CALL_V(obj, uptrrr(*trompeloeil::eq(3L))); 3738 obj.uptrrr(detail::make_unique<int>(3)); 3739 } 3740 REQUIRE(reports.empty()); 3741 } 3742 3743 //// 3744 3745 TEST_CASE_METHOD( 3746 Fixture, 3747 "C++11: unique_ptr const lvalue ref to equal value matches deref", 3748 "[C++11][C++14][matching][matchers][eq]") 3749 { 3750 { 3751 C_ptr obj; 3752 REQUIRE_CALL_V(obj, uptrcr(*trompeloeil::eq(3))); 3753 obj.uptrcr(detail::make_unique<int>(3)); 3754 } 3755 REQUIRE(reports.empty()); 3756 } 3757 3758 TEST_CASE_METHOD( 3759 Fixture, 3760 "C++11: unique_ptr<>() const lvalue ref when equal ptr deref expected is reported", 3761 "[C++11][C++14][matching][matchers][eq]") 3762 { 3763 try 3764 { 3765 C_ptr obj; 3766 REQUIRE_CALL_V(obj, uptrcr(*trompeloeil::eq(3))); 3767 obj.uptrcr(nullptr); 3768 FAIL("didn't throw"); 3769 } 3770 catch (reported) 3771 { 3772 REQUIRE(!reports.empty()); 3773 auto re = R":(No match for call of uptrcr with signature void\(std::unique_ptr<int> const&\) with\. 3774 param _1 == nullptr 3775 3776 Tried obj\.uptrcr\(\*trompeloeil::eq\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3777 Expected \*_1 == 3):"; 3778 INFO(reports.front().msg); 3779 REQUIRE(is_match(reports.front().msg, re)); 3780 } 3781 } 3782 3783 TEST_CASE_METHOD( 3784 Fixture, 3785 "C++11: unique ptr const lvalue ref to different value when equal ptr deref expected is reported", 3786 "[C++11][C++14][matching][matchers][eq]") 3787 { 3788 try 3789 { 3790 C_ptr obj; 3791 REQUIRE_CALL_V(obj, uptrcr(*trompeloeil::eq(3))); 3792 obj.uptrcr(detail::make_unique<int>(2)); 3793 FAIL("didn't throw"); 3794 } 3795 catch (reported) 3796 { 3797 REQUIRE(!reports.empty()); 3798 auto re = R":(No match for call of uptrcr with signature void\(std::unique_ptr<int> const&\) with\. 3799 param _1 == .* 3800 3801 Tried obj\.uptrcr\(\*trompeloeil::eq\(3\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3802 Expected \*_1 == 3):"; 3803 3804 INFO(reports.front().msg); 3805 REQUIRE(is_match(reports.front().msg, re)); 3806 } 3807 } 3808 3809 TEST_CASE_METHOD( 3810 Fixture, 3811 "C++11: unique ptr const lvalue ref to equal value of different size matches deref", 3812 "[C++11][C++14][matching][matchers][eq]") 3813 { 3814 { 3815 C_ptr obj; 3816 REQUIRE_CALL_V(obj, uptrcr(*trompeloeil::eq(3L))); 3817 obj.uptrcr(detail::make_unique<int>(3)); 3818 } 3819 REQUIRE(reports.empty()); 3820 } 3821 3822 TEST_CASE_METHOD( 3823 Fixture, 3824 "C++11: missing call to unique ptr const lvalue ref to equal value is reported", 3825 "[C++11][C++14][matching][matchers][eq]") 3826 { 3827 { 3828 C_ptr obj; 3829 REQUIRE_CALL_V(obj, uptrcr(*trompeloeil::eq(3))); 3830 } 3831 REQUIRE(!reports.empty()); 3832 auto re = R":(Unfulfilled expectation: 3833 Expected obj\.uptrcr\(\*trompeloeil::eq\(3\)\) to be called once, actually never called 3834 param \*_1 == 3):"; 3835 3836 INFO(reports.front().msg); 3837 REQUIRE(is_match(reports.front().msg, re)); 3838 3839 // REQUIRE(reports.front().msg == ""); 3840 } 3841 3842 #if TROMPELOEIL_TEST_REGEX_FAILURES 3843 3844 TEST_CASE_METHOD( 3845 Fixture, 3846 "C++11: call to string* function matching regex is not reported", 3847 "[C++11][C++14][matching][matchers][re]") 3848 { 3849 { 3850 C_ptr obj; 3851 REQUIRE_CALL_V(obj, strptr(*trompeloeil::re("end$"))); 3852 std::string s = "begin end"; 3853 obj.strptr(&s); 3854 } 3855 REQUIRE(reports.empty()); 3856 } 3857 3858 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3859 3860 #if TROMPELOEIL_TEST_REGEX_FAILURES 3861 3862 TEST_CASE_METHOD( 3863 Fixture, 3864 "C++11: call to string* function not matching regex is reported", 3865 "[C++11][C++14][matching][matchers][re]") 3866 { 3867 try { 3868 C_ptr obj; 3869 REQUIRE_CALL_V(obj, strptr(*trompeloeil::re("end$"))); 3870 std::string s = "begin end;"; 3871 obj.strptr(&s); 3872 } 3873 catch (reported) 3874 { 3875 REQUIRE(reports.size() == 1U); 3876 INFO("report=" << reports.front().msg); 3877 auto re = R":(No match for call of strptr with signature void\(std::string\*\) with\. 3878 param _1 == .* 3879 3880 Tried obj\.strptr\(\*trompeloeil::re\("end\$"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3881 Expected \*_1 matching regular expression /end\$/):"; 3882 REQUIRE(is_match(reports.front().msg, re)); 3883 } 3884 } 3885 3886 #endif /* TROMPELOEIL_TEST_REGEX_FAILURES */ 3887 3888 // tests of parameter matching using custom typed matcher 3889 3890 TEST_CASE_METHOD( 3891 Fixture, 3892 "C++11: custom matcher of first element is not reported", 3893 "[C++11][C++14][matching][matchers][custom]") 3894 { 3895 { 3896 mock_c obj; 3897 3898 REQUIRE_CALL_V(obj, getter(any_of({ 1, 5, 77 })), 3899 .RETURN(0)); 3900 3901 obj.getter(1); 3902 } 3903 REQUIRE(reports.empty()); 3904 } 3905 3906 TEST_CASE_METHOD( 3907 Fixture, 3908 "C++11: custom matcher of last element is not reported", 3909 "[C++11][C++14][matching][matchers][custom]") 3910 { 3911 { 3912 mock_c obj; 3913 3914 REQUIRE_CALL_V(obj, getter(any_of({ 1, 5, 77 })), 3915 .RETURN(0)); 3916 3917 obj.getter(77); 3918 } 3919 REQUIRE(reports.empty()); 3920 } 3921 3922 TEST_CASE_METHOD( 3923 Fixture, 3924 "C++11: custom matcher of mid element is not reported", 3925 "[C++11][C++14][matching][matchers][custom]") 3926 { 3927 { 3928 mock_c obj; 3929 3930 REQUIRE_CALL_V(obj, getter(any_of({ 1, 5, 77 })), 3931 .RETURN(0)); 3932 3933 obj.getter(5); 3934 } 3935 REQUIRE(reports.empty()); 3936 } 3937 3938 TEST_CASE_METHOD( 3939 Fixture, 3940 "C++11: custom matcher of unlisted element is reported", 3941 "[C++11][C++14][matching][matchers][custom]") 3942 { 3943 try { 3944 mock_c obj; 3945 3946 REQUIRE_CALL_V(obj, getter(any_of({ 1,5,77 })), 3947 .RETURN(0)); 3948 3949 obj.getter(4); 3950 FAIL("didn't report"); 3951 } 3952 catch (reported) 3953 { 3954 REQUIRE(reports.size() == 1U); 3955 INFO("report=" << reports.front().msg); 3956 auto re = R":(No match for call of getter with signature int\(int\) with\. 3957 param _1 == 4 3958 3959 Tried obj\.getter\(any_of\(\{ 1,5,77 \}\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 3960 Expected _1 matching any_of\(\{ 1, 5, 77 \}\)):"; 3961 REQUIRE(is_match(reports.front().msg, re)); 3962 } 3963 } 3964 3965 TEST_CASE_METHOD( 3966 Fixture, 3967 "C++11: custom matcher can math pointer via *deref", 3968 "[C++11][C++14][matching][matchers][custom]") 3969 { 3970 { 3971 C_ptr obj; 3972 REQUIRE_CALL_V(obj, ptr(*any_of({1,5,7}))); 3973 int n = 5; 3974 obj.ptr(&n); 3975 } 3976 REQUIRE(reports.empty()); 3977 } 3978 3979 // tests of parameter matching using custom duck-typed matcher 3980 3981 TEST_CASE_METHOD( 3982 Fixture, 3983 "C++11: a non empty string gives no report", 3984 "[C++11][C++14][matching][matchers][custom]") 3985 { 3986 { 3987 mock_c obj; 3988 REQUIRE_CALL_V(obj, foo(not_empty{})); 3989 obj.foo("bar"); 3990 } 3991 REQUIRE(reports.empty()); 3992 } 3993 3994 TEST_CASE_METHOD( 3995 Fixture, 3996 "C++11: an empty string is reported", 3997 "[C++11][C++14][matching][matchers][custom]") 3998 { 3999 try { 4000 mock_c obj; 4001 REQUIRE_CALL_V(obj, foo(not_empty{})); 4002 obj.foo(""); 4003 FAIL("didn't report"); 4004 } 4005 catch (reported) 4006 { 4007 REQUIRE(reports.size() == 1U); 4008 auto re = R":(No match for call of foo with signature void\(std::string\) with\. 4009 param _1 ==. 4010 4011 Tried obj\.foo\(not_empty\{\}\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 4012 Expected _1 is not empty):"; 4013 auto& msg = reports.front().msg; 4014 INFO("msg=" << msg); 4015 REQUIRE(is_match(msg, re)); 4016 } 4017 } 4018 4019 namespace 4020 { 4021 4022 auto cxx11_is_clamped_lambda = 4023 [](std::string x, std::string min, std::string max) 4024 -> decltype(x >= min && x <= max) { 4025 return x >= min && x <= max; 4026 }; 4027 4028 auto cxx11_is_clamped_printer = 4029 [](std::ostream &os, std::string amin, std::string amax) { 4030 os << " in range ["; 4031 ::trompeloeil::print(os, amin); 4032 os << ", "; 4033 ::trompeloeil::print(os, amax); 4034 os << "]"; 4035 }; 4036 4037 } /* unnamed namespace */ 4038 4039 template <typename kind = trompeloeil::wildcard, typename T> 4040 auto 4041 cxx11_is_clamped(T min, T max) 4042 -> decltype( 4043 trompeloeil::make_matcher<kind>( 4044 cxx11_is_clamped_lambda, 4045 cxx11_is_clamped_printer, 4046 min, 4047 max)) 4048 { 4049 using trompeloeil::make_matcher; 4050 return make_matcher<kind>( 4051 cxx11_is_clamped_lambda, 4052 cxx11_is_clamped_printer, 4053 min, 4054 max); 4055 } 4056 4057 TEST_CASE_METHOD( 4058 Fixture, 4059 "C++11: a custom duck typed make_matcher-matcher that fails is reported", 4060 "[C++11][C++14][matching][matchers][custom]") 4061 { 4062 try { 4063 mock_c obj; 4064 REQUIRE_CALL_V(obj, foo(cxx11_is_clamped("b", "d"))); 4065 obj.foo(std::string("a")); 4066 FAIL("didn't report"); 4067 } 4068 catch(reported) 4069 { 4070 REQUIRE(reports.size() == 1U); 4071 auto& msg = reports.front().msg; 4072 INFO(msg); 4073 auto re = R":(No match for call of foo with signature void\(std::string\) with\. 4074 param _1 == a 4075 4076 Tried obj\.foo\(cxx11_is_clamped\("b", "d"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 4077 Expected _1 in range \[b, d\]):"; 4078 REQUIRE(is_match(msg, re)); 4079 } 4080 } 4081 4082 TEST_CASE_METHOD( 4083 Fixture, 4084 "C++11: a custom duck typed make_matcher-matcher that succeeds is not reported", 4085 "[C++11][C++14][matching][matchers][custom]") 4086 { 4087 mock_c obj; 4088 REQUIRE_CALL_V(obj, foo(cxx11_is_clamped("b", "d"))); 4089 obj.foo(std::string("c")); 4090 } 4091 4092 // tests of parameter values ostream insertion 4093 4094 TEST_CASE_METHOD( 4095 Fixture, 4096 "C++11: unknown type is printed as hex dump", 4097 "[C++11][C++14][streaming]") 4098 { 4099 std::ostringstream os; 4100 trompeloeil::print(os, unknown{}); 4101 REQUIRE(os.str() == "4-byte object={ 0x10 0x11 0x12 0x13 }"); 4102 } 4103 4104 TEST_CASE_METHOD( 4105 Fixture, 4106 "C++11: print after unknown has flags back to default", 4107 "[C++11][C++14][streaming]") 4108 { 4109 std::ostringstream os; 4110 trompeloeil::print(os, unknown{}); 4111 int16_t u = 10000; 4112 os << u; 4113 REQUIRE(os.str() == "4-byte object={ 0x10 0x11 0x12 0x13 }10000"); 4114 } 4115 4116 TEST_CASE_METHOD( 4117 Fixture, 4118 "C++11: previous formatting is ignored before hexdump and then reset", 4119 "[C++11][C++14][streaming]") 4120 { 4121 std::ostringstream os; 4122 os << std::oct << std::setfill('_') << std::setw(4) << std::left; 4123 trompeloeil::print(os, unknown{}); 4124 os << 8; 4125 REQUIRE(os.str() == "4-byte object={ 0x10 0x11 0x12 0x13 }10__"); 4126 } 4127 4128 TEST_CASE_METHOD( 4129 Fixture, 4130 "C++11: call predefined ostream operator with defaulted flags and then reset", 4131 "[C++11][C++14][streaming]") 4132 { 4133 std::ostringstream os; 4134 os << std::oct << std::setfill('_') << std::setw(4) << std::left; 4135 trompeloeil::print(os, 25); 4136 os << 8; 4137 REQUIRE(os.str() == "2510__"); 4138 } 4139 4140 TEST_CASE_METHOD( 4141 Fixture, 4142 "C++11: large unknown is multi row hex dump", 4143 "[C++11][C++14][streaming]") 4144 { 4145 struct big { 4146 char c[64] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 4147 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 4148 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 4149 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 4150 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 4151 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 4152 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 4153 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }; 4154 }; 4155 std::ostringstream os; 4156 trompeloeil::print(os, big{}); 4157 char str[] = R"__(64-byte object={ 4158 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 4159 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 4160 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f 4161 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 4162 })__"; 4163 auto s = os.str(); 4164 REQUIRE(os.str() == str); 4165 } 4166 4167 TEST_CASE_METHOD( 4168 Fixture, 4169 "C++11: unknown object is one line if 8 bytes", 4170 "[C++11][C++14][streaming]") 4171 { 4172 struct big { 4173 char c[8] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }; 4174 }; 4175 4176 std::ostringstream os; 4177 trompeloeil::print(os, big{}); 4178 REQUIRE(os.str() == "8-byte object={ 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 }"); 4179 } 4180 4181 TEST_CASE_METHOD( 4182 Fixture, 4183 "C++11: A std::pair<> is printed as { val1, val2 }", 4184 "[C++11][C++14][streaming]") 4185 { 4186 std::ostringstream os; 4187 trompeloeil::print(os, std::make_pair(3, std::string("hello"))); 4188 REQUIRE(os.str() == "{ 3, hello }"); 4189 } 4190 4191 TEST_CASE_METHOD( 4192 Fixture, 4193 "C++11: A std::tuple<> is printed as { val1, val2, val3, }", 4194 "[C++11][C++14][streaming]") 4195 { 4196 std::ostringstream os; 4197 trompeloeil::print(os, std::make_tuple(3, "hello", std::string("world"))); 4198 REQUIRE(os.str() == "{ 3, hello, world }"); 4199 } 4200 4201 TEST_CASE_METHOD( 4202 Fixture, 4203 "C++11: A C-array is printed as { val1, val2, val3 }", 4204 "[C++11][C++14][streaming]") 4205 { 4206 std::ostringstream os; 4207 std::tuple<int, std::string> v[] { 4208 std::make_tuple(1, "one"), 4209 std::make_tuple(2, "two"), 4210 std::make_tuple(3, "three") 4211 }; 4212 static_assert(!trompeloeil::is_output_streamable<decltype(v)>(),""); 4213 static_assert(trompeloeil::is_collection<decltype(v)>(), ""); 4214 trompeloeil::print(os, v); 4215 REQUIRE(os.str() == "{ { 1, one }, { 2, two }, { 3, three } }"); 4216 } 4217 4218 TEST_CASE_METHOD( 4219 Fixture, 4220 "C++11: A std::map<> is printed as { { key1, val1 }, { key2, val2 } }", 4221 "[C++11][C++14][streaming]") 4222 { 4223 std::ostringstream os; 4224 std::map<std::string, int> m{ {"one", 1}, {"two", 2 }, {"three", 3 } }; 4225 trompeloeil::print(os, m); 4226 REQUIRE(os.str() == "{ { one, 1 }, { three, 3 }, { two, 2 } }"); 4227 } 4228 4229 TEST_CASE_METHOD( 4230 Fixture, 4231 "C++11: A tuple with pairs and maps is printed element wise", 4232 "[C++11][C++14][streaming]") 4233 { 4234 std::ostringstream os; 4235 auto v = std::make_tuple(std::make_pair(3, std::string("hello")), 4236 std::map<int, std::string>{{1, "one"},{2, "two"},{3, "three"}}); 4237 trompeloeil::print(os, v); 4238 REQUIRE(os.str() == "{ { 3, hello }, { { 1, one }, { 2, two }, { 3, three } } }"); 4239 } 4240 4241 TEST_CASE_METHOD( 4242 Fixture, 4243 "C++11: A range with rvalue access proxies is printed via proxy access", 4244 "[C++11][C++14][streaming]") 4245 { 4246 std::ostringstream os; 4247 std::vector<bool> evil{false,true,true,false}; 4248 os << std::noboolalpha; 4249 trompeloeil::print(os, evil); 4250 REQUIRE(os.str() == "{ 0, 1, 1, 0 }"); 4251 } 4252 4253 TEST_CASE_METHOD( 4254 Fixture, 4255 "C++11: failure on parameter of user type is printed with custom print func", 4256 "[C++11][C++14][streaming]") 4257 { 4258 TestOutputMock m; 4259 try 4260 { 4261 m.func(nn::TestOutput{ 3 }); 4262 FAIL("didn's throw"); 4263 } 4264 catch (reported) 4265 { 4266 REQUIRE(!reports.empty()); 4267 auto re = R":(No match for call of func with signature void\(nn::TestOutput\) with\. 4268 param _1 == trompeloeil::print\(nn::TestOutput\{3\}\)):"; 4269 REQUIRE(is_match(reports.front().msg, re)); 4270 } 4271 } 4272 4273 namespace trompeloeil { 4274 template<typename T> 4275 struct printer<nn::wrapped<T>> 4276 { 4277 static void print(std::ostream &os, const nn::wrapped<T> &t) 4278 { 4279 os << "spec wrapped("; 4280 trompeloeil::print(os, t.value); 4281 os << ")"; 4282 } 4283 }; 4284 } 4285 4286 TEST_CASE_METHOD( 4287 Fixture, 4288 "C++11: failure on parameter on user template type is printed with custom printer", 4289 "[C++11][C++14][streaming]" 4290 ) 4291 { 4292 TestOutputMock m; 4293 try 4294 { 4295 m.func(nn::wrapped<int>{3}); 4296 FAIL("didn't throw"); 4297 } 4298 catch (reported) 4299 { 4300 REQUIRE(!reports.empty()); 4301 auto re = R":(No match for call of func with signature void\(nn::wrapped<int>\) with\. 4302 param _1 == spec wrapped\(3\)):"; 4303 auto& msg = reports.front().msg; 4304 CAPTURE(msg); 4305 REQUIRE(is_match(msg, re)); 4306 } 4307 4308 } 4309 4310 TEST_CASE_METHOD( 4311 Fixture, 4312 "C++11: A null-comparable object is printed as 'nullptr' if equal", 4313 "[C++11][C++14][streaming]") 4314 { 4315 std::ostringstream os; 4316 trompeloeil::print(os, null_comparable{nullptr}); 4317 REQUIRE(os.str() == "nullptr"); 4318 } 4319 4320 TEST_CASE_METHOD( 4321 Fixture, 4322 "C++11: A null-comparable object is printed as using its ostream insertion if unequal", 4323 "[C++11][C++14][streaming]") 4324 { 4325 std::ostringstream os; 4326 trompeloeil::print(os, null_comparable{&os}); 4327 REQUIRE(os.str() == "null_comparable"); 4328 } 4329 4330 TEST_CASE_METHOD( 4331 Fixture, 4332 "C++11: An object for which null-compare is non-bool, is printed using its ostream insertion", 4333 "[C++11][C++14][streaming]") 4334 { 4335 std::ostringstream os; 4336 trompeloeil::print(os, pseudo_null_comparable{}); 4337 REQUIRE(os.str() == "pseudo_null_comparable"); 4338 } 4339 4340 #if !(defined(_MSC_VER) && _MSC_VER < 1910) 4341 // Disable this test case for Microsoft Visual Studio 2015 4342 // until a working implementation of is_null_comparable is found 4343 // for this compiler. 4344 TEST_CASE_METHOD( 4345 Fixture, 4346 "C++11: An object that is constructible from null, but not comparable with null, is printed using its ostream insertion", 4347 "[C++11][C++14][streaming]") 4348 { 4349 std::ostringstream os; 4350 trompeloeil::print(os, null_constructible{nullptr}); 4351 REQUIRE(os.str() == "null_constructible"); 4352 } 4353 #endif /* !(defined(_MSC_VER) && _MSC_VER < 1910) */ 4354 4355 // tests on scoping (lifetime) of expectations 4356 4357 TEST_CASE_METHOD( 4358 Fixture, 4359 "C++11: require calls are matched in reversed order of creation", 4360 "[C++11][C++14][scoping]") 4361 { 4362 { 4363 mock_c obj; 4364 4365 ALLOW_CALL_V(obj, getter(_, _), 4366 .SIDE_EFFECT(_2 = std::to_string(_1))); 4367 4368 REQUIRE_CALL_V(obj, getter(3, _), 4369 .SIDE_EFFECT(_2 = "III") 4370 .TIMES(2)); 4371 4372 std::string s; 4373 obj.getter(2, s); 4374 4375 REQUIRE(s == "2"); 4376 4377 obj.getter(3, s); 4378 4379 REQUIRE(s == "III"); 4380 4381 obj.getter(1, s); 4382 4383 REQUIRE(s == "1"); 4384 4385 obj.getter(3, s); 4386 4387 REQUIRE(s == "III"); 4388 4389 obj.getter(3, s); 4390 4391 REQUIRE(s == "3"); 4392 } 4393 REQUIRE(reports.empty()); 4394 } 4395 4396 TEST_CASE_METHOD( 4397 Fixture, 4398 "C++11: require calls are removed when they go out of scope", 4399 "[C++11][C++14][scoping]") 4400 { 4401 { 4402 mock_c obj; 4403 std::string s; 4404 4405 ALLOW_CALL_V(obj, getter(_, _), 4406 .SIDE_EFFECT(_2 = std::to_string(_1))); 4407 4408 { 4409 REQUIRE_CALL_V(obj, getter(3, _), 4410 .SIDE_EFFECT(_2 = "III") 4411 .TIMES(1, 8)); 4412 4413 obj.getter(3, s); 4414 4415 REQUIRE(s == "III"); 4416 4417 obj.getter(2, s); 4418 4419 REQUIRE(s == "2"); 4420 4421 obj.getter(3, s); 4422 4423 REQUIRE(s == "III"); 4424 } 4425 4426 obj.getter(3, s); 4427 4428 REQUIRE(s == "3"); 4429 } 4430 REQUIRE(reports.empty()); 4431 } 4432 4433 TEST_CASE_METHOD( 4434 Fixture, 4435 "C++11: a pending unsatisfied require call is reported at end of scope", 4436 "[C++11][C++14][scoping]") 4437 { 4438 mock_c obj; 4439 { 4440 REQUIRE_CALL_V(obj, foo("bar")); 4441 } 4442 REQUIRE(reports.size() == 1U); 4443 4444 auto re = R":(Unfulfilled expectation: 4445 Expected obj\.foo\("bar"\) to be called once, actually never called 4446 param _1 == bar):"; 4447 INFO(reports.front().msg); 4448 REQUIRE(is_match(reports.front().msg, re)); 4449 } 4450 4451 TEST_CASE_METHOD( 4452 Fixture, 4453 "C++11: Unfulfilled expectation with ANY is reported with expected values all parameters", 4454 "[C++11][C++14][scoping][wildcard]") 4455 { 4456 { 4457 mock_c obj; 4458 REQUIRE_CALL_V(obj, func(3, ANY(std::string&))); 4459 std::string s = "foo"; 4460 } 4461 4462 REQUIRE(reports.size() == 1U); 4463 auto re = std::string(R":(Unfulfilled expectation: 4464 Expected obj\.func\(3, ):") + 4465 escape_parens(CXX11_AS_STRING(ANY(std::string&))) + 4466 std::string(R":(\) to be called once, actually never called 4467 param _1 == 3 4468 param _2 matching ANY\(std::string&\)):"); 4469 auto& msg = reports.front().msg; 4470 INFO("msg=" << msg); 4471 INFO("re=" << re); 4472 REQUIRE(is_match(msg, re)); 4473 } 4474 4475 TEST_CASE_METHOD( 4476 Fixture, 4477 "C++11: Unfulfilled expectation with _ is reported with expected values all parameters", 4478 "[C++11][C++14][scoping][wildcard]") 4479 { 4480 { 4481 mock_c obj; 4482 REQUIRE_CALL_V(obj, func(3, _)); 4483 std::string s = "foo"; 4484 } 4485 4486 REQUIRE(reports.size() == 1U); 4487 auto re = R":(Unfulfilled expectation: 4488 Expected obj\.func\(3, _\) to be called once, actually never called 4489 param _1 == 3 4490 param _2 matching _):"; 4491 auto& msg = reports.front().msg; 4492 INFO("msg=" << msg); 4493 REQUIRE(is_match(msg, re)); 4494 } 4495 4496 // test of multiplicity retiring expectations, fulfilled or not 4497 4498 TEST_CASE_METHOD( 4499 Fixture, 4500 "C++11: FORBID_CALL is always both satisfied and saturated", 4501 "[C++11][C++14][multiplicity]") 4502 { 4503 { 4504 mock_c obj; 4505 auto e = NAMED_FORBID_CALL_V(obj, count()); 4506 REQUIRE(e->is_satisfied()); 4507 REQUIRE(e->is_saturated()); 4508 } 4509 REQUIRE(reports.empty()); 4510 } 4511 4512 TEST_CASE_METHOD( 4513 Fixture, 4514 "C++11: ALLOW_ALL is always satisfied and never saturated", 4515 "[C++11][C++14][multiplicity]") 4516 { 4517 { 4518 mock_c obj; 4519 4520 auto e = NAMED_ALLOW_CALL_V(obj, count(), 4521 .RETURN(1)); 4522 4523 REQUIRE(e->is_satisfied()); 4524 REQUIRE(!e->is_saturated()); 4525 obj.count(); 4526 REQUIRE(e->is_satisfied()); 4527 REQUIRE(!e->is_saturated()); 4528 obj.count(); 4529 REQUIRE(e->is_satisfied()); 4530 REQUIRE(!e->is_saturated()); 4531 } 4532 REQUIRE(reports.empty()); 4533 } 4534 4535 TEST_CASE_METHOD( 4536 Fixture, 4537 "C++11: .TIMES is satisfied when min calls is reached, and not saturated until max calls is reached", 4538 "[C++11][C++14][multiplicity]") 4539 { 4540 { 4541 mock_c obj; 4542 4543 auto e = NAMED_REQUIRE_CALL_V(obj, count(), 4544 .TIMES(1,3) 4545 .RETURN(1)); 4546 4547 REQUIRE(!e->is_satisfied()); 4548 REQUIRE(!e->is_saturated()); 4549 obj.count(); 4550 REQUIRE(e->is_satisfied()); 4551 REQUIRE(!e->is_saturated()); 4552 obj.count(); 4553 REQUIRE(e->is_satisfied()); 4554 REQUIRE(!e->is_saturated()); 4555 obj.count(); 4556 REQUIRE(e->is_satisfied()); 4557 REQUIRE(e->is_saturated()); 4558 } 4559 REQUIRE(reports.empty()); 4560 } 4561 4562 TEST_CASE_METHOD( 4563 Fixture, 4564 "C++11: unsatisfied expectation when mock dies is reported", 4565 "[C++11][C++14][scoping][multiplicity]") 4566 { 4567 auto m = detail::make_unique<mock_c>(); 4568 4569 REQUIRE_CALL_V(*m, count(), 4570 .RETURN(1)); 4571 4572 m.reset(); 4573 REQUIRE(reports.size() == 1U); 4574 INFO(reports.front().msg); 4575 auto re = R":(Pending expectation on destroyed mock object: 4576 Expected .*count\(\) to be called once, actually never called):"; 4577 REQUIRE(is_match(reports.front().msg, re)); 4578 } 4579 4580 TEST_CASE_METHOD( 4581 Fixture, 4582 "C++11: multiple unsatisfied expectation when mock dies are reported in mock definition order", 4583 "[C++11][C++14][scoping][multiplicity]") 4584 { 4585 auto m = detail::make_unique<mock_c>(); 4586 4587 REQUIRE_CALL_V(*m, count(), 4588 .RETURN(1)); 4589 4590 REQUIRE_CALL_V(*m, getter(1), 4591 .RETURN(1)); 4592 4593 m.reset(); 4594 REQUIRE(reports.size() == 2U); 4595 INFO(reports[0].msg); 4596 INFO(reports[1].msg); 4597 auto re_count = R":(Pending expectation on destroyed mock object: 4598 Expected .*count\(\) to be called once, actually never called):"; 4599 auto re_getter = R":(Pending expectation on destroyed mock object: 4600 Expected .*getter\(1\) to be called once, actually never called):"; 4601 REQUIRE(is_match(reports[0].msg, re_getter)); 4602 REQUIRE(is_match(reports[1].msg, re_count)); 4603 } 4604 4605 TEST_CASE_METHOD( 4606 Fixture, 4607 "C++11: active allow call when mock dies is not reported", 4608 "[C++11][C++14][scoping][multiplicity]") 4609 { 4610 { 4611 auto m = detail::make_unique<mock_c>(); 4612 4613 ALLOW_CALL_V(*m, count(), 4614 .RETURN(1)); 4615 4616 m.reset(); 4617 } 4618 REQUIRE(reports.empty()); 4619 } 4620 4621 TEST_CASE_METHOD( 4622 Fixture, 4623 "C++11: active forbid call when mock dies is not reported", 4624 "[C++11][C++14][scoping][multiplicity]") 4625 { 4626 { 4627 auto m = detail::make_unique<mock_c>(); 4628 FORBID_CALL_V(*m, count()); 4629 m.reset(); 4630 } 4631 REQUIRE(reports.empty()); 4632 } 4633 4634 TEST_CASE_METHOD( 4635 Fixture, 4636 "C++11: saturated expectation when mock dies is not reported", 4637 "[C++11][C++14][scoping]") 4638 { 4639 { 4640 auto m = detail::make_unique<mock_c>(); 4641 4642 REQUIRE_CALL_V(*m, count(), 4643 .RETURN(1)); 4644 4645 m->count(); 4646 m.reset(); 4647 } 4648 REQUIRE(reports.empty()); 4649 } 4650 4651 TEST_CASE_METHOD( 4652 Fixture, 4653 "C++11: no calls reported as never called", 4654 "[C++11][C++14][scoping][multiplicity]") 4655 { 4656 mock_c obj; 4657 { 4658 REQUIRE_CALL_V(obj, count(), 4659 .RETURN(1)); 4660 } 4661 REQUIRE(reports.size() == 1U); 4662 REQUIRE(is_match(reports.front().msg, "actually never called")); 4663 } 4664 4665 TEST_CASE_METHOD( 4666 Fixture, 4667 "C++11: undersatisfied with one call reported as once", 4668 "[C++11][C++14][scoping][multiplicity]") 4669 { 4670 mock_c obj; 4671 { 4672 REQUIRE_CALL_V(obj, count(), 4673 .RETURN(1) 4674 .TIMES(2)); 4675 4676 obj.count(); 4677 } 4678 REQUIRE(reports.size() == 1U); 4679 REQUIRE(is_match(reports.front().msg, "actually called once")); 4680 } 4681 4682 TEST_CASE_METHOD( 4683 Fixture, 4684 "C++11: undersatisfied with two call reported as count", 4685 "[C++11][C++14][scoping][multiplicity]") 4686 { 4687 mock_c obj; 4688 { 4689 REQUIRE_CALL_V(obj, count(), 4690 .RETURN(1) 4691 .TIMES(3)); 4692 4693 obj.count(); 4694 obj.count(); 4695 } 4696 REQUIRE(reports.size() == 1U); 4697 REQUIRE(is_match(reports.front().msg, "actually called 2 times")); 4698 } 4699 4700 TEST_CASE_METHOD( 4701 Fixture, 4702 "C++11: no calls when one required reported as expected once", 4703 "[C++11][C++14][scoping][multiplicity]") 4704 { 4705 mock_c obj; 4706 { 4707 REQUIRE_CALL_V(obj, count(), 4708 .RETURN(1)); 4709 } 4710 REQUIRE(reports.size() == 1U); 4711 REQUIRE(is_match(reports.front().msg, "to be called once")); 4712 } 4713 4714 TEST_CASE_METHOD( 4715 Fixture, 4716 "C++11: no calls when two required reported as expected 2 times", 4717 "[C++11][C++14][scoping][multiplicity]") 4718 { 4719 mock_c obj; 4720 { 4721 REQUIRE_CALL_V(obj, count(), 4722 .TIMES(2) 4723 .RETURN(1)); 4724 } 4725 REQUIRE(reports.size() == 1U); 4726 REQUIRE(is_match(reports.front().msg, "to be called 2 times")); 4727 } 4728 4729 TEST_CASE_METHOD( 4730 Fixture, 4731 "C++11: TIMES works for templated mock classes", 4732 "[C++11][C++14][multiplicity][templates]") 4733 { 4734 try 4735 { 4736 tmock<int> m; 4737 m.func(3); 4738 FAIL("didn't throw"); 4739 } 4740 catch (reported) 4741 { 4742 REQUIRE(reports.size() == 1U); 4743 INFO(reports.front().msg); 4744 REQUIRE(is_match(reports.front().msg, "Match of forbidden call")); 4745 } 4746 } 4747 4748 // test of destruction, or lack of, for deathwatched objects 4749 4750 TEST_CASE_METHOD( 4751 Fixture, 4752 "C++11: an unexpected destruction of monitored object is reported", 4753 "[C++11][C++14][deathwatched]") 4754 { 4755 { 4756 trompeloeil::deathwatched<mock_c> obj; 4757 } 4758 REQUIRE(reports.size() == 1U); 4759 REQUIRE(is_match(reports.front().msg, "Unexpected destruction of.*@")); 4760 } 4761 4762 TEST_CASE_METHOD( 4763 Fixture, 4764 "C++11: an expected destruction of monitored object is not reported", 4765 "[C++11][C++14][deathwatched]") 4766 { 4767 { 4768 auto obj = new trompeloeil::deathwatched<mock_c>; 4769 REQUIRE_DESTRUCTION(*obj); 4770 delete obj; 4771 } 4772 REQUIRE(reports.empty()); 4773 } 4774 4775 TEST_CASE_METHOD( 4776 Fixture, 4777 "C++11: a copy of a deathwatched object with expectation is not expected to die", 4778 "[C++11][C++14][deathwatched]") 4779 { 4780 auto orig = new trompeloeil::deathwatched<none>; 4781 REQUIRE_DESTRUCTION(*orig); 4782 auto copy = new trompeloeil::deathwatched<none>(*orig); 4783 4784 delete orig; 4785 REQUIRE(reports.empty()); 4786 4787 delete copy; 4788 REQUIRE(reports.size() == 1U); 4789 REQUIRE(is_match(reports.front().msg, "Unexpected destruction of .*@")); 4790 } 4791 4792 TEST_CASE_METHOD( 4793 Fixture, 4794 "C++11: a deathwatched object move constructed from original with expectation is not expected to die and the original still is", 4795 "[C++11][C++14][deathwatched]") 4796 { 4797 auto orig = new trompeloeil::deathwatched<none>; 4798 REQUIRE_DESTRUCTION(*orig); 4799 auto copy = new trompeloeil::deathwatched<none>(std::move(*orig)); 4800 4801 delete orig; 4802 REQUIRE(reports.empty()); 4803 4804 delete copy; 4805 REQUIRE(reports.size() == 1U); 4806 REQUIRE(is_match(reports.front().msg, "Unexpected destruction of .*@")); 4807 } 4808 4809 TEST_CASE_METHOD( 4810 Fixture, 4811 "C++11: object alive when destruction expectation goes out of scope is reported", 4812 "[C++11][C++14][deathwatched]") 4813 { 4814 trompeloeil::deathwatched<mock_c> obj; 4815 { 4816 std::unique_ptr<trompeloeil::expectation> p = NAMED_REQUIRE_DESTRUCTION(obj); 4817 } 4818 REQUIRE(reports.size() == 1U); 4819 REQUIRE(is_match(reports.front().msg, "Object obj is still alive")); 4820 } 4821 4822 TEST_CASE_METHOD( 4823 Fixture, 4824 "C++11: require destruction is neither satisfied nor saturated while object is alive", 4825 "[C++11][C++14][deathwatched]") 4826 { 4827 { 4828 using monitor = std::unique_ptr<trompeloeil::lifetime_monitor>; 4829 4830 auto obj = new trompeloeil::deathwatched<mock_c>(); 4831 monitor p = NAMED_REQUIRE_DESTRUCTION(*obj); 4832 REQUIRE(!p->is_saturated()); 4833 REQUIRE(!p->is_satisfied()); 4834 delete obj; 4835 } 4836 REQUIRE(reports.empty()); 4837 } 4838 4839 TEST_CASE_METHOD( 4840 Fixture, 4841 "C++11: require destruction is both satisfied and saturated when object is destroyed", 4842 "[C++11][C++14][deathwatched]") 4843 { 4844 auto obj = new trompeloeil::deathwatched<mock_c>(); 4845 auto p = NAMED_REQUIRE_DESTRUCTION(*obj); 4846 delete obj; 4847 REQUIRE(p->is_saturated()); 4848 REQUIRE(p->is_satisfied()); 4849 } 4850 4851 TEST_CASE_METHOD( 4852 Fixture, 4853 "C++11: require destruction succeeds also without deathwatch", 4854 "[C++11][C++14][deathwatched]") 4855 { 4856 { 4857 auto obj = new trompeloeil::deathwatched<mock_c>; 4858 REQUIRE_DESTRUCTION(*obj); 4859 delete obj; 4860 } 4861 REQUIRE(reports.empty()); 4862 } 4863 4864 TEST_CASE_METHOD( 4865 Fixture, 4866 "C++11: a deathwatched object's constructor passes params to mock", 4867 "[C++11][C++14][deathwatched]") 4868 { 4869 auto obj = new trompeloeil::deathwatched<mock_c>{ "apa" }; 4870 REQUIRE(obj->p_ == std::string("apa")); 4871 REQUIRE_DESTRUCTION(*obj); 4872 delete obj; 4873 } 4874 4875 TEST_CASE_METHOD( 4876 Fixture, 4877 "C++11: require destruction fulfilled in sequence is not reported", 4878 "[C++11][C++14][deathwatched][sequences]") 4879 { 4880 auto obj = new trompeloeil::deathwatched<mock_c>; 4881 trompeloeil::sequence s; 4882 REQUIRE_CALL_V(*obj, foo("foo"), 4883 .IN_SEQUENCE(s)); 4884 REQUIRE_DESTRUCTION(*obj) 4885 .IN_SEQUENCE(s); 4886 obj->foo("foo"); 4887 delete obj; 4888 REQUIRE(reports.empty()); 4889 } 4890 4891 TEST_CASE_METHOD( 4892 Fixture, 4893 "C++11: require destruction fulfilled in sequence with another object is not reported", 4894 "[C++11][C++14][deathwatched][sequences]") 4895 { 4896 auto obj = new trompeloeil::deathwatched<mock_c>; 4897 mock_c obj2; 4898 trompeloeil::sequence s; 4899 REQUIRE_DESTRUCTION(*obj) 4900 .IN_SEQUENCE(s); 4901 REQUIRE_CALL_V(obj2, foo("foo"), 4902 .IN_SEQUENCE(s)); 4903 delete obj; 4904 obj2.foo("foo"); 4905 REQUIRE(reports.empty()); 4906 } 4907 4908 TEST_CASE_METHOD( 4909 Fixture, 4910 "C++11: named require destruction fulfilled in sequence is not reported", 4911 "[C++11][C++14][deathwatched][sequences]") 4912 { 4913 auto obj = new trompeloeil::deathwatched<mock_c>; 4914 trompeloeil::sequence s; 4915 REQUIRE_CALL_V(*obj, foo("foo"), 4916 .IN_SEQUENCE(s)); 4917 auto d = NAMED_REQUIRE_DESTRUCTION(*obj) 4918 .IN_SEQUENCE(s); 4919 obj->foo("foo"); 4920 delete obj; 4921 REQUIRE(reports.empty()); 4922 } 4923 4924 TEST_CASE_METHOD( 4925 Fixture, 4926 "C++11: require destruction fulfilled out of sequence is reported", 4927 "[C++11][C++14][deathwatched][sequences]") 4928 { 4929 auto obj = new trompeloeil::deathwatched<mock_c>; 4930 trompeloeil::sequence s; 4931 REQUIRE_CALL_V(*obj, foo("foo"), 4932 .IN_SEQUENCE(s)); 4933 REQUIRE_DESTRUCTION(*obj) 4934 .IN_SEQUENCE(s); 4935 delete obj; 4936 REQUIRE(!reports.empty()); 4937 auto& msg = reports.front().msg; 4938 auto re = R":(Sequence mismatch for sequence "s".*destructor for \*obj at [A-Za-z0-9_ ./:\]*:[0-9]*.*foo"):"; 4939 INFO("msg=" << msg); 4940 REQUIRE(is_match(msg, re)); 4941 } 4942 4943 TEST_CASE_METHOD( 4944 Fixture, 4945 "C++11: sequence mismatch with require destruction first is reported", 4946 "[C++11][C++14][deathwatched][sequences]") 4947 { 4948 std::unique_ptr<trompeloeil::deathwatched<mock_c>> obj; 4949 try { 4950 obj.reset(new trompeloeil::deathwatched<mock_c>); 4951 trompeloeil::sequence s; 4952 REQUIRE_DESTRUCTION(*obj) 4953 .IN_SEQUENCE(s); 4954 REQUIRE_CALL_V(*obj, foo("foo"), 4955 .IN_SEQUENCE(s)); 4956 obj->foo("foo"); 4957 FAIL("didn't throw"); 4958 } 4959 catch (reported&) 4960 { 4961 REQUIRE(!reports.empty()); 4962 auto& msg = reports.front().msg; 4963 auto re = R":(Sequence mismatch for sequence "s".*\*obj.foo\("foo"\) at [A-Za-z0-9_ ./:\]*:[0-9]*.*Sequence.* REQUIRE_DESTRUCTION\(\*obj\)):"; 4964 INFO("msg=" << msg); 4965 REQUIRE(is_match(msg, re)); 4966 } 4967 } 4968 4969 TEST_CASE_METHOD( 4970 Fixture, 4971 "C++11: named require destruction fulfilled out of sequence is reported", 4972 "[C++11][C++14][deathwatched][sequences]") 4973 { 4974 auto obj = new trompeloeil::deathwatched<mock_c>; 4975 trompeloeil::sequence s; 4976 REQUIRE_CALL_V(*obj, foo("foo"), 4977 .IN_SEQUENCE(s)); 4978 auto d = NAMED_REQUIRE_DESTRUCTION(*obj) 4979 .IN_SEQUENCE(s); 4980 delete obj; 4981 REQUIRE(!reports.empty()); 4982 auto& msg = reports.front().msg; 4983 auto re = R":(Sequence mismatch for sequence "s".*destructor for \*obj at [A-Za-z0-9_ ./:\]*:[0-9]*.*foo"):"; 4984 INFO("msg=" << msg); 4985 REQUIRE(is_match(msg, re)); 4986 } 4987 4988 TEST_CASE_METHOD( 4989 Fixture, 4990 "C++11: sequence mismatch with named require destruction first is reported", 4991 "[C++11][C++14][deathwatched][sequences]") 4992 { 4993 std::unique_ptr<trompeloeil::deathwatched<mock_c>> obj; 4994 try { 4995 obj.reset(new trompeloeil::deathwatched<mock_c>); 4996 trompeloeil::sequence s; 4997 auto d = NAMED_REQUIRE_DESTRUCTION(*obj) 4998 .IN_SEQUENCE(s); 4999 REQUIRE_CALL_V(*obj, foo("foo"), 5000 .IN_SEQUENCE(s)); 5001 obj->foo("foo"); 5002 FAIL("didn't throw"); 5003 } 5004 catch (reported&) 5005 { 5006 REQUIRE(!reports.empty()); 5007 auto& msg = reports.front().msg; 5008 auto re = R":(Sequence mismatch for sequence "s".*\*obj.foo\("foo"\) at [A-Za-z0-9_ ./:\]*:[0-9]*.*Sequence.* NAMED_REQUIRE_DESTRUCTION\(\*obj\)):"; 5009 INFO("msg=" << msg); 5010 REQUIRE(is_match(msg, re)); 5011 } 5012 } 5013 5014 // tests of calls that do not match any valid expectations 5015 5016 TEST_CASE_METHOD( 5017 Fixture, 5018 "C++11: unmatched call is reported", 5019 "[C++11][C++14][mismatches]") 5020 { 5021 try { 5022 mock_c obj; 5023 obj.getter(7); 5024 FAIL("didn't throw!"); 5025 } 5026 catch (reported) 5027 { 5028 REQUIRE(reports.size() == 1U); 5029 auto re = R":(No match for call of getter with signature int\(int\) with\. 5030 param _1 == 7):"; 5031 REQUIRE(is_match(reports.front().msg, re)); 5032 } 5033 } 5034 5035 TEST_CASE_METHOD( 5036 Fixture, 5037 "C++11: match of saturated call is reported", 5038 "[C++11][C++14][mismatches]") 5039 { 5040 int count = 0; 5041 try { 5042 mock_c obj; 5043 5044 ALLOW_CALL_V(obj, getter(ANY(int)), 5045 .WITH(_1 != 3) 5046 .RETURN(1)); 5047 5048 REQUIRE_CALL_V(obj, getter(3), 5049 .TIMES(3) 5050 .RETURN(1)); 5051 5052 count += obj.getter(4); // 1 5053 count += obj.getter(2); // 2 5054 count += obj.getter(3); // 3 -> 1 5055 count += obj.getter(4); // 4 5056 count += obj.getter(2); // 5 5057 count += obj.getter(3); // 6 -> 2 5058 count += obj.getter(5); // 7 5059 count += obj.getter(3); // 8 -> 3 5060 count += obj.getter(8); // 9 5061 count += obj.getter(3); // boom! 5062 FAIL("didn't report"); 5063 } 5064 catch (reported) 5065 { 5066 REQUIRE(count == 9); 5067 REQUIRE(reports.size() == 1U); 5068 auto re = 5069 R":(No match for call of getter with signature int\(int\) with\. 5070 param _1 == 3 5071 5072 Matches saturated call requirement 5073 obj\.getter\(3\) at [A-Za-z0-9_ ./:\]*:[0-9]*.*):"; 5074 REQUIRE(is_match(reports.front().msg, re)); 5075 } 5076 } 5077 5078 TEST_CASE_METHOD( 5079 Fixture, 5080 "C++11: a matching call that throws is saturated", 5081 "[C++11][C++14][mismatches]") 5082 { 5083 int count = 0; 5084 try { 5085 mock_c obj; 5086 5087 ALLOW_CALL_V(obj, getter(ANY(int)), 5088 .WITH(_1 != 3) 5089 .RETURN(1)); 5090 5091 REQUIRE_CALL_V(obj, getter(3), 5092 .TIMES(3) 5093 .SIDE_EFFECT(throw 0) 5094 .RETURN(1)); 5095 5096 count += obj.getter(4); // 1 5097 count += obj.getter(2); // 2 5098 try { 5099 count += obj.getter(3); // 2 -> 1 5100 FAIL("didn't throw"); 5101 } 5102 catch (int) {} 5103 count += obj.getter(4); // 3 5104 count += obj.getter(2); // 4 5105 try { 5106 count += obj.getter(3); // 4 -> 2 5107 FAIL("didn't throw"); 5108 } 5109 catch (int) {} 5110 count += obj.getter(5); // 5 5111 try { 5112 count += obj.getter(3); // 5 -> 3 5113 FAIL("didn't throw"); 5114 } 5115 catch (int) {} 5116 count += obj.getter(8); // 6 5117 count += obj.getter(3); // boom! 5118 FAIL("didn't report"); 5119 } 5120 catch (reported) 5121 { 5122 REQUIRE(count == 6); 5123 REQUIRE(reports.size() == 1U); 5124 auto re = 5125 R":(No match for call of getter with signature int\(int\) with\. 5126 param _1 == 3 5127 5128 Matches saturated call requirement 5129 obj\.getter\(3\) at [A-Za-z0-9_ ./:\]*:[0-9]*.*):"; 5130 REQUIRE(is_match(reports.front().msg, re)); 5131 } 5132 } 5133 5134 TEST_CASE_METHOD( 5135 Fixture, 5136 "C++11: unmatched call with mismatching requirements is reported", 5137 "[C++11][C++14][mismatches]") 5138 { 5139 try { 5140 mock_c obj; 5141 5142 REQUIRE_CALL_V(obj, getter(4), 5143 .RETURN(0)); 5144 5145 REQUIRE_CALL_V(obj, getter(5), 5146 .RETURN(0)); 5147 5148 obj.getter(3); 5149 } 5150 catch (reported) 5151 { 5152 REQUIRE(reports.size() == 1U); 5153 auto re = 5154 R":(No match for call of getter with signature int\(int\) with\. 5155 param _1 == 3 5156 5157 Tried obj\.getter\(5\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 5158 Expected _1 == 5 5159 5160 Tried obj\.getter\(4\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 5161 Expected _1 == 4):"; 5162 REQUIRE(is_match(reports.front().msg, re)); 5163 } 5164 } 5165 5166 TEST_CASE_METHOD( 5167 Fixture, 5168 "C++11: unmatched with wildcard reports failed WITH clauses", 5169 "[C++11][C++14][mismatches]") 5170 { 5171 try { 5172 mock_c obj; 5173 5174 REQUIRE_CALL_V(obj, getter(ANY(int)), 5175 .WITH(_1 < 3) 5176 .WITH(_1 > 5) 5177 .RETURN(0)); 5178 5179 obj.getter(4); 5180 } 5181 catch (reported) 5182 { 5183 REQUIRE(reports.size() == 1U); 5184 auto re = 5185 R":(No match for call of getter with signature int\(int\) with\. 5186 param _1 == 4 5187 5188 Tried obj\.getter\():" + 5189 escape_parens(CXX11_AS_STRING(ANY(int))) + 5190 R":(\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 5191 Failed WITH\(_1 < 3\) 5192 Failed WITH\(_1 > 5\)):"; 5193 auto& msg = reports.front().msg; 5194 INFO("msg=" << msg); 5195 INFO("re=" << re); 5196 REQUIRE(is_match(reports.front().msg, re)); 5197 } 5198 } 5199 5200 TEST_CASE_METHOD( 5201 Fixture, 5202 "C++11: unmatched with wildcard reports only failed WITH clauses", 5203 "[C++11][C++14][mismatches]") 5204 { 5205 try { 5206 mock_c obj; 5207 5208 REQUIRE_CALL_V(obj, getter(ANY(int)), 5209 .WITH(_1 < 3) 5210 .WITH(_1 > 3) 5211 .RETURN(0)); 5212 5213 obj.getter(4); 5214 } 5215 catch (reported) 5216 { 5217 REQUIRE(reports.size() == 1U); 5218 auto re = 5219 R":(No match for call of getter with signature int\(int\) with\. 5220 param _1 == 4 5221 5222 Tried obj\.getter\():" + 5223 escape_parens(CXX11_AS_STRING(ANY(int))) + 5224 R":(\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 5225 Failed WITH\(_1 < 3\)):"; 5226 auto& msg = reports.front().msg; 5227 INFO("msg=" << msg); 5228 INFO("re=" << re); 5229 REQUIRE(is_match(msg, re)); 5230 } 5231 } 5232 5233 TEST_CASE_METHOD( 5234 Fixture, 5235 "C++11: match of forbidden call is reported", 5236 "[C++11][C++14][mismatches]") 5237 { 5238 try { 5239 mock_c obj; 5240 5241 ALLOW_CALL_V(obj, getter(ANY(int)), 5242 .RETURN(0)); 5243 5244 FORBID_CALL_V(obj, getter(3)); 5245 5246 obj.getter(4); 5247 obj.getter(2); 5248 obj.getter(3); 5249 FAIL("didn't report"); 5250 } 5251 catch (reported) 5252 { 5253 REQUIRE(reports.size() == 1U); 5254 INFO("report=" << reports.front().msg); 5255 auto re = R":(Match of forbidden call of obj\.getter\(3\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 5256 param _1 == 3):"; 5257 REQUIRE(is_match(reports.front().msg, re)); 5258 } 5259 } 5260 5261 TEST_CASE_METHOD( 5262 Fixture, 5263 "C++11: Mismatched call to a mocked function with param from template is reported", 5264 "[C++11][C++14][mismatches][templates]") 5265 { 5266 try { 5267 tmock<int> obj; 5268 REQUIRE_CALL_V(obj, tfunc(3)); 5269 obj.tfunc(2); 5270 FAIL("didn't report"); 5271 } 5272 catch (reported) 5273 { 5274 REQUIRE(reports.size() == 1U); 5275 INFO("report=" << reports.front().msg); 5276 auto re = R":(No match for call of tfunc with signature void\(T\) with\. 5277 param _1 == 2 5278 5279 Tried obj\.tfunc\(3\) at [A-Za-z0-9_ ./:\]*:[0-9]*.* 5280 Expected _1 == 3):"; 5281 REQUIRE(is_match(reports.front().msg, re)); 5282 } 5283 } 5284 5285 // tests of parameter passing to expectations 5286 5287 TEST_CASE_METHOD( 5288 Fixture, 5289 "C++11: parameters are passed in correct order when matching", 5290 "[C++11][C++14][parameters]") 5291 { 5292 T obj; 5293 5294 REQUIRE_CALL_V(obj, concats(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), 5295 .RETURN("")); 5296 5297 auto s = obj.concats(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); 5298 REQUIRE(s == ""); 5299 } 5300 5301 TEST_CASE_METHOD( 5302 Fixture, 5303 "C++11: parameters are passed in correct order to WITH", 5304 "[C++11][C++14][parameters]") 5305 { 5306 T obj; 5307 5308 REQUIRE_CALL_V(obj, concats(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _), 5309 .WITH(_1 == 1 && _2 == 2 && _3 == 3 && _4 == 4 && 5310 _5 == 5 && _6 == 6 && _7 == 7 && _8 == 8 && 5311 _9 == 9 && _10 == 10 && _11 == 11 && _12 == 12 && 5312 _13 == 13 && _14 == 14 && _15 == 15) 5313 .RETURN("")); 5314 5315 auto s = obj.concats(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); 5316 REQUIRE(s == ""); 5317 } 5318 5319 TEST_CASE_METHOD( 5320 Fixture, 5321 "C++11: parameters are passed in correct order to LR_SIDE_EFFECT", 5322 "[C++11][C++14][parameters]") 5323 { 5324 T obj; 5325 int n = 0; 5326 5327 REQUIRE_CALL_V(obj, concats(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _), 5328 .LR_SIDE_EFFECT(n = _1 + _2 - _3 + _4 - _5 + _6 - _7 + _8 - _9 + _10 - _11 + _12 - _13 + _14 - _15) 5329 .RETURN("")); 5330 5331 auto s = obj.concats(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); 5332 REQUIRE(n == -6); 5333 } 5334 5335 TEST_CASE_METHOD( 5336 Fixture, 5337 "C++11: parameters are passed in correct order to RETURN", 5338 "[C++11][C++14][parameters]") 5339 { 5340 T obj; 5341 5342 REQUIRE_CALL_V(obj, concats(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _), 5343 .RETURN(std::to_string(_1) + 5344 std::to_string(_2) + 5345 std::to_string(_3) + 5346 std::to_string(_4) + 5347 std::to_string(_5) + 5348 std::to_string(_6) + 5349 std::to_string(_7) + 5350 std::to_string(_8) + 5351 std::to_string(_9) + 5352 std::to_string(_10) + 5353 std::to_string(_11) + 5354 std::to_string(_12) + 5355 std::to_string(_13) + 5356 std::to_string(_14) + 5357 std::to_string(_15))); 5358 5359 auto s = obj.concats(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); 5360 REQUIRE(s == "123456789101112131415"); 5361 } 5362 5363 TEST_CASE_METHOD( 5364 Fixture, 5365 "C++11: parameters are passed in correct order to THROW", 5366 "[C++11][C++14][parameters]") 5367 { 5368 T obj; 5369 5370 REQUIRE_CALL_V(obj, concats(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _), 5371 .THROW(_1 + _2 - _3 + _4 - _5 + _6 - _7 + _8 - _9 + _10 - _11 + _12 - _13 + _14 - _15)); 5372 5373 try { 5374 obj.concats(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); 5375 FAIL("didn't throw"); 5376 } 5377 catch (int n) 5378 { 5379 REQUIRE(n == -6); 5380 } 5381 } 5382 5383 TEST_CASE_METHOD( 5384 Fixture, 5385 "C++11: shared ptr by value in expectation is copied", 5386 "[C++11][C++14][parameters]") 5387 { 5388 T obj; 5389 auto s = std::make_shared<int>(3); 5390 { 5391 REQUIRE_CALL_V(obj, ptr(s)); 5392 REQUIRE(s.use_count() == 2U); 5393 obj.ptr(s); 5394 } 5395 REQUIRE(reports.empty()); 5396 REQUIRE(s.use_count() == 1U); 5397 } 5398 5399 TEST_CASE_METHOD( 5400 Fixture, 5401 "C++11: shared ptr by std ref in expectation is not copied", 5402 "[C++11][C++14][parameters]") 5403 { 5404 T obj; 5405 auto s = std::make_shared<int>(3); 5406 { 5407 REQUIRE_CALL_V(obj, ptr(std::ref(s))); 5408 REQUIRE(s.use_count() == 1U); 5409 obj.ptr(s); 5410 } 5411 REQUIRE(reports.empty()); 5412 REQUIRE(s.use_count() == 1U); 5413 } 5414 5415 TEST_CASE_METHOD( 5416 Fixture, 5417 "C++11: unique ptr by value is matched with raw ptr in WITH", 5418 "[C++11][C++14][parameters]") 5419 { 5420 T obj; 5421 auto s = std::unique_ptr<int>(new int(3)); 5422 { 5423 auto sr = s.get(); 5424 5425 REQUIRE_CALL_V(obj, ptr(ANY(std::unique_ptr<int>)), 5426 .WITH(_1.get() == sr)); 5427 5428 obj.ptr(std::move(s)); 5429 REQUIRE_FALSE(s); 5430 } 5431 REQUIRE(reports.empty()); 5432 } 5433 5434 // tests of tracing of matched calls 5435 5436 TEST_CASE_METHOD( 5437 Fixture, 5438 "C++11: matching calls are traced", 5439 "[C++11][C++14][tracing]") 5440 { 5441 std::ostringstream os; 5442 trompeloeil::stream_tracer logger(os); 5443 mock_c obj1; 5444 mock_c obj2; 5445 REQUIRE_CALL_V(obj1, getter(_, _)); 5446 5447 REQUIRE_CALL_V(obj2, foo("bar"), 5448 .THROW(std::logic_error("nonono"))); 5449 5450 REQUIRE_CALL_V(obj1, getter(ANY(int)), 5451 .RETURN(_1 + 1)); 5452 5453 REQUIRE_CALL_V(obj2, foo("baz"), 5454 .THROW(3)); 5455 5456 std::string s = "foo"; 5457 obj1.getter(3, s); 5458 try { 5459 obj2.foo("bar"); 5460 } 5461 catch (std::logic_error&) { /* ignore, it's meant to happen */ } 5462 obj1.getter(4); 5463 try { 5464 obj2.foo("baz"); 5465 } 5466 catch (int) { /* ignore, it's meant to happen */ } 5467 auto re = 5468 R":([A-Za-z0-9_ ./:\]*:[0-9]*.* 5469 obj1\.getter\(_, _\) with. 5470 param _1 == 3 5471 param _2 == foo 5472 5473 [A-Za-z0-9_ ./:\]*:[0-9]*.* 5474 obj2\.foo\("bar"\) with\. 5475 param _1 == bar 5476 threw exception: what\(\) = nonono 5477 5478 [A-Za-z0-9_ ./:\]*:[0-9]*.* 5479 obj1\.getter\():" + 5480 escape_parens(CXX11_AS_STRING(ANY(int))) + 5481 R":(\) with\. 5482 param _1 == 4 5483 -> 5 5484 5485 [A-Za-z0-9_ ./:\]*:[0-9]*.* 5486 obj2\.foo\("baz"\) with\. 5487 param _1 == baz 5488 threw unknown exception 5489 ):"; 5490 INFO(os.str()); 5491 INFO("re=" << re); 5492 REQUIRE(is_match(os.str(), re)); 5493 } 5494 5495 TEST_CASE_METHOD( 5496 Fixture, 5497 "C++11: tracing is only active when tracer obj is alive", 5498 "[C++11][C++14][tracing]") 5499 { 5500 std::ostringstream os; 5501 mock_c obj1; 5502 mock_c obj2; 5503 ALLOW_CALL_V(obj1, getter(_, _)); 5504 REQUIRE_CALL_V(obj2, foo("bar")); 5505 std::string s = "foo"; 5506 obj1.getter(3, s); 5507 { 5508 trompeloeil::stream_tracer logger(os); 5509 obj2.foo("bar"); 5510 } 5511 obj1.getter(4, s); 5512 auto re = 5513 R":([A-Za-z0-9_ ./:\]*:[0-9]*.* 5514 obj2\.foo\("bar"\) with\. 5515 param _1 == bar 5516 ):"; 5517 REQUIRE(is_match(os.str(), re)); 5518 } 5519 5520 TEST_CASE( 5521 "C++11: all overridden short mocks can be expected and called", 5522 "[C++11][C++14][signatures][override]") 5523 { 5524 all_mock_if mock; 5525 REQUIRE_CALL_V(mock, f0()); 5526 REQUIRE_CALL_V(mock, f1(0)); 5527 REQUIRE_CALL_V(mock, f2(0,1)); 5528 REQUIRE_CALL_V(mock, f3(0,1,2)); 5529 REQUIRE_CALL_V(mock, f4(0,1,2,3)); 5530 REQUIRE_CALL_V(mock, f5(0,1,2,3,4)); 5531 REQUIRE_CALL_V(mock, f6(0,1,2,3,4,5)); 5532 REQUIRE_CALL_V(mock, f7(0,1,2,3,4,5,6)); 5533 REQUIRE_CALL_V(mock, f8(0,1,2,3,4,5,6,7)); 5534 REQUIRE_CALL_V(mock, f9(0,1,2,3,4,5,6,7,8)); 5535 REQUIRE_CALL_V(mock, f10(0,1,2,3,4,5,6,7,8,9)); 5536 REQUIRE_CALL_V(mock, f11(0,1,2,3,4,5,6,7,8,9,10)); 5537 REQUIRE_CALL_V(mock, f12(0,1,2,3,4,5,6,7,8,9,10,11)); 5538 REQUIRE_CALL_V(mock, f13(0,1,2,3,4,5,6,7,8,9,10,11,12)); 5539 REQUIRE_CALL_V(mock, f14(0,1,2,3,4,5,6,7,8,9,10,11,12,13)); 5540 REQUIRE_CALL_V(mock, f15(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)); 5541 REQUIRE_CALL_V(mock, cf0()); 5542 REQUIRE_CALL_V(mock, cf1(0)); 5543 REQUIRE_CALL_V(mock, cf2(0,1)); 5544 REQUIRE_CALL_V(mock, cf3(0,1,2)); 5545 REQUIRE_CALL_V(mock, cf4(0,1,2,3)); 5546 REQUIRE_CALL_V(mock, cf5(0,1,2,3,4)); 5547 REQUIRE_CALL_V(mock, cf6(0,1,2,3,4,5)); 5548 REQUIRE_CALL_V(mock, cf7(0,1,2,3,4,5,6)); 5549 REQUIRE_CALL_V(mock, cf8(0,1,2,3,4,5,6,7)); 5550 REQUIRE_CALL_V(mock, cf9(0,1,2,3,4,5,6,7,8)); 5551 REQUIRE_CALL_V(mock, cf10(0,1,2,3,4,5,6,7,8,9)); 5552 REQUIRE_CALL_V(mock, cf11(0,1,2,3,4,5,6,7,8,9,10)); 5553 REQUIRE_CALL_V(mock, cf12(0,1,2,3,4,5,6,7,8,9,10,11)); 5554 REQUIRE_CALL_V(mock, cf13(0,1,2,3,4,5,6,7,8,9,10,11,12)); 5555 REQUIRE_CALL_V(mock, cf14(0,1,2,3,4,5,6,7,8,9,10,11,12,13)); 5556 REQUIRE_CALL_V(mock, cf15(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)); 5557 5558 mock.f0(); 5559 mock.f1(0); 5560 mock.f2(0,1); 5561 mock.f3(0,1,2); 5562 mock.f4(0,1,2,3); 5563 mock.f5(0,1,2,3,4); 5564 mock.f6(0,1,2,3,4,5); 5565 mock.f7(0,1,2,3,4,5,6); 5566 mock.f8(0,1,2,3,4,5,6,7); 5567 mock.f9(0,1,2,3,4,5,6,7,8); 5568 mock.f10(0,1,2,3,4,5,6,7,8,9); 5569 mock.f11(0,1,2,3,4,5,6,7,8,9,10); 5570 mock.f12(0,1,2,3,4,5,6,7,8,9,10,11); 5571 mock.f13(0,1,2,3,4,5,6,7,8,9,10,11,12); 5572 mock.f14(0,1,2,3,4,5,6,7,8,9,10,11,12,13); 5573 mock.f15(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14); 5574 mock.cf0(); 5575 mock.cf1(0); 5576 mock.cf2(0,1); 5577 mock.cf3(0,1,2); 5578 mock.cf4(0,1,2,3); 5579 mock.cf5(0,1,2,3,4); 5580 mock.cf6(0,1,2,3,4,5); 5581 mock.cf7(0,1,2,3,4,5,6); 5582 mock.cf8(0,1,2,3,4,5,6,7); 5583 mock.cf9(0,1,2,3,4,5,6,7,8); 5584 mock.cf10(0,1,2,3,4,5,6,7,8,9); 5585 mock.cf11(0,1,2,3,4,5,6,7,8,9,10); 5586 mock.cf12(0,1,2,3,4,5,6,7,8,9,10,11); 5587 mock.cf13(0,1,2,3,4,5,6,7,8,9,10,11,12); 5588 mock.cf14(0,1,2,3,4,5,6,7,8,9,10,11,12,13); 5589 mock.cf15(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14); 5590 } 5591 5592 TEST_CASE( 5593 "C++11: all non-overridden short mocks can be expected and called", 5594 "[C++11][C++14][signatures]") 5595 { 5596 all_mock mock; 5597 REQUIRE_CALL_V(mock, f0()); 5598 REQUIRE_CALL_V(mock, f1(0)); 5599 REQUIRE_CALL_V(mock, f2(0,1)); 5600 REQUIRE_CALL_V(mock, f3(0,1,2)); 5601 REQUIRE_CALL_V(mock, f4(0,1,2,3)); 5602 REQUIRE_CALL_V(mock, f5(0,1,2,3,4)); 5603 REQUIRE_CALL_V(mock, f6(0,1,2,3,4,5)); 5604 REQUIRE_CALL_V(mock, f7(0,1,2,3,4,5,6)); 5605 REQUIRE_CALL_V(mock, f8(0,1,2,3,4,5,6,7)); 5606 REQUIRE_CALL_V(mock, f9(0,1,2,3,4,5,6,7,8)); 5607 REQUIRE_CALL_V(mock, f10(0,1,2,3,4,5,6,7,8,9)); 5608 REQUIRE_CALL_V(mock, f11(0,1,2,3,4,5,6,7,8,9,10)); 5609 REQUIRE_CALL_V(mock, f12(0,1,2,3,4,5,6,7,8,9,10,11)); 5610 REQUIRE_CALL_V(mock, f13(0,1,2,3,4,5,6,7,8,9,10,11,12)); 5611 REQUIRE_CALL_V(mock, f14(0,1,2,3,4,5,6,7,8,9,10,11,12,13)); 5612 REQUIRE_CALL_V(mock, f15(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)); 5613 REQUIRE_CALL_V(mock, cf0()); 5614 REQUIRE_CALL_V(mock, cf1(0)); 5615 REQUIRE_CALL_V(mock, cf2(0,1)); 5616 REQUIRE_CALL_V(mock, cf3(0,1,2)); 5617 REQUIRE_CALL_V(mock, cf4(0,1,2,3)); 5618 REQUIRE_CALL_V(mock, cf5(0,1,2,3,4)); 5619 REQUIRE_CALL_V(mock, cf6(0,1,2,3,4,5)); 5620 REQUIRE_CALL_V(mock, cf7(0,1,2,3,4,5,6)); 5621 REQUIRE_CALL_V(mock, cf8(0,1,2,3,4,5,6,7)); 5622 REQUIRE_CALL_V(mock, cf9(0,1,2,3,4,5,6,7,8)); 5623 REQUIRE_CALL_V(mock, cf10(0,1,2,3,4,5,6,7,8,9)); 5624 REQUIRE_CALL_V(mock, cf11(0,1,2,3,4,5,6,7,8,9,10)); 5625 REQUIRE_CALL_V(mock, cf12(0,1,2,3,4,5,6,7,8,9,10,11)); 5626 REQUIRE_CALL_V(mock, cf13(0,1,2,3,4,5,6,7,8,9,10,11,12)); 5627 REQUIRE_CALL_V(mock, cf14(0,1,2,3,4,5,6,7,8,9,10,11,12,13)); 5628 REQUIRE_CALL_V(mock, cf15(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)); 5629 5630 mock.f0(); 5631 mock.f1(0); 5632 mock.f2(0,1); 5633 mock.f3(0,1,2); 5634 mock.f4(0,1,2,3); 5635 mock.f5(0,1,2,3,4); 5636 mock.f6(0,1,2,3,4,5); 5637 mock.f7(0,1,2,3,4,5,6); 5638 mock.f8(0,1,2,3,4,5,6,7); 5639 mock.f9(0,1,2,3,4,5,6,7,8); 5640 mock.f10(0,1,2,3,4,5,6,7,8,9); 5641 mock.f11(0,1,2,3,4,5,6,7,8,9,10); 5642 mock.f12(0,1,2,3,4,5,6,7,8,9,10,11); 5643 mock.f13(0,1,2,3,4,5,6,7,8,9,10,11,12); 5644 mock.f14(0,1,2,3,4,5,6,7,8,9,10,11,12,13); 5645 mock.f15(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14); 5646 mock.cf0(); 5647 mock.cf1(0); 5648 mock.cf2(0,1); 5649 mock.cf3(0,1,2); 5650 mock.cf4(0,1,2,3); 5651 mock.cf5(0,1,2,3,4); 5652 mock.cf6(0,1,2,3,4,5); 5653 mock.cf7(0,1,2,3,4,5,6); 5654 mock.cf8(0,1,2,3,4,5,6,7); 5655 mock.cf9(0,1,2,3,4,5,6,7,8); 5656 mock.cf10(0,1,2,3,4,5,6,7,8,9); 5657 mock.cf11(0,1,2,3,4,5,6,7,8,9,10); 5658 mock.cf12(0,1,2,3,4,5,6,7,8,9,10,11); 5659 mock.cf13(0,1,2,3,4,5,6,7,8,9,10,11,12); 5660 mock.cf14(0,1,2,3,4,5,6,7,8,9,10,11,12,13); 5661 mock.cf15(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14); 5662 } 5663 5664 TEST_CASE_METHOD( 5665 Fixture, 5666 "C++11: a member function of a mock object can call a mocked function", 5667 "[C++11][C++14]") 5668 { 5669 { 5670 self_ref_mock m; 5671 m.expect_self(); 5672 m.mfunc(); 5673 } 5674 REQUIRE(reports.empty()); 5675 } 5676 5677 TEST_CASE_METHOD( 5678 Fixture, 5679 "C++11: expectation on a mock function can call the same mock func recursively as side effect", 5680 "[C++11][C++14]") 5681 { 5682 { 5683 self_ref_mock m; 5684 unsigned mask = 0U; 5685 5686 REQUIRE_CALL_V(m, mfunc(), 5687 .LR_SIDE_EFFECT(mask |= 2U)); 5688 5689 REQUIRE_CALL_V(m, mfunc(), 5690 .LR_SIDE_EFFECT(mask |= 1U) 5691 .LR_SIDE_EFFECT(m.mfunc())); 5692 5693 m.mfunc(); 5694 REQUIRE(mask == 3U); 5695 } 5696 REQUIRE(reports.empty()); 5697 } 5698 5699 TEST_CASE_METHOD( 5700 Fixture, 5701 "C++11: A macro may instantiate many expectations", 5702 "[C++11][C++14]") 5703 { 5704 all_mock m; 5705 MANY_REQS(m); 5706 m.f0(); 5707 m.f1(0); 5708 m.f2(0,1); 5709 } 5710 5711 TEST_CASE_METHOD( 5712 Fixture, 5713 "C++11: A named expectation follows a moved mock object", 5714 "[C++11][C++14]" 5715 ) 5716 { 5717 bool called = false; 5718 auto set_expectation = [&called](movable_mock obj) { 5719 auto exp = NAMED_REQUIRE_CALL_V(obj, func(3), 5720 .LR_SIDE_EFFECT(called = true)); 5721 return std::make_pair(std::move(obj), std::move(exp)); 5722 }; 5723 5724 auto e = set_expectation(movable_mock{}); 5725 e.first.func(3); 5726 REQUIRE(reports.empty()); 5727 REQUIRE(called); 5728 } 5729