@@ -7,6 +7,13 @@ namespace Microsoft.AspNetCore.SourceGenerators.Tests;
7
7
8
8
public class PublicTopLevelProgramGeneratorTests
9
9
{
10
+ private const string ExpectedGeneratedSource = """
11
+ // <auto-generated />
12
+ #pragma warning disable CS1591
13
+ public partial class Program { }
14
+ #pragma warning restore CS1591
15
+ """ ;
16
+
10
17
[ Fact ]
11
18
public async Task GeneratesSource_ProgramWithTopLevelStatements ( )
12
19
{
@@ -20,12 +27,33 @@ public async Task GeneratesSource_ProgramWithTopLevelStatements()
20
27
app.Run();
21
28
""" ;
22
29
23
- var expected = """
24
- // <auto-generated />
25
- public partial class Program { }
30
+ await VerifyCS . VerifyAsync ( source , "PublicTopLevelProgram.Generated.g.cs" , ExpectedGeneratedSource ) ;
31
+ }
32
+
33
+ // The compiler synthesizes a Program class in the global namespace due to top-level statements
34
+ // The Foo.Program class is completely unrelated to the entry point and is just as any regular type
35
+ // Hence, we will expect to see the source generated in these scenarios
36
+ [ Theory ]
37
+ [ InlineData ( "public partial class Program { }" ) ]
38
+ [ InlineData ( "internal partial class Program { }" ) ]
39
+ public async Task GeneratesSource_IfProgramDefinedInANamespace ( string declaration )
40
+ {
41
+ var source = $$ """
42
+ using Microsoft.AspNetCore.Builder;
43
+
44
+ var app = WebApplication.Create();
45
+
46
+ app.MapGet("/", () => "Hello, World!");
47
+
48
+ app.Run();
49
+
50
+ namespace Foo
51
+ {
52
+ {{ declaration }}
53
+ }
26
54
""" ;
27
55
28
- await VerifyCS . VerifyAsync ( source , "PublicTopLevelProgram.Generated.g.cs" , expected ) ;
56
+ await VerifyCS . VerifyAsync ( source , "PublicTopLevelProgram.Generated.g.cs" , ExpectedGeneratedSource ) ;
29
57
}
30
58
31
59
[ Fact ]
@@ -86,6 +114,31 @@ public static void Main()
86
114
await VerifyCS . VerifyAsync ( source ) ;
87
115
}
88
116
117
+ [ Fact ]
118
+ public async Task DoesNotGeneratorSource_ExplicitPublicProgramClassInNamespace ( )
119
+ {
120
+ var source = """
121
+ using Microsoft.AspNetCore.Builder;
122
+
123
+ namespace Foo
124
+ {
125
+ public class Program
126
+ {
127
+ public static void Main()
128
+ {
129
+ var app = WebApplication.Create();
130
+
131
+ app.MapGet("/", () => "Hello, World!");
132
+
133
+ app.Run();
134
+ }
135
+ }
136
+ }
137
+ """ ;
138
+
139
+ await VerifyCS . VerifyAsync ( source ) ;
140
+ }
141
+
89
142
[ Fact ]
90
143
public async Task DoesNotGeneratorSource_ExplicitInternalProgramClass ( )
91
144
{
@@ -108,6 +161,31 @@ public static void Main()
108
161
await VerifyCS . VerifyAsync ( source ) ;
109
162
}
110
163
164
+ [ Fact ]
165
+ public async Task DoesNotGeneratorSource_ExplicitInternalProgramClassInNamespace ( )
166
+ {
167
+ var source = """
168
+ using Microsoft.AspNetCore.Builder;
169
+
170
+ namespace Foo
171
+ {
172
+ internal class Program
173
+ {
174
+ public static void Main()
175
+ {
176
+ var app = WebApplication.Create();
177
+
178
+ app.MapGet("/", () => "Hello, World!");
179
+
180
+ app.Run();
181
+ }
182
+ }
183
+ }
184
+ """ ;
185
+
186
+ await VerifyCS . VerifyAsync ( source ) ;
187
+ }
188
+
111
189
[ Theory ]
112
190
[ InlineData ( "interface" ) ]
113
191
[ InlineData ( "struct" ) ]
@@ -127,6 +205,33 @@ public static void Main(string[] args)
127
205
app.Run();
128
206
}
129
207
}
208
+ """ ;
209
+
210
+ await VerifyCS . VerifyAsync ( source ) ;
211
+ }
212
+
213
+ [ Theory ]
214
+ [ InlineData ( "interface" ) ]
215
+ [ InlineData ( "struct" ) ]
216
+ public async Task DoesNotGeneratorSource_ExplicitInternalProgramTypeInNamespace ( string type )
217
+ {
218
+ var source = $$ """
219
+ using Microsoft.AspNetCore.Builder;
220
+
221
+ namespace Foo
222
+ {
223
+ internal {{ type }} Program
224
+ {
225
+ public static void Main(string[] args)
226
+ {
227
+ var app = WebApplication.Create();
228
+
229
+ app.MapGet("/", () => "Hello, World!");
230
+
231
+ app.Run();
232
+ }
233
+ }
234
+ }
130
235
""" ;
131
236
132
237
await VerifyCS . VerifyAsync ( source ) ;
0 commit comments