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

Refactor CookieCrumble to abstract test frameworks #6537

Merged
7 changes: 7 additions & 0 deletions src/CookieCrumble/CookieCrumble.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CookieCrumble.Fusion", "src
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CookieCrumble.HotChocolate", "src\CookieCrumble.HotChocolate\CookieCrumble.HotChocolate.csproj", "{9B57E4BC-E62A-4B7B-94FB-462C5F92D35B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CookieCrumble.Xunit", "src\CookieCrumble.Xunit\CookieCrumble.Xunit.csproj", "{CEE25A68-69B5-4CFD-9C35-E82736B1E205}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{EB8F1D90-60D6-48FA-9744-D4180A0E4AC0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CookieCrumble.Tests", "test\CookieCrumble.Tests\CookieCrumble.Tests.csproj", "{844E7501-7ED6-4548-8E99-D8E50D4F39A4}"
Expand All @@ -36,6 +38,10 @@ Global
{9B57E4BC-E62A-4B7B-94FB-462C5F92D35B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B57E4BC-E62A-4B7B-94FB-462C5F92D35B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B57E4BC-E62A-4B7B-94FB-462C5F92D35B}.Release|Any CPU.Build.0 = Release|Any CPU
{CEE25A68-69B5-4CFD-9C35-E82736B1E205}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CEE25A68-69B5-4CFD-9C35-E82736B1E205}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CEE25A68-69B5-4CFD-9C35-E82736B1E205}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CEE25A68-69B5-4CFD-9C35-E82736B1E205}.Release|Any CPU.Build.0 = Release|Any CPU
{844E7501-7ED6-4548-8E99-D8E50D4F39A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{844E7501-7ED6-4548-8E99-D8E50D4F39A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{844E7501-7ED6-4548-8E99-D8E50D4F39A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -45,6 +51,7 @@ Global
{8E71FA9B-8352-4675-A9B4-A934E40AF9E0} = {2465C122-714C-4D0A-A24D-D9C22A25D73A}
{E6862862-986B-4A67-8703-49C70051DBDC} = {2465C122-714C-4D0A-A24D-D9C22A25D73A}
{9B57E4BC-E62A-4B7B-94FB-462C5F92D35B} = {2465C122-714C-4D0A-A24D-D9C22A25D73A}
{CEE25A68-69B5-4CFD-9C35-E82736B1E205} = {2465C122-714C-4D0A-A24D-D9C22A25D73A}
{844E7501-7ED6-4548-8E99-D8E50D4F39A4} = {EB8F1D90-60D6-48FA-9744-D4180A0E4AC0}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Reflection;
using Xunit.Sdk;

namespace CookieCrumble.Attributes;
namespace CookieCrumble.Xunit.Attributes;

/// <summary>
/// Apply this attribute to your test method to replace the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../CookieCrumble/CookieCrumble.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CookieCrumble.Xunit;

public static class CookieCrumbleXunit
{
public static void Initialize()
{
Snapshot.RegisterTestFramework(new XunitFramework());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Xunit.Abstractions;
using Xunit.Sdk;

namespace CookieCrumble;
namespace CookieCrumble.Xunit;

[XunitTestCaseDiscoverer("LocalFactDiscoverer", "YourTestAssemblyName")]
public class LocalFactAttribute : FactAttribute;
Expand Down
22 changes: 22 additions & 0 deletions src/CookieCrumble/src/CookieCrumble.Xunit/XunitFramework.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Reflection;
using Xunit;
using Xunit.Sdk;

namespace CookieCrumble.Xunit;

public class XunitFramework : ITestFramework
{
public bool IsValidTestMethod(MemberInfo? method)
=> IsFactTestMethod(method) || IsTheoryTestMethod(method);

private static bool IsFactTestMethod(MemberInfo? method)
=> method?.GetCustomAttributes(typeof(FactAttribute)).Any() ?? false;

private static bool IsTheoryTestMethod(MemberInfo? method)
=> method?.GetCustomAttributes(typeof(TheoryAttribute)).Any() ?? false;

public void ThrowTestException(string message)
{
throw new XunitException(message);
}
}
1 change: 0 additions & 1 deletion src/CookieCrumble/src/CookieCrumble/CookieCrumble.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<PackageReference Include="DiffPlex" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Xunit" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions src/CookieCrumble/src/CookieCrumble/ITestFramework.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Reflection;

namespace CookieCrumble;

public interface ITestFramework
{
bool IsValidTestMethod(MemberInfo? method);

void ThrowTestException(string message);
}
64 changes: 34 additions & 30 deletions src/CookieCrumble/src/CookieCrumble/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Text;
using CookieCrumble.Formatters;
using DiffPlex.DiffBuilder;
using Xunit;
using static System.Collections.Immutable.ImmutableStack;
using static System.IO.Path;
using ChangeType = DiffPlex.DiffBuilder.Model.ChangeType;
Expand All @@ -28,6 +27,7 @@ public class Snapshot
});
private static readonly JsonSnapshotValueFormatter _defaultFormatter = new();

private static ITestFramework _testFramework = null!;
private readonly List<ISnapshotSegment> _segments = [];
private readonly string _title;
private readonly string _fileName;
Expand All @@ -36,6 +36,11 @@ public class Snapshot

public Snapshot(string? postFix = null, string? extension = null)
{
if (_testFramework is null)
{
throw new Exception("Please initialize a test framework before using Snapshot");
}

var frames = new StackTrace(true).GetFrames();
_title = CreateMarkdownTitle(frames);
_fileName = CreateFileName(frames);
Expand Down Expand Up @@ -88,6 +93,19 @@ public static void Match(
snapshot.Match();
}

public static void RegisterTestFramework(ITestFramework testFramework)
{
if (testFramework is null)
{
throw new ArgumentNullException(nameof(testFramework));
}

lock (_sync)
{
_testFramework = testFramework;
}
}

public static void RegisterFormatter(
ISnapshotValueFormatter formatter)
{
Expand Down Expand Up @@ -240,7 +258,7 @@ public async ValueTask MatchAsync(CancellationToken cancellationToken = default)
EnsureDirectoryExists(mismatchFile);
await using var stream = File.Create(mismatchFile);
await stream.WriteAsync(writer.WrittenMemory, cancellationToken);
throw new Xunit.Sdk.XunitException(diff);
_testFramework.ThrowTestException(diff);
}
}
}
Expand Down Expand Up @@ -271,7 +289,7 @@ public void Match()
EnsureDirectoryExists(mismatchFile);
using var stream = File.Create(mismatchFile);
stream.Write(writer.WrittenSpan);
throw new Xunit.Sdk.XunitException(diff);
_testFramework.ThrowTestException(diff);
}
}
}
Expand Down Expand Up @@ -309,7 +327,7 @@ public async ValueTask MatchMarkdownAsync(CancellationToken cancellationToken =
EnsureDirectoryExists(mismatchFile);
await using var stream = File.Create(mismatchFile);
await stream.WriteAsync(writer.WrittenMemory, cancellationToken);
throw new Xunit.Sdk.XunitException(diff);
_testFramework.ThrowTestException(diff);
}
}

Expand Down Expand Up @@ -346,7 +364,7 @@ public void MatchMarkdown()
EnsureDirectoryExists(mismatchFile);
using var stream = File.Create(mismatchFile);
stream.Write(writer.WrittenSpan);
throw new Xunit.Sdk.XunitException(diff);
_testFramework.ThrowTestException(diff);
}
}

Expand All @@ -359,7 +377,7 @@ public void MatchInline(string expected)

if (!MatchSnapshot(expected, after, true, out var diff))
{
throw new Xunit.Sdk.XunitException(diff);
_testFramework.ThrowTestException(diff);
}
}

Expand Down Expand Up @@ -621,18 +639,18 @@ private static string CreateFileName(StackFrame[] frames)

if (method is not null &&
!string.IsNullOrEmpty(fileName) &&
IsXunitTestMethod(method))
_testFramework.IsValidTestMethod(method))
{
return Combine(GetDirectoryName(fileName)!, method.ToName());
}

var asyncMethod = EvaluateAsynchronousMethodBase(method);
method = EvaluateAsynchronousMethodBase(method);

if (asyncMethod is not null &&
if (method is not null &&
!string.IsNullOrEmpty(fileName) &&
IsXunitTestMethod(asyncMethod))
_testFramework.IsValidTestMethod(method))
{
return Combine(GetDirectoryName(fileName)!, asyncMethod.ToName());
return Combine(GetDirectoryName(fileName)!, method.ToName());
}
}

Expand All @@ -654,18 +672,18 @@ private static string CreateMarkdownTitle(StackFrame[] frames)

if (method is not null &&
!string.IsNullOrEmpty(fileName) &&
IsXunitTestMethod(method))
_testFramework.IsValidTestMethod(method))
{
return method.Name;
}

var asyncMethod = EvaluateAsynchronousMethodBase(method);
method = EvaluateAsynchronousMethodBase(method);

if (asyncMethod is not null &&
if (method is not null &&
!string.IsNullOrEmpty(fileName) &&
IsXunitTestMethod(asyncMethod))
_testFramework.IsValidTestMethod(method))
{
return asyncMethod.Name;
return method.Name;
}
}

Expand Down Expand Up @@ -701,20 +719,6 @@ from methodInfo in classDeclaringType.GetMethods()
return actualMethodInfo;
}

private static bool IsXunitTestMethod(MemberInfo? method)
{
var isFactTest = IsFactTestMethod(method);
var isTheoryTest = IsTheoryTestMethod(method);

return isFactTest || isTheoryTest;
}

private static bool IsFactTestMethod(MemberInfo? method)
=> method?.GetCustomAttributes(typeof(FactAttribute)).Any() ?? false;

private static bool IsTheoryTestMethod(MemberInfo? method)
=> method?.GetCustomAttributes(typeof(TheoryAttribute)).Any() ?? false;

private readonly struct SnapshotSegment(string? name, object? value, ISnapshotValueFormatter formatter)
: ISnapshotSegment
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</ItemGroup>

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

</Project>
6 changes: 6 additions & 0 deletions src/CookieCrumble/test/CookieCrumble.Tests/SnapshotTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using System.Buffers;
using System.Text;
using CookieCrumble.Formatters;
using CookieCrumble.Xunit;

namespace CookieCrumble;

public class SnapshotTests
{
static SnapshotTests()
{
Snapshot.RegisterTestFramework(new XunitFramework());
}

[Fact]
public void MatchSnapshot()
{
Expand Down
12 changes: 12 additions & 0 deletions src/GreenDonut/test/Core.Tests/ModuleInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Runtime.CompilerServices;

namespace GreenDonut;

internal static class ModuleInitializer
{
[ModuleInitializer]
public static void Initialize()
{
CookieCrumbleXunit.Initialize();
}
}
3 changes: 2 additions & 1 deletion src/GreenDonut/test/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

<ItemGroup>
<Using Include="CookieCrumble" />
<Using Include="CookieCrumble.Xunit" />
</ItemGroup>

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

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal static class ModuleInitializer
[ModuleInitializer]
public static void Initialize()
{
CookieCrumbleXunit.Initialize();
CookieCrumbleHotChocolate.Initialize();
}
}
2 changes: 2 additions & 0 deletions src/HotChocolate/ApolloFederation/test/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
<ItemGroup>
<Using Include="CookieCrumble" />
<Using Include="CookieCrumble.HotChocolate" />
<Using Include="CookieCrumble.Xunit" />
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\CookieCrumble\src\CookieCrumble.HotChocolate\CookieCrumble.HotChocolate.csproj" />
<ProjectReference Include="..\..\..\..\CookieCrumble\src\CookieCrumble.Xunit\CookieCrumble.Xunit.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal static class ModuleInitializer
[ModuleInitializer]
public static void Initialize()
{
CookieCrumbleXunit.Initialize();
CookieCrumbleHotChocolate.Initialize();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Runtime.CompilerServices;

namespace HotChocolate.AspNetCore.CommandLine;

internal static class ModuleInitializer
{
[ModuleInitializer]
public static void Initialize()
{
CookieCrumbleXunit.Initialize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal static class ModuleInitializer
[ModuleInitializer]
public static void Initialize()
{
CookieCrumbleXunit.Initialize();
CookieCrumbleHotChocolate.Initialize();
}
}
2 changes: 2 additions & 0 deletions src/HotChocolate/AspNetCore/test/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
<ItemGroup>
<Using Include="CookieCrumble" />
<Using Include="CookieCrumble.HotChocolate" />
<Using Include="CookieCrumble.Xunit" />
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\CookieCrumble\src\CookieCrumble.HotChocolate\CookieCrumble.HotChocolate.csproj" />
<ProjectReference Include="..\..\..\..\CookieCrumble\src\CookieCrumble.Xunit\CookieCrumble.Xunit.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal static class ModuleInitializer
[ModuleInitializer]
public static void Initialize()
{
CookieCrumbleXunit.Initialize();
CookieCrumbleHotChocolate.Initialize();
}
}
Loading
Loading