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+ }
0 commit comments