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: add snapshot tests #122

Merged
merged 1 commit into from
Dec 7, 2023
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
13 changes: 13 additions & 0 deletions test/Mediator.SourceGenerator.Roslyn38.Tests/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ public static Compilation CreateLibrary(params string[] source)
return compilation;
}

public static Task VerifyGenerator(string source)
{
var compilation = CreateLibrary(source);

var generator = new MediatorGenerator();
var driver = CSharpGeneratorDriver.Create(generator);

var ranDriver = driver.RunGenerators(compilation);
var verify = VerifyXunit.Verifier.Verify(ranDriver);

return verify.ToTask();
}

public static Task<string> SourceFromResourceFile(string file) =>
File.ReadAllTextAsync(Path.Combine("resources", file));
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="7.0.0" />
<PackageReference Include="Verify.DiffPlex" Version="2.3.0" />
<PackageReference Include="Verify.SourceGenerators" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Verify.Xunit" Version="22.1.4" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down Expand Up @@ -43,6 +47,10 @@
<None Include="resources\*.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Compile Remove="_snapshots\**" />
<None Include="_snapshots\*.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand Down
22 changes: 22 additions & 0 deletions test/Mediator.SourceGenerator.Roslyn38.Tests/ModuleInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.IO;
using VerifyTests;
using VerifyXunit;

namespace Mediator.SourceGenerator.Tests;

public static class ModuleInitializer
{
// ModuleInitializer should only be used in apps
#pragma warning disable CA2255
[ModuleInitializer]
#pragma warning restore CA2255
public static void Init()
{
Verifier.DerivePathInfo(
(file, _, type, method) => new(Path.Join(Path.GetDirectoryName(file), "_snapshots"), type.Name, method.Name)
);

VerifySourceGenerators.Initialize();
VerifyDiffPlex.Initialize(VerifyTests.DiffPlex.OutputType.Compact);
}
}
40 changes: 40 additions & 0 deletions test/Mediator.SourceGenerator.Roslyn38.Tests/SnapshotTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Threading.Tasks;
using VerifyXunit;

namespace Mediator.SourceGenerator.Tests;

[UsesVerify]
public sealed class SnapshotTests
{
[Fact]
public async Task Test_SimpleConsole()
{
var source = await Fixture.SourceFromResourceFile("SimpleConsoleProgram.cs");

await Fixture.VerifyGenerator(source);
}

[Fact]
public async Task Test_SimpleEndToEnd()
{
var source = await Fixture.SourceFromResourceFile("SimpleEndToEndProgram.cs");

await Fixture.VerifyGenerator(source);
}

[Fact]
public async Task Test_SimpleStreaming()
{
var source = await Fixture.SourceFromResourceFile("SimpleStreamingProgram.cs");

await Fixture.VerifyGenerator(source);
}

[Fact]
public async Task Test_Multiple_AddMediator_Calls()
{
var source = await Fixture.SourceFromResourceFile("MultipleAddMediatorCalls.cs");

await Fixture.VerifyGenerator(source);
}
}
Loading
Loading