Skip to content

Commit

Permalink
Move FluentAssertion reference into its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
TimHolzherr committed Mar 17, 2023
1 parent fa2af95 commit a2a14a6
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using FluentAssertions;
using System;

namespace Snapshooter.NUnit;

public static class FluentSnapshotExtension
{
/// <summary>
/// Creates a json snapshot of the given object and compares it with the
/// already existing snapshot of the test.
/// If no snapshot exists, a new snapshot will be created from the current result
/// and saved under a certain file path, which will shown within the test message.
/// </summary>
/// <param name="currentResult">The object to match.</param>
/// <param name="matchOptions">
/// Additional compare actions, which can be applied during the snapshot comparison
/// </param>
public static void MatchSnapshot<TSubject>(
this TypedAssertions<TSubject> currentResult,
Func<MatchOptions<TSubject>, MatchOptions<TSubject>>? matchOptions = null)
{
var cleanedObject = currentResult.RemoveUnwantedWrappers();

Func<MatchOptions, MatchOptions>? chainedMatchOptions =
matchOptions != null ? m => matchOptions(new MatchOptions<TSubject>(m)) : null;

Snapshot.Match(
cleanedObject,
chainedMatchOptions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(CCResourceProjectProps)" Condition="Exists('$(CCResourceProjectProps)')" />

<PropertyGroup>
<AssemblyName>Snapshooter.NUnit.TypedFluentAssertions</AssemblyName>
<RootNamespace>Snapshooter.NUnit.TypedFluentAssertions</RootNamespace>
<PackageId>Snapshooter.NUnit.TypedFluentAssertions</PackageId>
<Description>
TypedFluentAssertions is an extention to FluentAssertions and Snapshooter.NUnit.
It allows to configure snapshots in a type save and fluent way.
</Description>
<IsTestProject>false</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Snapshooter.NUnit\Snapshooter.NUnit.csproj" />
</ItemGroup>

</Project>
24 changes: 0 additions & 24 deletions src/Snapshooter.NUnit/SnapshotExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using FluentAssertions;

namespace Snapshooter.NUnit
{
Expand Down Expand Up @@ -129,28 +128,5 @@ public static void MatchSnapshot(
Snapshot.Match(cleanedObject, snapshotFullName, matchOptions);
}

/// <summary>
/// Creates a json snapshot of the given object and compares it with the
/// already existing snapshot of the test.
/// If no snapshot exists, a new snapshot will be created from the current result
/// and saved under a certain file path, which will shown within the test message.
/// </summary>
/// <param name="currentResult">The object to match.</param>
/// <param name="matchOptions">
/// Additional compare actions, which can be applied during the snapshot comparison
/// </param>
public static void MatchSnapshot<TSubject>(
this TypedAssertions<TSubject> currentResult,
Func<MatchOptions<TSubject>, MatchOptions<TSubject>>? matchOptions = null)
{
var cleanedObject = currentResult.RemoveUnwantedWrappers();

Func<MatchOptions, MatchOptions>? chainedMatchOptions =
matchOptions != null ? m => matchOptions(new MatchOptions<TSubject>(m)) : null;

Snapshot.Match(
cleanedObject,
chainedMatchOptions);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using FluentAssertions.Primitives;

// Extension of Fluent assertion to not loose type information of asserted object
namespace FluentAssertions;

/// <summary>
/// Contains a number of methods to assert that an <see cref="object"/> is in the expected state.
/// </summary>
public class TypedAssertions<T> : ObjectAssertions<T, TypedAssertions<T>>
{
internal TypedAssertions(T value) : base(value)
{
}
}

/// <summary>
/// Contains extension methods for custom assertions in unit tests.
/// </summary>
public static class AssertionExtensions
{
/// <summary>
/// Returns an <see cref="TypedAssertions{T}"/> object that can be used to assert the
/// current <see cref="object"/>.
/// </summary>
public static TypedAssertions<TSubject> Should<TSubject>(this TSubject actualValue)
{
return new TypedAssertions<TSubject>(actualValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using FluentAssertions;
using System;

namespace Snapshooter.Xunit;

public static class FluentSnapshotExtension
{
/// <summary>
/// Creates a json snapshot of the given object and compares it with the
/// already existing snapshot of the test.
/// If no snapshot exists, a new snapshot will be created from the current result
/// and saved under a certain file path, which will shown within the test message.
/// </summary>
/// <param name="currentResult">The object to match.</param>
/// <param name="matchOptions">
/// Additional compare actions, which can be applied during the snapshot comparison
/// </param>
public static void MatchSnapshot<TSubject>(
this TypedAssertions<TSubject> currentResult,
Func<MatchOptions<TSubject>, MatchOptions<TSubject>>? matchOptions = null)
{
var cleanedObject = currentResult.RemoveUnwantedWrappers();

Func<MatchOptions, MatchOptions>? chainedMatchOptions =
matchOptions != null ? m => matchOptions(new MatchOptions<TSubject>(m)) : null;

Snapshot.Match(
cleanedObject,
chainedMatchOptions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(CCResourceProjectProps)" Condition="Exists('$(CCResourceProjectProps)')" />

<PropertyGroup>
<AssemblyName>Snapshooter.Xunit.TypedFluentAssertions</AssemblyName>
<RootNamespace>Snapshooter.Xunit.TypedFluentAssertions</RootNamespace>
<PackageId>Snapshooter.Xunit.TypedFluentAssertions</PackageId>
<Description>
TypedFluentAssertions is an extention to FluentAssertions and Snapshooter.Xunit.
It allows to configure snapshots in a type save and fluent way.
</Description>
<IsTestProject>false</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.0" />
</ItemGroup>

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

</Project>
24 changes: 0 additions & 24 deletions src/Snapshooter.Xunit/SnapshotExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using FluentAssertions;

namespace Snapshooter.Xunit
{
Expand Down Expand Up @@ -129,28 +128,5 @@ public static void MatchSnapshot(
Snapshot.Match(cleanedObject, snapshotFullName, matchOptions);
}

/// <summary>
/// Creates a json snapshot of the given object and compares it with the
/// already existing snapshot of the test.
/// If no snapshot exists, a new snapshot will be created from the current result
/// and saved under a certain file path, which will shown within the test message.
/// </summary>
/// <param name="currentResult">The object to match.</param>
/// <param name="matchOptions">
/// Additional compare actions, which can be applied during the snapshot comparison
/// </param>
public static void MatchSnapshot<TSubject>(
this TypedAssertions<TSubject> currentResult,
Func<MatchOptions<TSubject>, MatchOptions<TSubject>>? matchOptions = null)
{
var cleanedObject = currentResult.RemoveUnwantedWrappers();

Func<MatchOptions, MatchOptions>? chainedMatchOptions =
matchOptions != null ? m => matchOptions(new MatchOptions<TSubject>(m)) : null;

Snapshot.Match(
cleanedObject,
chainedMatchOptions);
}
}
}
37 changes: 37 additions & 0 deletions src/Snapshooter.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Snapshooter.Tests.Data", ".
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Snapshooter.Xunit.Tests", "..\test\Snapshooter.Xunit.Tests\Snapshooter.Xunit.Tests.csproj", "{3C7A875E-7B9C-45E6-93E1-E952F08758B4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{772D9217-4549-4DCE-ADFC-8AF209E5E9AC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Snapshooter.Xunit.TypedFluentAssertions", "Snapshooter.Xunit.TypedFluentAssertions\Snapshooter.Xunit.TypedFluentAssertions.csproj", "{F3D664A5-1877-4623-ABC2-05D18412CF57}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Snapshooter.NUnit.TypedFluentAssertions", "Snapshooter.NUnit.TypedFluentAssertions\Snapshooter.NUnit.TypedFluentAssertions.csproj", "{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -198,11 +204,40 @@ Global
{3C7A875E-7B9C-45E6-93E1-E952F08758B4}.Release|x64.Build.0 = Release|Any CPU
{3C7A875E-7B9C-45E6-93E1-E952F08758B4}.Release|x86.ActiveCfg = Release|Any CPU
{3C7A875E-7B9C-45E6-93E1-E952F08758B4}.Release|x86.Build.0 = Release|Any CPU
{F3D664A5-1877-4623-ABC2-05D18412CF57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3D664A5-1877-4623-ABC2-05D18412CF57}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3D664A5-1877-4623-ABC2-05D18412CF57}.Debug|x64.ActiveCfg = Debug|Any CPU
{F3D664A5-1877-4623-ABC2-05D18412CF57}.Debug|x64.Build.0 = Debug|Any CPU
{F3D664A5-1877-4623-ABC2-05D18412CF57}.Debug|x86.ActiveCfg = Debug|Any CPU
{F3D664A5-1877-4623-ABC2-05D18412CF57}.Debug|x86.Build.0 = Debug|Any CPU
{F3D664A5-1877-4623-ABC2-05D18412CF57}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3D664A5-1877-4623-ABC2-05D18412CF57}.Release|Any CPU.Build.0 = Release|Any CPU
{F3D664A5-1877-4623-ABC2-05D18412CF57}.Release|x64.ActiveCfg = Release|Any CPU
{F3D664A5-1877-4623-ABC2-05D18412CF57}.Release|x64.Build.0 = Release|Any CPU
{F3D664A5-1877-4623-ABC2-05D18412CF57}.Release|x86.ActiveCfg = Release|Any CPU
{F3D664A5-1877-4623-ABC2-05D18412CF57}.Release|x86.Build.0 = Release|Any CPU
{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19}.Debug|x64.ActiveCfg = Debug|Any CPU
{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19}.Debug|x64.Build.0 = Debug|Any CPU
{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19}.Debug|x86.ActiveCfg = Debug|Any CPU
{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19}.Debug|x86.Build.0 = Debug|Any CPU
{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19}.Release|Any CPU.Build.0 = Release|Any CPU
{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19}.Release|x64.ActiveCfg = Release|Any CPU
{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19}.Release|x64.Build.0 = Release|Any CPU
{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19}.Release|x86.ActiveCfg = Release|Any CPU
{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D888581A-CCA4-485F-AED0-4D7B53D4BE54} = {772D9217-4549-4DCE-ADFC-8AF209E5E9AC}
{CE01C341-C674-4E4A-B490-BBFF354803C9} = {772D9217-4549-4DCE-ADFC-8AF209E5E9AC}
{679B413A-F26D-4F83-8BBF-5747B76567F8} = {772D9217-4549-4DCE-ADFC-8AF209E5E9AC}
{37A500BF-5B82-435F-9CC8-6D9313A7E3CC} = {772D9217-4549-4DCE-ADFC-8AF209E5E9AC}
{7AFA7B30-7F2F-420D-B510-D2880A00F975} = {772D9217-4549-4DCE-ADFC-8AF209E5E9AC}
{5FCC450A-A047-48C7-8B3F-C5E973B5584D} = {45E598C5-3FD9-47E5-89D2-1F24A028F2BB}
{C5859230-A6A7-45E7-993C-79C23527CB73} = {F9DFF684-4ACF-45E4-B23E-E8928DE0C9FE}
{C2727126-A53A-4B13-AB09-559260F294D7} = {F9DFF684-4ACF-45E4-B23E-E8928DE0C9FE}
Expand All @@ -211,6 +246,8 @@ Global
{616F100E-A562-4E17-805A-8755B9D4D1AA} = {F9DFF684-4ACF-45E4-B23E-E8928DE0C9FE}
{A9A09C8D-E9D1-45CC-80F1-3C8DDF8F2600} = {F9DFF684-4ACF-45E4-B23E-E8928DE0C9FE}
{3C7A875E-7B9C-45E6-93E1-E952F08758B4} = {F9DFF684-4ACF-45E4-B23E-E8928DE0C9FE}
{F3D664A5-1877-4623-ABC2-05D18412CF57} = {772D9217-4549-4DCE-ADFC-8AF209E5E9AC}
{C16AB9AB-4F11-4A5A-BDBD-01ECD5D3BF19} = {772D9217-4549-4DCE-ADFC-8AF209E5E9AC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2F64A2AB-ACA2-4E2D-B7E2-B87E93C66A24}
Expand Down
1 change: 0 additions & 1 deletion src/Snapshooter/Snapshooter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Snapshooter.NUnit\Snapshooter.NUnit.csproj" />
<ProjectReference Include="..\..\src\Snapshooter.NUnit.TypedFluentAssertions\Snapshooter.NUnit.TypedFluentAssertions.csproj" />
<ProjectReference Include="..\Snapshooter.Tests.Data\Snapshooter.Tests.Data.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<ProjectReference Include="..\Snapshooter.Tests.Data\Snapshooter.Tests.Data.csproj" />
<ProjectReference Include="..\..\src\Snapshooter.Xunit\Snapshooter.Xunit.csproj" />
<ProjectReference Include="..\..\src\Snapshooter.Xunit.TypedFluentAssertions\Snapshooter.Xunit.TypedFluentAssertions.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit a2a14a6

Please sign in to comment.