File: SourceGenerators\PublicTopLevelProgramGeneratorTests.cs
Web Access
Project: src\src\Framework\AspNetCoreAnalyzers\test\Microsoft.AspNetCore.App.Analyzers.Test.csproj (Microsoft.AspNetCore.App.Analyzers.Test)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using VerifyCS = Microsoft.AspNetCore.Analyzers.Verifiers.CSharpSourceGeneratorVerifier<Microsoft.AspNetCore.SourceGenerators.PublicProgramSourceGenerator>;
 
namespace Microsoft.AspNetCore.SourceGenerators.Tests;
 
public class PublicTopLevelProgramGeneratorTests
{
    [Fact]
    public async Task GeneratesSource_ProgramWithTopLevelStatements()
    {
        var source = """
using Microsoft.AspNetCore.Builder;
 
var app = WebApplication.Create();
 
app.MapGet("/", () => "Hello, World!");
 
app.Run();
""";
 
        var expected = """
// <auto-generated />
public partial class Program { }
""";
 
        await VerifyCS.VerifyAsync(source, "PublicTopLevelProgram.Generated.g.cs", expected);
    }
 
    [Fact]
    public async Task DoesNotGeneratesSource_IfProgramIsAlreadyPublic()
    {
        var source = """
using Microsoft.AspNetCore.Builder;
 
var app = WebApplication.Create();
 
app.MapGet("/", () => "Hello, World!");
 
app.Run();
 
public partial class Program { }
""";
 
        await VerifyCS.VerifyAsync(source);
    }
 
    [Fact]
    public async Task DoesNotGeneratesSource_IfProgramDeclaresExplicitInternalAccess()
    {
        var source = """
using Microsoft.AspNetCore.Builder;
 
var app = WebApplication.Create();
 
app.MapGet("/", () => "Hello, World!");
 
app.Run();
 
internal partial class Program { }
""";
 
        await VerifyCS.VerifyAsync(source);
    }
 
    [Fact]
    public async Task DoesNotGeneratorSource_ExplicitPublicProgramClass()
    {
        var source = """
using Microsoft.AspNetCore.Builder;
 
public class Program
{
    public static void Main()
    {
        var app = WebApplication.Create();
 
        app.MapGet("/", () => "Hello, World!");
 
        app.Run();
    }
}
""";
 
        await VerifyCS.VerifyAsync(source);
    }
 
    [Fact]
    public async Task DoesNotGeneratorSource_ExplicitInternalProgramClass()
    {
        var source = """
using Microsoft.AspNetCore.Builder;
 
internal class Program
{
    public static void Main()
    {
        var app = WebApplication.Create();
 
        app.MapGet("/", () => "Hello, World!");
 
        app.Run();
    }
}
""";
 
        await VerifyCS.VerifyAsync(source);
    }
 
    [Theory]
    [InlineData("interface")]
    [InlineData("struct")]
    public async Task DoesNotGeneratorSource_ExplicitInternalProgramType(string type)
    {
        var source = $$"""
using Microsoft.AspNetCore.Builder;
 
internal {{type}} Program
{
    public static void Main(string[] args)
    {
        var app = WebApplication.Create();
 
        app.MapGet("/", () => "Hello, World!");
 
        app.Run();
    }
}
""";
 
        await VerifyCS.VerifyAsync(source);
    }
}