Skip to content

Commit

Permalink
add unittest of analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jun 2, 2021
1 parent f3b4591 commit 0edd54d
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 9 deletions.
13 changes: 11 additions & 2 deletions MessagePipe.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{7107
.github\workflows\build-debug.yml = .github\workflows\build-debug.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagePipe.Analyzer", "src\MessagePipe.Analyzer\MessagePipe.Analyzer.csproj", "{CB2E0099-5D84-4E14-9699-3AF5B9237DF4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagePipe.Analyzer", "src\MessagePipe.Analyzer\MessagePipe.Analyzer.csproj", "{CB2E0099-5D84-4E14-9699-3AF5B9237DF4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagePipe.AnalyzerTestApp", "sandbox\MessagePipe.AnalyzerTestApp\MessagePipe.AnalyzerTestApp.csproj", "{3F0CF7FC-D764-40C0-BA3A-0302DB2F205F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagePipe.AnalyzerTestApp", "sandbox\MessagePipe.AnalyzerTestApp\MessagePipe.AnalyzerTestApp.csproj", "{3F0CF7FC-D764-40C0-BA3A-0302DB2F205F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagePipe.Analyzer.Tests", "tests\MessagePipe.Analyzer.Tests\MessagePipe.Analyzer.Tests.csproj", "{64A0C890-301B-49D9-95C8-8CAFB53DCFDC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -103,6 +105,12 @@ Global
{3F0CF7FC-D764-40C0-BA3A-0302DB2F205F}.Release|Any CPU.Build.0 = Release|Any CPU
{3F0CF7FC-D764-40C0-BA3A-0302DB2F205F}.WinBenchmark|Any CPU.ActiveCfg = Debug|Any CPU
{3F0CF7FC-D764-40C0-BA3A-0302DB2F205F}.WinBenchmark|Any CPU.Build.0 = Debug|Any CPU
{64A0C890-301B-49D9-95C8-8CAFB53DCFDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{64A0C890-301B-49D9-95C8-8CAFB53DCFDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64A0C890-301B-49D9-95C8-8CAFB53DCFDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64A0C890-301B-49D9-95C8-8CAFB53DCFDC}.Release|Any CPU.Build.0 = Release|Any CPU
{64A0C890-301B-49D9-95C8-8CAFB53DCFDC}.WinBenchmark|Any CPU.ActiveCfg = Debug|Any CPU
{64A0C890-301B-49D9-95C8-8CAFB53DCFDC}.WinBenchmark|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -118,6 +126,7 @@ Global
{71072B57-914A-4CA8-915A-64E832DA6E1F} = {33ADB827-BA73-4B6A-AA57-13C74AB9183D}
{CB2E0099-5D84-4E14-9699-3AF5B9237DF4} = {381F6F79-110B-4CE4-9A49-583046D8C164}
{3F0CF7FC-D764-40C0-BA3A-0302DB2F205F} = {9813BFC3-7860-4697-A3AF-118BDF710BD0}
{64A0C890-301B-49D9-95C8-8CAFB53DCFDC} = {36546FD6-866F-4809-AFCE-87F7F4201361}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {138B7AA8-E4C0-46A3-A48B-0D373CAC365D}
Expand Down
4 changes: 3 additions & 1 deletion sandbox/MessagePipe.Sandbox.ConsoleApp/ChangedValueFilter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Extensions.Logging;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
9 changes: 6 additions & 3 deletions sandbox/MessagePipe.Sandbox.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using ConsoleAppFramework;
#pragma warning disable CS8603 // Possible null reference return.
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

using ConsoleAppFramework;
using MessagePipe.Sandbox.ConsoleApp;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -124,8 +127,8 @@ await Host.CreateDefaultBuilder()
IServiceScopeFactory scopeF;
MessagePipeDiagnosticsInfo diagnosticsInfo;

IServiceProvider provider;

IServiceProvider provider;

public Program(
IPublisher<string, MyMessage> publisher,
ISubscriber<string, MyMessage> subscriber,
Expand Down
68 changes: 68 additions & 0 deletions tests/MessagePipe.Analyzer.Tests/AnalyzerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;
using System.Collections.Immutable;
using System.Threading.Tasks;
using Xunit;

namespace MessagePipe.Analyzer.Tests
{
public class AnalyzerTest
{
static async Task VerifyAsync(string testCode, int startLine, int startColumn, int endLine, int endColumn)
{

await new CSharpAnalyzerTest<MessagePipeAnalyzer, XUnitVerifier>
{
ReferenceAssemblies = ReferenceAssemblies.Default.WithPackages(ImmutableArray.Create(new PackageIdentity("MessagePipe", "1.4.0"))),
ExpectedDiagnostics = { new DiagnosticResult("MPA001", DiagnosticSeverity.Error).WithSpan(startLine, startColumn, endLine, endColumn) },
TestCode = testCode
}.RunAsync();
}

static async Task VerifyNoErrorAsync(string testCode)
{

await new CSharpAnalyzerTest<MessagePipeAnalyzer, XUnitVerifier>
{
ReferenceAssemblies = ReferenceAssemblies.Default.WithPackages(ImmutableArray.Create(new PackageIdentity("MessagePipe", "1.4.0"))),
ExpectedDiagnostics = { },
TestCode = testCode
}.RunAsync();
}

[Fact]
public async Task SimpleTest()
{
var testCode = @"using MessagePipe;
class C
{
public void M(ISubscriber<int> subscriber)
{
subscriber.Subscribe(x => { });
}
}";

await VerifyAsync(testCode, 7, 9, 7, 39);
}

[Fact]
public async Task NoErrorReport()
{
var testCode = @"using MessagePipe;
class C
{
public void M(ISubscriber<int> subscriber)
{
var d = subscriber.Subscribe(x => { });
}
}";

await VerifyNoErrorAsync(testCode);
}
}

}
30 changes: 30 additions & 0 deletions tests/MessagePipe.Analyzer.Tests/MessagePipe.Analyzer.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.0" />

<PackageReference Include="FluentAssertions" Version="5.10.3" />
<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>

<ProjectReference Include="..\..\src\MessagePipe.Analyzer\MessagePipe.Analyzer.csproj" />

<ProjectReference Include="..\..\src\MessagePipe\MessagePipe.csproj" />
</ItemGroup>

</Project>
4 changes: 3 additions & 1 deletion tests/MessagePipe.Benchmark/BenchmarkDotNetRun.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BenchmarkDotNet.Attributes;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

using BenchmarkDotNet.Attributes;
using Easy.MessageHub;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
Expand Down
4 changes: 3 additions & 1 deletion tests/MessagePipe.Benchmark/PublishOps.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Easy.MessageHub;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

using Easy.MessageHub;

using MediatR;
using Microsoft.Extensions.DependencyInjection;
Expand Down
4 changes: 3 additions & 1 deletion tests/MessagePipe.Tests/ArrayUtilTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using FluentAssertions;
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.

using FluentAssertions;
using MessagePipe.Internal;
using System;
using System.Collections.Generic;
Expand Down

0 comments on commit 0edd54d

Please sign in to comment.