/// <summary> /// compare two snapshots with each other /// </summary> /// <param name="newshot"></param> /// <param name="savedshot"></param> /// <param name="options">the options</param> /// <returns></returns> public SnapshotResult Compare(Snapshot newshot, Snapshot savedshot, SnapshotOptions options) { if (newshot == null || savedshot == null) { return(SnapshotResult.SnapshotDoesNotExist(savedshot)); } var count = newshot.Count; if (count < savedshot.Count) { count = savedshot.Count; } var comparer = options.Comparer ?? LineCompare.Default; for (var i = 0; i < count; i++) { if (!comparer.Compare(newshot[i], savedshot[i])) { return(SnapshotResult.SnapshotsDoNotMatch(newshot, savedshot, i)); } } return(SnapshotResult.SnapshotsMatch(newshot, savedshot)); }
/// <summary> /// Create a new instance of the snapshot exception /// </summary> /// <param name="message"></param> /// <param name="result"></param> public SnapshotMatchException(string message, SnapshotResult result) : base(message) { SnapshotResult = result; OldSnapshot = result.OldSnapshot?.ToString(); NewSnapshot = result.NewSnapshot?.ToString(); }
/// <summary> /// assert invalid snapshots /// </summary> /// <param name="snapshotResult"></param> public static void AssertSnapshot(SnapshotResult snapshotResult) { switch (snapshotResult.Status) { case SnapshotStatus.SnapshotsDoNotMatch: Throw(snapshotResult, s => new SnapshotMatchException(s, snapshotResult)); break; case SnapshotStatus.SnapshotDoesNotExist: case SnapshotStatus.SnapshotUpdated: case SnapshotStatus.SnapshotsMatch: default: return; } }
public static void MatchSnapshot(this Snapshot snapshot, SnapshotOptions options) { options = options.MergeDefault(); var resolver = new SnapshotSetupResolver(); var setup = resolver.ResolveSnapshotSetup(); var client = GetClient(); var result = client.Validate(snapshot, setup, options); if (result.Status == SnapshotStatus.SnapshotDoesNotExist || result.Status == SnapshotStatus.UpdateSnapshot) { client.Write(snapshot, setup); result = SnapshotResult.SnapshotUpdated(snapshot, null); } SnapshotAsserter.AssertSnapshot(result); }
private static void Throw <T>(SnapshotResult result, Func <string, T> exception) where T : Exception { var(saved, actual, index) = Difference(result.OldSnapshot[result.Index].Value, result.NewSnapshot[result.Index].Value); var message = new StringBuilder(); message.Append(Environment.NewLine); message.AppendLine($"Snapshots do not match"); message.AppendLine($" - Line {result.Index + 1} position {index}"); message.AppendLine($"Expected - {saved}"); message.AppendLine($"Actual - {actual}"); message.AppendLine(string.Empty); message.AppendLine("Line:"); message.AppendLine($"Expected - {result.OldSnapshot[result.Index]}"); message.AppendLine($"Actual - {result.NewSnapshot[result.Index]}"); message.AppendLine(string.Empty); message.AppendLine("Original snapshot:"); message.AppendLine(result.OldSnapshot.ToString()); message.AppendLine(string.Empty); message.AppendLine("New snapshot:"); message.Append(result.NewSnapshot.ToString()); throw exception(message.ToString()); }