mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-12-28 19:45:48 +08:00
Moved analyzer tests to its own project
Also moves tests to `./test` which makes it possible for all test projects to share the same .editorconfig files and similar.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CodeAnalysis.CodeFixes;
|
||||
using Microsoft.VisualStudio.Composition;
|
||||
using Spectre.Console.Analyzer.FixProviders;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests
|
||||
{
|
||||
internal static class CodeFixProviderDiscovery
|
||||
{
|
||||
private static readonly Lazy<IExportProviderFactory> _exportProviderFactory;
|
||||
|
||||
static CodeFixProviderDiscovery()
|
||||
{
|
||||
_exportProviderFactory = new Lazy<IExportProviderFactory>(
|
||||
() =>
|
||||
{
|
||||
var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance, isNonPublicSupported: true);
|
||||
var parts = Task.Run(() => discovery.CreatePartsAsync(typeof(SystemConsoleToAnsiConsoleFix).Assembly)).GetAwaiter().GetResult();
|
||||
var catalog = ComposableCatalog.Create(Resolver.DefaultInstance).AddParts(parts);
|
||||
|
||||
var configuration = CompositionConfiguration.Create(catalog);
|
||||
var runtimeComposition = RuntimeComposition.CreateRuntimeComposition(configuration);
|
||||
return runtimeComposition.CreateExportProviderFactory();
|
||||
},
|
||||
LazyThreadSafetyMode.ExecutionAndPublication);
|
||||
}
|
||||
|
||||
public static IEnumerable<CodeFixProvider> GetCodeFixProviders(string language)
|
||||
{
|
||||
var exportProvider = _exportProviderFactory.Value.CreateExportProvider();
|
||||
var exports = exportProvider.GetExports<CodeFixProvider, LanguageMetadata>();
|
||||
return exports.Where(export => export.Metadata.Languages.Contains(language)).Select(export => export.Value);
|
||||
}
|
||||
|
||||
private class LanguageMetadata
|
||||
{
|
||||
public LanguageMetadata(IDictionary<string, object> data)
|
||||
{
|
||||
if (!data.TryGetValue(nameof(ExportCodeFixProviderAttribute.Languages), out var languages))
|
||||
{
|
||||
languages = Array.Empty<string>();
|
||||
}
|
||||
|
||||
Languages = ((string[])languages).ToImmutableArray();
|
||||
}
|
||||
|
||||
public ImmutableArray<string> Languages { get; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.6.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="3.0.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Spectre.Console.Analyzer\Spectre.Console.Analyzer.csproj" />
|
||||
<ProjectReference Include="..\..\src\Spectre.Console\Spectre.Console.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CodeAnalysis.CodeFixes;
|
||||
using Microsoft.CodeAnalysis.CSharp.Testing;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Microsoft.CodeAnalysis.Testing.Verifiers;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests
|
||||
{
|
||||
public static class SpectreAnalyzerVerifier<TAnalyzer>
|
||||
where TAnalyzer : DiagnosticAnalyzer, new()
|
||||
{
|
||||
public static Task VerifyCodeFixAsync(string source, DiagnosticResult expected, string fixedSource)
|
||||
=> VerifyCodeFixAsync(source, new[] { expected }, fixedSource);
|
||||
|
||||
private static Task VerifyCodeFixAsync(string source, IEnumerable<DiagnosticResult> expected, string fixedSource)
|
||||
{
|
||||
// Roslyn fixers always use \r\n for newlines, regardless of OS environment settings, so we normalize
|
||||
// the source as it typically comes from multi-line strings with varying newlines.
|
||||
if (Environment.NewLine != "\r\n")
|
||||
{
|
||||
source = source.Replace(Environment.NewLine, "\r\n");
|
||||
fixedSource = fixedSource.Replace(Environment.NewLine, "\r\n");
|
||||
}
|
||||
|
||||
var test = new Test
|
||||
{
|
||||
TestCode = source,
|
||||
FixedCode = fixedSource,
|
||||
};
|
||||
|
||||
test.ExpectedDiagnostics.AddRange(expected);
|
||||
return test.RunAsync();
|
||||
}
|
||||
|
||||
public static Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected)
|
||||
{
|
||||
var test = new Test
|
||||
{
|
||||
TestCode = source,
|
||||
CompilerDiagnostics = CompilerDiagnostics.All,
|
||||
};
|
||||
|
||||
test.ExpectedDiagnostics.AddRange(expected);
|
||||
return test.RunAsync();
|
||||
}
|
||||
|
||||
// Code fix tests support both analyzer and code fix testing. This test class is derived from the code fix test
|
||||
// to avoid the need to maintain duplicate copies of the customization work.
|
||||
private class Test : CSharpCodeFixTest<TAnalyzer, EmptyCodeFixProvider, XUnitVerifier>
|
||||
{
|
||||
public Test()
|
||||
{
|
||||
ReferenceAssemblies = CodeAnalyzerHelper.CurrentSpectre;
|
||||
TestBehaviors |= TestBehaviors.SkipGeneratedCodeCheck;
|
||||
}
|
||||
|
||||
protected override IEnumerable<CodeFixProvider> GetCodeFixProviders()
|
||||
{
|
||||
var analyzer = new TAnalyzer();
|
||||
foreach (var provider in CodeFixProviderDiscovery.GetCodeFixProviders(Language))
|
||||
{
|
||||
if (analyzer.SupportedDiagnostics.Any(diagnostic => provider.FixableDiagnosticIds.Contains(diagnostic.Id)))
|
||||
{
|
||||
yield return provider;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static class CodeAnalyzerHelper
|
||||
{
|
||||
internal static ReferenceAssemblies CurrentSpectre { get; }
|
||||
|
||||
static CodeAnalyzerHelper()
|
||||
{
|
||||
CurrentSpectre = ReferenceAssemblies.Net.Net50.AddAssemblies(
|
||||
ImmutableArray.Create(typeof(AnsiConsole).Assembly.Location.Replace(".dll", string.Empty)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Analyzers
|
||||
{
|
||||
public class NoCurrentLiveRenderablesTests
|
||||
{
|
||||
private static readonly DiagnosticResult _expectedDiagnostics = new(
|
||||
Descriptors.S1020_AvoidConcurrentCallsToMultipleLiveRenderables.Id,
|
||||
DiagnosticSeverity.Warning);
|
||||
|
||||
[Fact]
|
||||
public async void Status_call_within_live_call_warns()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
void Go()
|
||||
{
|
||||
AnsiConsole.Live(new Table()).Start(ctx =>
|
||||
{
|
||||
AnsiConsole.Status().Start(""go"", innerCtx => {});
|
||||
});
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoConcurrentLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(10, 13))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Status_call_within_live_call_warns_with_instance()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class Child
|
||||
{
|
||||
public readonly IAnsiConsole _console = AnsiConsole.Console;
|
||||
|
||||
public void Go()
|
||||
{
|
||||
_console.Status().Start(""starting"", context =>
|
||||
{
|
||||
_console.Progress().Start(progressContext => { });
|
||||
});
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoConcurrentLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(12, 13))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Calling_start_on_non_live_renderable_has_no_warning()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
Start();
|
||||
}
|
||||
|
||||
static void Start() => AnsiConsole.WriteLine(""Starting..."");
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoConcurrentLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Analyzers
|
||||
{
|
||||
public class NoPromptsDuringLiveRenderablesTests
|
||||
{
|
||||
private static readonly DiagnosticResult _expectedDiagnostics = new(
|
||||
Descriptors.S1021_AvoidPromptCallsDuringLiveRenderables.Id,
|
||||
DiagnosticSeverity.Warning);
|
||||
|
||||
[Fact]
|
||||
public async Task Prompt_out_of_progress_does_not_warn()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
void Go()
|
||||
{
|
||||
var s = AnsiConsole.Ask<string>(""How are you?"");
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoPromptsDuringLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Instance_variables_warn()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
public IAnsiConsole _console = AnsiConsole.Console;
|
||||
|
||||
public void Go()
|
||||
{
|
||||
_console.Status().Start(""starting"", context =>
|
||||
{
|
||||
var result = _console.Confirm(""we ok?"");
|
||||
});
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoPromptsDuringLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(12, 26))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Prompt_in_progress_warns()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
void Go()
|
||||
{
|
||||
AnsiConsole.Progress().Start(_ =>
|
||||
{
|
||||
AnsiConsole.Ask<string>(""How are you?"");
|
||||
});
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoPromptsDuringLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(10, 13))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Can_call_other_methods_from_within_renderables()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
AnsiConsole.Status().Start(""here we go"", context =>
|
||||
{
|
||||
var result = Confirm();
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
static string Confirm() => string.Empty;
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoPromptsDuringLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Analyzers
|
||||
{
|
||||
public class FavorInstanceAnsiConsoleOverStaticAnalyzerTests
|
||||
{
|
||||
private static readonly DiagnosticResult _expectedDiagnostics = new(
|
||||
Descriptors.S1010_FavorInstanceAnsiConsoleOverStatic.Id,
|
||||
DiagnosticSeverity.Info);
|
||||
|
||||
[Fact]
|
||||
public async void Instance_console_has_no_warnings()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
IAnsiConsole _ansiConsole = AnsiConsole.Console;
|
||||
|
||||
void TestMethod()
|
||||
{
|
||||
_ansiConsole.Write(""this is fine"");
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Static_console_with_no_instance_variables_has_no_warnings()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
void TestMethod()
|
||||
{
|
||||
AnsiConsole.Write(""this is fine"");
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Console_Write_Has_Warning()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
IAnsiConsole _ansiConsole = AnsiConsole.Console;
|
||||
|
||||
void TestMethod()
|
||||
{
|
||||
_ansiConsole.Write(""this is fine"");
|
||||
AnsiConsole.Write(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(11, 9))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Analyzers
|
||||
{
|
||||
public class UseSpectreInsteadOfSystemConsoleAnalyzerTests
|
||||
{
|
||||
private static readonly DiagnosticResult _expectedDiagnostics = new(
|
||||
Descriptors.S1000_UseAnsiConsoleOverSystemConsole.Id,
|
||||
DiagnosticSeverity.Warning);
|
||||
|
||||
[Fact]
|
||||
public async void Non_configured_SystemConsole_methods_report_no_warnings()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
|
||||
class TestClass {
|
||||
void TestMethod()
|
||||
{
|
||||
var s = Console.ReadLine();
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Console_Write_Has_Warning()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
|
||||
class TestClass {
|
||||
void TestMethod()
|
||||
{
|
||||
Console.Write(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(7, 9))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Console_WriteLine_Has_Warning()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
void TestMethod() {
|
||||
Console.WriteLine(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(7, 9))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Fixes
|
||||
{
|
||||
public class UseInstanceOfStaticAnsiConsoleTests
|
||||
{
|
||||
private static readonly DiagnosticResult _expectedDiagnostic = new(
|
||||
Descriptors.S1010_FavorInstanceAnsiConsoleOverStatic.Id,
|
||||
DiagnosticSeverity.Info);
|
||||
|
||||
[Fact]
|
||||
public async Task Static_call_replaced_with_field_call()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
IAnsiConsole _ansiConsole = AnsiConsole.Console;
|
||||
|
||||
void TestMethod()
|
||||
{
|
||||
_ansiConsole.Write(""this is fine"");
|
||||
AnsiConsole.Write(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
IAnsiConsole _ansiConsole = AnsiConsole.Console;
|
||||
|
||||
void TestMethod()
|
||||
{
|
||||
_ansiConsole.Write(""this is fine"");
|
||||
_ansiConsole.Write(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(11, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Static_call_replaced_with_parameter_call()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
void TestMethod(IAnsiConsole ansiConsole)
|
||||
{
|
||||
AnsiConsole.Write(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
void TestMethod(IAnsiConsole ansiConsole)
|
||||
{
|
||||
ansiConsole.Write(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(8, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Static_call_replaced_with_static_field_if_valid()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
static IAnsiConsole staticConsole;
|
||||
IAnsiConsole instanceConsole;
|
||||
|
||||
static void TestMethod()
|
||||
{
|
||||
AnsiConsole.Write(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
static IAnsiConsole staticConsole;
|
||||
IAnsiConsole instanceConsole;
|
||||
|
||||
static void TestMethod()
|
||||
{
|
||||
staticConsole.Write(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(11, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Fixes
|
||||
{
|
||||
public class UseSpectreInsteadOfSystemConsoleFixTests
|
||||
{
|
||||
private static readonly DiagnosticResult _expectedDiagnostic = new(
|
||||
Descriptors.S1000_UseAnsiConsoleOverSystemConsole.Id,
|
||||
DiagnosticSeverity.Warning);
|
||||
|
||||
[Fact]
|
||||
public async Task SystemConsole_replaced_with_AnsiConsole()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
void TestMethod()
|
||||
{
|
||||
Console.WriteLine(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
using System;
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
void TestMethod()
|
||||
{
|
||||
AnsiConsole.WriteLine(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(8, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SystemConsole_replaced_with_imported_AnsiConsole()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
void TestMethod()
|
||||
{
|
||||
Console.WriteLine(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
using System;
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
void TestMethod()
|
||||
{
|
||||
AnsiConsole.WriteLine(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(8, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SystemConsole_replaced_with_field_AnsiConsole()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
IAnsiConsole _ansiConsole;
|
||||
|
||||
void TestMethod()
|
||||
{
|
||||
Console.WriteLine(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
using System;
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
IAnsiConsole _ansiConsole;
|
||||
|
||||
void TestMethod()
|
||||
{
|
||||
_ansiConsole.WriteLine(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(11, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SystemConsole_replaced_with_static_field_AnsiConsole()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
static IAnsiConsole _ansiConsole;
|
||||
|
||||
static void TestMethod()
|
||||
{
|
||||
Console.WriteLine(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
using System;
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
{
|
||||
static IAnsiConsole _ansiConsole;
|
||||
|
||||
static void TestMethod()
|
||||
{
|
||||
_ansiConsole.WriteLine(""Hello, World"");
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(11, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user