Skip to content

Add PiExample and PiExampleFluent tests; extract shared test helpers#12

Merged
dex3r merged 2 commits into
mainfrom
copilot/add-tests-for-pi-example
Feb 27, 2026
Merged

Add PiExample and PiExampleFluent tests; extract shared test helpers#12
dex3r merged 2 commits into
mainfrom
copilot/add-tests-for-pi-example

Conversation

Copilot AI commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

The test suite only covered the simple ColorsClass pattern. This adds coverage for the attribute-based switch pattern (SwitchCase/SwitchDefault) and the fluent generator API (Generator.MethodImplementation<TArg, TReturn>().WithSwitchBody()...), while eliminating duplication across test helpers.

Changes

  • GeneratedCodeTestHelper (new) — centralizes FindProjectDirectory() and ReadGeneratedCode(fileName) previously duplicated inline in UnitTest1
  • TestSlowMath (new) — Pi-digit lookup shared by both Pi test classes, mirrors SlowMath from Examples
  • PiExampleTests (new) — TestPiClass mirrors PiExample using [SwitchCase]/[SwitchDefault] attributes; tests verify both runtime values and exact generated switch source
  • PiExampleFluentTests (new) — TestPiFluentClass mirrors PiExampleFluent using the fluent API with Integer.Range(300, 303); tests cover the expanded case set (0–2, 300–303) and default fallback
  • UnitTest1 (refactored) — drops the now-redundant private helpers in favour of GeneratedCodeTestHelper

Each new test fixture has parameterized runtime-output tests and a generated-code assertion:

// Attribute-based switch pattern
[GeneratesMethod(nameof(GetPiDecimal))]
[SwitchCase(arg1: 0)]
[SwitchCase(arg1: 1)]
[SwitchCase(arg1: 2)]
static int GetPiDecimal_Generator_Specialized(int decimalNumber) =>
    TestSlowMath.CalculatePiDecimal(decimalNumber);

// Fluent API pattern
[GeneratesMethod(nameof(GetPiDecimal))]
static IMethodImplementationGenerator GetPiDecimal_Generator() =>
    Generator
        .MethodImplementation<int, int>()
        .WithSwitchBody()
        .ForCases(0, 1, 2, Integer.Range(300, 303)).CompileTimeBody(decimalNumber => TestSlowMath.CalculatePiDecimal(decimalNumber))
        .ForDefaultCase().RuntimeBody(decimalNumber => () => TestSlowMath.CalculatePiDecimal(decimalNumber));
Original prompt

This section details on the original issue you should resolve

<issue_title>Add tests for similar case as PiExample</issue_title>
<issue_description>Re-use code from other tests. Refactor to avoid duplication, create helper code if needed.

After doing that, also add tests for PiExampleFluent. Also re-factor and re-use as needed.</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

… code

Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Copilot AI changed the title [WIP] Add tests for similar case as PiExample Add PiExample and PiExampleFluent tests; extract shared test helpers Feb 27, 2026
@dex3r dex3r marked this pull request as ready for review February 27, 2026 17:56
Copilot AI review requested due to automatic review settings February 27, 2026 17:56
@dex3r dex3r merged commit 1909eb9 into main Feb 27, 2026
3 checks passed
@dex3r dex3r deleted the copilot/add-tests-for-pi-example branch February 27, 2026 17:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR expands the test suite to cover both attribute-based and fluent API code generation patterns while eliminating test code duplication. The PR mirrors the PiExample and PiExampleFluent examples from the Examples project, creating comprehensive test coverage for switch-based method generation with compile-time and runtime branches.

Changes:

  • Extracted shared test helpers (GeneratedCodeTestHelper, TestSlowMath) to eliminate duplication and provide reusable test infrastructure
  • Added PiExampleTests covering attribute-based [SwitchCase]/[SwitchDefault] pattern with tests for cases 0-2 (compile-time) and 5 (runtime)
  • Added PiExampleFluentTests covering fluent API pattern with Integer.Range() support, testing cases 0-2, 300-303 (compile-time) and 5 (runtime)

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
MattSourceGenHelpers.Tests/GeneratedCodeTestHelper.cs Centralized helper for locating and reading generated .g.cs files with improved error messages
MattSourceGenHelpers.Tests/TestSlowMath.cs Pi digit calculation utility mirroring Examples/SlowMath.cs for test isolation
MattSourceGenHelpers.Tests/PiExampleTests.cs Test fixture for attribute-based switch generation pattern with runtime and generated-code assertions
MattSourceGenHelpers.Tests/PiExampleFluentTests.cs Test fixture for fluent API switch generation with Integer.Range support and comprehensive case coverage
MattSourceGenHelpers.Tests/UnitTest1.cs Refactored to use GeneratedCodeTestHelper, removing duplicate helper methods

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

Add tests for similar case as PiExample

3 participants