-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[rustdoc] stabilize cfg(doctest) #63803
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -379,3 +379,49 @@ However, it's preferable to use fenced code blocks over indented code blocks. | |
Not only are fenced code blocks considered more idiomatic for Rust code, | ||
but there is no way to use directives such as `ignore` or `should_panic` with | ||
indented code blocks. | ||
|
||
### Include items only when collecting doctests | ||
|
||
Rustdoc's documentation tests can do some things that regular unit tests can't, so it can | ||
sometimes be useful to extend your doctests with samples that wouldn't otherwise need to be in | ||
documentation. To this end, Rustdoc allows you to have certain items only appear when it's | ||
collecting doctests, so you can utilize doctest functionality without forcing the test to appear in | ||
docs, or to find an arbitrary private item to include it on. | ||
|
||
When compiling a crate for use in doctests (with `--test` option), rustdoc will set `cfg(doctest)`. | ||
Note that they will still link against only the public items of your crate; if you need to test | ||
private items, you need to write a unit test. | ||
|
||
In this example, we're adding doctests that we know won't compile, to verify that our struct can | ||
only take in valid data: | ||
|
||
```rust | ||
/// We have a struct here. Remember it doesn't accept negative numbers! | ||
pub struct MyStruct(pub usize); | ||
|
||
/// ```compile_fail | ||
/// let x = my_crate::MyStruct(-5); | ||
/// ``` | ||
#[cfg(doctest)] | ||
pub struct MyStructOnlyTakesUsize; | ||
``` | ||
|
||
Note that the struct `MyStructOnlyTakesUsize` here isn't actually part of your public crate | ||
API. The use of `#[cfg(doctest)]` makes sure that this struct only exists while rustdoc is | ||
collecting doctests. This means that its doctest is executed when `--test` is passed to rustdoc, | ||
but is hidden from the public documentation. | ||
|
||
Another possible use of `cfg(doctest)` is to test doctests that are included in your README file | ||
without including it in your main documentation. For example, you could write this into your | ||
`lib.rs` to test your README as part of your doctests: | ||
|
||
```rust,ignore | ||
#![feature(extern_doc)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I misspelled the feature name here, it's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated! |
||
|
||
#[doc(include="../README.md")] | ||
#[cfg(doctest)] | ||
pub struct ReadmeDoctests; | ||
``` | ||
|
||
This will include your README as documentation on the hidden struct `ReadmeDoctests`, which will | ||
then be tested alongside the rest of your doctests. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
|
||
running 2 tests | ||
test $DIR/cfg-test.rs - Bar (line 28) ... ok | ||
test $DIR/cfg-test.rs - Foo (line 20) ... ok | ||
test $DIR/cfg-test.rs - Bar (line 26) ... ok | ||
test $DIR/cfg-test.rs - Foo (line 18) ... ok | ||
|
||
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out | ||
|
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to add a note somewhere that this struct isn't actually exposed in anyway, i.e., it won't be in documentation or in the public API of this crate.