Skip to content

Commit f8ed0e6

Browse files
Abseil Teamcopybara-github
authored andcommitted
Add documentation for exception matchers.
PiperOrigin-RevId: 775205267 Change-Id: I3ad59ff4b1fa095cbf7dd04dd97a152cb33c7435
1 parent 35b75a2 commit f8ed0e6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

docs/reference/matchers.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,33 @@ use the regular expression syntax defined
111111
[here](../advanced.md#regular-expression-syntax). All of these matchers, except
112112
`ContainsRegex()` and `MatchesRegex()` work for wide strings as well.
113113

114+
## Exception Matchers
115+
116+
| Matcher | Description |
117+
| :---------------------------------------- | :------------------------------- |
118+
| `Throws<E>()` | The `argument` is a callable object that, when called, throws an exception of the expected type `E`. |
119+
| `Throws<E>(m)` | The `argument` is a callable object that, when called, throws an exception of type `E` that satisfies the matcher `m`. |
120+
| `ThrowsMessage<E>(m)` | The `argument` is a callable object that, when called, throws an exception of type `E` with a message that satisfies the matcher `m`. |
121+
122+
Examples:
123+
124+
```cpp
125+
auto argument = [] { throw std::runtime_error("error msg"); };
126+
127+
// Checks if the lambda throws a `std::runtime_error`.
128+
EXPECT_THAT(argument, Throws<std::runtime_error>());
129+
130+
// Checks if the lambda throws a `std::runtime_error` with a specific message
131+
// that matches "error msg".
132+
EXPECT_THAT(argument,
133+
Throws<std::runtime_error>(Property(&std::runtime_error::what,
134+
Eq("error msg"))));
135+
136+
// Checks if the lambda throws a `std::runtime_error` with a message that
137+
// contains "msg".
138+
EXPECT_THAT(argument, ThrowsMessage<std::runtime_error>(HasSubstr("msg")));
139+
```
140+
114141
## Container Matchers
115142
116143
Most STL-style containers support `==`, so you can use `Eq(expected_container)`

0 commit comments

Comments
 (0)