reference.md (71450B)
1 # Reference 2 3 <!-- no toc --> 4 - [Notions](#notions) 5 - [Mock function](#mock_function) 6 - [Mock object](#mock_object) 7 - [Expectation](#expectation) 8 - [Matcher](#matcher) 9 - [_](#wildcard) 10 - [**`ANY(`** *type* **`)`**](#ANY) 11 - [**`eq(`** *value* **`)`**](#eq) 12 - [**`ne(`** *value* **`)`**](#ne) 13 - [**`gt(`** *value* **`)`**](#gt) 14 - [**`ge(`** *value* **`)`**](#ge) 15 - [**`lt(`** *value* **`)`**](#lt) 16 - [**`le(`** *value* **`)`**](#le) 17 - [**`re(`** *string* **`)`**](#re) 18 - [**`*`** *matcher*](#deref_matcher) 19 - [**`!`** *matcher*](#negate_matcher) 20 - [Macros](#macros) (alphabetical order) 21 - [**`ALLOW_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`**](#ALLOW_CALL) 22 - [**`ANY(`** *type* **`)`**](#ANY_MACRO) 23 - [**`AT_LEAST(`** *number* **`)`**](#AT_LEAST) 24 - [**`AT_MOST(`** *number* **`)`**](#AT_MOST) 25 - [**`FORBID_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`**](#FORBID_CALL) 26 - [**`IMPLEMENT_CONST_MOCKn(`** *func_name* **`)`**`](#IMPLEMENT_CONST_MOCKn) 27 - [**`IMPLEMENT_MOCKn(`** *func_name* **`)`**`](#IMPLEMENT_MOCKn) 28 - [**`IN_SEQUENCE(`** *seq...* **`)`**](#IN_SEQUENCE) 29 - [**`LR_RETURN(`** *expr* **`)`**](#LR_RETURN) 30 - [**`LR_SIDE_EFFECT(`** *expr* **`)`**](#LR_SIDE_EFFECT) 31 - [**`LR_THROW(`** *expr* **`)`**](#LR_THROW) 32 - [**`LR_WITH(`** *expr* **`)`**](#LR_WITH) 33 - [**`MAKE_CONST_MOCKn(`** *func_name*, *signature* **`)`**](#MAKE_CONST_MOCKn) 34 - [**`MAKE_MOCKn(`** *name*, *signature* **`)`**](#MAKE_MOCKn) 35 - [**`NAMED_ALLOW_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`**](#NAMED_ALLOW_CALL) 36 - [**`NAMED_FORBID_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`**](#NAMED_FORBID_CALL) 37 - [**`NAMED_REQUIRE_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`**](#NAMED_REQUIRE_CALL) 38 - [**`NAMED_REQUIRE_DESTRUCTION(`** *mock_object* **`)`**](#NAMED_REQUIRE_DESTRUCTION) 39 - [**`REQUIRE_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`**](#REQUIRE_CALL) 40 - [**`REQUIRE_DESTRUCTION(`** *mock_object* **`)`**](#REQUIRE_DESTRUCTION) 41 - [**`RETURN(`** *expr* **`)`**](#RETURN) 42 - [**`SIDE_EFFECT(`** *expr* **`)`**](#SIDE_EFFECT) 43 - [**`THROW(`** *expr* **`)`**](#THROW) 44 - [**`TIMES(`** *limit* **`)`**](#TIMES) 45 - [**`WITH(`** *expr* **`)`**](#WITH) 46 - [Types and Type Templates](#types_and_templates) (alphabetical order) 47 - [`trompeloeil::deathwatched<T>`](#deathwatched_type) 48 - [`trompeloeil::expectation`](#expectation_type) 49 - [`trompeloeil::expectation_violation`](#expectation_violation_type) 50 - [`trompeloeil::lifetime_monitor`](#lifetime_monitor_type) 51 - [`trompeloeil::matcher`](#matcher_type) 52 - [`trompeloeil::mock_interface<T>`](#mock_interface) 53 - [`trompeloeil::ok_reporter_func`](#ok_reporter_func) 54 - [`trompeloeil::printer`](#printer) 55 - [`trompeloeil::reporter_func`](#reporter_func) 56 - [`trompeloeil::sequence`](#sequence_type) 57 - [`trompeloeil::severity`](#severity_type) 58 - [`trompeloeil::stream_tracer`](#stream_tracer) 59 - [`trompeloeil::tracer`](#tracer_type) 60 - [`trompeloeil::typed_matcher<T>`](#typed_matcher) 61 - [Functions and Function Templates](#functions) 62 - [`trompeloeil::expectation::is_satisfied()`](#is_satisfied) 63 - [`trompeloeil::expectation::is_saturated()`](#is_saturated) 64 - [`trompeloeil::get_lock()`](#get_lock) 65 - [`trompeloeil::print(std::ostream&, T const&)`](#print) 66 - [`trompeloeil::is_null(T const &)`](#is_null) 67 - [`trompeloeil::make_matcher<Type>(...)`](#make_matcher) 68 - [`trompeloeil::set_reporter(...)`](#set_reporter) 69 - [`trompeloeil::sequence::is_completed()`](#is_completed) 70 - [Constants](#constants) 71 - [`trompeloeil_movable_mock`](#movable_mock) 72 73 ## <A name="notions"/>Notions 74 75 ### <A name="mock_function"/>Mock function 76 77 A *mock function* is a member function that is mocked with 78 [**`MAKE_MOCKn(name, signature)`**](#MAKE_MOCKn) or 79 [**`MAKE_CONST_MOCKn(name, signature)`**](#MAKE_CONST_MOCKn). 80 81 Example: 82 83 ```Cpp 84 class C 85 { 86 public: 87 MAKE_MOCK1(func, void(int)); 88 MAKE_CONST_MOCK2(cfunc, int(std::string, int)); 89 }; 90 ``` 91 92 Above `C` is a type that has two mock functions `void func(int)` and 93 `int cfunc(std::string, int) const`. With a [mock object](#mock_object) 94 of type `C` it is possible to place [expectations](#expectation) 95 on the functions `func(...)` and `cfunc(...)`. 96 97 ### <A name="mock_object"/>Mock object 98 99 A *mock object* is an object of a type that has [mock functions](#mock_function). 100 101 Example: 102 103 ```Cpp 104 class C 105 { 106 public: 107 MAKE_MOCK1(func, void(int)); 108 }; 109 110 C obj; 111 ``` 112 113 Above, `obj` is a mock object. It is possible to place 114 [expectations](#expectation) on [mock functions](#mock_function) for the object 115 `obj`. 116 117 ### <A name="expectation"/>Expectation 118 119 By default it is illegal to call [mock functions](#mock_function). Expectations 120 change that. Example: 121 122 ```Cpp 123 class C 124 { 125 public: 126 MAKE_MOCK1(func, void(int)); 127 }; 128 129 TEST(atest) 130 { 131 C mock_obj; 132 133 REQUIRE_CALL(mock_obj, func(3)); 134 135 tested_function(mock_obj); 136 } 137 ``` 138 139 Above `mock_obj` is a [mock object](#mock_object) with one 140 [mock function](#mock_function) `void func(int)`. 141 142 The line [*`REQUIRE_CALL(mock_obj, func(3))`*](#REQUIRE_CALL) places an 143 expectation that `mock_obj.func(3)` is called before the end of the scope. 144 Unless `tested_function(mock_obj)` calls `mock_obj.func(3)` a violation is 145 reported. 146 147 The ways to set expectations are: 148 149 - [**`REQUIRE_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`**](#REQUIRE_CALL) 150 - [**`ALLOW_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`**](#ALLOW_CALL) 151 - [**`FORBID_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`**](#FORBID_CALL) 152 - [**`NAMED_REQUIRE_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`**](#NAMED_REQUIRE_CALL) 153 - [**`NAMED_ALLOW_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`**](#NAMED_ALLOW_CALL) 154 - [**`NAMED_FORBID_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`**](#NAMED_FORBID_CALL) 155 156 Each **NAMED** variant returns an expectation as 157 [`std::unique_ptr<trompeloeil::expectation>`](#expectation_type) which can be 158 saved in variables for storage in test fixtures or other programmatic lifetime 159 control. 160 161 If expectations are not met by the time they go out or scope (or in case of the 162 **NAMED** variants, when the object held by the `std::unique_ptr<>` is 163 destroyed) a violation is reported. 164 165 By default there is no order imposed on expectations. One way to impose order is 166 through their lifetimes. Another is by using 167 [**`IN_SEQUENCE(...)`**](#IN_SEQUENCE). 168 169 If there are several expectations that match the same call, they are tried in 170 the reverse order of creation, and the first found match is accepted. In other 171 words, the last created matching expectation is the one used. 172 173 Example: 174 175 ```Cpp 176 class C 177 { 178 public: 179 MAKE_MOCK1(func, void(int)); 180 }; 181 182 using trompeloeil::_; 183 184 TEST(atest) 185 { 186 C mock_obj; 187 188 ALLOW_CALL(mock_obj, func(_)); 189 FORBID_CALL(mock_obj, func(3)); 190 FORBID_CALL(mock_obj, func(4)); 191 192 tested_function(mock_obj); 193 } 194 ``` 195 196 Above, the first expectation [**`ALLOW_CALL(...)`**](#ALLOW_CALL) matches 197 everything using the wildcard [`trompeloeil::_`](#wildcard), but the two 198 [**`FORBID_CALL(...)`**](#FORBID_CALL) are created later and are thus matched 199 first. This means that if `tested_function(...)` calls `mock_obj.func(int)` with 200 `5`, the two [**`FORBID_CALL(...)`**](#FORBID_CALL) do not match, but the 201 [**`ALLOW_CALL(...)`**](#ALLOW_CALL) does, so the call is allowed. A call with 202 `3` or `4`, results in a violation is report since a 203 [**`FORBID_CALL(...)`**](#FORBID_CALL) is matched. 204 205 ### <A name="matcher"/>Matcher 206 207 Each parameter in the parameter list of an [expectation](#expectation) can be 208 an exact value to match for equality (using `operator==`,) or a matcher. 209 Matchers check a condition on the parameter value. Trompeloeil provides the 210 matchers 211 212 - [_](#wildcard) 213 - [**`ANY(`** *type* **`)`**](#ANY) 214 - [**`eq(`** *value* **`)`**](#eq) 215 - [**`ne(`** *value* **`)`**](#ne) 216 - [**`gt(`** *value* **`)`**](#gt) 217 - [**`ge(`** *value* **`)`**](#ge) 218 - [**`lt(`** *value* **`)`**](#lt) 219 - [**`le(`** *value* **`)`**](#le) 220 221 You can also provide [your own matchers](CookBook.md/#custom_matchers). 222 223 #### <A name="wildcard"/>**`_`** 224 225 Used in the parameter list of an [expectation](#expectation), `trompeloeil::_` 226 matches any value of any type. 227 228 Example: 229 230 ```Cpp 231 class C 232 { 233 public: 234 MAKE_MOCK1(func, int(int)); 235 }; 236 237 trompeloeil::_; 238 239 TEST(atest) 240 { 241 C mock_obj; 242 ALLOW_CALL(mock_obj, func(_)) 243 .RETURN(_1 + 1); 244 245 test_function(&mock_obj); 246 } 247 ``` 248 249 Above, `mock_obj.func()` is allowed to be called with any value, and it will 250 return 1 + the value provided. 251 252 If type information is needed, for example to disambiguate overloads, use 253 [**`ANY(`** *type* **`)`**](#ANY). 254 255 #### <A name="ANY"/>**`ANY(`** *type* **`)`** 256 257 Used in the parameter list of an [expectation](#expectation) to match any value 258 of a specified type. This can be used as an alternative to 259 [`trompeloeil::_`](#wildcard) when it is important to disambiguate between 260 overloads. 261 262 Example: 263 264 ```Cpp 265 class C 266 { 267 public: 268 MAKE_MOCK1(func, void(int)); 269 MAKE_MOCK2(func, void(std::string)); 270 }; 271 272 TEST(atest) 273 { 274 C mock_obj; 275 ALLOW_CALL(mock_obj, func(ANY(int))); 276 277 test_function(&mock_obj); 278 } 279 ``` 280 281 Above, any call to `mock_obj.func(int)` is accepted, but calls to 282 `mock_obj.func(std::string)` renders a violation report since there is no 283 matching [expectation](#expectation). 284 285 #### <A name="eq"/>**`eq(`** *value* **`)`** 286 287 Used in the parameter list of an [expectation](#expectation) to match a 288 value equal to the one provided. By default it matches any parameter type 289 that supports `operator==()` with the value, but an explicit type can be 290 specified if needed for disambiguation. 291 292 Example: 293 294 ```Cpp 295 class C 296 { 297 public: 298 MAKE_MOCK1(func, void(int*)); 299 MAKE_MOCK1(func, void(const char*)); 300 MAKE_MOCK1(func, void(const std::string&)); 301 }; 302 303 using trompeloeil::eq; 304 305 TEST(atest) 306 { 307 C mock_obj; 308 ALLOW_CALL(mock_obj, func(*eq(3))); 309 310 std::string expected = "foo"; 311 REQUIRE_CALL(mock_obj, func(eq<const char*>(expected))); 312 313 test_function(&mock_obj); 314 } 315 ``` 316 317 Above, the first [expectation](#expectation) matches only calls to 318 `mock_obj.func(int*)` with a non-null pointer pointing to the value `3`. Any 319 call with a `nullptr` or a pointer pointing to a value other than `3` renders 320 a violation report since no [expectation](#expectation) matches. 321 322 The second [expectation](#expectation) matches only calls to 323 `mock_obj.func(const char*)`, with a C-string `"foo"`. 324 325 #### <A name="ne"/>**`ne(`** *value* **`)`** 326 327 Used in the parameter list of an [expectation](#expectation) to match a 328 value not equal to the one provided. By default it matches any parameter type 329 that supports `operator!=()` with the value, but an explicit type can be 330 specified if needed for disambiguation. 331 332 Example: 333 334 ```Cpp 335 class C 336 { 337 public: 338 MAKE_MOCK1(func, void(const char*)); 339 MAKE_MOCK1(func, void(const std::string&)); 340 }; 341 342 using trompeloeil::ne; 343 344 TEST(atest) 345 { 346 C mock_obj; 347 ALLOW_CALL(mock_obj, func(ne(nullptr))); 348 REQUIRE_CALL(mock_obj, func(ne<std::string>(""))); 349 test_function(&mock_obj); 350 } 351 ``` 352 353 Above, the first [expectation](#expectation) matches only calls to 354 `mock_obj.func(const char*)` with non-null pointer. Any call with a `nullptr` 355 renders a violation report since no [expectation](#expectation) matches. 356 357 The second [expectation](#expectation) matches only calls to 358 `mock_obj.func(const std::string&)`, with a non-empty string. 359 360 It is also possible to use `*ne(val)` to match a pointer to a non-equal value. 361 362 #### <A name="gt"/>**`gt(`** *value* **`)`** 363 364 Used in the parameter list of an [expectation](#expectation) to match a 365 value greater than the one provided. By default it matches any parameter type 366 that supports `operator>()` with the value, but an explicit type can be 367 specified if needed for disambiguation. 368 369 Example: 370 371 ```Cpp 372 class C 373 { 374 public: 375 MAKE_MOCK1(func, void(short)); 376 MAKE_MOCK1(func, void(int)); 377 MAKE_MOCK1(func, void(std::string)); 378 }; 379 380 using trompeloeil::gt; 381 382 TEST(atest) 383 { 384 C mock_obj; 385 ALLOW_CALL(mock_obj, func(gt<short>(0))); 386 ALLOW_CALL(mock_obj, func(gt("foo"))); 387 388 test_function(&mock_obj); 389 } 390 ``` 391 392 Above, the first [expectation](#expectation) matches only calls to 393 `mock_obj.func(short)` with positive values. Any call with 0 or negative, and 394 any calls to `mock_obj.func(int)` renders a violation report since no 395 [expectation](#expectation) matches. 396 397 The second [expectation](#expectation) matches calls to 398 `mock_obj.func(std::string)`, since 399 [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) is 400 greater-than comparable with a string literal. 401 402 It is also possible to use `*gt(val)` to match a pointer to a greater-than 403 value. 404 405 #### <A name="ge"/>**`ge(`** *value* **`)`** 406 407 Used in the parameter list of an [expectation](#expectation) to match a 408 value greater than on equal to the one provided. By default it matches any 409 parameter type that supports `operator>=()` with the value, but an explicit 410 type can be specified if needed for disambiguation. 411 412 Example: 413 414 ```Cpp 415 class C 416 { 417 public: 418 MAKE_MOCK1(func, void(int)); 419 MAKE_MOCK1(func, void(short)); 420 MAKE_MOCK1(func, void(std::string)); 421 }; 422 423 using trompeloeil::ge; 424 425 TEST(atest) 426 { 427 C mock_obj; 428 ALLOW_CALL(mock_obj, func(ge<short>(0))); 429 REQUIRE_CALL(mock_obj, func(ge("foo"))); 430 431 test_function(&mock_obj); 432 } 433 ``` 434 435 Above, the first [expectation](#expectation) matches only calls to 436 `mock_obj.func(short)` with zero or positive values. Any call with a negative 437 value renders a violation report since no [expectation](#expectation) matches. 438 439 The second [expectation](#expectation) matches only calls to 440 `mock_obj.func(std::string)`, since 441 [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) is 442 greater-than-or-equal-to comparable with string literal. 443 444 It is also possible to use `*ge(val)` to match a pointer to a greater-than or 445 equal value. 446 447 #### <A name="lt"/>**`lt(`** *value* **`)`** 448 449 Used in the parameter list of an [expectation](#expectation) to match a 450 value less than the one provided. By default it matches any parameter type 451 that supports `operator<()` with the value, but an explicit type can be 452 specified if needed for disambiguation. 453 454 Example: 455 456 ```Cpp 457 class C 458 { 459 public: 460 MAKE_MOCK1(func, void(long)); 461 MAKE_MOCK1(func, void(int)); 462 MAKE_MOCK1(func, void(const std::string&)); 463 }; 464 465 using trompeloeil::lt; 466 467 TEST(atest) 468 { 469 C mock_obj; 470 ALLOW_CALL(mock_obj, func(lt<int>(0))); 471 REQUIRE_CALL(mock_obj, func(lt("foo"))); 472 473 test_function(&mock_obj); 474 } 475 ``` 476 477 Above, the first [expectation](#expectation) matches only calls to 478 `mock_obj.func(int)` with negative values. Any call with 0 or positive 479 renders a violation report since no [expectation](#expectation) matches. 480 481 The second [expectation](#expectation) matches calls to 482 `mock_obj.func(cost std::string&)`, since 483 [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) is 484 less-than comparable with string a literal. 485 486 It is also possible to use `*lt(val)` to match a pointer to a less-than value. 487 488 #### <A name="le"/>**`le(`** *value* **`)`** 489 490 Used in the parameter list of an [expectation](#expectation) to match a 491 value less than or equal to the one provided. By default it matches any 492 parameter type that supports `operator<=()` with the value, but an explicit type 493 can be specified if needed for disambiguation. 494 495 Example: 496 497 ```Cpp 498 class C 499 { 500 public: 501 MAKE_MOCK1(func, void(int)); 502 MAKE_MOCK1(func, void(short)); 503 MAKE_MOCK1(func, void(const char*)); 504 }; 505 506 using trompeloeil::le; 507 using std::string_literals; 508 509 TEST(atest) 510 { 511 C mock_obj; 512 ALLOW_CALL(mock_obj, func(le<short>(0))); 513 REQUIRE_CALL(mock_obj, func(le("foo"s))); 514 test_function(&mock_obj); 515 } 516 ``` 517 518 Above, first the [expectation](#expectation) matches only calls to 519 `mock_obj.func(short)` with zero or negative values. Any call with a positive 520 value renders a violation report since no [expectation](#expectation) matches. 521 522 The second [expectation](#expectation) matches calls to 523 `mock_obj.func(const char*)`, since a c-string is less-than comparable 524 with a [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) 525 526 It is also possible to use `*le(val)` to match a pointer to a less-than or 527 equal value. 528 529 #### <A name="re"/>**`re(`** *string* **`)`** 530 531 Used in the parameter list of an [expectation](#expectation) to match a 532 string with a regular expression. 533 534 **`re()`** exists in two flavours. 535 536 - **`re(`** *string*, *flags...* **`)`** 537 which can match both C-strings (`char*`, `const char*`) as well as 538 `C++` `std::string`. 539 - **`re<type>(`** *string*, *flags...* **`)`** 540 which can be used to disambiguate overloads. 541 542 For both versions, the string can be either `std::string` or a C-string. 543 544 *flags...* can be 545 546 - empty 547 - `std::regex_constants::syntax_option_type` 548 - `std::regex_constants::match_flag_type` 549 - `std::regex_constants::syntax_option_type, std::regex_constants::match_flag_type` 550 551 Regular expression matching is made with 552 [`std::regex_search()`](http://en.cppreference.com/w/cpp/regex/regex_search) 553 554 Example: 555 556 ```Cpp 557 class C 558 { 559 public: 560 MAKE_MOCK1(unique, void(std::string&)); 561 MAKE_MOCK1(overloaded, void(const char*)); 562 MAKE_MOCK1(overloaded, void(const std::string&)); 563 }; 564 565 using trompeloeil::re; 566 567 TEST(atest) 568 { 569 C mock_obj; 570 REQUIRE_CALL(mock_obj, unique(re("end$", std::regex_constants::icase))); 571 REQUIRE_CALL(mock_obj, overloaded(re<const char*>("end", std::regex_constants::match_not_eol))); 572 test_function(&mock_obj); 573 } 574 ``` 575 576 Above, `test_function(&mock_obj)` must call `mock_obj.unique()` with a string 577 case insensitively matching the regular expression `/end$/`, and also call 578 `mock_obj.overloaded(const char*)` with a regular expression matching 579 the regular expression `/end/`. 580 581 It is also possible to use `*re(string)` to match a pointer to a string with 582 a regular expression, or `!re(string)` to allow only strings that do not match 583 a regular expression. 584 585 #### <A name="deref_matcher"/>**`*`** *matcher* 586 587 Used in the parameter list of an [expectation](#expectation) together with a 588 matcher, to match a value pointed to by a pointer. A 589 [`nullptr`](http://en.cppreference.com/w/cpp/language/nullptr) value fails the 590 matcher. 591 592 Example: 593 594 ```Cpp 595 struct C { 596 MAKE_MOCK1(func, void(int*)); 597 }; 598 599 using trompeloeil::eq; // matching equal values 600 601 TEST(atest) 602 { 603 C mock_obj; 604 REQUIRE_CALL(mock_obj, func(*eq(3))); 605 test_function(&mock_obj); 606 } 607 ``` 608 609 Above, `test_function(&mock_obj)` must call `mock_obj.func()` with a pointer 610 to the value `3`. 611 612 #### <A name="negate_matcher"/>**`!`** *matcher* 613 614 Used in the parameter list of an [expectation](#expectation) together with a 615 matcher, to negate a matcher, i.e. to fail what the matcher allows, and to 616 allow what the matcher fails. 617 618 Example: 619 620 ```Cpp 621 struct C { 622 MAKE_MOCK1(func, void(const std::string&)); 623 }; 624 625 using trompeloeil::re; // matching regular expressions 626 627 TEST(atest) 628 { 629 C mock_obj; 630 REQUIRE_CALL(mock_obj, func(!re("^foo"))); 631 test_function(&mock_obj); 632 } 633 ``` 634 635 Above, `test_function(&mock_obj)` must call `mock_obj.func()` with a string 636 that does not begin with `"foo"`. 637 638 ## <A name="macros"/>Macros 639 640 <A name="ALLOW_CALL"/> 641 642 ### **`ALLOW_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`** 643 644 Make an expectation that *mock_object*.*func_name*(*parameter_list*) may be 645 called zero or more times until the end of the surrounding scope. 646 *parameter_list* may contain exact values or [matchers](#matcher) 647 that describes matching calls. 648 649 This is the same as 650 [**`REQUIRE_CALL(...)`**](#REQUIRE_CALL).[**`TIMES(`**](#TIMES) 0,infinity **`)`**. 651 652 Matches any number of times, but is not required to match. (_actually the limit is 653 0..~0ULL, but that is for all practical purposes "infinity"_) 654 655 Example: 656 657 ```Cpp 658 class C 659 { 660 public: 661 MAKE_MOCK1(func, int(int)); 662 }; 663 664 trompeloeil::_; 665 666 TEST(atest) 667 { 668 C mock_obj; 669 ALLOW_CALL(mock_obj, func(_)) 670 .RETURN(_1 + 1); 671 672 test_function(&mock_obj); 673 } 674 ``` 675 676 Above **`ALLOW_CALL(mock_obj, func(_))`** places an expectation that 677 `mock_obj.func()` may be called any number of times with any parameter value 678 and will always return the parameter value + 1. `test_function(...)` 679 is allowed to call `mock_obj.func()` any number of times (including no call at 680 all). 681 682 The expectation is valid until the end of the scope, which in the example above 683 is until after the return from `test_function(...)`. 684 685 See also [**`NAMED_ALLOW_CALL(...)`**](#NAMED_ALLOW_CALL) which creates an 686 expectation as a 687 [`std::unique_ptr<trompeloeil::expectation>`](#expectation_type) which can be 688 stored in test fixtures or otherwise have its lifetime programmatically controlled. 689 690 <A name="ANY_MACRO"/> 691 692 ### **`ANY(`** *type* **`)`** 693 694 A [matcher](#matcher) for use in the parameter list of an 695 [expectation](#expectation) to disambiguate overloaded functions on type when 696 the exact value is unimportant. See the matcher [**`ANY(`** *type* **`)`**](#ANY) above. 697 698 <A name="AT_LEAST"/> 699 700 ### **`AT_LEAST(`** *number* **`)`** 701 702 Used in [**`TIMES(...)`**](#TIMES) to set the range *number*..infinity. 703 *number* must be 704 [`constexpr`](http://en.cppreference.com/w/cpp/language/constexpr). 705 706 Example: 707 708 ```Cpp 709 class C 710 { 711 public: 712 MAKE_MOCK1(func, void(int)); 713 }; 714 715 using trompeloeil::_; 716 717 TEST(atest) 718 { 719 C mock_obj; 720 REQUIRE_CALL(mock_obj, func(_)) 721 .TIMES(AT_LEAST(3)); 722 tested_function(&mock_obj); 723 } 724 ``` 725 726 Above, the line [**`TIMES(`**](#TIMES)**`AT_LEAST(3))`** modifies the 727 [expectation](#expectation) such that *mock_obj.func()* must be called 3 times 728 or more, before the end of the scope, or a violation is reported. 729 730 _In reality the upper limit is ~0ULL, but that is for all practical purposes 731 "infinity"_. 732 733 <A name="AT_MOST"/> 734 735 ### **`AT_MOST(`** *number* **`)`** 736 737 Used in [**`TIMES(...)`**](#TIMES) to set the range 0..*number*. 738 *number* must be 739 [`constexpr`](http://en.cppreference.com/w/cpp/language/constexpr). 740 741 Example: 742 743 ```Cpp 744 class C 745 { 746 public: 747 MAKE_MOCK1(func, void(int)); 748 }; 749 750 using trompeloeil::_; 751 752 TEST(atest) 753 { 754 C mock_obj; 755 REQUIRE_CALL(mock_obj, func(_)) 756 .TIMES(AT_MOST(3)); 757 tested_function(&mock_obj); 758 } 759 ``` 760 761 Above, the line [**`TIMES(`**](#TIMES)**`AT_MOST(3))`** modifies the 762 [expectation](#expectation) such that *mock_obj.func()* must be called 3 times 763 or less (including no call at all) before the end of the scope, or a violation 764 is reported. 765 766 <A name="FORBID_CALL"/> 767 768 ### **`FORBID_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`** 769 770 Make an expectation that *mock_object*.*func_name*(*parameter_list*) must not 771 be called until the end of the scope. *parameter_list* may contain exact values 772 or [matchers](#matcher) that describes matching calls. 773 774 This is the same as 775 [**`REQUIRE_CALL(...)`**](#REQUIRE_CALL).[**`TIMES(`**](#TIMES) 0 **`)`**, 776 making any matching call an error. This is often done in a narrow scope 777 where the wider scope would allow the call. [**`LR_RETURN(...)`**](#LR_RETURN), 778 [**`RETURN(...)`**](#RETURN), [**`LR_THROW(...)`**](#LR_THROW) and 779 [**`THROW(...)`**](#THROW) are illegal in a **`FORBID_CALL(...)`**. 780 781 Example: 782 783 ```Cpp 784 class C 785 { 786 public: 787 MAKE_MOCK1(func, void(int)); 788 }; 789 790 using trompeloeil::_; 791 792 TEST(atest) 793 { 794 C mock_obj; 795 ALLOW_CALL(mock_obj, func(_)); 796 797 tested_function(1, &mock_obj); 798 799 { 800 FORBID_CALL(mock_obj, func(2)); 801 802 tested_function(2, &mock_obj); 803 } 804 805 tested_function(3, &mock_obj); 806 } 807 ``` 808 809 Above, the [mock function](#mock_function) *C::func(int)* may be called with any 810 value for *mock_obj*, except in the scope of the *tested_function(2, &mock_obj)*, 811 where *mock_obj.func(2)* would lead to a violation being reported. At 812 *tested_function(3, &mock_obj)* any value is allowed again. 813 814 See also [**`NAMED_FORBID_CALL(...)`**](#NAMED_FORBID_CALL) which creates an 815 expectation as a 816 [`std::unique_ptr<trompeloeil::expectation>`](#expectation_type) which can be 817 stored in test fixtures or otherwise have its lifetime programmatically controlled. 818 819 <A name="IMPLEMENT_CONST_MOCKn"/> 820 821 ### **`IMPLEMENT_CONST_MOCKn(`** *func_name* **`)`** 822 823 Make a `const` [mock function](#mock_function) implementation of the 824 `virtual` function named *func_name* from the inherited interface. This macro 825 is only usable with `virtual` non-`final` functions, and only when used with 826 [`mock_interface<T>`](#mock_interface), where `T` is the interface. 827 828 Example: 829 830 ```Cpp 831 class I 832 { 833 public: 834 virtual ~I() = default; 835 virtual int func(int, const std::vector<int>&)) const = 0; 836 }; 837 838 class C : public trompeloeil::mock_interface<I> 839 { 840 public: 841 IMPLEMENT_CONST_MOCK2(func); 842 }; 843 ``` 844 845 Above, class `C` will effectively become: 846 847 ```Cpp 848 class C : public trompeloeil::mock_interface<I> 849 { 850 public: 851 int func(int, const std::vector<int>&) const override; 852 }; 853 ``` 854 855 It is not possible to mock operators, constructors or the destructor, but 856 you can call [mock functions](#mock_function) from those. 857 858 **NOTE!** **`IMPLEMENT_CONST_MOCKn(...)`** cannot handle overloaded functions. 859 860 See also [**`IMPLEMENT_MOCKn(...)`**](#IMPLEMENT_MOCKn) for non-`const` 861 member functions. 862 863 See also [**`MAKE_MOCKn(...)`**](#MAKE_MOCKn) and 864 [**`MAKE_CONST_MOCKn(...)`**](#MAKE_CONST_MOCKn) for making mock implementations 865 of any member functions. 866 867 <A name="IMPLEMENT_MOCKn"/> 868 869 ### **`IMPLEMENT_MOCKn(`** *func_name* **`)`** 870 871 Make a non-`const` [mock function](#mock_function) implementation of the 872 `virtual` function named *func_name* from the inherited interface. This macro 873 is only usable with `virtual` non-`final` functions, and only when used with 874 [`mock_interface<T>`](#mock_interface), where `T` is the interface. 875 876 Example: 877 878 ```Cpp 879 class I 880 { 881 public: 882 virtual ~I() = default; 883 virtual int func(int, const std::vector<int>&)) = 0; 884 }; 885 886 class C : public trompeloeil::mock_interface<I> 887 { 888 public: 889 IMPLEMENT_MOCK2(func1); 890 }; 891 ``` 892 893 Above, class `C` will effectively become: 894 895 ```Cpp 896 class C : public trompeloeil::mock_interface<I> 897 { 898 public: 899 int func(int, const std::vector<int>&) override; 900 }; 901 ``` 902 903 It is not possible to mock operators, constructors or the destructor, but 904 you can call [mock functions](#mock_function) from those. 905 906 **NOTE!** **`IMPLEMENT_MOCKn(...)`** cannot handle overloaded functions. 907 908 See also [**`IMPLEMENT_CONST_MOCKn(...)`**](#IMPLEMENT_CONST_MOCKn) for `const` 909 member functions. 910 911 See also [**`MAKE_CONST_MOCKn(...)`**](#MAKE_CONST_MOCKn) for `const` 912 member functions. 913 914 <A name="IN_SEQUENCE"/> 915 916 ### **`IN_SEQUENCE(`** *seq...* **`)`** 917 918 Where *seq...* is one or more instances of `trompeloeil::sequence`. Impose an 919 order in which [expectations](#expectation) and destruction of 920 [**`deathwatched_type`**](#deathwatched_type) objects must match. 921 Several sequences can be parallel and interleaved. A sequence for an 922 [expectation](#expectation) can move forward to the next once the lower 923 limit from [**`TIMES(...)`**](#TIMES) is reached (defaults to 1). This means 924 that if the lower limit is 0 (see [**`ALLOW_CALL(...)`**](#ALLOW_CALL)), the 925 the expectation may be skipped in the sequence. 926 927 Example: 928 929 ```Cpp 930 class Mock 931 { 932 public: 933 MAKE_MOCK1(func, void(int)); 934 MAKE_MOCK1(func, void(const std::string&)); 935 }; 936 937 class ephemeral 938 { 939 public: 940 virtual ~ephemeral() {}; 941 }; 942 943 TEST(atest) 944 { 945 Mock m[2]; 946 auto e = new trompeloeil::deathwatched<ephemeral>; 947 948 trompeloeil::sequence seq1, seq2; 949 950 REQUIRE_CALL(m[0], func(ANY(int)) 951 .IN_SEQUENCE(seq1, seq2); 952 953 REQUIRE_CALL(m[0], func(ANY(const std::string&)) 954 .IN_SEQUENCE(seq1); 955 956 REQUIRE_CALL(m[1], func(ANY(const std::string&)) 957 .IN_SEQUENCE(seq2); 958 959 REQUIRE_CALL(m[1], func(ANY(int)) 960 .IN_SEQUENCE(seq1, seq2); 961 962 REQUIRE_DESTRUCTION(*e) 963 .IN_SEQUENCE(seq1, seq2); 964 965 tested_func(&m[0], &m[1], e); 966 } 967 ``` 968 969 All sequence objects are listed in the first [**`REQUIRE_CALL(...)`**](#REQUIRE_CALL), 970 thus it must be the first [expectation](#expectation) matched. Likewise all 971 sequences are listed in the last 972 [**`REQUIRE_CALL(...)`**](#REQUIRE_CALL), so it must be last 973 [expectation](#expectation) matched. The intermediate 974 [expectations](#expectation) has one sequence object each, thus they have no 975 matching order imposed between them. Last of all is the 976 [**`REQUIRE_DESTRUCTION(...)`**](#REQUIRE_DESTRUCTION), which also lists 977 all sequence objects and must happen after all other 978 [expectations](#expectation) are fulfilled. 979 980 The above allows the following two sequences only. 981 982 - `m[0].func(int)` -> `m[0].func(string)` -> `m[1].func(string)` -> `m[1].func(int)` -> `delete e` 983 - `m[0].func(int)` -> `m[1].func(string)` -> `m[0].func(string)` -> `m[1].func(int)` -> `delete e` 984 985 Any other sequence of calls renders a violation report. 986 987 <A name="LR_RETURN"/> 988 989 ### **`LR_RETURN(`** *expr* **`)`** 990 991 Used in [expectations](#expectation) to set the return value after having 992 evaluated every [**`SIDE_EFFECT(...)`**](#SIDE_EFFECT) and 993 [**`LR_SIDE_EFFECT(...)`**](#LR_SIDE_EFFECT). 994 For `void` functions **`LR_RETURN(...)`** is illegal. For non-`void` functions 995 exactly one of [**`RETURN(...)`**](#RETURN), **`LR_RETURN(...)`**, 996 [**`LR_THROW(...)`**](#LR_THROW) or [**`THROW(...)`**](#THROW) is required. 997 *expr* may refer to parameters in the call with their positional names `_1`, 998 `_2`, etc. 999 This code may alter out-parameters. 1000 1001 If you need to return an 1002 [lvalue-reference](http://en.cppreference.com/w/cpp/language/reference), 1003 to a variable, use 1004 [`std::ref(value)`](http://en.cppreference.com/w/cpp/utility/functional/ref) or 1005 [`std::cref(value)`](http://en.cppreference.com/w/cpp/utility/functional/cref) 1006 for it, or just enclose the value in an extra parenthesis, like this 1007 [**`.LR_RETURN((value))`**](reference.md/#RETURN). 1008 1009 **NOTE!** Any named local objects named in *expr* are captured by reference so 1010 lifetime management is important. 1011 1012 Example: 1013 1014 ```Cpp 1015 class C 1016 { 1017 public: 1018 MAKE_MOCK1(func, int&(unsigned)); 1019 }; 1020 1021 TEST(atest) 1022 { 1023 C mock_obj; 1024 1025 int rv = 3; 1026 ALLOW_CALL(mock_obj, func(0)) 1027 .LR_RETURN(std::ref(rv)); // reference to captured local variable 1028 1029 rv = 4; 1030 test_func(&mock_obj); 1031 } 1032 ``` 1033 1034 Above, the **`LR_RETURN(...)`** clause tells matching calls of 1035 `mock_obj.func(...)` to return a reference to the local variable `rv`. 1036 Since **`LR_RETURN(...)`** accesses local variables by reference, the value 1037 of the returned reference will be 4 if called from within `test_func(...)`. 1038 1039 See also [**`RETURN(...)`**](#RETURN) which accesses copies of local variables. 1040 1041 <A name="LR_SIDE_EFFECT"/> 1042 1043 ### **`LR_SIDE_EFFECT(`** *expr* **`)`** 1044 1045 Used in [expectations](#expectation) to cause side effects for matching calls. 1046 *expr* is only evaluated when all [**`WITH(...)`**](#WITH) and 1047 [**`LR_WITH(...)`**](#LR_WITH) clauses are matched. *expr* may refer to 1048 parameters in the call with their positional names `_1`, `_2`, etc. This code 1049 may alter out-parameters. Several **`LR_SIDE_EFFECT(...)`** and 1050 [**`SIDE_EFFECT(...)`**](#SIDE_EFFECT) 1051 clauses can be added to a single [expectation](#expectation), and they are 1052 evaluated in order. 1053 1054 Example: 1055 1056 ```Cpp 1057 class C 1058 { 1059 public: 1060 MAKE_MOCK1(func, void(unsigned)); 1061 }; 1062 1063 TEST(atest) 1064 { 1065 C mock_obj; 1066 unsigned sum = 0; 1067 ALLOW_CALL(mock_obj, func(ANY(unsigned)) 1068 .LR_SIDE_EFFECT(sum += _1); 1069 1070 tested_func(&mock_obj); 1071 1072 std::cout << "parameter sum=" << sum << "\n"; 1073 } 1074 ``` 1075 1076 Above, `tested_func(&mock_obj)` is allowed to call `C::func(int)` any 1077 number of times on `mock_obj`. Each time a side effect is that the local 1078 variable `sum` gets the parameter value added to it. Since 1079 **`LR_SIDE_EFFECT(...)`** refers to `sum` by reference, it is the actual 1080 local variable that is changed is every call. 1081 1082 See also [**`SIDE_EFFECT(...)`**](#SIDE_EFFECT) which accesses copies of local 1083 objects. 1084 1085 <A name="LR_THROW"/> 1086 1087 ### **`LR_THROW(`** *expr* **`)`** 1088 1089 Used in [expectations](#expectation) to throw after having evaluated every 1090 [**`SIDE_EFFECT(...)`**](#SIDE_EFFECT) and 1091 [**`LR_SIDE_EFFECT(...)`**](#LR_SIDE_EFFECT) for a matching call. 1092 *expr* may refer to parameters in the call with their positional names `_1`, 1093 `_2`, etc. This code may alter out-parameters. It is not legal to combine 1094 **`LR_THROW(...)`** with any of [**`THROW(...)`**](#THROW), 1095 [**`LR_RETURN(...)`**](#LR_RETURN) or [**`RETURN(...)`**](#RETURN). Named local 1096 objects are accessed by reference so lifetime management is important. 1097 1098 Example: 1099 1100 ```Cpp 1101 class C 1102 { 1103 public: 1104 MAKE_MOCK1(func, void(unsigned)); 1105 }; 1106 1107 TEST(atest) 1108 { 1109 C mock_obj; 1110 const char* what=""; 1111 1112 ALLOW_CALL(mock_obj, func(3)) 1113 .LR_THROW(std::invalid_argument(what)); 1114 1115 what = "nonsense"; 1116 tested_func(&mock_obj); 1117 } 1118 ``` 1119 1120 Above, **`LR_THROW(std::invalid_argument(what))`** will refer to the C-string 1121 `what` with the value it has at the time of a call to `mock_obj.func(3)`, i.e. 1122 `"nonsense"` if `tested_func()` does the call. 1123 1124 See also [**`THROW(...)`**](#THROW) which accesses copies of local objects. 1125 1126 <A name="LR_WITH"/> 1127 1128 ### **`LR_WITH(`** *expr* **`)`** 1129 1130 Used with [expectations](#expectation) to add further conditions for a 1131 matching call. Typically used when [matchers](#matcher) are used for the 1132 parameters, and often when the condition requires several parameter values 1133 together. 1134 *expr* can refer to parameters in the call with their positional names `_1`, 1135 `_2`, etc. Even if the function signature has parameters as non-`const` 1136 references, they are immutable in this context. Several **`LR_WITH(...)`** 1137 and [**`WITH(...)`**](#WITH) clauses can be added to a single expectation and 1138 they are tried in the order they are added until one has failed, or they all 1139 have passed. 1140 1141 Named local objects are accessed by reference so lifetime management is 1142 important. 1143 1144 Example: 1145 1146 ```Cpp 1147 class C 1148 { 1149 public: 1150 MAKE_MOCK1(func, void(const char*)); 1151 }; 1152 1153 using trompeloeil::_; 1154 1155 TEST(atest) 1156 { 1157 C mock_obj; 1158 1159 const char buff[] = "string"; 1160 1161 REQUIRE_CALL(mock_obj, func(_)) 1162 .LR_WITH(_1 == buff); 1163 1164 tested_func(buff, &mock_obj); 1165 } 1166 ``` 1167 1168 Above, **`LR_WITH(_1 == buff)`** checks the condition that the `const char*` 1169 parameter is the same pointer value as the address to the local array `buff`. 1170 1171 **NOTE!** It is legal, but a *very* bad idea, to modify global/static objects in 1172 **`LR_WITH(...)`**. If several [expectations](#expectation) could match and 1173 are disambiguated by **`LR_WITH(...)`** and [**`WITH(...)`**](#WITH) the 1174 global/static objects will be modified also by those 1175 [expectations](#expectation) that do not match. 1176 1177 See also [**`WITH(...)`**](#WITH) which accesses copies of local objects. 1178 1179 <A name="MAKE_CONST_MOCKn"/> 1180 1181 ### **`MAKE_CONST_MOCKn(`** *func_name*, *signature* {, *specifiers* } **`)`** 1182 1183 Make a `const` [mock function](#mock_function) named *func_name*. It is a good 1184 idea for this to implement a pure virtual function from an interface, but 1185 it is not a requirement. `n` is the number of parameters in *signature*. 1186 *specifiers* is an optional list which may include attributes or specifiers like 1187 [`override`](http://en.cppreference.com/w/cpp/language/override) or 1188 [`noexcept`](https://en.cppreference.com/w/cpp/language/noexcept_spec). 1189 1190 Example: 1191 1192 ```Cpp 1193 class I 1194 { 1195 public: 1196 virtual ~I() = default; 1197 virtual int func1(int, const std::vector<int>&)) const = 0; 1198 }; 1199 1200 class C 1201 { 1202 public: 1203 MAKE_CONST_MOCK2(func1, int(int, const std::vector<int>&), override); 1204 MAKE_CONST_MOCK1(func2, int(std::string)); 1205 }; 1206 ``` 1207 1208 Above, class `C` will effectively become: 1209 1210 ```Cpp 1211 class C : public I 1212 { 1213 public: 1214 int func1(int, const std::vector<int>&) const override; 1215 int func2(std::string) const; 1216 }; 1217 ``` 1218 1219 It is not possible to mock operators, constructors or the destructor, but 1220 you can call [mock functions](#mock_function) from those. 1221 1222 See also [**`MAKE_MOCKn(...)`**](#MAKE_MOCKn) for non-`const` 1223 member functions. 1224 1225 <A name="MAKE_MOCKn"/> 1226 1227 ### **`MAKE_MOCKn(`** *func_name*, *signature* {, *specifiers* } **`)`** 1228 1229 Make a non-const [mock function](#mock_function) named *func_name*. It is a 1230 good idea for this to implement a pure virtual function from an interface, but 1231 it is not a requirement. `n` is the number of parameters in *signature*. 1232 *specifiers* is an optional list which may include attributes or specifiers like 1233 [`override`](http://en.cppreference.com/w/cpp/language/override) or 1234 [`noexcept`](https://en.cppreference.com/w/cpp/language/noexcept_spec). 1235 1236 Example: 1237 1238 ```Cpp 1239 class I 1240 { 1241 public: 1242 virtual ~I() = default; 1243 virtual int func1(int, const std::vector<int>&)) = 0; 1244 }; 1245 1246 class C : public I 1247 { 1248 public: 1249 MAKE_MOCK2(func1, int(int, const std::vector<int>&), override); 1250 MAKE_MOCK1(func2, int(std::string)); 1251 }; 1252 ``` 1253 1254 Above, class `C` will effectively become: 1255 1256 ```Cpp 1257 class C : public I 1258 { 1259 public: 1260 int func1(int, const std::vector<int>&) override; 1261 int func2(std::string); 1262 }; 1263 ``` 1264 1265 It is not possible to mock operators, constructors or the destructor, but 1266 you can call [mock functions](#mock_function) from those. 1267 1268 See also [**`MAKE_CONST_MOCKn(...)`**](#MAKE_CONST_MOCKn) for `const` 1269 member functions. 1270 1271 <A name="NAMED_ALLOW_CALL"/> 1272 1273 ### **`NAMED_ALLOW_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`** 1274 1275 Make a [`std::unique_ptr<trompeloeil::expectation>`](#expectation_type) 1276 allowing *mock_object*.*func_name*(*parameter_list*) to be 1277 called zero or more times until the expectation object is destroyed. 1278 *parameter_list* may contain exact values or [matchers](#matcher) 1279 that describes matching calls. 1280 1281 This is the same as 1282 [**`NAMED_REQUIRE_CALL(...)`**](#NAMED_REQUIRE_CALL).[**`TIMES(`**](#TIMES) 0,infinity **`)`**. 1283 1284 Matches any number of times, but is not required to match. (_Actually the limit is 1285 0..~0ULL, but that is for all practical purposes "infinity"_.) 1286 1287 **NOTE!** Any named objects referenced in attached 1288 [**`LR_WITH(...)`**](#LR_WITH), [**`LR_SIDE_EFFECT(...)`**](#LR_SIDE_EFFECT), 1289 [**`LR_RETURN(...)`**](#LR_RETURN) and [**`LR_THROW(...)`**](#LR_THROW) are 1290 captured by reference so lifetime management is important. 1291 1292 Example: 1293 1294 ```Cpp 1295 class C 1296 { 1297 public: 1298 MAKE_MOCK1(func, void(int)); 1299 }; 1300 1301 using trompeloeil::gt; 1302 using trompeloeil::lt; 1303 using expectation = std::unique_ptr<trompeloeil::expectation>; 1304 1305 TEST(atest) 1306 { 1307 C mock_obj; 1308 1309 expectation x1 = NAMED_ALLOW_CALL(mock_obj, func(gt(0)); 1310 1311 test_function(0, &mock_obj); 1312 1313 expectation x2 = NAMED_ALLOW_CALL(mock_obj, func(lt(0)); 1314 1315 test_function(1, &mock_obj); 1316 1317 x1.reset(); // no longer allow calls with positive values 1318 1319 test_function(2, &mock_obj); 1320 } 1321 ``` 1322 1323 Above each **`NAMED_ALLOW_CALL(mock_obj, func(...))`** creates an expectation 1324 that *mock_obj.func()* may be called any number of times. Each expectation is 1325 valid for the lifetime of the expectation object. In the example above, 1326 this means that `x1` is valid for the first two calls to `test_function(...)`, 1327 while `x2` is valid for the last two calls to `test_function(...)`. 1328 1329 See also [**`ALLOW_CALL(...)`**](#ALLOW_CALL) which creates an expectation 1330 that is valid until the end of the surrounding scope. 1331 1332 <A name="NAMED_FORBID_CALL"/> 1333 1334 ### **`NAMED_FORBID_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`** 1335 1336 Make a [`std::unique_ptr<trompeloeil::expectation>`](#expectation_type) 1337 disallowing calls to *mock_object*.*func_name*(*parameter_list*) until the 1338 expectation object is destroyed. *parameter_list* may contain exact values or 1339 [matchers](#matcher) that describes matching calls. 1340 1341 This is the same as 1342 [**`NAMED_REQUIRE_CALL(...)`**](#NAMED_REQUIRE_CALL).[**`TIMES(`**](#TIMES) 1343 0 **`)`**, making any matching call an error. This is typically done when a wider 1344 scope would allow the call. [**`RETURN(...)`**](#RETURN), 1345 [**`LR_RETURN(...)`**](#LR_RETURN), [**`LR_THROW(...)`**](#LR_THROW) and 1346 [**`THROW(...)`**](#THROW) are illegal in a **`NAMED_FORBID_CALL(...)`**. 1347 1348 **NOTE!** Any named objects referenced in attached 1349 [**`LR_WITH(...)`**](#LR_WITH) are captured by reference so lifetime management 1350 is important. 1351 1352 Example: 1353 1354 ```Cpp 1355 class C 1356 { 1357 public: 1358 MAKE_MOCK1(func, void(int)); 1359 } 1360 1361 using trompeloeil::_; 1362 using trompeloeil::gt; 1363 using trompeloeil::lt; 1364 1365 using expectation = std::unique_ptr<trompeloeil::expectation>; 1366 1367 TEST(atest) 1368 { 1369 C mock_obj; 1370 1371 ALLOW_CALL(mock_obj, func(_)); 1372 1373 expectation x1 = NAMED_FORBID_CALL(mock_obj, func(gt(0)); 1374 1375 test_function(0, &mock_obj); 1376 1377 expectation x2 = NAMED_FORBID_CALL(mock_obj, func(lt(0)); 1378 1379 test_function(1, &mock_obj); 1380 1381 x1.reset(); // allow calls with positive values again 1382 1383 test_function(2, &mock_obj); 1384 } 1385 ``` 1386 1387 Above, calls to `mock_obj.func()` are generally allowed throughout the test. 1388 However, `x1` imposes a restriction that calls with positive values are illegal, 1389 and that restriction is in place for the first two calls to 1390 `test_function(...)`. `x2` imposes a restrictions that calls with negative 1391 values are illegal, and that restriction is in place for the last two calls to 1392 `test_function(...)`. 1393 1394 See also [**`FORBID_CALL(...)`**](#FORBID_CALL) which creates an 1395 [expectation](#expectation) that is valid until the end of the surrounding scope. 1396 1397 <A name="NAMED_REQUIRE_CALL"/> 1398 1399 ### **`NAMED_REQUIRE_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`** 1400 1401 Make a [`std::unique_ptr<trompeloeil::expectation>`](#expectation_type) 1402 requiring that *mock_obj*.*func_name*(*parameter_list*) is called exactly once 1403 before the expectation object is destroyed. *parameter_list* may contain exact 1404 values or [matchers](#matcher) that describes matching calls. 1405 1406 The number of matches required before the [expectation](#expectation) object 1407 is destroyed can be changed with an optional [**`TIMES(...)`**](#TIMES) clause. 1408 1409 **NOTE!** Any named objects referenced in attached 1410 [**`LR_WITH(...)`**](#LR_WITH), [**`LR_SIDE_EFFECT(...)`**](#LR_SIDE_EFFECT), 1411 [**`LR_RETURN(...)`**](#LR_RETURN) and [**`LR_THROW(...)`**](#LR_THROW) are 1412 captured by reference so lifetime management is important. 1413 1414 Example: 1415 1416 ```Cpp 1417 class C 1418 { 1419 public: 1420 MAKE_MOCK1(func, void(int)); 1421 } 1422 1423 using trompeloeil::gt; 1424 using trompeloeil::lt; 1425 1426 using expectation = std::unique_ptr<trompeloeil::expectation>; 1427 1428 TEST(atest) 1429 { 1430 C mock_obj; 1431 1432 expectation x1 = NAMED_REQUIRE_CALL(mock_obj, func(gt(0)); 1433 1434 test_function(0, &mock_obj); 1435 1436 expectation x2 = NAMED_REQUIRE_CALL(mock_obj, func(lt(0)); 1437 1438 test_function(1, &mock_obj); 1439 1440 x1.reset(); // The call with positive number must be done here. 1441 1442 test_function(2, &mock_obj); 1443 } 1444 ``` 1445 1446 Above, the first two calls to `test_function(...)` must together call 1447 `mock_obj.func(...)` exactly once with a positive value, and the last two 1448 calls to `test_function(...)` must together call `mock_obj.func(...)` 1449 exactly once with a negative number. 1450 1451 See also [**`REQUIRE_CALL(...)`**](#REQUIRE_CALL) which creates an 1452 [expectation](#expectation) that is valid until the end of the surrounding scope. 1453 1454 <A name="NAMED_REQUIRE_DESTRUCTION"/> 1455 1456 ### **`NAMED_REQUIRE_DESTRUCTION(`** *mock_object* **`)`** 1457 1458 Create a 1459 [`std::unique_ptr<trompeloeil::expectation>`](#expectation_type) 1460 object which reports a violation if the 1461 [**`deathwatched_type`**](#deathwatched_type) [mock object](#mock_object) is 1462 not destroyed by the time the `expectation` is destroyed. 1463 1464 Example: 1465 1466 ```Cpp 1467 class C 1468 { 1469 public: 1470 virtual ~C() = default; // must be virtual for deathwatched 1471 MAKE_MOCK1(func, void(int)); 1472 } 1473 1474 using monitor = std::unique_ptr<trompeloeil::expectation>; 1475 using trompeloeil::deathwatched; 1476 1477 TEST(atest) 1478 { 1479 C* p = new deathwatched<C>(); 1480 1481 test_function(0, p); // must not destroy *p 1482 1483 monitor m = NAMED_REQUIRE_DESTRUCTION(*p); 1484 1485 test_function(1, p); 1486 1487 m.reset(); // *p must have been destroyed here 1488 } 1489 ``` 1490 1491 Above, `p` points to a [`deathwatched`](#deathwatched_type) 1492 [mock object](#mock_object), meaning that a violation is reported if `*p` is 1493 destroyed without having a destruction requirement. 1494 1495 The monitor `m` is a requirement that `*p` is destroyed before the 1496 [`lifetime_monitor`](#lifetime_monitor_type) 1497 (subtype of [`expectation`](#expectation_type)) held by `m` is destroyed. 1498 1499 It is thus a violation if the first call to `test_function(...)` destroys 1500 `*p`, and another violation if the second call to `test_function(...)` 1501 does not destroy `*p` 1502 1503 See also [**`REQUIRE_DESTRUCTION(...)`**](#REQUIRE_DESTRUCTION) which places 1504 a requirement that the [`deathwatched`](#deathwatched_type) 1505 [mock object](#mock_object) is destroyed before the end of the scope. 1506 1507 <A name="REQUIRE_CALL"/> 1508 1509 ### **`REQUIRE_CALL(`** *mock_object*, *func_name*(*parameter_list*)**`)`** 1510 1511 Make an [expectation](#expectation) requiring that 1512 *mock_obj*.*func_name*(*parameter_list*) is called exactly once before 1513 the end of the scope. *parameter_list* may contain exact values 1514 or [matchers](#matcher) that describes matching parameter values for the 1515 [expectation](#expectation). 1516 1517 The number of matches required before the [expectation](#expectation) object 1518 is destroyed can be changed with an optional [**`TIMES(...)`**](#TIMES) clause. 1519 1520 Example: 1521 1522 ```Cpp 1523 class C 1524 { 1525 public: 1526 MAKE_MOCK1(func, void(int)); 1527 } 1528 1529 using trompeloeil::gt; 1530 using trompeloeil::lt; 1531 1532 TEST(atest) 1533 { 1534 C mock_obj; 1535 { 1536 REQUIRE_CALL(mock_obj, func(gt(0)); 1537 1538 test_function(0, &mock_obj); 1539 // end of scope, requirement must be fulfilled here 1540 } 1541 { 1542 REQUIRE_CALL(mock_obj, func(lt(0)); 1543 1544 test_function(1, &mock_obj); 1545 // end of scope, requirement must be fulfilled here 1546 } 1547 } 1548 ``` 1549 1550 Above, the first call to `test_function(...)` must call 1551 `mock_obj.func(...)` exactly once with a positive value, and the second 1552 call to `test_function(...)` must call `mock_obj.func(...)` 1553 exactly once with a negative number. 1554 1555 See also [**`NAMED_REQUIRE_CALL(...)`**](#NAMED_REQUIRE_CALL) which creates an 1556 [expectation](#expectation) that is held by a 1557 [`std::unique_ptr<trompeloeil::expectation>`](#expectation_type) which can be stored in test 1558 fixtures. 1559 1560 <A name="REQUIRE_DESTRUCTION"/> 1561 1562 ### **`REQUIRE_DESTRUCTION(`** *mock_object* **`)`** 1563 1564 Create an anonymous [`lifetime_monitor`](#lifetime_monitor_type) which reports 1565 a violation if the [**`deathwatched`**](#deathwatched_type) 1566 [mock object](#mock_object) is not destroyed by the end of the scope. 1567 1568 Example: 1569 1570 ```Cpp 1571 class C 1572 { 1573 public: 1574 virtual ~C() = default; // must be virtual for deathwatched 1575 MAKE_MOCK1(func, void(int)); 1576 } 1577 1578 using trompeloeil::deathwatched; 1579 1580 TEST(atest) 1581 { 1582 C* p = new deathwatched<C>(); 1583 1584 test_function(0, p); // must not destroy *p 1585 1586 { 1587 REQUIRE_DESTRUCTION(*p); 1588 1589 test_function(1, p); 1590 // end of scope, *p must have been destroyed here 1591 } 1592 } 1593 ``` 1594 1595 Above, `p` points to a [`deathwatched`](#deathwatched_type) 1596 [mock object](#mock_object), meaning that a violation is reported if `*p` is 1597 destroyed without having a destruction requirement. 1598 1599 **`REQUIRE_DESTRUCTION(...)`** in the local scope puts a requirement on 1600 `*p` that it must be destroyed by the end of the scope. 1601 1602 It is thus a violation if the first call to `test_function(...)` destroys 1603 `*p`, and another violation if the second call to `test_function(...)` 1604 does not destroy `*p`. 1605 1606 See also [**`NAMED_REQUIRE_DESTRUCTION(...)`**](#NAMED_REQUIRE_DESTRUCTION) 1607 which creates the requirement that the [`deathwatched`](#deathwatched_type) 1608 [mock object](#mock_object) is destroyed as a 1609 [`std::unique_ptr<trompeloeil::lifetime_monitor>`](#lifetime_monitor_type) 1610 which can be stored in test fixtures. 1611 1612 <A name="RETURN"/> 1613 1614 ### **`RETURN(`** *expr* **`)`** 1615 1616 Used in [expectations](#expectation) to set the return value after having 1617 evaluated every [**`SIDE_EFFECT(...)`**](#SIDE_EFFECT) and 1618 [**`LR_SIDE_EFFECT(...)`**](#LR_SIDE_EFFECT). 1619 For `void` functions **`RETURN(...)`** is illegal. For non-`void` functions 1620 exactly one of [**`LR_RETURN(...)`**](#LR_RETURN), **`RETURN(...)`**, 1621 [**`LR_THROW(...)`**](#LR_THROW) or [**`THROW(...)`**](#THROW) is required. 1622 *expr* may refer to parameters in the call with their positional names `_1`, 1623 `_2`, etc. 1624 This code may alter out-parameters. 1625 1626 Named local objects accessed here refers to a immutable copies. 1627 1628 Example: 1629 1630 ```Cpp 1631 class C 1632 { 1633 public: 1634 MAKE_MOCK1(func, int&(unsigned)); 1635 }; 1636 1637 using trompeloeil::_; 1638 1639 std::vector<int> values{3,2,1,0}; 1640 1641 TEST(atest) 1642 { 1643 C mock_obj; 1644 1645 int offset = 1; 1646 ALLOW_CALL(mock_obj, func(_)) 1647 .WITH(_1 + offset < values.size()) 1648 .RETURN(values[_1 + offset]); 1649 1650 offset = 2; 1651 test_func(&mock_obj); 1652 } 1653 ``` 1654 1655 Above, the **`RETURN(...)`** clause tells matching calls of 1656 `mock_obj.func(...)` to return a reference to an element in the global 1657 `std::vector<int> values`. Since **`RETURN(...)`** accesses copies of local 1658 variables, the value of `offset` is 1 in the index calculation if called from 1659 within `test_func(...)`. 1660 1661 **NOTE!** It is illegal to return a reference to a captured local variable. 1662 1663 See also [**`LR_RETURN(...)`**](#LR_RETURN) which accesses local variables 1664 by reference. 1665 1666 <A name="SIDE_EFFECT"/> 1667 1668 ### **`SIDE_EFFECT(`** *expr* **`)`** 1669 1670 Used in [expectations](#expectation) to cause side effects for matching calls. 1671 *expr* is only evaluated when all [**`WITH(...)`**](#WITH) and 1672 [**`LR_WITH(...)`**](#LR_WITH) clauses are matched. *expr* may refer to 1673 parameters in the call with their positional names `_1`, `_2`, etc. This code 1674 may alter out-parameters. 1675 Several **`SIDE_EFFECT(...)`** and [**`LR_SIDE_EFFECT(...)`**](#LR_SIDE_EFFECT) 1676 clauses can be added to a single [expectation](#expectation), and they are 1677 evaluated in order. 1678 1679 Named local objects accessed here refers to immutable copies. 1680 1681 Example: 1682 1683 ```Cpp 1684 class C 1685 { 1686 public: 1687 MAKE_MOCK1(func, void(unsigned)); 1688 }; 1689 1690 unsigned sum = 0; 1691 1692 TEST(atest) 1693 { 1694 C mock_obj; 1695 unsigned offset = 0; 1696 ALLOW_CALL(mock_obj, func(ANY(unsigned)) 1697 .SIDE_EFFECT(sum += offset + _1); 1698 1699 offset = 2; 1700 tested_func(&mock_obj); 1701 1702 std::cout << "offset corrected parameter sum=" << sum << "\n"; 1703 } 1704 ``` 1705 1706 Above, `tested_func(...)` is allowed to call `mock_obj.func()` any 1707 number of times. Each time a side effect is that the global 1708 variable `sum` gets the parameter value added to it adjusted for `offset`. 1709 Since **`SIDE_EFFECT(...)`** refers to a copy of `offset`, the value of 1710 `offset` is `0` in any matching calls from within `tested_func(...)` 1711 1712 See also [**`LR_SIDE_EFFECT(...)`**](#LR_SIDE_EFFECT) which accesses local 1713 objects by reference. 1714 1715 <A name="THROW"/> 1716 1717 ### **`THROW(`** *expr* **`)`** 1718 1719 Used in [expectations](#expectation) to throw after having evaluated every 1720 [**`SIDE_EFFECT(...)`**](#SIDE_EFFECT) and 1721 [**`LR_SIDE_EFFECT(...)`**](#LR_SIDE_EFFECT) for a matching call. 1722 *expr* may refer to parameters in the call with their positional names `_1`, 1723 `_2`, etc. This code may alter out-parameters. It is not legal to combine 1724 **`THROW(...)`** with any of [**`LR_THROW(...)`**](#LR_THROW), 1725 [**`LR_RETURN(...)`**](#LR_RETURN) or [**`RETURN(...)`**](#RETURN). 1726 1727 Named local objects here refers to immutable copies. 1728 1729 Example: 1730 1731 ```Cpp 1732 class C 1733 { 1734 public: 1735 MAKE_MOCK1(func, void(unsigned)); 1736 }; 1737 1738 TEST(atest) 1739 { 1740 C mock_obj; 1741 std::string what="<unknown>"; 1742 1743 ALLOW_CALL(mock_obj, func(3)) 1744 .THROW(std::invalid_argument(what)); 1745 1746 what = ""; 1747 tested_func(&mock_obj); 1748 } 1749 ``` 1750 1751 Above, **`THROW(...)`** will refer to a copy of the string `what` with the value 1752 `"<unknown>"` if a matching call is made from within `tested_func(...)` 1753 1754 See also [**`LR_THROW(...)`**](#LR_THROW) which accesses copies of local objects. 1755 1756 <A name="TIMES"/> 1757 1758 ### **`TIMES(`** *limits* **`)`** 1759 1760 Used in [**`REQUIRE_CALL(...)`**](#REQUIRE_CALL) and 1761 [**`NAMED_REQUIRE_CALL(...)`**](#NAMED_REQUIRE_CALL) to set the limits on 1762 the number of matching calls required. 1763 1764 *limits* may be a single number, in which case it is the exact number of 1765 matching calls required. 1766 1767 *limits* may also be two numbers, describing a range *min-inclusive*, 1768 *max-inclusive*. 1769 1770 If the minimum number of matching calls in not met before the end of the 1771 lifetime of the [expectation](#expectation), a violation is reported. 1772 1773 If the maximum number of matching calls is exceeded, a violation is reported. 1774 1775 *limits* must be 1776 [`constexpr`](http://en.cppreference.com/w/cpp/language/constexpr). 1777 1778 **`TIMES(...)`** may only be used once for each 1779 [**`REQUIRE_CALL(..)`**](#REQUIRE_CALL) or 1780 [**`NAMED_REQUIRE_CALL(...)`**](#NAMED_REQUIRE_CALL). 1781 1782 Example: 1783 1784 ```Cpp 1785 class C 1786 { 1787 public: 1788 MAKE_MOCK1(func, void(unsigned)); 1789 }; 1790 1791 using trompeloeil::_; 1792 1793 TEST(atest) 1794 { 1795 C mock_obj; 1796 1797 REQUIRE_CALL(mock_obj, func(_)) 1798 .TIMES(2, 5); 1799 1800 tested_func(&mock_obj); 1801 ``` 1802 1803 Above, `tested_func(...)` is expected to call `mock_obj.func()` at least two 1804 times, and no more than 5 times. 1805 1806 See also the helpers [**`AT_LEAST(...)`**](#AT_LEAST) and 1807 [**`AT_MOST(...)`**](#AT_MOST). 1808 1809 <A name="WITH"/> 1810 1811 ### **`WITH(`** *expr* **`)`** 1812 1813 Used with [expectations](#expectation) to add further conditions for a 1814 matching call. Typically used when [matchers](#matcher) are used for the 1815 parameters, and often when the condition requires several parameter values 1816 together. 1817 *expr* can refer to parameters in the call with their positional names `_1`, 1818 `_2`, etc. Even if the function signature has parameters as non-`const` 1819 references, they are immutable in this context. Several **`WITH(...)`** 1820 and [**`LR_WITH(...)`**](#LR_WITH) clauses can be added to a single expectation 1821 and they are tried in the order until one has failed, or they all have passed. 1822 1823 Named local objects here refers to immutable copies. 1824 1825 Example: 1826 1827 ```Cpp 1828 class C 1829 { 1830 public: 1831 MAKE_MOCK1(func, void(const char*, size_t)); 1832 }; 1833 1834 using trompeloeil::_; 1835 1836 TEST(atest) 1837 { 1838 C mock_obj; 1839 1840 std::string str = "string"; 1841 1842 REQUIRE_CALL(mock_obj, func(_,_)) 1843 .WITH(std::string(_1, _2) == str); 1844 1845 str = ""; // does not alter the copy in the expectation above. 1846 1847 tested_func(buff, &mock_obj); 1848 } 1849 ``` 1850 1851 Above, **`WITH(std::string(_1, _2) == str)`** checks the condition that the 1852 string constructed from the parameters is equal to a copy of the local variable 1853 `str`. To pass the test, `tested_func(...)` must in other words call 1854 `mock_obj.func()` with string `"string"`. 1855 1856 **NOTE!** It is legal, but a *very* bad idea, to modify global/static objects in 1857 **`WITH(...)`**. If several [expectations](#expectation) could match and 1858 are disambiguated by [**`LR_WITH(...)`**](#LR_WITH) and **`WITH(...)`** the 1859 global/static objects will be modified also by those 1860 [expectations](#expectation) that do not match. 1861 1862 See also [**`LR_WITH(...)`**](#LR_WITH) which accesses local objects by 1863 reference. 1864 1865 ## <A name="types_and_templates"/>Types and Templates (alphabetical order) 1866 1867 ### <A name="deathwatched_type"/>`trompeloeil::deathwatched<T>` 1868 1869 Template used when it is necessary to control the life time of a 1870 [mock object](#mock_object). The macros 1871 [**`REQUIRE_DESTRUCTION(...)`**](#REQUIRE_DESTRUCTION) and 1872 [**`NAMED_REQUIRE_DESTRUCTION(...)`**](#NAMED_REQUIRE_DESTRUCTION) 1873 operates on instances of `trompeloeil::deathwatched<T>`. 1874 1875 Example: 1876 1877 ```Cpp 1878 class Mock 1879 { 1880 public: 1881 virtual ~Mock() = default; // virtual destructor needed for deathwatched<> 1882 MAKE_MOCK1(func, void(int)); 1883 }; 1884 1885 using trompeloeil::_; 1886 1887 void test_func() 1888 { 1889 auto p = new trompeloeil::deathwatched<Mock>(); 1890 ALLOW_CALL(*p, func(_)); 1891 func1(p); 1892 { 1893 FORBID_CALL(*p, func(_)); 1894 REQUIRE_DESTRUCTION(*p); 1895 func2(p); 1896 } 1897 } 1898 ``` 1899 1900 Above, `func1(p)` must not destroy `p`, or a violation is reported, and 1901 `func2(p)` may not call the [mock function](#mock_function) on `p`, but 1902 is required to destroy the [mock object](#mock_object), or a violation will 1903 be reported. 1904 1905 `trompeloeil::deathwatched<T>` inherits from `T`, and the constructor 1906 accepts any parameters and 1907 [perfectly forwards](http://www.cppsamples.com/common-tasks/perfect-forwarding.html) 1908 them to the constructor of `T`. The mock type `T` must have a virtual 1909 destructor. 1910 1911 ### <A name="expectation_type"/>`trompeloeil::expectation` 1912 1913 Base class for all [expectations](#expectation). The macros 1914 [**`NAMED_ALLOW_CALL(...)`**](#NAMED_ALLOW_CALL), 1915 [**`NAMED_FORBID_CALL(...)`**](#NAMED_FORBID_CALL) and 1916 [**`NAMED_REQUIRE_CALL(...)`**](#NAMED_REQUIRE_CALL) results in a 1917 [`std::unique_ptr<trompeloeil::expectation>`](http://en.cppreference.com/w/cpp/memory/unique_ptr) 1918 which you can hold in a variable. 1919 1920 ### <A name="expectation_violation_type"/>`trompeloeil::expectation_violation` 1921 1922 The exception type used by default to report violations. 1923 1924 ```Cpp 1925 class expectation_violation : public std::logic_error 1926 { 1927 public: 1928 using std::logic_error::logic_error; 1929 }; 1930 ``` 1931 1932 The `what()` string contains the violation report message. 1933 1934 ### <A name="lifetime_monitor_type"/>`trompeloeil::lifetime_monitor` 1935 1936 The macro [**`NAMED_REQUIRE_DESTRUCTION(...)`**](#NAMED_REQUIRE_DESTRUCTION) 1937 results in a 1938 [`std::unique_ptr<trompeloeil::lifetime_monitor>`](http://en.cppreference.com/w/cpp/memory/unique_ptr) 1939 which you can hold in a variable. `trompeloeil::lifetime_monitor` inherits from 1940 [`trompeloeil::expectation`](#expectation_type). 1941 1942 Example: 1943 1944 ```Cpp 1945 class Mock 1946 { 1947 public: 1948 virtual ~Mock() = default; // virtual destructor needed for deathwatched<> 1949 MAKE_MOCK1(func, void(int)); 1950 }; 1951 1952 using trompeloeil::_; 1953 using monitor = std::unique_ptr<trompeloeil::lifetime_monitor>; 1954 1955 void test_func() 1956 { 1957 auto p = new trompeloeil::deathwatched<Mock>(); 1958 ALLOW_CALL(*p, func(_)); 1959 func1(p); 1960 { 1961 FORBID_CALL(*p, func(_)); 1962 monitor m = NAMED_REQUIRE_DESTRUCTION(*p); 1963 std::unique_ptr<trompeloeil::expectation> e = std::move(m); 1964 func2(p); 1965 e.reset(); 1966 } 1967 } 1968 ``` 1969 1970 ### <A name="matcher_type"/>`trompeloeil::matcher` 1971 1972 `trompeloeil::matcher` is the base class for all [matchers](#matcher). It does 1973 not do anything and is solely used in internal 1974 [`SFINAE`](http://en.cppreference.com/w/cpp/language/sfinae) constructions 1975 and [tag dispatch](http://www.generic-programming.org/languages/cpp/techniques.php#tag_dispatching) 1976 1977 Use it, or [`trompeloeil::typed_matcher<T>`](#typed_matcher), as the base class 1978 when writing custom [matchers](CookBook.md/#custom_matchers). 1979 1980 ### <A name="mock_interface"/>`trompeloeil::mock_interface<T>` 1981 1982 `trompeloeil::mock_interface<T>` is a template useful when creating a mock from 1983 an existing interface (i.e. a `struct` or `class` with virtual functions that 1984 you want to mock). 1985 1986 It enables use of the [**`IMPLEMENT_MOCKn(...)`**](#IMPLEMENT_MOCKn) and 1987 [**`IMPLEMENT_CONST_MOCKn(...)`**](#IMPLEMENT_CONST_MOCKn) macros. 1988 The [**`MAKE_MOCKn(...)`**](#MAKE_MOCKn) and 1989 [**`MAKE_CONST_MOCKn(...)`**](#MAKE_CONST_MOCKn) macros can also be used. 1990 1991 The interface type `T` must not be final. 1992 1993 Example: 1994 1995 ```Cpp 1996 class interface 1997 { 1998 public: 1999 virtual ~interface() = default; 2000 virtual void func(int) = 0; 2001 }; 2002 2003 class mock : trompeloeil::mock_interface<interface> 2004 { 2005 public: 2006 IMPLEMENT_MOCK1(func); // implements pure virtual interface::func(int); 2007 }; 2008 2009 void tested_func(interface& i); 2010 2011 void test() 2012 { 2013 mock m; 2014 REQUIRE_CALL(m, func(3)); 2015 tested_func(m); 2016 } 2017 ``` 2018 2019 **NOTE!** `mock_interface<T>` cannot be used to inherit multiple interfaces. 2020 2021 ### <A name="ok_reporter_func"/>`trompeloeil::ok_reporter_func` 2022 2023 A type used to pass information to the unit testing frame work that a call to a 2024 [mock function](#mock_function) has not been reported as a violation. 2025 2026 ```Cpp 2027 using trompeloeil::ok_reporter_func = std::function<const char*>; 2028 ``` 2029 2030 The string passed is the parameters to the expectation. E.g. 2031 2032 ```Cpp 2033 struct Mock 2034 { 2035 MAKE_MOCK1(func, void(int)); 2036 }; 2037 2038 TEST(...) 2039 { 2040 Mock m; 2041 REQUIRE_CALL(m, func(3)); // passes "m.func(3)" to OK reporter 2042 2043 ... 2044 } 2045 ``` 2046 2047 ### <A name="printer"/> `trompeloeil::printer<T>` 2048 2049 `printer<T>` is a type that formats values to strings in reports from *Trompeloeil*. 2050 2051 ```Cpp 2052 template <typename T> 2053 struct printer 2054 { 2055 static void print(ostream& os, const T& t); 2056 }; 2057 ``` 2058 2059 By default the `print` function formats using `os << t`, provided the type `T` 2060 can be inserted into an `ostream`, otherwise it gives a hex-dump of the bytes 2061 occupied by the object. 2062 2063 The type `trompeloeil::printer<T>` is a customization point that you can use 2064 to define string formatting for types that do not support `os << t`, or for 2065 which you want a different representation in reports from *Trompeloeil*. 2066 See example in the [Cook Book](CookBook.md/#custom_formatting). 2067 2068 ### <A name="reporter_func"/>`trompeloeil::reporter_func` 2069 2070 A type used to pass information to the unit testing frame work that a call has 2071 been made in violation of a [mock function](#mock_function). 2072 2073 ```Cpp 2074 using trompeloeil::reporter_func = std::function<void(trompeloeil::severity, 2075 char const *file, 2076 unsigned long line, 2077 const std::string& msg)>; 2078 ``` 2079 2080 See [`trompeloeil::severity`](#severity_type). 2081 2082 The parameter `msg` contains detailed information about the violation and 2083 which (if any) [expectations](#expectation) there are on the 2084 [mock function](#mock_function). 2085 2086 ### <A name="sequence_type"/>`trompeloeil::sequence` 2087 2088 Type of object used for fine-tuned control of sequencing of matched 2089 [expectations](#expectation). 2090 2091 Example: 2092 2093 ```Cpp 2094 class FileOps 2095 { 2096 public: 2097 using handle = int; 2098 MAKE_MOCK1(open, handle(const std::string&)); 2099 MAKE_MOCK3(write, size_t(handle, const char*, size_t)); 2100 MAKE_MOCK1(close, void(handle)); 2101 }; 2102 2103 using trompeloeil::ne; 2104 2105 void test() 2106 { 2107 FileOps ops; 2108 2109 auto seq = trompeloeil::sequence; // sequence object 2110 2111 int handle = 4711; 2112 2113 REQUIRE_CALL(ops, open("name")) 2114 .RETURN(handle) 2115 .IN_SEQUENCE(seq); 2116 2117 REQUIRE_CALL(ops, write(handle, ne(nullptr), ne(0))) 2118 .RETURN(0) // indicate failure 2119 .IN_SEQUENCE(seq); 2120 2121 REQUIRE_CALL(ops, write(handle, ne(nullptr), ne(0))) 2122 .RETURN(_3) // successful retry 2123 .IN_SEQUENCE(seq); 2124 2125 REQUIRE_CALL(ops, close(handle)) 2126 .IN_SEQUENCE(seq); 2127 2128 test_writes(&ops); 2129 } 2130 ``` 2131 2132 Sequence objects are movable but not copyable. 2133 2134 **NOTE!** The [**`.IN_SEQUENCE(...)`**](#IN_SEQUENCE) macro accepts many 2135 sequence objects. 2136 2137 ### <A name="severity_type"/> `trompeloeil::severity` 2138 2139 Type used in violation reports to dictate what actions are allowed by the 2140 report handler. 2141 2142 ```Cpp 2143 namespace trompeloeil { 2144 enum class severity { fatal, nonfatal }; 2145 } 2146 ``` 2147 2148 A value of `trompeloeil::severity::fatal` dictates that the report handler 2149 must not return. It may throw or end the program execution. 2150 2151 A value of `trompeloeil::severity::nonfatal` dictates that the report handler 2152 is called from stack rollback and must not throw, lest 2153 [`std::terminate`](http://en.cppreference.com/w/cpp/error/terminate) is 2154 called. 2155 2156 ### <A name="stream_tracer"/>`trompeloeil::stream_tracer` 2157 2158 An instance of `trompeloeil::stream_tracer` prints information about 2159 matched calls to the 2160 [output stream](http://en.cppreference.com/w/cpp/io/basic_ostream) 2161 it refers to. `stream_tracer` inherits from 2162 [`trompeloeil::tracer`](#tracer_type). 2163 2164 ```Cpp 2165 namespace trompeloeil { 2166 class stream_tracer : public ::trompeloeil::tracer 2167 { 2168 public: 2169 stream_tracer(std::ostream& stream); 2170 void trace(char const* file, unsigned long line, std::string const& call) override; 2171 }; 2172 } 2173 ``` 2174 2175 See "[Using `trompeloeil::stream_tracer`](CookBook.md/#stream_tracer)" in the 2176 [Cook Book](CookBook.md). 2177 2178 ### <A name="tracer_type"/>`trompeloeil::tracer` 2179 2180 Base class for tracers. Inherit from it when writing custom tracers. 2181 2182 ```Cpp 2183 namespace trompeloeil { 2184 class tracer 2185 { 2186 public: 2187 virtual void trace(char const* file, unsigned long line, std::string const& call) = 0; 2188 protected: 2189 tracer(); 2190 tracer(tracer const&) = delete; 2191 virtual ~tracer(); 2192 ... 2193 }; 2194 } 2195 ``` 2196 2197 See "[Writing custom tracers](CookBook.md/#custom_tracer)" in the 2198 [Cook Book](CookBook.md) for an example. 2199 2200 ### <A name="typed_matcher"/> `trompeloeil::typed_matcher<T>` 2201 2202 Convenience class available when writing custom matchers for a specific 2203 type. It inherits from [`trompeloeil::matcher`](#matcher_type). 2204 2205 See "[Writing custom matchers](CookBook.md/#custom_matchers)" in the 2206 [Cook Book](CookBook.md) for examples. 2207 2208 ## <A name="functions"/>Functions 2209 2210 ### <A name="is_satisfied"/> `trompeloeil::expectation::is_satisfied() const` 2211 2212 Query an [expectation object](#expectation_type) if it is satisfied, i.e. if 2213 it will not report a missing call if it is destroyed. If 2214 [**`.TIMES()`**](#TIMES) is used, this is true if the minimum number of calls 2215 has been reached. 2216 2217 ```Cpp 2218 test(...) 2219 { 2220 ... 2221 auto e = NAMED_REQUIRE_CALL(mock_obj, func()) 2222 .TIMES(2,5); 2223 assert(!e->is_satisfied()); // no calls made yet. 2224 mock_obj.func(); 2225 assert(!e->is_satisfied()); // Only one call made, min is 2. 2226 mock_obj.func(); 2227 assert(e->is_satisfied()); // now 2 calls are made, so it's satisfied 2228 mock_obj.func(); 2229 assert(e->is_satisfied()); // 3 calls are made, it's still satisfied 2230 } 2231 ``` 2232 2233 ### <A name="is_saturated"/> `trompeloeil::expectation::is_saturated() const` 2234 2235 Query an [expectation object](#expectation_type) if it is saturated, i.e. if 2236 another call will report an unexpected call. If [**`.TIMES()`**](#TIMES) is 2237 used, this is true if the maximum number of calls has been reached. 2238 2239 ```Cpp 2240 ... 2241 auto e = NAMED_REQUIRE_CALL(mock_obj, func()) 2242 .TIMES(2,4); 2243 assert(!e->is_saturated()); // no calls made yet. 2244 mock_obj.func(); 2245 assert(!e->is_saturated()); // Only one call made, max is 4. 2246 mock_obj.func(); 2247 assert(!e->is_saturated()); // now 2 calls are made, still not saturated 2248 mock_obj.func(); 2249 assert(!e->is_saturated()); // 3 calls, one more to go. 2250 mock_obj.func(); 2251 assert(e->is_saturated()); // 4 calls, the expectation is now saturated 2252 2253 /* mock_obj.func();*/ // would cause "unexpected call" error. 2254 ``` 2255 2256 ### <A name="get_lock"/> `trompeloeil::get_lock()` 2257 2258 Get the global 2259 [`recursive_mutex`](http://en.cppreference.com/w/cpp/thread/recursive_mutex) 2260 used by *Trompeloeil*. The mutex is held until the end of the scope. 2261 2262 ### <A name="print"/>`trompeloeil::print(std::ostream& os, T const& t)` 2263 2264 By default `print()` uses the type [`printer<T>`](#printer) to format 2265 data to strings. 2266 2267 You can write specializations of 2268 `trompeloeil::print(std::ostream& os, T const& t)` for your own types 2269 `T`, but it is preferable to write a specialization of the type 2270 [`printer<T>`](#printer) instead, which also works for partial 2271 specializations. See example in the 2272 [Cook Book](CookBook.md/#custom_formatting). 2273 2274 ### <A name="is_null"/>`trompeloeil::is_null(T const&)` 2275 2276 Null check that works for all types. If `T` is not comparable with 2277 `nullptr` the value is false. This is mostly used when writing 2278 [duck typed matchers](CookBook.md/#custom_matchers). 2279 2280 ### <A name="make_matcher"/>`trompeloeil::make_matcher<Type>(...)` 2281 2282 ```Cpp 2283 template <typename Type, typename Predicate, typename Printer, typename ... T> 2284 auto make_matcher(Predicate predicate /* bool (Type& value, T const& ...) */, 2285 Printer printer /* void (std::ostream&, T const& ...) */, 2286 T&& ... stored_values); 2287 ``` 2288 2289 If `Type` is `trompeloeil::wildcard` a 2290 [duck typed matcher](CookBook.md/#duck_typed_matcher) is created, otherwise 2291 a matcher for the specified type `Type` is created. 2292 2293 `T&&...` is any number of values you want stored in the matcher. 2294 2295 `predicate` is a callable object, typically a lambda, that accepts the 2296 value to check for, and each of the stored values `T&&...` in order as 2297 `const&`. When `Type` is `trompeloeil::wildcard`, the first parameter must 2298 be of `auto` type. The return value must be convertible to `bool`. 2299 2300 `printer` is a callable object, typically a lambda, that accepts an 2301 [`ostream&`](http://en.cppreference.com/w/cpp/io/basic_ostream) and the 2302 stored values `T&&...` in order as `const&`. 2303 2304 Examples are found in the Cook Book under 2305 [Writing custom matchers](CookBook.md/#custom_matchers) 2306 2307 ### <A name="set_reporter"/>`trompeloeil::set_reporter(...)` 2308 2309 These functions are used to adapt *Trompeloeil* to your unit test framework 2310 of choice. 2311 2312 The default reporter throws 2313 [`trompeloeil::expectation_violation`](#expectation_violation_type) for 2314 all reports, with the violation message in the `what()` string. 2315 2316 If this is not suitable, you can change the report mechanism by 2317 calling `trompeloeil::set_reporter(...)` 2318 2319 ```Cpp 2320 reporter_func 2321 trompeloeil::set_reporter(std::function<void(trompeloeil::severity, 2322 char const *file, 2323 unsigned long line, 2324 const std::string& msg)>) 2325 ``` 2326 2327 The return value is the previous reporter function. 2328 2329 ```Cpp 2330 std::pair<reporter_func, ok_reporter_func> 2331 trompeloeil::set_reporter(std::function<void(trompeloeil::severity, 2332 char const *file, 2333 unsigned long line, 2334 const std::string& msg)> reporter, 2335 std::function<void(char const* msg> ok_reporter) 2336 ) 2337 ``` 2338 2339 The return value is the previous `reporter` and `ok_reporter`. An `ok_reporter` 2340 is called for every call to a [mock function](#mock_function) that is not 2341 reported as a violation. By default OK reports are ignored. 2342 2343 See [`trompeloeil::severity`](#severity_type) for the rules that it 2344 dictates. 2345 2346 See [`trompeloeil::reporter_func`](#reporter_func) and 2347 [`trompeloeil::ok_reporter_func`](#ok_reporter_func) for details. 2348 2349 The [Cook Book](CookBook.md) lists 2350 [adapter code](CookBook.md/#unit_test_frameworks) for a number of popular 2351 unit test frame works. 2352 2353 ### <A name="is_completed"/> `bool trompeloeil::sequence::is_completed() const` 2354 2355 Member function of [`sequence`](#sequence_type) object, used to query if 2356 the sequence it describes is completed or not. 2357 2358 Example: 2359 2360 ```Cpp 2361 void test() 2362 { 2363 auto seq = trompeloeil::sequence; 2364 mock_type mock; 2365 REQUIRE_CALL(mock, func1()) 2366 .IN_SEQUENCE(seq); 2367 REQUIRE_CALL(mock, func2()) 2368 .TIMES(100) 2369 .IN_SEQUENCE(seq); 2370 assert(!seq.is_completed()); // no calls yet 2371 mock.func1(); 2372 assert(!seq.is_completed()); // only first call, one remaining 2373 mock.func2(); 2374 assert(seq.is_completed()); // now sequence is completed 2375 } 2376 ``` 2377 2378 ## <A name="constants"/>Constants 2379 2380 ### <A name="movable_mock"/> `trompeloeil_movable_mock` 2381 2382 By adding a static constexpr bool member `trompeloeil_movable_mock` with the 2383 value `true` to your mock struct/class, you make it move constructible. Note 2384 that when a mock object is moved, any current expectations will be taken over 2385 by the newly constructed mock object, but note also that if the implicitly 2386 created lambdas associated with 2387 [**`.WITH()`**](reference.md/#WITH), 2388 [**`.SIDE_EFFECT()`**](reference.md/#SIDE_EFFECT), 2389 [**`.RETURN()`**](reference.md/#RETURN) and 2390 [**`.THROW()`**](reference.md/#THROW) and their **`LR_`** counter parts, refers 2391 to member variables in the mock objects, they will continue to refer the old 2392 moved from object. 2393 2394 ```Cpp 2395 class immobile 2396 { 2397 public: 2398 MAKE_MOCK1(func, void(int)); 2399 }; 2400 2401 class movable 2402 { 2403 public: 2404 int i = 0; 2405 2406 static constexpr bool trompeloeil_movable_mock = true; 2407 // allow move construction 2408 2409 MAKE_MOCK1(func, void(int)); 2410 }; 2411 2412 template <typename T> 2413 T transfer(T t) 2414 { 2415 return t; 2416 } 2417 2418 test(...) 2419 { 2420 auto m = transfer(immobile{}); // compilation error 2421 ... 2422 } 2423 test(...) 2424 { 2425 movable m; 2426 auto e = NAMED_REQUIRE_CALL(m, func(3)); 2427 auto mm = transfer(std::move(m)); 2428 // A call to mm.func() now satisfies e 2429 ... 2430 } 2431 test(...) 2432 { 2433 movable m{3}; 2434 auto e = NAMED_REQUIRE_CALL(m, func(_)) 2435 .LR_WITH(_1 == m.i); 2436 auto mm = transfer(std::move(m)); // Danger! e still refers to m.i. 2437 ... 2438 } 2439 ``` 2440 2441 Also, keep in mind the lifetime of expectations. If the lifetime of an 2442 expectation is associated with the life of the moved-from object, your test 2443 will likely fail, since the expectation object would then be destroyed before it 2444 has been satisfied. Example: 2445 2446 ```Cpp 2447 class movable 2448 { 2449 public: 2450 static constexpr bool trompeloeil_movable_mock = true; 2451 2452 MAKE_MOCK0(func, void()); 2453 }; 2454 2455 movable setup() 2456 { 2457 movable obj; 2458 REQUIRE_CALL(obj, func()); 2459 return obj; 2460 // Expectation dies here, unsatisfied, failing the test 2461 } 2462 2463 test(...) 2464 { 2465 movable obj = setup(); // test fails when returning from setup() 2466 ... 2467 } 2468 ``` 2469 2470 Using 2471 [**`NAMED_REQUIRE_CALL()`**](reference.md/#NAMED_REQUIRE_CALL), 2472 [**`NAMED_ALLOW_CALL()`**](reference.md/#NAMED_ALLOW_CALL) or 2473 [**`NAMED_FORBID_CALL()`**](reference.md/#NAMED_FORBID_CALL) can help, since 2474 they make the expectation life times more visible.