Skip to content

Commit 3d49d4d

Browse files
committed
Convert should_panic to use the attribute template
1 parent c02c690 commit 3d49d4d

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

src/attributes/testing.md

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,61 @@ r[attributes.testing.should_panic]
7575
## The `should_panic` attribute
7676

7777
r[attributes.testing.should_panic.intro]
78-
A function annotated with the `test` attribute that returns `()` can also be annotated with the `should_panic` attribute.
78+
The *`should_panic` [attribute][attributes]* changes a [test function][attributes.testing.test] so that it passes only if it panics.
7979

80-
r[attributes.testing.should_panic.behavior]
81-
The *`should_panic` attribute* makes the test only pass if it actually panics.
80+
> [!EXAMPLE]
81+
> ```rust
82+
> #[test]
83+
> #[should_panic(expected = "values don't match")]
84+
> fn mytest() {
85+
> assert_eq!(1, 2, "values don't match");
86+
> }
87+
> ```
8288
8389
r[attributes.testing.should_panic.syntax]
84-
The `should_panic` attribute may optionally take an input string that must appear within the panic message. If the string is not found in the message, then the test will fail. The string may be passed using the [MetaNameValueStr] syntax or the [MetaListNameValueStr] syntax with an `expected` field.
90+
The `should_panic` attribute is specified with one of the following forms:
91+
92+
- [MetaWord]
93+
> [!EXAMPLE]
94+
> ```rust
95+
> #[test]
96+
> #[should_panic]
97+
> fn mytest() { panic!("some message"); }
98+
> ```
99+
100+
- [MetaNameValueStr] --- This indicates that the given string should appear within the panic message.
101+
> [!EXAMPLE]
102+
> ```rust
103+
> #[test]
104+
> #[should_panic = "some message"]
105+
> fn mytest() { panic!("some message"); }
106+
> ```
107+
108+
- [MetaListNameValueStr] --- Specified with the key `expected`. Same behavior as [MetaNameValueStr], just with an explicit key.
109+
> [!EXAMPLE]
110+
> ```rust
111+
> #[test]
112+
> #[should_panic(expected = "some message")]
113+
> fn mytest() { panic!("some message"); }
114+
> ```
115+
116+
r[attributes.testing.should_panic.allowed-positions]
117+
The `should_panic` attribute may be applied to functions annotated with the `test` attribute.
85118
86-
```rust
87-
#[test]
88-
#[should_panic(expected = "values don't match")]
89-
fn mytest() {
90-
assert_eq!(1, 2, "values don't match");
91-
}
92-
```
119+
> [!NOTE]
120+
> `rustc` currently warns in some other positions. This may become a hard error in the future.
121+
122+
r[attributes.testing.should_panic.duplicates]
123+
Only the first instance of `should_panic` on a function is honored. Subsequent `should_panic` attributes are ignored.
124+
125+
> [!NOTE]
126+
> `rustc` currently ignores subsequent duplicate `should_panic` attributes. This may become an error in the future.
127+
128+
r[attributes.testing.should_panic.expected]
129+
The string specified with the [MetaNameValueStr] form or the `expected` key in [MetaListNameValueStr] indicates that the string must appear somewhere within the panic message. If the string is not found in the message, then the test will fail.
130+
131+
r[attributes.testing.should_panic.return]
132+
The return type of the test function must be `()`.
93133
94134
[`Termination`]: std::process::Termination
95135
[`report`]: std::process::Termination::report

0 commit comments

Comments
 (0)