]> git.proxmox.com Git - rustc.git/blob - src/doc/rustdoc/src/documentation-tests.md
New upstream version 1.27.1+dfsg1
[rustc.git] / src / doc / rustdoc / src / documentation-tests.md
1 # Documentation tests
2
3 `rustdoc` supports executing your documentation examples as tests. This makes sure
4 that your tests are up to date and working.
5
6 The basic idea is this:
7
8 ```ignore
9 /// # Examples
10 ///
11 /// ```
12 /// let x = 5;
13 /// ```
14 ```
15
16 The triple backticks start and end code blocks. If this were in a file named `foo.rs`,
17 running `rustdoc --test foo.rs` will extract this example, and then run it as a test.
18
19 Please note that by default, if no language is set for the block code, `rustdoc`
20 assumes it is `Rust` code. So the following:
21
22 ``````markdown
23 ```rust
24 let x = 5;
25 ```
26 ``````
27
28 is strictly equivalent to:
29
30 ``````markdown
31 ```
32 let x = 5;
33 ```
34 ``````
35
36 There's some subtlety though! Read on for more details.
37
38 ## Passing or failing a doctest
39
40 Like regular unit tests, regular doctests are considered to "pass"
41 if they compile and run without panicking.
42 So if you want to demonstrate that some computation gives a certain result,
43 the `assert!` family of macros works the same as other Rust code:
44
45 ```rust
46 let foo = "foo";
47
48 assert_eq!(foo, "foo");
49 ```
50
51 This way, if the computation ever returns something different,
52 the code panics and the doctest fails.
53
54 ## Pre-processing examples
55
56 In the example above, you'll note something strange: there's no `main`
57 function! Forcing you to write `main` for every example, no matter how small,
58 adds friction. So `rustdoc` processes your examples slightly before
59 running them. Here's the full algorithm rustdoc uses to preprocess examples:
60
61 1. Some common `allow` attributes are inserted, including
62 `unused_variables`, `unused_assignments`, `unused_mut`,
63 `unused_attributes`, and `dead_code`. Small examples often trigger
64 these lints.
65 2. Any attributes specified with `#![doc(test(attr(...)))]` are added.
66 3. Any leading `#![foo]` attributes are left intact as crate attributes.
67 4. If the example does not contain `extern crate`, and
68 `#![doc(test(no_crate_inject))]` was not specified, then `extern crate
69 <mycrate>;` is inserted (note the lack of `#[macro_use]`).
70 5. Finally, if the example does not contain `fn main`, the remainder of the
71 text is wrapped in `fn main() { your_code }`.
72
73 For more about that caveat in rule 4, see "Documenting Macros" below.
74
75 ## Hiding portions of the example
76
77 Sometimes, you need some setup code, or other things that would distract
78 from your example, but are important to make the tests work. Consider
79 an example block that looks like this:
80
81 ```text
82 /// Some documentation.
83 # fn foo() {}
84 ```
85
86 It will render like this:
87
88 ```rust
89 /// Some documentation.
90 # fn foo() {}
91 ```
92
93 Yes, that's right: you can add lines that start with `# `, and they will
94 be hidden from the output, but will be used when compiling your code. You
95 can use this to your advantage. In this case, documentation comments need
96 to apply to some kind of function, so if I want to show you just a
97 documentation comment, I need to add a little function definition below
98 it. At the same time, it's only there to satisfy the compiler, so hiding
99 it makes the example more clear. You can use this technique to explain
100 longer examples in detail, while still preserving the testability of your
101 documentation.
102
103 For example, imagine that we wanted to document this code:
104
105 ```rust
106 let x = 5;
107 let y = 6;
108 println!("{}", x + y);
109 ```
110
111 We might want the documentation to end up looking like this:
112
113 > First, we set `x` to five:
114 >
115 > ```rust
116 > let x = 5;
117 > # let y = 6;
118 > # println!("{}", x + y);
119 > ```
120 >
121 > Next, we set `y` to six:
122 >
123 > ```rust
124 > # let x = 5;
125 > let y = 6;
126 > # println!("{}", x + y);
127 > ```
128 >
129 > Finally, we print the sum of `x` and `y`:
130 >
131 > ```rust
132 > # let x = 5;
133 > # let y = 6;
134 > println!("{}", x + y);
135 > ```
136
137 To keep each code block testable, we want the whole program in each block, but
138 we don't want the reader to see every line every time. Here's what we put in
139 our source code:
140
141 ``````markdown
142 First, we set `x` to five:
143
144 ```
145 let x = 5;
146 # let y = 6;
147 # println!("{}", x + y);
148 ```
149
150 Next, we set `y` to six:
151
152 ```
153 # let x = 5;
154 let y = 6;
155 # println!("{}", x + y);
156 ```
157
158 Finally, we print the sum of `x` and `y`:
159
160 ```
161 # let x = 5;
162 # let y = 6;
163 println!("{}", x + y);
164 ```
165 ``````
166
167 By repeating all parts of the example, you can ensure that your example still
168 compiles, while only showing the parts that are relevant to that part of your
169 explanation.
170
171 Another case where the use of `#` is handy is when you want to ignore
172 error handling. Lets say you want the following,
173
174 ```ignore
175 /// use std::io;
176 /// let mut input = String::new();
177 /// io::stdin().read_line(&mut input)?;
178 ```
179
180 The problem is that `?` returns a `Result<T, E>` and test functions
181 don't return anything so this will give a mismatched types error.
182
183 ```ignore
184 /// A doc test using ?
185 ///
186 /// ```
187 /// use std::io;
188 /// # fn foo() -> io::Result<()> {
189 /// let mut input = String::new();
190 /// io::stdin().read_line(&mut input)?;
191 /// # Ok(())
192 /// # }
193 /// ```
194 # fn foo() {}
195 ```
196
197 You can get around this by wrapping the code in a function. This catches
198 and swallows the `Result<T, E>` when running tests on the docs. This
199 pattern appears regularly in the standard library.
200
201 ### Documenting macros
202
203 Here’s an example of documenting a macro:
204
205 ```rust
206 /// Panic with a given message unless an expression evaluates to true.
207 ///
208 /// # Examples
209 ///
210 /// ```
211 /// # #[macro_use] extern crate foo;
212 /// # fn main() {
213 /// panic_unless!(1 + 1 == 2, “Math is broken.”);
214 /// # }
215 /// ```
216 ///
217 /// ```should_panic
218 /// # #[macro_use] extern crate foo;
219 /// # fn main() {
220 /// panic_unless!(true == false, “I’m broken.”);
221 /// # }
222 /// ```
223 #[macro_export]
224 macro_rules! panic_unless {
225 ($condition:expr, $($rest:expr),+) => ({ if ! $condition { panic!($($rest),+); } });
226 }
227 # fn main() {}
228 ```
229
230 You’ll note three things: we need to add our own `extern crate` line, so that
231 we can add the `#[macro_use]` attribute. Second, we’ll need to add our own
232 `main()` as well (for reasons discussed above). Finally, a judicious use of
233 `#` to comment out those two things, so they don’t show up in the output.
234
235 ## Attributes
236
237 There are a few annotations that are useful to help `rustdoc` do the right
238 thing when testing your code:
239
240 ```rust
241 /// ```ignore
242 /// fn foo() {
243 /// ```
244 # fn foo() {}
245 ```
246
247 The `ignore` directive tells Rust to ignore your code. This is almost never
248 what you want, as it's the most generic. Instead, consider annotating it
249 with `text` if it's not code, or using `#`s to get a working example that
250 only shows the part you care about.
251
252 ```rust
253 /// ```should_panic
254 /// assert!(false);
255 /// ```
256 # fn foo() {}
257 ```
258
259 `should_panic` tells `rustdoc` that the code should compile correctly, but
260 not actually pass as a test.
261
262 ```text
263 /// ```no_run
264 /// loop {
265 /// println!("Hello, world");
266 /// }
267 /// ```
268 # fn foo() {}
269 ```
270
271 `compile_fail` tells `rustdoc` that the compilation should fail. If it
272 compiles, then the test will fail. However please note that code failing
273 with the current Rust release may work in a future release, as new features
274 are added.
275
276 ```text
277 /// ```compile_fail
278 /// let x = 5;
279 /// x += 2; // shouldn't compile!
280 /// ```
281 ```
282
283 The `no_run` attribute will compile your code, but not run it. This is
284 important for examples such as "Here's how to retrieve a web page,"
285 which you would want to ensure compiles, but might be run in a test
286 environment that has no network access.