Skip to content

Commit 6d32840

Browse files
committed
Revise editorially
The test functions within our code blocks will not actually be run in any case, so let's mark these tests as `no_run`. In the examples, let's demonstrate the behavior that the expected string must be a substring of the panic message. Let's also tighten up some wording.
1 parent 5789bb7 commit 6d32840

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/attributes/testing.md

Lines changed: 19 additions & 20 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() {
@@ -124,10 +123,10 @@ r[attributes.testing.should_panic]
124123
## The `should_panic` attribute
125124
126125
r[attributes.testing.should_panic.intro]
127-
The *`should_panic` [attribute][attributes]* changes a [test function][attributes.testing.test] so that it passes only if it panics.
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.
128127
129128
> [!EXAMPLE]
130-
> ```rust
129+
> ```rust,no_run
131130
> #[test]
132131
> #[should_panic(expected = "values don't match")]
133132
> fn mytest() {
@@ -136,46 +135,46 @@ The *`should_panic` [attribute][attributes]* changes a [test function][attribute
136135
> ```
137136
138137
r[attributes.testing.should_panic.syntax]
139-
The `should_panic` attribute is specified with one of the following forms:
138+
The `should_panic` attribute has one of the following forms:
140139
141140
- [MetaWord]
142141
> [!EXAMPLE]
143-
> ```rust
142+
> ```rust,no_run
144143
> #[test]
145144
> #[should_panic]
146-
> fn mytest() { panic!("some message"); }
145+
> fn mytest() { panic!("error: some message, and more"); }
147146
> ```
148147
149-
- [MetaNameValueStr] --- This indicates that the given string should appear within the panic message.
148+
- [MetaNameValueStr] --- The given string must appear within the panic message for the test to pass.
150149
> [!EXAMPLE]
151-
> ```rust
150+
> ```rust,no_run
152151
> #[test]
153152
> #[should_panic = "some message"]
154-
> fn mytest() { panic!("some message"); }
153+
> fn mytest() { panic!("error: some message, and more"); }
155154
> ```
156155
157-
- [MetaListNameValueStr] --- Specified with the key `expected`. Same behavior as [MetaNameValueStr], just with an explicit key.
156+
- [MetaListNameValueStr] --- As with the [MetaNameValueStr] syntax, the given string must appear within the panic message.
158157
> [!EXAMPLE]
159-
> ```rust
158+
> ```rust,no_run
160159
> #[test]
161160
> #[should_panic(expected = "some message")]
162-
> fn mytest() { panic!("some message"); }
161+
> fn mytest() { panic!("error: some message, and more"); }
163162
> ```
164163
165164
r[attributes.testing.should_panic.allowed-positions]
166-
The `should_panic` attribute may be applied to functions annotated with the `test` attribute.
165+
The `should_panic` attribute may only be applied to functions annotated with the `test` attribute.
167166
168167
> [!NOTE]
169-
> `rustc` currently warns in some other positions. This may become a hard error in the future.
168+
> `rustc` currently accepts this attribute in other positions with a warning. This may become a hard error in the future.
170169
171170
r[attributes.testing.should_panic.duplicates]
172-
Only the first instance of `should_panic` on a function is honored. Subsequent `should_panic` attributes are ignored.
171+
Only the first instance of `should_panic` on a function is honored.
173172
174173
> [!NOTE]
175-
> `rustc` currently ignores subsequent duplicate `should_panic` attributes. This may become an error in the future.
174+
> `rustc` currently ignores subsequent `should_panic` attributes and emits a future-compatibility warning. This may become a hard error in the future.
176175
177176
r[attributes.testing.should_panic.expected]
178-
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.
177+
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.
179178
180179
r[attributes.testing.should_panic.return]
181180
The return type of the test function must be `()`.

0 commit comments

Comments
 (0)