-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[rustdoc] stabilize cfg(doctest) #63803
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.