Skip to content

Commit cdb998b

Browse files
authored
Merge pull request #1880 from ehuss/test
Update `test` to use the attribute template
2 parents 837c4a8 + d8fb884 commit cdb998b

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

src/attributes/testing.md

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,43 @@ r[attributes.testing.test]
1010
## The `test` attribute
1111

1212
r[attributes.testing.test.intro]
13-
The *`test` attribute* marks a function to be executed as a test.
14-
15-
r[attributes.testing.test.enabled]
16-
These functions are only compiled when in test mode.
13+
The *`test` [attribute][attributes]* marks a function to be executed as a test.
14+
15+
> [!EXAMPLE]
16+
> ```rust
17+
> # pub fn add(left: u64, right: u64) -> u64 { left + right }
18+
>
19+
> #[test]
20+
> fn it_works() {
21+
> let result = add(2, 2);
22+
> assert_eq!(result, 4);
23+
> }
24+
> ```
25+
26+
r[attributes.testing.test.syntax]
27+
The `test` attribute uses the [MetaWord] syntax and thus does not take any inputs.
1728
1829
r[attributes.testing.test.allowed-positions]
19-
Test functions must be free, monomorphic functions that take no arguments, and the return type must implement the [`Termination`] trait, for example:
30+
The `test` attribute may only be applied to [free functions] that are monomorphic, that take no arguments, and where the return type implements the [`Termination`] trait.
2031
21-
* `()`
22-
* `Result<T, E> where T: Termination, E: Debug`
23-
* `!`
32+
> [!NOTE]
33+
> Some of types that implement the [`Termination`] trait include:
34+
> * `()`
35+
> * `Result<T, E> where T: Termination, E: Debug`
2436
25-
<!-- If the previous section needs updating (from "must take no arguments"
26-
onwards, also update it in the crates-and-source-files.md file -->
37+
r[attributes.testing.test.duplicates]
38+
Only the first instance of `test` on a function is honored.
39+
40+
> [!NOTE]
41+
> Subsequent `test` attributes are currently ignored and `rustc` warns about these.
42+
43+
<!-- TODO: This is a minor lie. Currently rustc warns that duplicates are ignored, but it then generates multiple test entries with the same name. I would vote for rejecting this in the future. -->
44+
45+
r[attributes.testing.test.stdlib]
46+
The `test` attribute is exported from the standard library prelude as [`std::prelude::v1::test`].
47+
48+
r[attributes.testing.test.enabled]
49+
These functions are only compiled when in test mode.
2750
2851
> [!NOTE]
2952
> The test mode is enabled by passing the `--test` argument to `rustc` or using `cargo test`.
@@ -36,17 +59,18 @@ In particular:
3659
* Tests that return `ExitCode::SUCCESS` pass, and tests that return `ExitCode::FAILURE` fail.
3760
* Tests that do not terminate neither pass nor fail.
3861
39-
```rust
40-
# use std::io;
41-
# fn setup_the_thing() -> io::Result<i32> { Ok(1) }
42-
# fn do_the_thing(s: &i32) -> io::Result<()> { Ok(()) }
43-
#[test]
44-
fn test_the_thing() -> io::Result<()> {
45-
let state = setup_the_thing()?; // expected to succeed
46-
do_the_thing(&state)?; // expected to succeed
47-
Ok(())
48-
}
49-
```
62+
> [!EXAMPLE]
63+
> ```rust
64+
> # use std::io;
65+
> # fn setup_the_thing() -> io::Result<i32> { Ok(1) }
66+
> # fn do_the_thing(s: &i32) -> io::Result<()> { Ok(()) }
67+
> #[test]
68+
> fn test_the_thing() -> io::Result<()> {
69+
> let state = setup_the_thing()?; // expected to succeed
70+
> do_the_thing(&state)?; // expected to succeed
71+
> Ok(())
72+
> }
73+
> ```
5074
5175
r[attributes.testing.ignore]
5276
## The `ignore` attribute
@@ -102,3 +126,4 @@ fn mytest() {
102126
[`test` conditional compilation option]: ../conditional-compilation.md#test
103127
[attributes]: ../attributes.md
104128
[`ExitCode`]: std::process::ExitCode
129+
[free functions]: ../glossary.md#free-item

0 commit comments

Comments
 (0)