Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/docs
diff options
context:
space:
mode:
authorMartin Hořeňovský2024-07-27 14:01:47 +0200
committerMartin Hořeňovský2024-07-27 14:02:28 +0200
commita40dd478f33e04964007ae5330e248b8eda175de (patch)
treec44aba0670a3c06d3bf65b35c7a87cf3094e9836 /docs
parent85b7f3d6abbf11cfde34e274180a5539d2ac8bfe (diff)
downloadcatch3-a40dd478f33e04964007ae5330e248b8eda175de.tar.gz
Update docs for REQUIRE_THROWS_MATCHES
Diffstat (limited to 'docs')
-rw-r--r--docs/matchers.md36
1 files changed, 27 insertions, 9 deletions
diff --git a/docs/matchers.md b/docs/matchers.md
index d5be1f5a..4b9445ae 100644
--- a/docs/matchers.md
+++ b/docs/matchers.md
@@ -210,15 +210,36 @@ The other miscellaneous matcher utility is exception matching.
#### Matching exceptions
-Catch2 provides a utility macro for asserting that an expression
-throws exception of specific type, and that the exception has desired
-properties. The macro is `REQUIRE_THROWS_MATCHES(expr, ExceptionType, Matcher)`.
+Because exceptions are a bit special, Catch2 has a separate macro for them.
+
+
+The basic form is
+
+```
+REQUIRE_THROWS_MATCHES(expr, ExceptionType, Matcher)
+```
+
+and it checks that the `expr` throws an exception, that exception is derived
+from the `ExceptionType` type, and then `Matcher::match` is called on
+the caught exception.
> `REQUIRE_THROWS_MATCHES` macro lives in `catch2/matchers/catch_matchers.hpp`
+For one-off checks you can use the `Predicate` matcher above, e.g.
-Catch2 currently provides two matchers for exceptions.
-These are:
+```cpp
+REQUIRE_THROWS_MATCHES(parse(...),
+ parse_error,
+ Predicate<parse_error>([] (parse_error const& err) -> bool { return err.line() == 1; })
+);
+```
+
+but if you intend to thoroughly test your error reporting, I recommend
+defining a specialized matcher.
+
+
+Catch2 also provides 2 built-in matchers for checking the error message
+inside an exception (it must be derived from `std::exception`):
* `Message(std::string message)`.
* `MessageMatches(Matcher matcher)`.
@@ -236,10 +257,7 @@ REQUIRE_THROWS_MATCHES(throwsDerivedException(), DerivedException, Message("De
REQUIRE_THROWS_MATCHES(throwsDerivedException(), DerivedException, MessageMatches(StartsWith("DerivedException")));
```
-Note that `DerivedException` in the example above has to derive from
-`std::exception` for the example to work.
-
-> the exception message matcher lives in `catch2/matchers/catch_matchers_exception.hpp`
+> the exception message matchers live in `catch2/matchers/catch_matchers_exception.hpp`
### Generic range Matchers