/// <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()); }
/// <summary> /// Verifies that an actual value is not an instance of some unexpected type. /// </summary> /// <remarks> /// <para> /// This assertion will fail if the object is null. /// </para> /// </remarks> /// <param name="unexpectedType">The unexpected type.</param> /// <param name="actualValue">The actual value.</param> /// <param name="messageFormat">The custom assertion message format, or null if none.</param> /// <param name="messageArgs">The custom assertion message arguments, or null if none.</param> /// <exception cref="AssertionException">Thrown if the verification failed unless the current <see cref="AssertionContext.AssertionFailureBehavior" /> indicates otherwise.</exception> /// <exception cref="ArgumentNullException">Thrown if <paramref name="unexpectedType"/> is null.</exception> public static void IsNotInstanceOfType(Type unexpectedType, object actualValue, string messageFormat, params object[] messageArgs) { AssertionHelper.Verify(() => { bool isNullValue = Object.ReferenceEquals(null, actualValue); if (!isNullValue && (unexpectedType == null || !unexpectedType.IsInstanceOfType(actualValue))) { return(null); } var builder = new AssertionFailureBuilder("Expected value to not be an instance of a particular type.") .SetMessage(messageFormat, messageArgs) .AddRawLabeledValue("Unexpected Type", unexpectedType); if (!isNullValue) { builder.AddRawLabeledValue("Actual Type", actualValue.GetType()); } return(builder .AddRawActualValue(actualValue) .ToAssertionFailure()); }); }
public void CanAddRawActualValue() { AssertionFailureBuilder builder = new AssertionFailureBuilder("Description"); builder.AddRawActualValue("Abc"); Assert.AreElementsEqual(new[] { new AssertionFailure.LabeledValue("Actual Value", "\"Abc\"") }, 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); } }