mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-11-03 21:49:32 +01:00 
			
		
		
		
	Add PredicateMatcher that takes an arbitrary predicate functions
Also adds `Predicate` helper function to create `PredicateMatcher`.
Because of limitations in type inference it needs to be explicitly
typed, like so
`Predicate<std::string>([](std::string const& str) { ... })`.
It also takes an optional second argument for description of the
predicate.
It is possible to infer the argument with sufficient TMP, see
https://stackoverflow.com/questions/43560492/how-to-extract-lambdas-return-type-and-variadic-parameters-pack-back-from-gener/43561563#43561563
but I don't think that the magic is worth introducing ATM.
Closes #1236
			
			
This commit is contained in:
		@@ -53,6 +53,25 @@ The floating point matchers are `WithinULP` and `WithinAbs`. `WithinAbs` accepts
 | 
			
		||||
Do note that ULP-based checks only make sense when both compared numbers are of the same type and `WithinULP` will use type of its argument as the target type. This means that `WithinULP(1.f, 1)` will expect to compare `float`s, but `WithinULP(1., 1)` will expect to compare `double`s.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### Generic matchers
 | 
			
		||||
Catch also aims to provide a set of generic matchers. Currently this set
 | 
			
		||||
contains only a matcher that takes arbitrary callable predicate and applies
 | 
			
		||||
it onto the provided object.
 | 
			
		||||
 | 
			
		||||
Because of type inference limitations, the argument type of the predicate
 | 
			
		||||
has to be provided explicitly. Example:
 | 
			
		||||
```cpp
 | 
			
		||||
REQUIRE_THAT("Hello olleH",
 | 
			
		||||
             Predicate<std::string>(
 | 
			
		||||
                 [] (std::string const& str) -> bool { return str.front() == str.back(); },
 | 
			
		||||
                 "First and last character should be equal")
 | 
			
		||||
);
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
The second argument is an optional description of the predicate, and is
 | 
			
		||||
used only during reporting of the result.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## Custom matchers
 | 
			
		||||
It's easy to provide your own matchers to extend Catch or just to work with your own types.
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user