提交 b62de923 编写于 作者: S Sam Harwell

Implement AssertEx.EqualOrDiff

Based on work by @AArnott in dotnet/roslyn-sdk#240
上级 336c3766
......@@ -30,6 +30,7 @@
<BasicUndoVersion>0.9.3</BasicUndoVersion>
<BenchmarkDotNetVersion>0.11.1</BenchmarkDotNetVersion>
<CommandLineParserVersion>2.0.273-beta</CommandLineParserVersion>
<DiffPlexVersion>1.4.4</DiffPlexVersion>
<DropAppVersion>17.144.28413-buildid7983345</DropAppVersion>
<EnvDTEVersion>8.0.2</EnvDTEVersion>
<EnvDTE80Version>8.0.0</EnvDTE80Version>
......
......@@ -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<T>(ImmutableArray<T> expected, ImmutableArray<T> actual
Assert.True(false, assertMessage);
}
/// <summary>
/// Asserts that two strings are equal, and prints a diff between the two if they are not.
/// </summary>
/// <param name="expected">The expected string. This is presented as the "baseline/before" side in the diff.</param>
/// <param name="actual">The actual string. This is presented as the changed or "after" side in the diff.</param>
/// <param name="message">The message to precede the diff, if the values are not equal.</param>
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<T>(IEnumerable<T> expected, IEnumerable<T> actual, IEqualityComparer<T> comparer = null, string message = null,
string itemSeparator = null, Func<T, string> itemInspector = null)
{
......
......@@ -92,6 +92,7 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="DiffPlex" Version="$(DiffPlexVersion)" />
<PackageReference Include="Microsoft.CodeAnalysis.Test.Resources.Proprietary" Version="$(MicrosoftCodeAnalysisTestResourcesProprietaryVersion)" />
<PackageReference Include="Microsoft.CSharp" Version="$(MicrosoftCSharpVersion)" />
<PackageReference Include="Microsoft.DiaSymReader.Converter.Xml" Version="$(MicrosoftDiaSymReaderConverterXmlVersion)" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册