Add showcase and invalid examples, pinned by golden-file tests - #20
Merged
Conversation
Two runnable examples and a test that runs each and compares its output
against a stored file.
`showcase` is a small but complete generator: it builds KtFile
fragments, merges them so each package collapses to one file, and
renders every construct the model can emit — classes with each modifier,
objects, enums with a constructor their entries call, data and value
classes, a sealed interface with nested alternatives, interfaces,
`fun interface`s, type aliases, properties with accessors and delegates,
`external` natives, raw blocks, and every KtCode block form. It ends
with the identifier helpers.
`invalid` shows the two ways a mistake surfaces. The validator reports
one case of each of its ten checks, and the last section catches the
builder panics for the shapes the model refuses to construct at all —
`object Foo(x: Int)`, two constructed superclasses, `external` as a
modifier.
The examples are the crate's documentation, so pinning their output
keeps them working; and because the output *is* generated Kotlin, a diff
shows the change in the emitted source rather than in a builder call.
`UPDATE_GOLDEN=1` rewrites the stored files.
`the_invalid_example_demonstrates_every_check` is pinned separately
against `Check::ALL`, so adding a check without an example fails with an
explanation instead of looking like golden noise.
Two things the examples turned up:
* `merge_files` silently dropped a fragment's `banner` override — the
merged file is rebuilt from scratch and never copied it. The first
fragment of a package to set one now wins.
* The test cannot locate example binaries under `target/`: cargo does
not build example targets for `cargo test --test examples`. It
invokes cargo instead, so it behaves the same however it is run.
Contributor
There was a problem hiding this comment.
Pull request overview
Adds two runnable examples (showcase, invalid) to serve as executable documentation for the Kotlin model/renderer/validator, and introduces golden-file tests that run those examples and pin their stdout. Also fixes merge_files_with to preserve KtFile::banner overrides during merges.
Changes:
- Add
examples/showcase.rs(comprehensive generator demo) andexamples/invalid.rs(validator + builder refusal demo), with pinned golden outputs. - Add
tests/examples.rsintegration test to run examples viacargo run --example ...and compare stdout totests/golden/*.txt(withUPDATE_GOLDEN=1update path). - Fix
merge_files_withto carry the firstKtFile::banneroverride forward into the merged file, plus a unit test for it.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/golden/showcase.txt | Golden output pinned for the showcase example’s generated Kotlin + identifier helper output. |
| tests/golden/invalid.txt | Golden output pinned for the invalid example’s diagnostics, merge refusal, warn policy behavior, and builder-refusal messages. |
| tests/examples.rs | New test harness that runs examples through Cargo and compares stdout to golden files; includes a completeness check for Check::ALL. |
| src/tests.rs | Adds a regression test asserting banner overrides survive merge_files and that “first override wins.” |
| src/file.rs | Updates merge logic to preserve KtFile::banner overrides while merging fragments of the same package. |
| README.md | Documents the new examples and how to update golden files. |
| examples/showcase.rs | New example generating a broad set of Kotlin constructs and printing merged rendered output. |
| examples/invalid.rs | New example demonstrating validator diagnostics, merge refusal, policy tuning, and builder-rejected shapes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Merged
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.
Two runnable examples, and a test that runs each and compares its output against
a stored file.
showcaseA small but complete generator — it builds
KtFilefragments, merges them soeach package collapses to one file, and renders every construct the model has:
classes with each modifier, objects, an enum with a constructor its entries
call, data and value classes, a sealed interface with nested alternatives,
interfaces,
fun interfaces, type aliases, properties with accessors anddelegates,
externalnatives, raw blocks, and everyKtCodeblock form(
blk,blk_with,try_finally,wline,raw_reindent,import). It endswith the identifier helpers.
The output is real, compilable-looking Kotlin across four files, including a
width-broken signature and a width-broken call — so the layout rules are
visible rather than described.
invalidShows the two ways a mistake surfaces, which are caught at different moments:
path locating each (
io.example.broken/My-Class/object). Then the same modelunder
warn_all(), and under a policy with one check downgraded and oneswitched off.
last section catches those panics so the example can print what the author
would see:
Why golden files
The examples are the crate's documentation, so they have to keep working — and
because their output is generated Kotlin, pinning it turns them into readable
regression tests: a diff shows the change in the emitted source, not in a
builder call.
UPDATE_GOLDEN=1 cargo test --test examplesrewrites them.the_invalid_example_demonstrates_every_checkis pinned separately againstCheck::ALLrather than only by the golden file, because that is the propertythat matters — adding a check without demonstrating it should fail with an
explanation, not look like golden noise.
Two things the examples turned up
A real bug:
merge_filessilently dropped a fragment'sbanneroverride.The merged file is rebuilt from scratch and never copied it, so a per-package
banner was lost the moment anything was merged. The first fragment of a package
to set one now wins, with a test.
A wrong assumption of mine: the test originally located example binaries
under
target/<profile>/examples/. That works forcargo testbut not forcargo test --test examples— cargo does not build example targets for thelatter, so the test passed or failed depending on how the suite was started. It
now invokes cargo to build and run them, which behaves the same either way at
the cost of a no-op build.
Verification
107 unit tests, 3 example tests, 10 doctests.
cargo clippy --all-targets -- --deny warningsand CI's exact rustfmt invocation both clean.cargo package --listconfirms the examples and golden files ship with the crate.