Improve test coverage: edge cases + fix bool/string switch key formatting#46
Conversation
Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
There was a problem hiding this comment.
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
boolandstring[SwitchCase]keys as valid C# literals when generatingswitchstatements. - Add: new edge-case tests for void/bool/char return literals and
internalaccessibility 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.
|
|
||
| return key switch | ||
| { | ||
| bool b => b ? "true" : "false", | ||
| // SyntaxFactory.Literal handles escaping and quoting (e.g. "hello" → "\"hello\"") | ||
| string s => SyntaxFactory.Literal(s).Text, |
There was a problem hiding this comment.
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.
| 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, |
| bool b => b ? "true" : "false", | ||
| // SyntaxFactory.Literal handles escaping and quoting (e.g. "hello" → "\"hello\"") | ||
| string s => SyntaxFactory.Literal(s).Text, | ||
| _ => key.ToString()! |
There was a problem hiding this comment.
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.
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.cs—GenerateSwitchMethodSourceusedkey.ToString()for all non-enum switch keys, which:case True:/case False:forbool(not valid C# — needstrue/false)case hello:forstring(missing quotes)Added
FormatKeyAsCSharpLiteralto handle these types correctly:New Tests
EdgeCaseSimplePatternTests.cs— covers previously untested branches inGeneratesMethodPatternSourceBuilder:if (!partialMethod.ReturnsVoid)skip-return branchFormatValueAsCSharpLiteralSystem_Booleancase (emitstrue/false)FormatValueAsCSharpLiteralSystem_Charcase (emits'A')internalaccessibility —AppendNamespaceAndTypeHeaderAccessibility.InternalbranchBoolSwitchKeyTests.cs— end-to-end integration test for[SwitchCase(arg1: true/false)]verifying correct key formatting in generated code.GeneratorDiagnosticsTests.csadditions:[GeneratesMethod]targets a method that exists but is notpartialOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.