Skip to content

Write README examples as tests and run them from tests/readme.rs - #14

Merged
tanglearncode merged 1 commit into
mainfrom
docs/readme-examples-as-tests
Sep 13, 2026
Merged

tanglearncode merged 1 commit into
mainfrom
docs/readme-examples-as-tests

Conversation

@tanglearncode

@tanglearncode tanglearncode commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

README examples were doctests with hidden # lines, which GitHub shows.

  • Each README example is now a complete #[test] function.
  • tests/readme.rs holds the same code and runs it on every CI target.
  • readme_examples_match_this_file fails if the README and that file differ.

To change an example, edit tests/readme.rs, run cargo fmt, and copy the code into the README.

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.
@tanglearncode
tanglearncode merged commit 7fcf570 into main Sep 13, 2026
7 checks passed
@tanglearncode
tanglearncode deleted the docs/readme-examples-as-tests branch September 13, 2026 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant