-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnapshotAssert.cs
83 lines (75 loc) · 4.25 KB
/
SnapshotAssert.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
namespace AMWD.Protocols.Modbus.Tests
{
// ================================================================================================================================ //
// Source: https://git.am-wd.de/am-wd/common/-/blob/fb26e441a48214aaae72003c4a5ac33d5c7b929a/src/AMWD.Common.Test/SnapshotAssert.cs //
// ================================================================================================================================ //
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal 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 optional message to display if the assertion fails.</param>
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically on compile time).</param>
/// <param name="callerMemberName">The name of the calling method (filled automatically on compile time).</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"); // 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 optional message to display if the assertion fails.</param>
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically on compile time).</param>
/// <param name="callerMemberName">The name of the calling method (filled automatically on compile time).</param>
public static void AreEqual(byte[] actual, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
=> AreEqual(actual, null, 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 you can specify which sequences of bytes should be excluded from the comparison.
/// </remarks>
/// <param name="actual">The current aggregated content bytes.</param>
/// <param name="excludedSequences">The excluded sequences.</param>
/// <param name="message">An optional message to display if the assertion fails.</param>
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically on compile time).</param>
/// <param name="callerMemberName">The name of the calling method (filled automatically on compile time).</param>
public static void AreEqual(byte[] actual, List<(int Start, int Length)> excludedSequences = null, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
{
string callerDirectory = Path.GetDirectoryName(callerFilePath);
string callerFileName = Path.GetFileNameWithoutExtension(callerFilePath);
string snapshotDirectory = Path.Combine(callerDirectory, "Snapshots", callerFileName);
string snapshotFilePath = Path.Combine(snapshotDirectory, $"{callerMemberName}.snap.bin");
if (File.Exists(snapshotFilePath))
{
byte[] expected = File.ReadAllBytes(snapshotFilePath);
if (actual.Length != expected.Length)
Assert.Fail(message);
for (int i = 0; i < actual.Length; i++)
{
if (excludedSequences?.Any(s => s.Start <= i && i < s.Start + s.Length) == true)
continue;
if (actual[i] != expected[i])
Assert.Fail(message);
}
}
else
{
if (!Directory.Exists(snapshotDirectory))
Directory.CreateDirectory(snapshotDirectory);
File.WriteAllBytes(snapshotFilePath, actual);
}
}
}
}