Write README examples as tests and run them from tests/readme.rs - #14
Merged
Merged
Conversation
The README examples were doctests. To compile, they carried hidden setup lines starting with # and ended with # Ok::<(), shimforge::Error>(()). GitHub shows those lines, so readers could take them for part of the API. Each example is now a complete #[test] function that returns Result<(), shimforge::Error>, next to the code it tests. The same code lives in tests/readme.rs, one module per README section, so the normal test run compiles and runs every example on every CI target. readme_examples_match_this_file compares the README's Rust blocks with those modules line by line and fails on any difference, naming the line in each file. src/lib.rs still uses the README as the crate documentation, but only outside doctest collection, so rustdoc no longer runs the blocks. Clippy's test_attr_in_doctest lint is allowed for the same reason. README code now follows rustfmt, because it has to match the formatted test file. The async example's Ledger::balance reads its name field, so the field is not reported as dead code. Checked locally with cargo fmt --check, cargo clippy --test readme -- -D warnings, and a script that repeats the comparison: 405 example lines match.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
README examples were doctests with hidden
#lines, which GitHub shows.#[test]function.tests/readme.rsholds the same code and runs it on every CI target.readme_examples_match_this_filefails if the README and that file differ.To change an example, edit
tests/readme.rs, runcargo fmt, and copy the code into the README.