Document that matcher combinators (&&, ||, !) do not take ownership

Closes #1781
This commit is contained in:
Martin Hořeňovský 2019-10-26 21:07:38 +02:00
parent 50cc14c94c
commit c5c688820c
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A

View File

@ -17,7 +17,7 @@ For example, to assert that a string ends with a certain substring:
using Catch::Matchers::EndsWith; // or Catch::EndsWith using Catch::Matchers::EndsWith; // or Catch::EndsWith
std::string str = getStringFromSomewhere(); std::string str = getStringFromSomewhere();
REQUIRE_THAT( str, EndsWith( "as a service" ) ); REQUIRE_THAT( str, EndsWith( "as a service" ) );
``` ```
The matcher objects can take multiple arguments, allowing more fine tuning. The matcher objects can take multiple arguments, allowing more fine tuning.
The built-in string matchers, for example, take a second argument specifying whether the comparison is The built-in string matchers, for example, take a second argument specifying whether the comparison is
@ -35,6 +35,22 @@ REQUIRE_THAT( str,
(StartsWith( "Big data" ) && !Contains( "web scale" ) ) ); (StartsWith( "Big data" ) && !Contains( "web scale" ) ) );
``` ```
_The combining operators do not take ownership of the matcher objects.
This means that if you store the combined object, you have to ensure that
the matcher objects outlive its last use. What this means is that code
like this leads to a use-after-free and (hopefully) a crash:_
```cpp
TEST_CASE("Bugs, bugs, bugs", "[Bug]"){
std::string str = "Bugs as a service";
auto match_expression = Catch::EndsWith( "as a service" ) ||
(Catch::StartsWith( "Big data" ) && !Catch::Contains( "web scale" ) );
REQUIRE_THAT(str, match_expression);
}
```
## Built in matchers ## Built in matchers
Catch2 provides some matchers by default. They can be found in the Catch2 provides some matchers by default. They can be found in the
`Catch::Matchers::foo` namespace and are imported into the `Catch` `Catch::Matchers::foo` namespace and are imported into the `Catch`