Skip to content

Commit 38cecac

Browse files
authored
Merge pull request #1882 from ehuss/should_panic
Update `should_panic` to use the attribute template
2 parents c1720b1 + f7b664c commit 38cecac

File tree

1 file changed

+59
-23
lines changed

1 file changed

+59
-23
lines changed

src/attributes/testing.md

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ r[attributes.testing.test.intro]
1313
The *`test` [attribute][attributes]* marks a function to be executed as a test.
1414

1515
> [!EXAMPLE]
16-
> ```rust
16+
> ```rust,no_run
1717
> # pub fn add(left: u64, right: u64) -> u64 { left + right }
18-
>
1918
> #[test]
2019
> fn it_works() {
2120
> let result = add(2, 2);
@@ -60,7 +59,7 @@ In particular:
6059
* Tests that do not terminate neither pass nor fail.
6160
6261
> [!EXAMPLE]
63-
> ```rust
62+
> ```rust,no_run
6463
> # use std::io;
6564
> # fn setup_the_thing() -> io::Result<i32> { Ok(1) }
6665
> # fn do_the_thing(s: &i32) -> io::Result<()> { Ok(()) }
@@ -79,7 +78,7 @@ r[attributes.testing.ignore.intro]
7978
The *`ignore` [attribute][attributes]* can be used with the [`test` attribute][attributes.testing.test] to tell the test harness to not execute that function as a test.
8079
8180
> [!EXAMPLE]
82-
> ```rust
81+
> ```rust,no_run
8382
> #[test]
8483
> #[ignore]
8584
> fn check_thing() {
@@ -97,7 +96,7 @@ r[attributes.testing.ignore.reason]
9796
The [MetaNameValueStr] form of the `ignore` attribute provides a way to specify a reason why the test is ignored.
9897
9998
> [!EXAMPLE]
100-
> ```rust
99+
> ```rust,no_run
101100
> #[test]
102101
> #[ignore = "not yet implemented"]
103102
> fn mytest() {
@@ -124,27 +123,64 @@ r[attributes.testing.should_panic]
124123
## The `should_panic` attribute
125124
126125
r[attributes.testing.should_panic.intro]
127-
A function annotated with the `test` attribute that returns `()` can also be
128-
annotated with the `should_panic` attribute.
126+
The *`should_panic` [attribute][attributes]* causes a test to pass only if the [test function][attributes.testing.test] to which the attribute is applied panics.
129127
130-
r[attributes.testing.should_panic.behavior]
131-
The *`should_panic` attribute*
132-
makes the test only pass if it actually panics.
128+
> [!EXAMPLE]
129+
> ```rust,no_run
130+
> #[test]
131+
> #[should_panic(expected = "values don't match")]
132+
> fn mytest() {
133+
> assert_eq!(1, 2, "values don't match");
134+
> }
135+
> ```
133136
134137
r[attributes.testing.should_panic.syntax]
135-
The `should_panic` attribute may optionally take an input string that must
136-
appear within the panic message. If the string is not found in the message,
137-
then the test will fail. The string may be passed using the
138-
[MetaNameValueStr] syntax or the [MetaListNameValueStr] syntax with an
139-
`expected` field.
140-
141-
```rust
142-
#[test]
143-
#[should_panic(expected = "values don't match")]
144-
fn mytest() {
145-
assert_eq!(1, 2, "values don't match");
146-
}
147-
```
138+
The `should_panic` attribute has one of the following forms:
139+
140+
- [MetaWord]
141+
> [!EXAMPLE]
142+
> ```rust,no_run
143+
> #[test]
144+
> #[should_panic]
145+
> fn mytest() { panic!("error: some message, and more"); }
146+
> ```
147+
148+
- [MetaNameValueStr] --- The given string must appear within the panic message for the test to pass.
149+
> [!EXAMPLE]
150+
> ```rust,no_run
151+
> #[test]
152+
> #[should_panic = "some message"]
153+
> fn mytest() { panic!("error: some message, and more"); }
154+
> ```
155+
156+
- [MetaListNameValueStr] --- As with the [MetaNameValueStr] syntax, the given string must appear within the panic message.
157+
> [!EXAMPLE]
158+
> ```rust,no_run
159+
> #[test]
160+
> #[should_panic(expected = "some message")]
161+
> fn mytest() { panic!("error: some message, and more"); }
162+
> ```
163+
164+
> [!NOTE]
165+
> `rustc` currently accepts the [MetaListNameValueStr] form with invalid syntax between the parentheses and emits a future-compatibility warning. This may become a hard error in the future.
166+
167+
r[attributes.testing.should_panic.allowed-positions]
168+
The `should_panic` attribute may only be applied to functions annotated with the `test` attribute.
169+
170+
> [!NOTE]
171+
> `rustc` currently accepts this attribute in other positions with a warning. This may become a hard error in the future.
172+
173+
r[attributes.testing.should_panic.duplicates]
174+
Only the first instance of `should_panic` on a function is honored.
175+
176+
> [!NOTE]
177+
> `rustc` currently ignores subsequent `should_panic` attributes and emits a future-compatibility warning. This may become a hard error in the future.
178+
179+
r[attributes.testing.should_panic.expected]
180+
When the [MetaNameValueStr] form or the [MetaListNameValueStr] form with the `expected` key is used, the given string must appear somewhere within the panic message for the test to pass.
181+
182+
r[attributes.testing.should_panic.return]
183+
The return type of the test function must be `()`.
148184
149185
[`Termination`]: std::process::Termination
150186
[`report`]: std::process::Termination::report

0 commit comments

Comments
 (0)