EqualConstraintResult
The EqualConstraintResult class is tailored for formatting
and displaying the result of an EqualConstraint.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Constraints
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class EqualConstraintResult : ConstraintResult
{
[System.Runtime.CompilerServices.Nullable(2)]
private readonly object _expectedValue;
private readonly Tolerance _tolerance;
private readonly bool _caseInsensitive;
private readonly bool _clipStrings;
private readonly IList<NUnitEqualityComparer.FailurePoint> _failurePoints;
private static readonly string StringsDiffer_1 = "String lengths are both {0}. Strings differ at index {1}.";
private static readonly string StringsDiffer_2 = "Expected string length {0} but was {1}. Strings differ at index {2}.";
private static readonly string StreamsDiffer_1 = "Stream lengths are both {0}. Streams differ at offset {1}.";
private static readonly string StreamsDiffer_2 = "Expected Stream length {0} but was {1}.";
private static readonly string UnSeekableStreamsDiffer = "Streams differ at offset {0}.";
private static readonly string CollectionType_1 = "Expected and actual are both {0}";
private static readonly string CollectionType_2 = "Expected is {0}, actual is {1}";
private static readonly string ValuesDiffer_1 = "Values differ at index {0}";
private static readonly string ValuesDiffer_2 = "Values differ at expected index {0}, actual index {1}";
public EqualConstraintResult(EqualConstraint constraint, [System.Runtime.CompilerServices.Nullable(2)] object actual, bool hasSucceeded)
: base(constraint, actual, hasSucceeded)
{
_expectedValue = constraint.Arguments[0];
_tolerance = constraint.Tolerance;
_caseInsensitive = constraint.CaseInsensitive;
_clipStrings = constraint.ClipStrings;
_failurePoints = constraint.FailurePoints;
}
public override void WriteMessageTo(MessageWriter writer)
{
DisplayDifferences(writer, _expectedValue, base.ActualValue, 0);
}
[System.Runtime.CompilerServices.NullableContext(2)]
private void DisplayDifferences([System.Runtime.CompilerServices.Nullable(1)] MessageWriter writer, object expected, object actual, int depth)
{
string text = expected as string;
if (text != null) {
string text2 = actual as string;
if (text2 != null) {
DisplayStringDifferences(writer, text, text2);
return;
}
}
ICollection collection = expected as ICollection;
if (collection != null) {
ICollection collection2 = actual as ICollection;
if (collection2 != null) {
DisplayCollectionDifferences(writer, collection, collection2, depth);
return;
}
}
IEnumerable enumerable = expected as IEnumerable;
if (enumerable != null) {
IEnumerable enumerable2 = actual as IEnumerable;
if (enumerable2 != null) {
DisplayEnumerableDifferences(writer, enumerable, enumerable2, depth);
return;
}
}
Stream stream = expected as Stream;
if (stream != null) {
Stream stream2 = actual as Stream;
if (stream2 != null) {
DisplayStreamDifferences(writer, stream, stream2, depth);
return;
}
}
if (_tolerance != null)
writer.DisplayDifferences(expected, actual, _tolerance);
else
writer.DisplayDifferences(expected, actual);
}
private void DisplayStringDifferences(MessageWriter writer, string expected, string actual)
{
int num = MsgUtils.FindMismatchPosition(expected, actual, 0, _caseInsensitive);
if (expected.Length == actual.Length)
writer.WriteMessageLine(StringsDiffer_1, expected.Length, num);
else
writer.WriteMessageLine(StringsDiffer_2, expected.Length, actual.Length, num);
writer.DisplayStringDifferences(expected, actual, num, _caseInsensitive, _clipStrings);
}
private void DisplayStreamDifferences(MessageWriter writer, Stream expected, Stream actual, int depth)
{
if (expected.CanSeek && actual.CanSeek) {
if (expected.Length == actual.Length) {
long position = _failurePoints[depth].Position;
writer.WriteMessageLine(StreamsDiffer_1, expected.Length, position);
} else
writer.WriteMessageLine(StreamsDiffer_2, expected.Length, actual.Length);
} else
writer.WriteMessageLine(UnSeekableStreamsDiffer, _failurePoints[depth].Position);
}
private void DisplayCollectionDifferences(MessageWriter writer, ICollection expected, ICollection actual, int depth)
{
DisplayTypesAndSizes(writer, expected, actual, depth);
if (_failurePoints.Count > depth) {
NUnitEqualityComparer.FailurePoint failurePoint = _failurePoints[depth];
DisplayFailurePoint(writer, expected, actual, failurePoint, depth);
if (failurePoint.ExpectedHasData && failurePoint.ActualHasData)
DisplayCollectionDifferenceWithFailurePoint(writer, expected, actual, failurePoint, depth);
else if (failurePoint.ActualHasData) {
writer.Write(" Extra: ");
writer.WriteCollectionElements(actual.Skip(failurePoint.Position), 0, 3);
} else {
writer.Write(" Missing: ");
writer.WriteCollectionElements(expected.Skip(failurePoint.Position), 0, 3);
}
}
}
private void DisplayCollectionDifferenceWithFailurePoint(MessageWriter writer, ICollection expected, ICollection actual, NUnitEqualityComparer.FailurePoint failurePoint, int depth)
{
string text = failurePoint.ExpectedValue as string;
if (text != null) {
string text2 = failurePoint.ActualValue as string;
if (text2 != null) {
int num = MsgUtils.FindMismatchPosition(text, text2, 0, _caseInsensitive);
if (text.Length == text2.Length)
writer.WriteMessageLine(StringsDiffer_1, text.Length, num);
else
writer.WriteMessageLine(StringsDiffer_2, text.Length, text2.Length, num);
writer.WriteLine(" Expected: " + MsgUtils.FormatCollection(expected, 0, 10));
writer.WriteLine(" But was: " + MsgUtils.FormatCollection(actual, 0, 10));
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(41, 2);
defaultInterpolatedStringHandler.AppendLiteral(" First non-matching item at index [");
defaultInterpolatedStringHandler.AppendFormatted(failurePoint.Position);
defaultInterpolatedStringHandler.AppendLiteral("]: \"");
defaultInterpolatedStringHandler.AppendFormatted<object>(failurePoint.ExpectedValue);
defaultInterpolatedStringHandler.AppendLiteral("\"");
writer.WriteLine(defaultInterpolatedStringHandler.ToStringAndClear());
return;
}
}
DisplayDifferences(writer, failurePoint.ExpectedValue, failurePoint.ActualValue, ++depth);
}
private void DisplayTypesAndSizes(MessageWriter writer, IEnumerable expected, IEnumerable actual, int indent)
{
string text = MsgUtils.GetTypeRepresentation(expected);
ICollection collection = expected as ICollection;
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler;
if (collection != null && !(expected is Array)) {
string str = text;
defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(15, 1);
defaultInterpolatedStringHandler.AppendLiteral(" with ");
defaultInterpolatedStringHandler.AppendFormatted(collection.Count);
defaultInterpolatedStringHandler.AppendLiteral(" elements");
text = str + defaultInterpolatedStringHandler.ToStringAndClear();
}
string text2 = MsgUtils.GetTypeRepresentation(actual);
ICollection collection2 = actual as ICollection;
if (collection2 != null && !(actual is Array)) {
string str2 = text2;
defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(15, 1);
defaultInterpolatedStringHandler.AppendLiteral(" with ");
defaultInterpolatedStringHandler.AppendFormatted(collection2.Count);
defaultInterpolatedStringHandler.AppendLiteral(" elements");
text2 = str2 + defaultInterpolatedStringHandler.ToStringAndClear();
}
if (text == text2)
writer.WriteMessageLine(indent, CollectionType_1, text);
else
writer.WriteMessageLine(indent, CollectionType_2, text, text2);
}
private void DisplayFailurePoint(MessageWriter writer, IEnumerable expected, IEnumerable actual, NUnitEqualityComparer.FailurePoint failurePoint, int indent)
{
Array array = expected as Array;
Array array2 = actual as Array;
int num = array?.Rank ?? 1;
int num2 = array2?.Rank ?? 1;
bool flag = num == num2;
if (array != null && array2 != null) {
for (int i = 1; (i < num) & flag; i++) {
if (array.GetLength(i) != array2.GetLength(i))
flag = false;
}
}
int[] arrayIndicesFromCollectionIndex = MsgUtils.GetArrayIndicesFromCollectionIndex(expected, failurePoint.Position);
if (flag)
writer.WriteMessageLine(indent, ValuesDiffer_1, MsgUtils.GetArrayIndicesAsString(arrayIndicesFromCollectionIndex));
else {
int[] arrayIndicesFromCollectionIndex2 = MsgUtils.GetArrayIndicesFromCollectionIndex(actual, failurePoint.Position);
writer.WriteMessageLine(indent, ValuesDiffer_2, MsgUtils.GetArrayIndicesAsString(arrayIndicesFromCollectionIndex), MsgUtils.GetArrayIndicesAsString(arrayIndicesFromCollectionIndex2));
}
}
private void DisplayEnumerableDifferences(MessageWriter writer, IEnumerable expected, IEnumerable actual, int depth)
{
DisplayTypesAndSizes(writer, expected, actual, depth);
if (_failurePoints.Count > depth) {
NUnitEqualityComparer.FailurePoint failurePoint = _failurePoints[depth];
DisplayFailurePoint(writer, expected, actual, failurePoint, depth);
if (failurePoint.ExpectedHasData && failurePoint.ActualHasData)
DisplayDifferences(writer, failurePoint.ExpectedValue, failurePoint.ActualValue, ++depth);
else if (failurePoint.ActualHasData) {
writer.Write(" Extra: < " + MsgUtils.FormatValue(failurePoint.ActualValue) + ", ... >");
} else {
writer.Write(" Missing: < " + MsgUtils.FormatValue(failurePoint.ExpectedValue) + ", ... >");
}
}
}
}
}