Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Analyzers.Verifiers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;

namespace Microsoft.AspNetCore.Analyzers;

public static class CSharpAnalyzerTestExtensions
{
extension<TAnalyzer, TVerifier>(CSharpAnalyzerTest<TAnalyzer, TVerifier>)
where TAnalyzer : DiagnosticAnalyzer, new()
where TVerifier : IVerifier, new()
{
public static CSharpAnalyzerTest<TAnalyzer, TVerifier> Create([StringSyntax("C#-test")] string source, params ReadOnlySpan<DiagnosticResult> expectedDiagnostics)
{
var test = new CSharpAnalyzerTest<TAnalyzer, TVerifier>
{
TestCode = source.ReplaceLineEndings(),
// We need to set the output type to an exe to properly
// support top-level programs in the tests. Otherwise,
// the test infra will assume we are trying to build a library.
TestState = { OutputKind = OutputKind.ConsoleApplication },
ReferenceAssemblies = CSharpAnalyzerVerifier<TAnalyzer>.GetReferenceAssemblies(),
};

test.ExpectedDiagnostics.AddRange(expectedDiagnostics);
return test;
}
}

public static CSharpAnalyzerTest<TAnalyzer, TVerifier> WithSource<TAnalyzer, TVerifier>(this CSharpAnalyzerTest<TAnalyzer, TVerifier> test, [StringSyntax("C#-test")] string source)
where TAnalyzer : DiagnosticAnalyzer, new()
where TVerifier : IVerifier, new()
{
test.TestState.Sources.Add(source);
return test;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.InternalTesting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;

namespace Microsoft.AspNetCore.Analyzers.Verifiers;

Expand All @@ -30,19 +31,9 @@ public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor)
=> CSharpAnalyzerVerifier<TAnalyzer, DefaultVerifier>.Diagnostic(descriptor);

/// <inheritdoc cref="AnalyzerVerifier{TAnalyzer, TTest, TVerifier}.VerifyAnalyzerAsync(string, DiagnosticResult[])"/>
public static async Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected)
public static async Task VerifyAnalyzerAsync([StringSyntax("C#-test")] string source, params DiagnosticResult[] expected)
{
var test = new CSharpAnalyzerTest<TAnalyzer, DefaultVerifier>
{
TestCode = source.ReplaceLineEndings(),
// We need to set the output type to an exe to properly
// support top-level programs in the tests. Otherwise,
// the test infra will assume we are trying to build a library.
TestState = { OutputKind = OutputKind.ConsoleApplication },
ReferenceAssemblies = GetReferenceAssemblies(),
};

test.ExpectedDiagnostics.AddRange(expected);
var test = CSharpAnalyzerTest<TAnalyzer, DefaultVerifier>.Create(source, expected);
await test.RunAsync(CancellationToken.None);
}

Expand Down
Loading