Skip to content

Add incremental edit tests — verify editing one file only regenerates affected contracts #213

Description

@Skymly

Problem

IncrementalCacheTests only verifies "same compilation, second run → all stages Cached/Unchanged". This proves the value-equatable model refactoring works, but does not prove the core incremental value of source generators: when one file is edited, only the contracts in that file are regenerated; other contracts remain cached.

This is the primary performance benefit of IIncrementalGenerator in large projects. Without an edit-scenario test, there is no evidence that the incremental pipeline actually works for the real-world case it was designed for.

Current state

SourceGeneratorTestContext.AssertCacheHit runs the generator twice on the same compilation object (not Clone), then checks all tracked stages are Cached/Unchanged. This is a "no-op" cache test.

The Location reference equality limitation (P1-#5) means Clone would force re-execution, so the current test avoids it. But a proper incremental edit test needs to modify a syntax tree and re-run, which is the real scenario.

What's needed

A new test method (or test class) that:

  1. Creates a compilation with multiple contracts in separate files (e.g., two [RegisterStrategy] contracts: IShippingStrategy in Shipping.cs and IPaymentStrategy in Payment.cs)
  2. Runs the generator once (populates cache)
  3. Modifies one file (e.g., adds a new strategy to Shipping.cs, leaves Payment.cs unchanged)
  4. Re-runs the generator with the updated compilation
  5. Asserts that:
    • The transform stage for the modified file's contract returns Modified (re-executed)
    • The transform stage for the unmodified file's contract returns Cached or Unchanged
    • The Collect stage returns Modified (because one input changed)
    • The Combine stage returns Modified (because Collect changed)

This requires a new helper method in SourceGeneratorTestContext that:

  • Accepts an initial set of sources and a modified set of sources
  • Runs the generator on the first compilation, then on the second (with Clone + tree replacement)
  • Returns the GeneratorDriverRunResult for the second run so the test can inspect tracked step reasons

Affected files

  • tests/DesignPatterns.SourceGenerators.Tests/SourceGeneratorTestContext.cs — add RunIncrementalEdit helper
  • tests/DesignPatterns.SourceGenerators.Tests/Generators/IncrementalCacheTests.cs — add edit-scenario tests

Test scope

Start with RegisterStrategyGenerator (most representative: dual-branch Collect + Combine). If the pattern works, extend to other generators.

Module

SourceGenerators (tests)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions