Skip to content
Open
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
12 changes: 11 additions & 1 deletion src/Snapshooter.MSTest/MSTestAssert.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Snapshooter.Core;
using MsTest = Microsoft.VisualStudio.TestTools.UnitTesting;

Expand All @@ -16,7 +17,16 @@ public class MSTestAssert : IAssert
/// <param name="actualSnapshot">The actual snapshot.</param>
public void Assert(string expectedSnapshot, string actualSnapshot)
{
MsTest.Assert.AreEqual(expectedSnapshot, actualSnapshot);
try
{
MsTest.Assert.AreEqual(expectedSnapshot, actualSnapshot);
}
catch (AssertFailedException ex)
{
string displayableDiff = DiffGenerator.GenerateDiff(expectedSnapshot, actualSnapshot);

throw new AssertFailedException($"Assert.AreEqual failed. {displayableDiff}", ex);
}
}
}
}
11 changes: 9 additions & 2 deletions src/Snapshooter.NUnit/NUnitAssert.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Diagnostics;
using NUnit.Framework;
using Snapshooter.Core;
using NAssert = NUnit.Framework.Assert;
Expand All @@ -18,7 +17,15 @@ public class NUnitAssert : IAssert
/// <param name="actualSnapshot">The actual snapshot.</param>
public void Assert(string expectedSnapshot, string actualSnapshot)
{
NAssert.That(actualSnapshot, Is.EqualTo(expectedSnapshot));
try
{
NAssert.That(actualSnapshot, Is.EqualTo(expectedSnapshot));
} catch (AssertionException ex)
{
string displayableDiff = DiffGenerator.GenerateDiff(expectedSnapshot, actualSnapshot);

throw new AssertionException($"Assert.That failed. {displayableDiff}", ex);
}
}
}
}
13 changes: 11 additions & 2 deletions src/Snapshooter.Xunit/XunitAssert.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Snapshooter.Core;
using Snapshooter.Core;
using Xunit.Sdk;
using x = Xunit;

namespace Snapshooter.Xunit
Expand All @@ -16,7 +17,15 @@ public class XunitAssert : IAssert
/// <param name="actualSnapshot">The actual snapshot.</param>
public void Assert(string expectedSnapshot, string actualSnapshot)
{
x.Assert.Equal(expectedSnapshot, actualSnapshot);
try
{
x.Assert.Equal(expectedSnapshot, actualSnapshot);
}
catch (EqualException ex)
{
string displayableDiff = DiffGenerator.GenerateDiff(expectedSnapshot, actualSnapshot);
throw new EqualException($"Assert.Equal failed. {displayableDiff}", ex);
}
}
}
}
33 changes: 33 additions & 0 deletions src/Snapshooter/DiffGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Linq;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;

namespace Snapshooter
{
/// <summary>
/// Class for generating a diff between two strings.
/// </summary>
public static class DiffGenerator
{
/// <summary>
/// Generate a displayable diff between two strings.
/// </summary>
/// <param name="expectedSnapshot">The expected snapshot used in the assert.</param>
/// <param name="actualSnapshot">The actual snapshot used in the assert.</param>
/// <returns>A new string showing the added/removed lines between the two snapshots.</returns>
public static string GenerateDiff(string expectedSnapshot, string actualSnapshot)
{
DiffPaneModel diff = InlineDiffBuilder.Diff(expectedSnapshot, actualSnapshot);
string displayableDiff = string.Join(
Environment.NewLine,
diff.Lines.Select(line => line.Type switch
{
ChangeType.Inserted => $"+ {line.Text}",
ChangeType.Deleted => $"- {line.Text}",
_ => line.Text
}));
return displayableDiff;
}
}
}
1 change: 1 addition & 0 deletions src/Snapshooter/Snapshooter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="DiffPlex" Version="1.7.1" />
</ItemGroup>

</Project>
Loading