From b62de9230644ef1247031d82dd9af60f6854ac6d Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 7 Mar 2019 12:27:41 -0600 Subject: [PATCH] Implement AssertEx.EqualOrDiff Based on work by @AArnott in dotnet/roslyn-sdk#240 --- eng/Versions.props | 1 + .../Utilities/Portable/Assert/AssertEx.cs | 45 +++++++++++++++++++ .../Portable/Roslyn.Test.Utilities.csproj | 1 + 3 files changed, 47 insertions(+) diff --git a/eng/Versions.props b/eng/Versions.props index 4624793f67d..3d20cfbda04 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -30,6 +30,7 @@ 0.9.3 0.11.1 2.0.273-beta + 1.4.4 17.144.28413-buildid7983345 8.0.2 8.0.0 diff --git a/src/Test/Utilities/Portable/Assert/AssertEx.cs b/src/Test/Utilities/Portable/Assert/AssertEx.cs index 88c71b833bf..b48c4920d88 100644 --- a/src/Test/Utilities/Portable/Assert/AssertEx.cs +++ b/src/Test/Utilities/Portable/Assert/AssertEx.cs @@ -9,6 +9,9 @@ using System.Reflection; using System.Runtime.CompilerServices; using System.Text; +using DiffPlex; +using DiffPlex.DiffBuilder; +using DiffPlex.DiffBuilder.Model; using Microsoft.CodeAnalysis.Test.Utilities; using Xunit; @@ -210,6 +213,48 @@ public static void Equal(ImmutableArray expected, ImmutableArray actual Assert.True(false, assertMessage); } + /// + /// Asserts that two strings are equal, and prints a diff between the two if they are not. + /// + /// The expected string. This is presented as the "baseline/before" side in the diff. + /// The actual string. This is presented as the changed or "after" side in the diff. + /// The message to precede the diff, if the values are not equal. + public static void EqualOrDiff(string expected, string actual, string message = null) + { + if (expected == actual) + { + return; + } + + var diffBuilder = new InlineDiffBuilder(new Differ()); + var diff = diffBuilder.BuildDiffModel(expected, actual, ignoreWhitespace: false); + var messageBuilder = new StringBuilder(); + messageBuilder.AppendLine( + string.IsNullOrEmpty(message) + ? "Actual and expected values differ. Expected shown in baseline of diff:" + : message); + + foreach (var line in diff.Lines) + { + switch (line.Type) + { + case ChangeType.Inserted: + messageBuilder.Append("+"); + break; + case ChangeType.Deleted: + messageBuilder.Append("-"); + break; + default: + messageBuilder.Append(" "); + break; + } + + messageBuilder.AppendLine(line.Text); + } + + Assert.True(false, messageBuilder.ToString()); + } + public static void NotEqual(IEnumerable expected, IEnumerable actual, IEqualityComparer comparer = null, string message = null, string itemSeparator = null, Func itemInspector = null) { diff --git a/src/Test/Utilities/Portable/Roslyn.Test.Utilities.csproj b/src/Test/Utilities/Portable/Roslyn.Test.Utilities.csproj index 260517afbba..5cceca0bb4b 100644 --- a/src/Test/Utilities/Portable/Roslyn.Test.Utilities.csproj +++ b/src/Test/Utilities/Portable/Roslyn.Test.Utilities.csproj @@ -92,6 +92,7 @@ + -- GitLab