mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-12-28 19:45:48 +08:00
Add global usings (#668)
* Use global usings * Fix namespace declarations for test projects
This commit is contained in:
12
test/Spectre.Console.Analyzer.Tests/CodeAnalyzerHelper.cs
Normal file
12
test/Spectre.Console.Analyzer.Tests/CodeAnalyzerHelper.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace Spectre.Console.Analyzer.Tests;
|
||||
|
||||
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)));
|
||||
}
|
||||
}
|
||||
@@ -1,55 +1,44 @@
|
||||
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;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests
|
||||
internal static class CodeFixProviderDiscovery
|
||||
{
|
||||
internal static class CodeFixProviderDiscovery
|
||||
private static readonly Lazy<IExportProviderFactory> _exportProviderFactory;
|
||||
|
||||
static 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)
|
||||
_exportProviderFactory = new Lazy<IExportProviderFactory>(
|
||||
() =>
|
||||
{
|
||||
if (!data.TryGetValue(nameof(ExportCodeFixProviderAttribute.Languages), out var languages))
|
||||
{
|
||||
languages = Array.Empty<string>();
|
||||
}
|
||||
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);
|
||||
|
||||
Languages = ((string[])languages).ToImmutableArray();
|
||||
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>();
|
||||
}
|
||||
|
||||
public ImmutableArray<string> Languages { get; }
|
||||
Languages = ((string[])languages).ToImmutableArray();
|
||||
}
|
||||
|
||||
public ImmutableArray<string> Languages { get; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
15
test/Spectre.Console.Analyzer.Tests/Properties/Usings.cs
Normal file
15
test/Spectre.Console.Analyzer.Tests/Properties/Usings.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
global using System;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Collections.Immutable;
|
||||
global using System.Linq;
|
||||
global using System.Threading;
|
||||
global using System.Threading.Tasks;
|
||||
global using Microsoft.CodeAnalysis;
|
||||
global using Microsoft.CodeAnalysis.CodeFixes;
|
||||
global using Microsoft.CodeAnalysis.CSharp.Testing;
|
||||
global using Microsoft.CodeAnalysis.Diagnostics;
|
||||
global using Microsoft.CodeAnalysis.Testing;
|
||||
global using Microsoft.CodeAnalysis.Testing.Verifiers;
|
||||
global using Microsoft.VisualStudio.Composition;
|
||||
global using Spectre.Console.Analyzer.FixProviders;
|
||||
global using Xunit;
|
||||
@@ -5,6 +5,10 @@
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AdditionalFiles Include="..\..\src\stylecop.json" Link="Properties/stylecop.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<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" />
|
||||
|
||||
@@ -1,86 +1,63 @@
|
||||
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;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests
|
||||
public static class SpectreAnalyzerVerifier<TAnalyzer>
|
||||
where TAnalyzer : DiagnosticAnalyzer, new()
|
||||
{
|
||||
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)
|
||||
{
|
||||
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")
|
||||
{
|
||||
// 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();
|
||||
source = source.Replace(Environment.NewLine, "\r\n");
|
||||
fixedSource = fixedSource.Replace(Environment.NewLine, "\r\n");
|
||||
}
|
||||
|
||||
public static Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected)
|
||||
var test = new Test
|
||||
{
|
||||
var test = new Test
|
||||
{
|
||||
TestCode = source,
|
||||
CompilerDiagnostics = CompilerDiagnostics.All,
|
||||
};
|
||||
TestCode = source,
|
||||
FixedCode = fixedSource,
|
||||
};
|
||||
|
||||
test.ExpectedDiagnostics.AddRange(expected);
|
||||
return test.RunAsync();
|
||||
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;
|
||||
}
|
||||
|
||||
// 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>
|
||||
protected override IEnumerable<CodeFixProvider> GetCodeFixProviders()
|
||||
{
|
||||
public Test()
|
||||
var analyzer = new TAnalyzer();
|
||||
foreach (var provider in CodeFixProviderDiscovery.GetCodeFixProviders(Language))
|
||||
{
|
||||
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)))
|
||||
{
|
||||
if (analyzer.SupportedDiagnostics.Any(diagnostic => provider.FixableDiagnosticIds.Contains(diagnostic.Id)))
|
||||
{
|
||||
yield return provider;
|
||||
}
|
||||
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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Xunit;
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Analyzers;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Analyzers
|
||||
public class NoCurrentLiveRenderablesTests
|
||||
{
|
||||
public class NoCurrentLiveRenderablesTests
|
||||
{
|
||||
private static readonly DiagnosticResult _expectedDiagnostics = new(
|
||||
Descriptors.S1020_AvoidConcurrentCallsToMultipleLiveRenderables.Id,
|
||||
DiagnosticSeverity.Warning);
|
||||
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 = @"
|
||||
[Fact]
|
||||
public async void Status_call_within_live_call_warns()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -27,15 +23,15 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoConcurrentLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(10, 13))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
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 = @"
|
||||
[Fact]
|
||||
public async void Status_call_within_live_call_warns_with_instance()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class Child
|
||||
@@ -51,15 +47,15 @@ class Child
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoConcurrentLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(12, 13))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
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 = @"
|
||||
[Fact]
|
||||
public async void Calling_start_on_non_live_renderable_has_no_warning()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class Program
|
||||
@@ -72,9 +68,8 @@ class Program
|
||||
static void Start() => AnsiConsole.WriteLine(""Starting..."");
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoConcurrentLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<NoConcurrentLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Xunit;
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Analyzers;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Analyzers
|
||||
public class NoPromptsDuringLiveRenderablesTests
|
||||
{
|
||||
public class NoPromptsDuringLiveRenderablesTests
|
||||
{
|
||||
private static readonly DiagnosticResult _expectedDiagnostics = new(
|
||||
Descriptors.S1021_AvoidPromptCallsDuringLiveRenderables.Id,
|
||||
DiagnosticSeverity.Warning);
|
||||
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 = @"
|
||||
[Fact]
|
||||
public async Task Prompt_out_of_progress_does_not_warn()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -25,15 +20,15 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoPromptsDuringLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<NoPromptsDuringLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Instance_variables_warn()
|
||||
{
|
||||
const string Source = @"
|
||||
[Fact]
|
||||
public async Task Instance_variables_warn()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -49,15 +44,15 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoPromptsDuringLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(12, 26))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<NoPromptsDuringLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(12, 26))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Prompt_in_progress_warns()
|
||||
{
|
||||
const string Source = @"
|
||||
[Fact]
|
||||
public async Task Prompt_in_progress_warns()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -71,15 +66,15 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoPromptsDuringLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(10, 13))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
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 = @"
|
||||
[Fact]
|
||||
public async Task Can_call_other_methods_from_within_renderables()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class Program
|
||||
@@ -96,9 +91,8 @@ class Program
|
||||
static string Confirm() => string.Empty;
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<NoPromptsDuringLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<NoPromptsDuringLiveRenderablesAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Xunit;
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Analyzers;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Analyzers
|
||||
public class FavorInstanceAnsiConsoleOverStaticAnalyzerTests
|
||||
{
|
||||
public class FavorInstanceAnsiConsoleOverStaticAnalyzerTests
|
||||
{
|
||||
private static readonly DiagnosticResult _expectedDiagnostics = new(
|
||||
Descriptors.S1010_FavorInstanceAnsiConsoleOverStatic.Id,
|
||||
DiagnosticSeverity.Info);
|
||||
private static readonly DiagnosticResult _expectedDiagnostics = new(
|
||||
Descriptors.S1010_FavorInstanceAnsiConsoleOverStatic.Id,
|
||||
DiagnosticSeverity.Info);
|
||||
|
||||
[Fact]
|
||||
public async void Should_only_warn_within_methods()
|
||||
{
|
||||
const string Source = @"
|
||||
[Fact]
|
||||
public async void Should_only_warn_within_methods()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
internal sealed class Foo
|
||||
@@ -27,15 +23,15 @@ internal sealed class Foo
|
||||
}
|
||||
";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Instance_console_has_no_warnings()
|
||||
{
|
||||
const string Source = @"
|
||||
[Fact]
|
||||
public async void Instance_console_has_no_warnings()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -48,15 +44,15 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Static_console_with_no_instance_variables_has_no_warnings()
|
||||
{
|
||||
const string Source = @"
|
||||
[Fact]
|
||||
public async void Static_console_with_no_instance_variables_has_no_warnings()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -67,15 +63,15 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Console_Write_Has_Warning()
|
||||
{
|
||||
const string Source = @"
|
||||
[Fact]
|
||||
public async void Console_Write_Has_Warning()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -89,9 +85,8 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(11, 9))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(11, 9))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Xunit;
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Analyzers;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Analyzers
|
||||
public class UseSpectreInsteadOfSystemConsoleAnalyzerTests
|
||||
{
|
||||
public class UseSpectreInsteadOfSystemConsoleAnalyzerTests
|
||||
{
|
||||
private static readonly DiagnosticResult _expectedDiagnostics = new(
|
||||
Descriptors.S1000_UseAnsiConsoleOverSystemConsole.Id,
|
||||
DiagnosticSeverity.Warning);
|
||||
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 = @"
|
||||
[Fact]
|
||||
public async void Non_configured_SystemConsole_methods_report_no_warnings()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
|
||||
class TestClass {
|
||||
@@ -23,15 +19,15 @@ class TestClass {
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Console_Write_Has_Warning()
|
||||
{
|
||||
const string Source = @"
|
||||
[Fact]
|
||||
public async void Console_Write_Has_Warning()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
|
||||
class TestClass {
|
||||
@@ -41,15 +37,15 @@ class TestClass {
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(7, 9))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(7, 9))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Console_WriteLine_Has_Warning()
|
||||
{
|
||||
const string Source = @"
|
||||
[Fact]
|
||||
public async void Console_WriteLine_Has_Warning()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
|
||||
class TestClass
|
||||
@@ -59,9 +55,8 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(7, 9))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyAnalyzerAsync(Source, _expectedDiagnostics.WithLocation(7, 9))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Xunit;
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Fixes;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Fixes
|
||||
public class UseInstanceOfStaticAnsiConsoleTests
|
||||
{
|
||||
public class UseInstanceOfStaticAnsiConsoleTests
|
||||
{
|
||||
private static readonly DiagnosticResult _expectedDiagnostic = new(
|
||||
Descriptors.S1010_FavorInstanceAnsiConsoleOverStatic.Id,
|
||||
DiagnosticSeverity.Info);
|
||||
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 = @"
|
||||
[Fact]
|
||||
public async Task Static_call_replaced_with_field_call()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -28,7 +23,7 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
const string FixedSource = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -42,15 +37,15 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(11, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(11, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Static_call_replaced_with_field_call_Should_Preserve_Trivia()
|
||||
{
|
||||
const string Source = @"
|
||||
[Fact]
|
||||
public async Task Static_call_replaced_with_field_call_Should_Preserve_Trivia()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -66,7 +61,7 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
const string FixedSource = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -82,15 +77,15 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(12, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(12, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Static_call_replaced_with_parameter_call()
|
||||
{
|
||||
const string Source = @"
|
||||
[Fact]
|
||||
public async Task Static_call_replaced_with_parameter_call()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -101,7 +96,7 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
const string FixedSource = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -112,15 +107,15 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(8, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
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 = @"
|
||||
[Fact]
|
||||
public async Task Static_call_replaced_with_static_field_if_valid()
|
||||
{
|
||||
const string Source = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -134,7 +129,7 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
const string FixedSource = @"
|
||||
using Spectre.Console;
|
||||
|
||||
class TestClass
|
||||
@@ -148,9 +143,8 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(11, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(11, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Xunit;
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Fixes;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests.Unit.Fixes
|
||||
public class UseSpectreInsteadOfSystemConsoleFixTests
|
||||
{
|
||||
public class UseSpectreInsteadOfSystemConsoleFixTests
|
||||
{
|
||||
private static readonly DiagnosticResult _expectedDiagnostic = new(
|
||||
Descriptors.S1000_UseAnsiConsoleOverSystemConsole.Id,
|
||||
DiagnosticSeverity.Warning);
|
||||
private static readonly DiagnosticResult _expectedDiagnostic = new(
|
||||
Descriptors.S1000_UseAnsiConsoleOverSystemConsole.Id,
|
||||
DiagnosticSeverity.Warning);
|
||||
|
||||
[Fact]
|
||||
public async Task SystemConsole_replaced_with_AnsiConsole()
|
||||
{
|
||||
const string Source = @"
|
||||
[Fact]
|
||||
public async Task SystemConsole_replaced_with_AnsiConsole()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
|
||||
class TestClass
|
||||
@@ -25,7 +20,7 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
const string FixedSource = @"
|
||||
using System;
|
||||
using Spectre.Console;
|
||||
|
||||
@@ -37,15 +32,15 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(8, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(8, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SystemConsole_replaced_with_imported_AnsiConsole()
|
||||
{
|
||||
const string Source = @"
|
||||
[Fact]
|
||||
public async Task SystemConsole_replaced_with_imported_AnsiConsole()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
|
||||
class TestClass
|
||||
@@ -56,7 +51,7 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
const string FixedSource = @"
|
||||
using System;
|
||||
using Spectre.Console;
|
||||
|
||||
@@ -68,15 +63,15 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(8, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(8, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SystemConsole_replaced_with_field_AnsiConsole()
|
||||
{
|
||||
const string Source = @"
|
||||
[Fact]
|
||||
public async Task SystemConsole_replaced_with_field_AnsiConsole()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
using Spectre.Console;
|
||||
|
||||
@@ -90,7 +85,7 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
const string FixedSource = @"
|
||||
using System;
|
||||
using Spectre.Console;
|
||||
|
||||
@@ -104,15 +99,15 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(11, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
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 = @"
|
||||
[Fact]
|
||||
public async Task SystemConsole_replaced_with_static_field_AnsiConsole()
|
||||
{
|
||||
const string Source = @"
|
||||
using System;
|
||||
using Spectre.Console;
|
||||
|
||||
@@ -126,7 +121,7 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
const string FixedSource = @"
|
||||
const string FixedSource = @"
|
||||
using System;
|
||||
using Spectre.Console;
|
||||
|
||||
@@ -140,9 +135,8 @@ class TestClass
|
||||
}
|
||||
}";
|
||||
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(11, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
await SpectreAnalyzerVerifier<UseSpectreInsteadOfSystemConsoleAnalyzer>
|
||||
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(11, 9), FixedSource)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user