Skip to content

Improve test coverage: edge cases + fix bool/string switch key formatting#46

Merged
dex3r merged 2 commits into
mainfrom
copilot/improve-test-coverage
Mar 4, 2026
Merged

Improve test coverage: edge cases + fix bool/string switch key formatting#46
dex3r merged 2 commits into
mainfrom
copilot/improve-test-coverage

Conversation

Copilot AI commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

Test coverage was missing several code paths in the generator, including one active bug where bool and string values used as [SwitchCase] keys produced invalid C# code.

Bug Fix

GeneratesMethodPatternSourceBuilder.csGenerateSwitchMethodSource used key.ToString() for all non-enum switch keys, which:

  • Produced case True: / case False: for bool (not valid C# — needs true/false)
  • Produced case hello: for string (missing quotes)

Added FormatKeyAsCSharpLiteral to handle these types correctly:

// Before: key.ToString()! for all non-enum types
// After:
return key switch
{
    bool b => b ? "true" : "false",
    // SyntaxFactory.Literal handles escaping and quoting (e.g. "hello" → "\"hello\"")
    string s => SyntaxFactory.Literal(s).Text,
    _ => key.ToString()!
};

New Tests

EdgeCaseSimplePatternTests.cs — covers previously untested branches in GeneratesMethodPatternSourceBuilder:

  • Void returnif (!partialMethod.ReturnsVoid) skip-return branch
  • Bool returnFormatValueAsCSharpLiteral System_Boolean case (emits true/false)
  • Char returnFormatValueAsCSharpLiteral System_Char case (emits 'A')
  • internal accessibilityAppendNamespaceAndTypeHeader Accessibility.Internal branch

BoolSwitchKeyTests.cs — end-to-end integration test for [SwitchCase(arg1: true/false)] verifying correct key formatting in generated code.

GeneratorDiagnosticsTests.cs additions:

  • MSGH001 is emitted when [GeneratesMethod] targets a method that exists but is not partial
  • Valid bool switch configuration produces no error diagnostics
Original prompt

This section details on the original issue you should resolve

<issue_title>Improve test converage</issue_title>
<issue_description>Test coverage is not the best. Create test cases that test edge cases and improve test coverage.</issue_description>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve test coverage with edge case test cases Improve test coverage: edge cases + fix bool/string switch key formatting Mar 3, 2026
Copilot finished work on behalf of dex3r March 3, 2026 17:25
@dex3r dex3r marked this pull request as ready for review March 4, 2026 17:57
Copilot AI review requested due to automatic review settings March 4, 2026 17:57
@dex3r dex3r merged commit cec2835 into main Mar 4, 2026
2 checks passed
@dex3r dex3r deleted the copilot/improve-test-coverage branch March 4, 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

Improves the GeneratesMethod source generator’s correctness and test coverage by fixing invalid switch case label formatting for non-enum keys (notably bool and string) and adding new tests to cover previously untested branches and diagnostics.

Changes:

  • Fix: format bool and string [SwitchCase] keys as valid C# literals when generating switch statements.
  • Add: new edge-case tests for void/bool/char return literals and internal accessibility generation.
  • Add: diagnostic tests for MSGH001 (target method exists but isn’t partial) and for valid bool switch configurations producing no errors.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
MattSourceGenHelpers.Tests/EdgeCaseSimplePatternTests.cs Adds runtime + generated-code assertions for void/bool/char returns and internal accessibility branch coverage.
MattSourceGenHelpers.Tests/BoolSwitchKeyTests.cs Adds an integration test validating bool switch key generation produces case true/false.
MattSourceGenHelpers.Generators/GeneratesMethodPatternSourceBuilder.cs Introduces FormatKeyAsCSharpLiteral and uses it for switch-case key emission.
MattSourceGenHelpers.GeneratorTests/GeneratorDiagnosticsTests.cs Adds coverage for MSGH001 scenario and validates no error diagnostics for valid bool switch usage.

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

Comment on lines +326 to +331

return key switch
{
bool b => b ? "true" : "false",
// SyntaxFactory.Literal handles escaping and quoting (e.g. "hello" → "\"hello\"")
string s => SyntaxFactory.Literal(s).Text,

Copilot AI Mar 4, 2026

Copy link

Choose a reason for hiding this comment

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

FormatKeyAsCSharpLiteral fixes bool/string, but char switch keys will still generate invalid C# (e.g., [SwitchCase(arg1: 'A')] will end up as case A: because the fallback uses key.ToString()). Consider handling char (and potentially other primitive literal types) similarly to FormatValueAsCSharpLiteral, e.g., use SyntaxFactory.Literal(c).Text when key is char or when parameterType.SpecialType == System_Char.

Suggested change
return key switch
{
bool b => b ? "true" : "false",
// SyntaxFactory.Literal handles escaping and quoting (e.g. "hello" → "\"hello\"")
string s => SyntaxFactory.Literal(s).Text,
// Handle char keys explicitly to produce valid C# char literals (e.g., 'A')
if (parameterType?.SpecialType == SpecialType.System_Char)
{
if (key is char c)
{
return SyntaxFactory.Literal(c).Text;
}
var s = key.ToString() ?? string.Empty;
if (s.Length == 1)
{
return SyntaxFactory.Literal(s[0]).Text;
}
// Fallback: treat as string literal; this should not normally be hit for valid char keys
return SyntaxFactory.Literal(s).Text;
}
return key switch
{
bool b => b ? "true" : "false",
// SyntaxFactory.Literal handles escaping and quoting (e.g. "hello" → "\"hello\"")
string s => SyntaxFactory.Literal(s).Text,
char c => SyntaxFactory.Literal(c).Text,

Copilot uses AI. Check for mistakes.
Comment on lines +329 to +332
bool b => b ? "true" : "false",
// SyntaxFactory.Literal handles escaping and quoting (e.g. "hello" → "\"hello\"")
string s => SyntaxFactory.Literal(s).Text,
_ => key.ToString()!

Copilot AI Mar 4, 2026

Copy link

Choose a reason for hiding this comment

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

The new string s => SyntaxFactory.Literal(s).Text branch in FormatKeyAsCSharpLiteral isn’t covered by a test (current new integration coverage only exercises the bool branch). Consider adding an end-to-end test similar to BoolSwitchKeyTests but with a string switch parameter (including a value that needs escaping) to prevent regressions in quoting/escaping.

Copilot uses AI. Check for mistakes.
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 61.53846% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
....Generators/GeneratesMethodPatternSourceBuilder.cs 61.53% 3 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

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.

Improve test converage

4 participants