|
| 1 | +# CLAUDE.md — maintaining the CGP test suite |
| 2 | + |
| 3 | +This file governs the test crates under `crates/tests`. Read it before adding, |
| 4 | +moving, or refactoring any test here. Invoke the `/cgp` skill first — every test |
| 5 | +in this tree is CGP code, and the skill is the authoritative source for CGP |
| 6 | +semantics and vocabulary. |
| 7 | + |
| 8 | +The test suite has three jobs, split across crates: |
| 9 | + |
| 10 | +- **`cgp-tests`** is the main suite: realistic example code that must **compile and |
| 11 | + run**. A passing test is often just successful compilation, because much of CGP |
| 12 | + is compile-time wiring. This is where behavior is verified and where the |
| 13 | + user-facing macros are exercised end-to-end. |
| 14 | +- **`cgp-macro-tests`** tests the **internals** of the CGP macros by calling the |
| 15 | + functions in `cgp-macro-core` directly (parsers, AST types), and is the home for |
| 16 | + **failure cases** — inputs CGP should reject, and cases where a macro currently |
| 17 | + emits invalid or wrong code. |
| 18 | +- **`cgp-test-crate-a` / `cgp-test-crate-b`** are auxiliary packages for |
| 19 | + **cross-crate** behavior: whether a downstream crate can extend a namespace or |
| 20 | + provide a provider for a component defined elsewhere, under Rust's coherence and |
| 21 | + orphan rules. |
| 22 | + |
| 23 | +## Organize by concept, not by construct |
| 24 | + |
| 25 | +Group tests by the **CGP concept or feature** under test, never by the macro that |
| 26 | +happens to appear. A single construct such as `delegate_components!` serves many |
| 27 | +concepts — basic delegation, `open` dispatch, namespace headers, `UseDelegate` |
| 28 | +tables — so a bucket named after the construct mixes unrelated concerns and hides |
| 29 | +what is actually being verified. Name each group for the concept: `basic_delegation`, |
| 30 | +`abstract_types`, `implicit_arguments`, `namespaces`, `higher_order_providers`, and |
| 31 | +so on. |
| 32 | + |
| 33 | +The right granularity is driven by the feature, its implementation complexity, and |
| 34 | +how many cases are needed to cover it exhaustively — **not** by mirroring the |
| 35 | +concept documents under `docs/concepts/`. The names may coincide, but the split is |
| 36 | +chosen for coverage. **When a category accumulates too many test cases to stay |
| 37 | +coherent, split it into finer categories** rather than letting it sprawl; prefer |
| 38 | +splitting early. |
| 39 | + |
| 40 | +## A test target is a "sub-crate" |
| 41 | + |
| 42 | +Each concept is one **integration test target**, which Cargo compiles as its own |
| 43 | +crate — so each concept has its own coherence scope, exactly like a separate crate. |
| 44 | +A target is two things: |
| 45 | + |
| 46 | +- an **entrypoint file** `tests/<concept>_tests.rs` — the `_tests` suffix marks it |
| 47 | + as the target root; it carries a module doc comment, `#![allow(dead_code)]` when |
| 48 | + the target is mostly compile-time wiring, and a single `pub mod <concept>;`; |
| 49 | +- a **module directory** `tests/<concept>/` — the clean concept name — whose |
| 50 | + `mod.rs` lists the unit-test modules, one `pub mod` per file. |
| 51 | + |
| 52 | +`basic_delegation` is the reference implementation of this layout — copy its shape |
| 53 | +when adding a concept. |
| 54 | + |
| 55 | +## One unit test per file |
| 56 | + |
| 57 | +Put each unit test in its own `.rs` file under the concept directory, and make the |
| 58 | +file **self-contained**: define its own components, providers, and context types at |
| 59 | +module scope. Do **not** separate unrelated units with `#[test]` functions or nested |
| 60 | +`mod`s inside one file. CGP tests are dominated by type-level constructs and |
| 61 | +compile-time wiring that live at module scope and cannot be isolated by a function |
| 62 | +boundary; separate files are the only reliable isolation within a target. A file may |
| 63 | +still contain a `#[test]` fn for its runtime assertions, plus the module-scope items |
| 64 | +that test exercises. |
| 65 | + |
| 66 | +## Explain what each test covers |
| 67 | + |
| 68 | +Open every test file with a brief comment stating **what behavior it exercises**, |
| 69 | +and annotate individual tricky cases inline. Where it helps a reader, link to the |
| 70 | +owning reference document (for example `// see docs/reference/macros/cgp_impl.md`). |
| 71 | +Tests link **to** the documentation; the documentation never links back to a test |
| 72 | +(per `docs/CLAUDE.md`). |
| 73 | + |
| 74 | +## Use macro snapshots sparingly |
| 75 | + |
| 76 | +`cgp-macro-test-util` provides `snapshot_*!` macros (`snapshot_cgp_component!`, |
| 77 | +`snapshot_cgp_impl!`, `snapshot_delegate_components!`, …). Each **emits the real |
| 78 | +generated code** into the module *and* generates a `#[test]` that asserts a |
| 79 | +pretty-printed inline `insta` snapshot of it — so adding or removing a snapshot |
| 80 | +never changes the compile/runtime coverage, only the golden assertion. Always keep |
| 81 | +the snapshot string **inline** in the file (`@"…"`). |
| 82 | + |
| 83 | +The rule for when to snapshot: **snapshot a macro only in the concept target that |
| 84 | +owns that macro's feature; everywhere else invoke the macro plainly.** Concretely, |
| 85 | +each macro has one canonical full-expansion snapshot (plus snapshots for its |
| 86 | +genuinely distinct variants) in its owning target, and nowhere else: |
| 87 | + |
| 88 | +| Macro | Owning target(s) | |
| 89 | +| --- | --- | |
| 90 | +| `#[cgp_component]` | `basic_delegation` (+ generic variant in `generic_components`) | |
| 91 | +| `#[cgp_impl]` | `basic_delegation` (+ `higher_order_providers`, `implicit_arguments` variants) | |
| 92 | +| `#[cgp_type]` | `abstract_types` | |
| 93 | +| `#[cgp_getter]` / `#[cgp_auto_getter]` | `getters` | |
| 94 | +| `#[cgp_fn]` | `implicit_arguments`, `impl_side_dependencies` | |
| 95 | +| `delegate_components!` | `basic_delegation` (basic), `namespaces` (open/namespace), `dispatching` (`UseDelegate`) | |
| 96 | +| `check_components!` / `delegate_and_check_components!` | `checking` | |
| 97 | +| `cgp_namespace!` | `namespaces` | |
| 98 | +| `#[blanket_trait]` | `blanket_traits` | |
| 99 | +| `#[derive(HasField)]` / `HasFields` / `CgpData` | `field_access` / `extensible_records` / `extensible_variants` | |
| 100 | + |
| 101 | +When a file uses one of these macros as **incidental scaffolding** — a |
| 102 | +`#[cgp_component]` needed to set up a `delegate_components!` test, say — write the |
| 103 | +plain macro, not the snapshot form. The expansion is already pinned in the owning |
| 104 | +target, and a redundant snapshot only adds golden output that breaks on unrelated |
| 105 | +macro changes. |
| 106 | + |
| 107 | +## Adding a failure case (in `cgp-macro-tests`) |
| 108 | + |
| 109 | +CGP will have corner cases it does not yet handle. Do **not** try to fix them inline |
| 110 | +while refactoring; capture them as failing-behavior tests instead, in a dedicated |
| 111 | +failure-case target: |
| 112 | + |
| 113 | +- **Input that should be rejected** — assert the `cgp-macro-core` parser rejects it, |
| 114 | + using the `assert_rejects` helper pattern (see `ident_with_type_params`). |
| 115 | +- **A macro that emits invalid Rust** — capture the expanded code as an `insta` |
| 116 | + inline snapshot (the snapshot is a *string*, so it compiles even though the code |
| 117 | + would not), and add a code comment explaining **why** the output is wrong and |
| 118 | + **what the correct output should be**. |
| 119 | + |
| 120 | +Every failure case must also be recorded in the reference document that owns the |
| 121 | +construct, under its `## Known issues` section (the heading `docs/CLAUDE.md` |
| 122 | +mandates), describing the behavior without referring to the test. Put a link from |
| 123 | +the test's comment to that reference document. |
| 124 | + |
| 125 | +## Keep the docs in sync |
| 126 | + |
| 127 | +This suite is one of the four views of CGP's truth, alongside the macro |
| 128 | +implementation in `cgp-macro-core`, the reference documents in `docs/reference`, and |
| 129 | +the `/cgp` skill (see `docs/CLAUDE.md`). When a test reveals or pins a behavior |
| 130 | +worth documenting, update the reference document to explain that behavior directly — |
| 131 | +without referring to the test. When you move a test that a reference document's |
| 132 | +`## Source` section links to, update the link in the same change. |
| 133 | + |
| 134 | +## Running the suite |
| 135 | + |
| 136 | +``` |
| 137 | +cargo nextest run -p cgp-tests # the main suite |
| 138 | +cargo nextest run -p cgp-macro-tests # macro internals + failures |
| 139 | +cargo nextest run --workspace # everything |
| 140 | +
|
| 141 | +cargo insta test -p cgp-tests --review # review snapshot diffs |
| 142 | +cargo insta test -p cgp-tests --accept # accept intended snapshot changes |
| 143 | +``` |
| 144 | + |
| 145 | +A snapshot test that fails prints a diff of the generated code; accept it with |
| 146 | +`cargo insta` only after confirming the change is intended. |
| 147 | + |
| 148 | +## Migration status |
| 149 | + |
| 150 | +The suite was reorganized from a by-construct layout to this by-concept layout. As |
| 151 | +categories grow, keep splitting them per the rule above, and keep expanding failure |
| 152 | +coverage in `cgp-macro-tests` and cross-crate coverage in the `cgp-test-crate-*` |
| 153 | +packages — these were established with representative cases and are meant to grow. |
0 commit comments