Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: combine SnapshotTests and SamplesTests #130

Merged
merged 3 commits into from
Mar 3, 2024
Merged
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
58 changes: 0 additions & 58 deletions test/Mediator.SourceGenerator.Roslyn38.Tests/SamplesTests.cs

This file was deleted.

9 changes: 7 additions & 2 deletions test/Mediator.SourceGenerator.Roslyn38.Tests/SnapshotTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace Mediator.SourceGenerator.Tests;

using System.Threading.Tasks;
using VerifyXunit;

namespace Mediator.SourceGenerator.Tests;
using Verifier = CSharpSourceGeneratorVerifier<MediatorGenerator>;

[UsesVerify]
public sealed class SnapshotTests
Expand All @@ -11,6 +12,7 @@ public async Task Test_SimpleConsole()
{
var source = await Fixture.SourceFromResourceFile("SimpleConsoleProgram.cs");

await Verifier.VerifySolution(source);
await Fixture.VerifyGenerator(source);
}

Expand All @@ -19,6 +21,7 @@ public async Task Test_SimpleEndToEnd()
{
var source = await Fixture.SourceFromResourceFile("SimpleEndToEndProgram.cs");

await Verifier.VerifySolution(source);
await Fixture.VerifyGenerator(source);
}

Expand All @@ -27,6 +30,7 @@ public async Task Test_SimpleStreaming()
{
var source = await Fixture.SourceFromResourceFile("SimpleStreamingProgram.cs");

await Verifier.VerifySolution(source);
await Fixture.VerifyGenerator(source);
}

Expand All @@ -35,6 +39,7 @@ public async Task Test_Multiple_AddMediator_Calls()
{
var source = await Fixture.SourceFromResourceFile("MultipleAddMediatorCalls.cs");

await Verifier.VerifySolution(source);
await Fixture.VerifyGenerator(source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;

public static class CSharpSourceGeneratorVerifier<TSourceGenerator> where TSourceGenerator : ISourceGenerator, new()
{
public class Test : CSharpSourceGeneratorTest<TSourceGenerator, XUnitVerifier>
public static Task VerifySolution(string source)
{
var test = new Test
{
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck,
TestState = { Sources = { source }, },
};
return test.RunAsync();
}

private class Test : CSharpSourceGeneratorTest<TSourceGenerator, XUnitVerifier>
{
public Test()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Build.Locator" Version="1.4.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.0.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.0.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" PrivateAssets="all" />
Expand Down
95 changes: 95 additions & 0 deletions test/Mediator.SourceGenerator.Roslyn40.Tests/SourceGenVerifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Mediator.SourceGenerator.Tests;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;

public static class CSharpSourceGeneratorVerifier<TSourceGenerator>
where TSourceGenerator : IIncrementalGenerator, new()
{
public static Task VerifySolution(string source)
{
var test = new Test
{
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck,
TestState = { Sources = { source }, },
};
return test.RunAsync();
}

private class Test : CSharpSourceGeneratorTest<EmptySourceGeneratorProvider, XUnitVerifier>
{
public Test()
{
var refs = Fixture.AssemblyReferencesForCodegen.Select(a => a.Location).ToImmutableArray();

var metadataRefs = refs.Select(l => MetadataReference.CreateFromFile(l)).ToImmutableArray();

this.ReferenceAssemblies = Microsoft.CodeAnalysis.Testing.ReferenceAssemblies.Net.Net60.WithAssemblies(
refs
);

this.SolutionTransforms.Add(
(sln, projectId) =>
{
var compilationOptions = ((CSharpCompilationOptions)sln.GetProject(projectId)!.CompilationOptions!)
.WithOutputKind(OutputKind.ConsoleApplication)
.WithUsings(
"System",
"System.Collections.Generic",
"System.IO",
"System.Linq",
"System.Net.Http",
"System.Threading",
"System.Threading.Tasks"
);

sln = sln.AddMetadataReferences(projectId, metadataRefs)
.WithProjectCompilationOptions(projectId, compilationOptions);
return sln;
}
);
}

public TSourceGenerator SourceGenerator => (TSourceGenerator)GetSourceGenerators().Single();

protected override IEnumerable<ISourceGenerator> GetSourceGenerators() =>
new ISourceGenerator[] { new TSourceGenerator().AsSourceGenerator() };

protected override CompilationOptions CreateCompilationOptions()
{
var compilationOptions = base.CreateCompilationOptions();
return compilationOptions
.WithSpecificDiagnosticOptions(
compilationOptions.SpecificDiagnosticOptions.SetItems(GetNullableWarningsFromCompiler())
)
.WithOutputKind(OutputKind.ConsoleApplication);
}

public LanguageVersion LanguageVersion { get; set; } = LanguageVersion.Latest;

private static ImmutableDictionary<string, ReportDiagnostic> GetNullableWarningsFromCompiler()
{
string[] args = { "/warnaserror:nullable" };
var commandLineArguments = CSharpCommandLineParser.Default.Parse(
args,
baseDirectory: Environment.CurrentDirectory,
sdkDirectory: Environment.CurrentDirectory
);
var nullableWarnings = commandLineArguments.CompilationOptions.SpecificDiagnosticOptions;

return nullableWarnings;
}

protected override ParseOptions CreateParseOptions()
{
return ((CSharpParseOptions)base.CreateParseOptions()).WithLanguageVersion(LanguageVersion);
}
}
}
Loading