Skip to content

Commit 327c625

Browse files
committed
Added WrongReturnType tests
1 parent fa858eb commit 327c625

5 files changed

Lines changed: 134 additions & 28 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System.Collections.Immutable;
2+
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.Text;
4+
5+
namespace EasySourceGenerators.GeneratorTests;
6+
7+
public class WrongReturnType
8+
{
9+
[Test]
10+
public void WrongReturnTypeTest_Object()
11+
{
12+
string source = """
13+
using EasySourceGenerators.Abstractions;
14+
15+
namespace TestNamespace;
16+
17+
public static partial class JustReturnConstantTestClass
18+
{
19+
public static partial int JustReturnConstant();
20+
21+
[MethodBodyGenerator(nameof(JustReturnConstant))]
22+
public static object JustReturnConstantGenerator() =>
23+
Generate.MethodBody()
24+
.ForMethod().WithReturnType<int>().WithNoParameters()
25+
.UseProvidedBody(() => 42);
26+
}
27+
""";
28+
29+
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
30+
31+
Diagnostic? diag = diagnostics.FirstOrDefault(diagnostic => diagnostic.Id == "MSGH008");
32+
Assert.That(diag, Is.Not.Null);
33+
Assert.That(diag!.Location.IsInSource, Is.True);
34+
35+
TextSpan span = diag.Location.SourceSpan;
36+
string highlightedCode = source.Substring(span.Start, span.Length);
37+
Assert.That(highlightedCode, Is.EqualTo("object"), "Diagnostic should highlight only the return type");
38+
}
39+
40+
[Test]
41+
public void WrongReturnTypeTest_Double()
42+
{
43+
string source = """
44+
using EasySourceGenerators.Abstractions;
45+
46+
namespace TestNamespace;
47+
48+
public static partial class JustReturnConstantTestClass
49+
{
50+
public static partial int JustReturnConstant();
51+
52+
[MethodBodyGenerator(nameof(JustReturnConstant))]
53+
public static double JustReturnConstantGenerator() => 42.0;
54+
}
55+
""";
56+
57+
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
58+
59+
Diagnostic? diag = diagnostics.FirstOrDefault(diagnostic => diagnostic.Id == "MSGH008");
60+
Assert.That(diag, Is.Not.Null);
61+
Assert.That(diag!.Location.IsInSource, Is.True);
62+
63+
TextSpan span = diag.Location.SourceSpan;
64+
string highlightedCode = source.Substring(span.Start, span.Length);
65+
Assert.That(highlightedCode, Is.EqualTo("double"), "Diagnostic should highlight only the return type");
66+
}
67+
68+
[Test]
69+
public void WrongReturnTypeTest_String()
70+
{
71+
string source = """
72+
using EasySourceGenerators.Abstractions;
73+
74+
namespace TestNamespace;
75+
76+
public static partial class JustReturnConstantTestClass
77+
{
78+
public static partial int JustReturnConstant();
79+
80+
[MethodBodyGenerator(nameof(JustReturnConstant))]
81+
public static string JustReturnConstantGenerator() => "abc";
82+
}
83+
""";
84+
85+
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
86+
87+
Diagnostic? diag = diagnostics.FirstOrDefault(diagnostic => diagnostic.Id == "MSGH008");
88+
Assert.That(diag, Is.Not.Null);
89+
Assert.That(diag!.Location.IsInSource, Is.True);
90+
91+
TextSpan span = diag.Location.SourceSpan;
92+
string highlightedCode = source.Substring(span.Start, span.Length);
93+
Assert.That(highlightedCode, Is.EqualTo("string"), "Diagnostic should highlight only the return type");
94+
}
95+
}

EasySourceGenerators.Generators/AnalyzerReleases.Unshipped.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
Rule ID | Category | Severity | Notes
44
--------|----------|----------|--------------------
5-
MSGH001 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
6-
MSGH002 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
7-
MSGH003 | GeneratesMethodGenerator | Disabled | GeneratesMethodGenerator
8-
MSGH004 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
9-
MSGH005 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
10-
MSGH006 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
11-
MSGH007 | GeneratesMethodGenerator | Error | GeneratesMethodGeneratorDiagnostics
5+
MSGH001 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
6+
MSGH002 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
7+
MSGH003 | GeneratesMethodGenerator | Info | GeneratesMethodGenerator
8+
MSGH004 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
9+
MSGH007 | GeneratesMethodGenerator | Error | GeneratesMethodGeneratorDiagnostics
10+
MSGH008 | GeneratesMethodGenerator | Error | GeneratesMethodGeneratorDiagnostics
Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using EasySourceGenerators.Abstractions;
12
using Microsoft.CodeAnalysis;
23

34
namespace EasySourceGenerators.Generators;
@@ -17,7 +18,7 @@ internal static class GeneratesMethodGeneratorDiagnostics
1718
internal static readonly DiagnosticDescriptor GeneratorMethodMustBeStaticError = new(
1819
id: "MSGH002",
1920
title: "Generator method must be static",
20-
messageFormat: "Method '{0}' marked with [MethodBodyGenerator] must be static",
21+
messageFormat: $"Method '{{0}}' marked with [{nameof(MethodBodyGenerator)}] must be static",
2122
category: Category,
2223
defaultSeverity: DiagnosticSeverity.Error,
2324
isEnabledByDefault: true);
@@ -28,7 +29,7 @@ internal static class GeneratesMethodGeneratorDiagnostics
2829
messageFormat: "Generating implementation for partial method '{0}' in class '{1}' using generator '{2}'",
2930
category: Category,
3031
defaultSeverity: DiagnosticSeverity.Info,
31-
isEnabledByDefault: false);
32+
isEnabledByDefault: true);
3233

3334
internal static readonly DiagnosticDescriptor GeneratorMethodExecutionError = new(
3435
id: "MSGH004",
@@ -37,22 +38,6 @@ internal static class GeneratesMethodGeneratorDiagnostics
3738
category: Category,
3839
defaultSeverity: DiagnosticSeverity.Error,
3940
isEnabledByDefault: true);
40-
41-
internal static readonly DiagnosticDescriptor GeneratorMethodTooManyParametersError = new(
42-
id: "MSGH005",
43-
title: "Generator method has too many parameters",
44-
messageFormat: "Method '{0}' marked with [MethodBodyGenerator] and [SwitchCase] has {1} parameter(s), but only methods with zero or one parameter are supported. Remove extra parameters or use the fluent API for more complex scenarios.",
45-
category: Category,
46-
defaultSeverity: DiagnosticSeverity.Error,
47-
isEnabledByDefault: true);
48-
49-
internal static readonly DiagnosticDescriptor SwitchCaseArgumentTypeMismatchError = new(
50-
id: "MSGH006",
51-
title: "SwitchCase argument type mismatch",
52-
messageFormat: "SwitchCase argument type '{0}' does not match the method parameter type '{1}'",
53-
category: Category,
54-
defaultSeverity: DiagnosticSeverity.Error,
55-
isEnabledByDefault: true);
5641

5742
internal static readonly DiagnosticDescriptor CannotUseRuntimeParameterForCompileTimeGeneratorError = new(
5843
id: "MSGH007",
@@ -61,4 +46,12 @@ internal static class GeneratesMethodGeneratorDiagnostics
6146
category: Category,
6247
defaultSeverity: DiagnosticSeverity.Error,
6348
isEnabledByDefault: true);
49+
50+
internal static readonly DiagnosticDescriptor MethodBodyGeneratorInvalidReturnType = new(
51+
id: "MSGH008",
52+
title: "MethodBodyGenerator has invalid return type",
53+
messageFormat: $"Method '{{0}}' marked with [{nameof(MethodBodyGenerator)}] must return either IMethodBodyGenerator (for fluent API builder) or the exact type the target method returns (for compile type const return body)",
54+
category: Category,
55+
defaultSeverity: DiagnosticSeverity.Error,
56+
isEnabledByDefault: true);
6457
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using EasySourceGenerators.Abstractions;
2+
3+
namespace EasySourceGenerators.Tests;
4+
5+
public class DelegateBodyTests
6+
{
7+
[Test]
8+
public void JustReturnConstantTest()
9+
{
10+
11+
}
12+
}
13+
14+
public static partial class JustReturnConstantTestClass
15+
{
16+
public static partial int JustReturnConstant();
17+
18+
[MethodBodyGenerator(nameof(JustReturnConstant))]
19+
public static int JustReturnConstantGenerator() => 2;
20+
}

EasySourceGenerators.sln.DotSettings.user

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111

1212
<s:Boolean x:Key="/Default/Environment/Hierarchy/Build/SolutionBuilderNext/IncludeAllAttributesInAssemblyPublicSurfaceHash/@EntryValue">True</s:Boolean>
1313
<s:Boolean x:Key="/Default/Environment/Hierarchy/Build/SolutionBuilderNext/ShouldRestoreNugetPackages/@EntryValue">True</s:Boolean>
14-
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=707ea923_002D2845_002D4e3b_002D89a6_002Da2b3a9c9d135/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
15-
&lt;Solution /&gt;&#xD;
16-
&lt;/SessionState&gt;</s:String>
14+
15+
1716

1817

1918

0 commit comments

Comments
 (0)