/// <summary> /// Returns the diff as an assertion failure. /// </summary> /// <param name="expected">The expected fragment used to format the diff.</param> /// <param name="actual">The actual fragment used to format the diff.</param> /// <returns>The resulting assertion failure.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="expected"/> or <paramref name="actual"/> is null.</exception> public AssertionFailure ToAssertionFailure(NodeFragment expected, NodeFragment actual) { bool showActual = ((targets & DiffTargets.Actual) != 0); bool showExpected = ((targets & DiffTargets.Expected) != 0); var builder = new AssertionFailureBuilder(diffType.Description, new NullFormatter()); const XmlPathRenderingOptions options = XmlPathRenderingOptions.UseIndentation; if (showActual && showExpected) { var actualFormatted = XmlPathRenderer.Run(path, actual, options); var expectedFormatted = XmlPathRenderer.Run(path, expected, options); builder.AddRawExpectedAndActualValuesWithDiffs(expectedFormatted, actualFormatted); } else if (showActual) { var actualFormatted = XmlPathRenderer.Run(path, actual, options); builder.AddRawActualValue(actualFormatted); } else if (showExpected) { var expectedFormatted = XmlPathRenderer.Run(path, expected, options); builder.AddRawExpectedValue(expectedFormatted); } return(builder.ToAssertionFailure()); }
public void ShowsExpectedAndActualValueWithDiffs_RepresentationalEquality() { AssertionFailureBuilder builder = new AssertionFailureBuilder("description"); builder.AddRawExpectedAndActualValuesWithDiffs(1, 1u); Assert.AreElementsEqual(new[] { new AssertionFailure.LabeledValue("Expected Value & Actual Value", new StructuredText("1")), new AssertionFailure.LabeledValue("Remark", "Both values look the same when formatted but they are distinct instances.") }, builder.ToAssertionFailure().LabeledValues); }
public void ShowsExpectedAndActualValueWithDiffs_ReferentialEquality() { const string str = "123"; AssertionFailureBuilder builder = new AssertionFailureBuilder("description"); builder.AddRawExpectedAndActualValuesWithDiffs(str, str); Assert.AreElementsEqual(new[] { new AssertionFailure.LabeledValue("Expected Value & Actual Value", new StructuredText("\"123\"")), new AssertionFailure.LabeledValue("Remark", "Both values are the same instance.") }, builder.ToAssertionFailure().LabeledValues); }
/// <summary> /// Reports an assertion failure. /// </summary> /// <param name="testContext"></param> /// <param name="testInfoData"></param> /// <param name="testStepResult"></param> public void Run(ITestContext testContext, TestInfoData testInfoData, TestStepResult testStepResult) { if (testStepResult.TestOutcome == TestOutcome.Failed) { MbUnitCppAssertionFailure failure = testStepResult.Failure; var builder = new AssertionFailureBuilder(failure.Description); if (failure.HasExpectedValue && failure.HasActualValue && failure.Diffing) { builder.AddRawExpectedAndActualValuesWithDiffs(failure.ExpectedValue, failure.ActualValue); } else if (failure.HasUnexpectedValue && failure.HasActualValue && failure.Diffing) { builder.AddRawLabeledValuesWithDiffs("Unexpected Value", failure.UnexpectedValue, "Actual Value", failure.ActualValue); } else { if (failure.HasExpectedValue) { builder.AddRawExpectedValue(failure.ExpectedValue); } if (failure.HasActualValue) { builder.AddRawActualValue(failure.ActualValue); } if (failure.HasUnexpectedValue) { builder.AddRawLabeledValue("Unexpected Value", failure.UnexpectedValue); } } foreach (var extra in failure.ExtraLabeledValues) { builder.AddRawLabeledValue(extra.First, extra.Second); } if (failure.Message.Length > 0) { builder.SetMessage(failure.Message); } builder.SetStackTrace(testInfoData.GetStackTraceData(failure.Line)); builder.ToAssertionFailure().WriteTo(testContext.LogWriter.Failures); } }
public void TruncatesDiffContextWhenTooLong() { string expectedValue = "z" + new string('x', AssertionFailure.MaxFormattedValueLength) + "z"; string actualValue = "Z" + new string('x', AssertionFailure.MaxFormattedValueLength) + "Z"; AssertionFailureBuilder builder = new AssertionFailureBuilder("description"); builder.AddRawExpectedAndActualValuesWithDiffs(expectedValue, actualValue); AssertionFailure failure = builder.ToAssertionFailure(); TestLog.Write(failure); int split = AssertionFailureBuilder.CompressedDiffContextLength / 2; Assert.AreEqual("\"z" + new string('x', split) + "..." + new string('x', split) + "z\"", failure.LabeledValues[0].FormattedValue.ToString()); Assert.AreEqual("\"Z" + new string('x', split) + "..." + new string('x', split) + "Z\"", failure.LabeledValues[1].FormattedValue.ToString()); }
public void ShowsExpectedAndActualValueWithDiffs_Difference() { AssertionFailureBuilder builder = new AssertionFailureBuilder("description"); builder.AddRawExpectedAndActualValuesWithDiffs("acde", "bcef"); DiffSet diffSet = DiffSet.GetDiffSet("\"acde\"", "\"bcef\"").Simplify(); StructuredTextWriter expectedValueWriter = new StructuredTextWriter(); diffSet.WriteTo(expectedValueWriter, DiffStyle.LeftOnly); StructuredTextWriter actualValueWriter = new StructuredTextWriter(); diffSet.WriteTo(actualValueWriter, DiffStyle.RightOnly); Assert.AreElementsEqual(new[] { new AssertionFailure.LabeledValue("Expected Value", expectedValueWriter.ToStructuredText()), new AssertionFailure.LabeledValue("Actual Value", actualValueWriter.ToStructuredText()) }, builder.ToAssertionFailure().LabeledValues); }