Skip to content
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

generate tests from code example docstrings (to fight bitrot) #2925

Closed
olsonjeffery opened this issue Jul 15, 2012 · 8 comments · Fixed by #11120
Closed

generate tests from code example docstrings (to fight bitrot) #2925

olsonjeffery opened this issue Jul 15, 2012 · 8 comments · Fixed by #11120
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc C-enhancement Category: An issue proposing an enhancement or a PR with one. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@olsonjeffery
Copy link
Contributor

So I'm going through my contributions and adding code examples for most of the API stuff out there that isn't immediately obvious.

I only have one example of an existing code sample that is live in the libstd docs right now, and that's for net::tcp::listen. Suffice to say: it is very out-of-date with the current state of the art for rust. This is a sad discovery, on my part.

Since more literate code examples in the docs is probably a good thing, this is where I'm putting my energies for documentation of my work (as opposed to blog posts, emails to the list, etc). I'm trying to make the examples complete, illustrative and durable (the latter of which I have no control over).

Cutting to the chase:

I'd be happy with something like what (I'm told) we have in the tutorial markdown: have the build automation (rustdoc, in this case?) do a run-pass on all code blocks (by some known convention) that appear in docstrings, for the greater good.

Thoughts?

@catamorphism
Copy link
Contributor

Yes, this seems like an obviously good idea to me. I think it is an RFC-level change, though, since it would result in rejecting more programs (which is a good thing!), so I'm adding the RFC label.

@brson
Copy link
Contributor

brson commented Jul 26, 2012

The easiest way is probably to use rustdoc to convert the documentation to markdown, then run the python script over it to generate tests.

Here's my preference:

  • Move the --test flag out of rustc completely, to a new tool called rusttest (or just leave all this logic inside rustc, but I'm wary of continuing to stuff non-compiler stuff into rustc)
  • Teach rusttest to parse test cases out of doc attributes the way that the existing python script does
  • Inject new tests into the AST based on the docs
  • Compile the whole thing as a test runner

Later, once we've created a unified rust tool we would end up just writing rust test foo.rc

@pnkfelix
Copy link
Member

So, this issue is a request for infrastructure for generating execution unit tests from the embedded documentation for a symbol. That sounds like a match for Maturity Milestone 4: well covered.

@ghost ghost assigned jaredhirsch and emberian Jul 5, 2013
@emberian
Copy link
Member

emberian commented Jul 5, 2013

Absorbing under rustdoc2

@emberian
Copy link
Member

Tests can be extracted from the doc comments present in the JSON rustdoc_ng emits. Tooling needs to be made to do so, and to build+run the tests.

@pnkfelix
Copy link
Member

part of #8125

@emberian
Copy link
Member

Unassigning self.

@alexcrichton
Copy link
Member

Nominating, this is the only way we're going to have code examples be reliable

@bors bors closed this as completed in d9c0658 Dec 23, 2013
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 11, 2018
Changes:

Remove now-useless `allow(unknown_lints)`
Stabilize tool lints
Use `impl Iterator` in arg position in clippy_dev
Fix fn_to_numeric_cast_with_truncation suppression
Limit commutative assign op lint to primitive types
Clarify code
Fix rust-lang#2937
Fix cast_possible_wrap and cast_sign_loss warnings
Fix cast_possible_truncation warnings
Fixes rust-lang#2925 cmp_owned false positive
if_let_redundant_pattern_matching: use Span.to() instead of Span.with_hi() to fix crash.
Improve diagnostics in case of lifetime elision (closes rust-lang#3284)
Fix items_after_statements for `const`s
Fix items_after_statements for sub-functions
Fix items_after_statements for `use` statements
Don't suggest cloned() for map Box deref
Fix excessive_precision false positive
Fix FP in `fn_to_numeric_cast_with_truncation`
new_without_default should not warn about unsafe new
fix command to manually test an example
Add license to README
Adding more detail to filter_map lint documentation.
additional people
Add license header to other files
Add license header to Rust files
Relicense clippy
Document relicensing process
Fix util/export.py to include lints from methods
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 13, 2018
Changes:

Remove now-useless `allow(unknown_lints)`
Stabilize tool lints
Use `impl Iterator` in arg position in clippy_dev
Fix fn_to_numeric_cast_with_truncation suppression
Limit commutative assign op lint to primitive types
Clarify code
Fix rust-lang#2937
Fix cast_possible_wrap and cast_sign_loss warnings
Fix cast_possible_truncation warnings
Fixes rust-lang#2925 cmp_owned false positive
if_let_redundant_pattern_matching: use Span.to() instead of Span.with_hi() to fix crash.
Improve diagnostics in case of lifetime elision (closes rust-lang#3284)
Fix items_after_statements for `const`s
Fix items_after_statements for sub-functions
Fix items_after_statements for `use` statements
Don't suggest cloned() for map Box deref
Fix excessive_precision false positive
Fix FP in `fn_to_numeric_cast_with_truncation`
new_without_default should not warn about unsafe new
fix command to manually test an example
Add license to README
Adding more detail to filter_map lint documentation.
additional people
Add license header to other files
Add license header to Rust files
Relicense clippy
Document relicensing process
Fix util/export.py to include lints from methods
celinval added a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
This check is redundant in our CI since the bookrunner job already
builds the documentation. It is also often breaking the regression job.


Co-authored-by: Jaisurya Nanduri <91620234+jaisnan@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc C-enhancement Category: An issue proposing an enhancement or a PR with one. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants