Adding SnapshotAssert
This commit is contained in:
87
AMWD.Common.Test/SnapshotAssert.cs
Normal file
87
AMWD.Common.Test/SnapshotAssert.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace AMWD.Common.Test
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements a snapshot comparison for content aggregation (e.g. files).
|
||||
/// </summary>
|
||||
public sealed class SnapshotAssert
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests whether the specified string is equal to the saved snapshot.
|
||||
/// </summary>
|
||||
/// <param name="actual">The current aggregated content string.</param>
|
||||
/// <param name="message">An error message.</param>
|
||||
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically by the compiler service).</param>
|
||||
/// <param name="callerMemberName">The name of the calling method (filled automatically by the compiler service).</param>
|
||||
public static void AreEqual(string actual, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
|
||||
{
|
||||
string cleanLineEnding = actual
|
||||
.Replace("\r\n", "\n") // Windows
|
||||
.Replace("\r", "\n"); // old MacOS
|
||||
AreEqual(Encoding.UTF8.GetBytes(cleanLineEnding), message, callerFilePath, callerMemberName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the specified byte array is equal to the saved snapshot.
|
||||
/// </summary>
|
||||
/// <param name="actual">The current aggregated content bytes.</param>
|
||||
/// <param name="message">An error message.</param>
|
||||
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically by the compiler service).</param>
|
||||
/// <param name="callerMemberName">The name of the calling method (filled automatically by the compiler service).</param>
|
||||
public static void AreEqual(byte[] actual, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
|
||||
=> AreEqual(actual, 0, -1, message, callerFilePath, callerMemberName);
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the specified byte array is equal to the saved snapshot.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The past has shown, that e.g. wkhtmltopdf prints the current timestamp at the beginning of the PDF file.
|
||||
/// Therefore only a specific part of that file can be asserted to be equal.
|
||||
/// </remarks>
|
||||
/// <param name="actual">The current aggregated content bytes.</param>
|
||||
/// <param name="firstByteIndex">The first byte to compare.</param>
|
||||
/// <param name="lastByteIndex">The last byte to compare.</param>
|
||||
/// <param name="message">An error message.</param>
|
||||
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically by the compiler service).</param>
|
||||
/// <param name="callerMemberName">The name of the calling method (filled automatically by the compiler service).</param>
|
||||
public static void AreEqual(byte[] actual, int firstByteIndex, int lastByteIndex, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
|
||||
{
|
||||
string callerDir = Path.GetDirectoryName(callerFilePath);
|
||||
string callerFile = Path.GetFileNameWithoutExtension(callerFilePath);
|
||||
|
||||
string snapshotDir = Path.Combine(callerDir, "Snapshots", callerFile);
|
||||
string snapshotFile = Path.Combine(snapshotDir, $"{callerMemberName}.snap");
|
||||
|
||||
if (File.Exists(snapshotFile))
|
||||
{
|
||||
byte[] expected = File.ReadAllBytes(snapshotFile);
|
||||
|
||||
var actualBytes = actual.Skip(firstByteIndex);
|
||||
var expectedBytes = expected.Skip(firstByteIndex);
|
||||
|
||||
if (lastByteIndex > firstByteIndex)
|
||||
{
|
||||
actualBytes = actualBytes.Take(lastByteIndex - firstByteIndex);
|
||||
expectedBytes = expectedBytes.Take(lastByteIndex - firstByteIndex);
|
||||
}
|
||||
|
||||
if (message == null)
|
||||
CollectionAssert.AreEqual(expectedBytes.ToArray(), actualBytes.ToArray());
|
||||
else
|
||||
CollectionAssert.AreEqual(expectedBytes.ToArray(), actualBytes.ToArray(), message);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!Directory.Exists(snapshotDir))
|
||||
Directory.CreateDirectory(snapshotDir);
|
||||
|
||||
File.WriteAllBytes(snapshotFile, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user