Skip to content

Commit 7641de1

Browse files
committed
Initial draft of doctest-name RFC
This RFC proposes the ability to annotate documentation test code blocks with metadata of the form `name=NAME`. The given name will be used to identify the documentation test when it is run by the test runner, in addition to the current information of test binary, module, and line number. For example, this documention test: ```name=arithmetic assert_eq!(2 + 2, 4); ``` Assuming that it is in crate `foo`, in file `src/lib.rs`, in module `bar`, on line 37, will produce this output when run: Doc-tests foo running 1 test test src/lib.rs - bar::arithmetic (line 37) ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
1 parent ff29887 commit 7641de1

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

text/0000-doctest-name.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
- Feature Name: doctest-name
2+
- Start Date: 2010-10-07
3+
- RFC PR: [rust-lang/rfcs#0000](https://github.com/rust-lang/rfcs/pull/0000)
4+
- Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000)
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
Allow giving documentation tests names for identification in teest runner
10+
output by including `name=NAME` in doctest code block info strings.
11+
12+
# Motivation
13+
[motivation]: #motivation
14+
15+
When doctests run, the test runner prints the name of the module that contains
16+
them and the line number that they start on. If a module contains many
17+
doctests, it is hard to identify which doctest is which in the test runner
18+
output by line number alone.
19+
20+
By giving users the option to give names to doctests, identifying individual
21+
doctests in test runner output will be easier.
22+
23+
# Guide-level explanation
24+
[guide-level-explanation]: #guide-level-explanation
25+
26+
Doctests are written as fenced markdown code blocks that start and end
27+
with lines containing three backticks.
28+
29+
The first set of backticks may be followed by an info string. For example, to
30+
tell the Rust compiler to ignore the doctest:
31+
32+
````rust
33+
34+
// This doctest won't run.
35+
```ignore
36+
assert_eq!(2 + 2, 4);
37+
```
38+
39+
````
40+
41+
Normally, doctests do not have names, and when run, the test runner will
42+
identify them by their test binary, module name, and line number:
43+
44+
```
45+
Doc-tests foo
46+
47+
running 1 test
48+
test src/lib.rs - bar (line 37) ... ok
49+
50+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
51+
```
52+
53+
If you'd like to have the test runner print a more informative name, perhaps
54+
because there are many doctests in a single file, you can put `name=NAME`,
55+
where `NAME` is the name the test should have, in the doctest's info string.
56+
`NAME` should be a valid Rust identifer.
57+
58+
59+
````rust
60+
61+
```name=arithmetic
62+
assert_eq!(2 + 2, 4);
63+
```
64+
65+
````
66+
67+
```
68+
Doc-tests foo
69+
70+
running 1 test
71+
test src/lib.rs - bar::arithmetic (line 37) ... ok
72+
73+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
74+
```
75+
76+
# Reference-level explanation
77+
[reference-level-explanation]: #reference-level-explanation
78+
79+
The set of valid words that may appear in code block info strings is extended
80+
to include patterns of the form `name=IDENT`.
81+
82+
This `IDENT` will be the name of the documentation test generated by the code
83+
block.
84+
85+
When the test runner runs the documentation test, the test will be identified
86+
by this name, in addition to the test binary, module, and line number.
87+
88+
Multiple `name=IDENT` words may not appear in a single code block's info
89+
string.
90+
91+
`name=IDENT` may be combined with existing annotations like `rust` or
92+
`should_panic` by separating the annotations with commas:
93+
94+
````
95+
96+
```rust,name=foo
97+
assert!(true)
98+
```
99+
100+
````
101+
102+
The ability to include multiple comma-separated annotations is an existing
103+
feature.
104+
105+
# Drawbacks
106+
[drawbacks]: #drawbacks
107+
108+
- Adds complexity to code block parsing.
109+
110+
- Requires parsing multi-word info strings, which are standard markdown, but
111+
which does increase the complexity of the markdown parser.
112+
113+
# Rationale and alternatives
114+
[rationale-and-alternatives]: #rationale-and-alternatives
115+
116+
This design seems like the simplest design possible, although viable
117+
alternatives may have been overlooked.
118+
119+
A previous version of this RFC included the additional restrictions that labels
120+
must be valid rust identifiers, and that labels must be unique within the same
121+
module. These restrictions were intended to allow the machinery that generates
122+
doctest tests to use the labels as the generated doctest function names. If
123+
this simplifies or improves the generated doctest functions, these restrictions
124+
might be desirable.
125+
126+
The impact of adopting this RFC is minimal. Doctests test output will remain a
127+
little hard to make sense of.
128+
129+
# Prior art
130+
[prior-art]: #prior-art
131+
132+
None.
133+
134+
# Unresolved questions
135+
[unresolved-questions]: #unresolved-questions
136+
137+
None.
138+
139+
# Future possibilities
140+
[future-possibilities]: #future-possibilities
141+
142+
None.

0 commit comments

Comments
 (0)