<PackageReference Include="NUnit" Version="3.7.0" />

TextMessageWriter

TextMessageWriter writes constraint descriptions and messages in displayable form as a text stream. It tailors the display of individual message components to form the standard message format of NUnit assertion failure messages.
using NUnit.Framework.Constraints; using System.Collections; namespace NUnit.Framework.Internal { public class TextMessageWriter : MessageWriter { private static readonly int DEFAULT_LINE_LENGTH = 78; public static readonly string Pfx_Expected = " Expected: "; public static readonly string Pfx_Actual = " But was: "; public static readonly int PrefixLength = Pfx_Expected.Length; private int maxLineLength = DEFAULT_LINE_LENGTH; private bool _sameValDiffTypes; private string _expectedType; private string _actualType; public override int MaxLineLength { get { return maxLineLength; } set { maxLineLength = value; } } public TextMessageWriter() { } public TextMessageWriter(string userMessage, params object[] args) { if (userMessage != null && userMessage != string.Empty) WriteMessageLine(userMessage, args); } public override void WriteMessageLine(int level, string message, params object[] args) { if (message != null) { while (level-- >= 0) { Write(" "); } if (args != null && args.Length != 0) message = string.Format(message, args); WriteLine(MsgUtils.EscapeNullCharacters(message)); } } public override void DisplayDifferences(ConstraintResult result) { WriteExpectedLine(result); WriteActualLine(result); } private void ResolveTypeNameDifference(object expected, object actual, out string expectedType, out string actualType) { string[] array = expected.GetType().ToString().Split(new char[1] { '.' }); string[] array2 = actual.GetType().ToString().Split(new char[1] { '.' }); int num = 0; int num2 = 0; int num3 = array.Length - 1; int num4 = array2.Length - 1; while (num3 >= 0) { if (num4 < 0) break; if (array[num3] != array2[num4]) { num = num4; num2 = num3; break; } num3--; num4--; } expectedType = " (" + string.Join(".", array, num2, array.Length - num2) + ")"; actualType = " (" + string.Join(".", array2, num, array2.Length - num) + ")"; } public override void DisplayDifferences(object expected, object actual) { DisplayDifferences(expected, actual, null); } public override void DisplayDifferences(object expected, object actual, Tolerance tolerance) { if (expected != null && actual != null && expected.GetType() != actual.GetType() && MsgUtils.FormatValue(expected) == MsgUtils.FormatValue(actual)) { _sameValDiffTypes = true; ResolveTypeNameDifference(expected, actual, out _expectedType, out _actualType); } WriteExpectedLine(expected, tolerance); WriteActualLine(actual); } public override void DisplayStringDifferences(string expected, string actual, int mismatch, bool ignoreCase, bool clipping) { int maxDisplayLength = MaxLineLength - PrefixLength - 2; if (clipping) MsgUtils.ClipExpectedAndActual(ref expected, ref actual, maxDisplayLength, mismatch); expected = MsgUtils.EscapeControlChars(expected); actual = MsgUtils.EscapeControlChars(actual); mismatch = MsgUtils.FindMismatchPosition(expected, actual, 0, ignoreCase); Write(Pfx_Expected); Write(MsgUtils.FormatValue(expected)); if (ignoreCase) Write(", ignoring case"); WriteLine(); WriteActualLine(actual); if (mismatch >= 0) WriteCaretLine(mismatch); } public override void WriteActualValue(object actual) { WriteValue(actual); } public override void WriteValue(object val) { Write(MsgUtils.FormatValue(val)); } public override void WriteCollectionElements(IEnumerable collection, long start, int max) { Write(MsgUtils.FormatCollection(collection, start, max)); } private void WriteExpectedLine(ConstraintResult result) { Write(Pfx_Expected); WriteLine(result.Description); } private void WriteExpectedLine(object expected) { WriteExpectedLine(expected, null); } private void WriteExpectedLine(object expected, Tolerance tolerance) { Write(Pfx_Expected); Write(MsgUtils.FormatValue(expected)); if (_sameValDiffTypes) Write(_expectedType); if (tolerance != null && !tolerance.IsUnsetOrDefault) { Write(" +/- "); Write(MsgUtils.FormatValue(tolerance.Amount)); if (tolerance.Mode != ToleranceMode.Linear) Write(" {0}", tolerance.Mode); } WriteLine(); } private void WriteActualLine(ConstraintResult result) { Write(Pfx_Actual); result.WriteActualValueTo(this); WriteLine(); } private void WriteActualLine(object actual) { Write(Pfx_Actual); WriteActualValue(actual); if (_sameValDiffTypes) Write(_actualType); WriteLine(); } private void WriteCaretLine(int mismatch) { WriteLine(" {0}^", new string('-', PrefixLength + mismatch - 2 + 1)); } } }