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