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:
Patrik Svensson
2021-06-23 22:47:12 +02:00
parent 721d73e9eb
commit c9b178ac96
307 changed files with 114 additions and 84 deletions

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}