protected void TestArraySequenceEquity(Array expected, Array actual, string message, bool equals, IEqualityComparer <object> comparer, [CallerMemberName] string methodName = "") { if (expected.Rank != actual.Rank) { if (equals) { AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_DimensionMismatchFormat, expected.Rank, actual.Rank, message).Trim(), methodName); return; } AssertHandler.OnAssertSucceeded(); } for (var rank = 0; rank < expected.Rank; rank++) { var expectedBound = expected.GetLowerBound(rank); var actualBound = actual.GetLowerBound(rank); if (expectedBound != actualBound) { if (equals) { AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_LBoundMismatchFormat, rank + 1, expectedBound, actualBound, message).Trim(), methodName); return; } AssertHandler.OnAssertSucceeded(); } expectedBound = expected.GetUpperBound(rank); actualBound = actual.GetUpperBound(rank); if (expectedBound != actualBound) { if (equals) { AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_UBoundMismatchFormat, rank + 1, expectedBound, actualBound, message).Trim(), methodName); return; } AssertHandler.OnAssertSucceeded(); } } var flattenedExpected = expected.Cast <object>().ToList(); var flattenedActual = actual.Cast <object>().ToList(); if (!flattenedActual.SequenceEqual(flattenedExpected, comparer)) { if (equals) { AssertHandler.OnAssertFailed(message, methodName); } AssertHandler.OnAssertSucceeded(); } if (!equals) { AssertHandler.OnAssertFailed(message, methodName); } AssertHandler.OnAssertSucceeded(); }
/// <summary> /// Verifies that two specified object variables refer to different objects. The assertion fails if they refer to the same object. /// </summary> /// <param name="expected">The expected reference.</param> /// <param name="actual">The actual reference.</param> /// <param name="message">An optional message to display if the assertion fails.</param> public void AreNotSame(object expected, object actual, string message = "") { if (!ReferenceEquityAssertTypesMatch(expected, actual, false)) { return; } if (expected == null && actual == null) { AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_ParameterResultFormat, Tokens.Nothing, Tokens.Nothing, message).Trim()); return; } if (expected == null || actual == null) { AssertHandler.OnAssertSucceeded(); return; } if (!ReferenceEquals(expected, actual)) { AssertHandler.OnAssertSucceeded(); return; } AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_ParameterResultFormat, expected.GetHashCode(), actual.GetHashCode(), message).Trim()); }
private bool ValueEquityAssertTypesMatch(object expected, object actual, bool equals) { var expectedType = expected.GetType(); var actualType = actual.GetType(); if (expectedType.IsArray && actualType.IsArray) { AssertHandler.OnAssertInconclusive( string.Format(AssertMessages.Assert_UnexpectedArrayFormat, equals ? "Assert.SequenceEquals" : "Assert.NotSequenceEquals")); return(false); } if (!ReferenceOrValueTypesMatch(expectedType, actualType)) { return(false); } if (expectedType.IsCOMObject && actualType.IsCOMObject) { AssertHandler.OnAssertInconclusive( string.Format(AssertMessages.Assert_UnexpectedReferenceComparisonFormat, equals ? "Assert.AreSame" : "Assert.AreNotSame")); return(false); } if (expectedType != actualType) { AssertHandler.OnAssertInconclusive(AssertMessages.Assert_MismatchedTypes); return(false); } return(true); }
public void ParameterInRange(string parameter, double minimum, double maximum, int invocation = 1, string message = "") { var usage = GetUsageOrAssert(parameter, invocation, message); if (Asserted || !usage.HasValue) { return; } if (usage.Value.IsMissing) { AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_VerifyParameterNotPassed, parameter, invocation, message)); Asserted = true; return; } var underTest = usage.Value.Value is ComVariant ? ((ComVariant)(usage.Value.Value)).Value : usage.Value.Value; if (!(underTest is double)) { AssertHandler.OnAssertInconclusive(string.Format(AssertMessages.Assert_VerifyParameterNonNumeric, parameter, invocation, message)); Asserted = true; return; } // passing case. if ((double)underTest >= minimum && (double)underTest <= maximum) { return; } AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_ParameterResultFormat, $"{minimum} - {maximum}", underTest, message)); Asserted = true; }
/// <summary> /// Verifies that two specified object variables refer to the same object. The assertion fails if they refer to different objects. /// </summary> /// <param name="expected">The expected reference.</param> /// <param name="actual">The actual reference.</param> /// <param name="message">An optional message to display if the assertion fails.</param> public void AreSame(object expected, object actual, string message = "") { if (!ReferenceEquityAssertTypesMatch(expected, actual, true)) { return; } if (expected == null && actual != null) { AssertHandler.OnAssertFailed(string.Format(RubberduckUI.Assert_ParameterResultFormat, Tokens.Nothing, actual.GetHashCode(), message).Trim()); return; } if (actual == null && expected != null) { AssertHandler.OnAssertFailed(string.Format(RubberduckUI.Assert_ParameterResultFormat, expected.GetHashCode(), Tokens.Nothing, message).Trim()); return; } if (ReferenceEquals(expected, actual)) { AssertHandler.OnAssertSucceeded(); } else { AssertHandler.OnAssertFailed(string.Format(RubberduckUI.Assert_ParameterResultFormat, expected.GetHashCode(), actual.GetHashCode(), message).Trim()); } }
public void AtMost(int invocations, string message = "") { if (Asserted || InvocationCount <= invocations) { return; } AssertHandler.OnAssertFailed(string.Format(RubberduckUI.Assert_ParameterResultFormat, invocations, InvocationCount, message)); Asserted = true; }
public void Once(string message = "") { if (Asserted || InvocationCount == 1) { return; } AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_ParameterResultFormat, 1, InvocationCount, message)); Asserted = true; }
public void Exactly(int invocations, string message = "") { if (Asserted || InvocationCount == invocations) { return; } AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_ParameterResultFormat, invocations, InvocationCount, message)); Asserted = true; }
public void Between(int minimum, int maximum, string message = "") { if (Asserted || InvocationCount >= minimum && InvocationCount <= maximum) { return; } AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_ParameterResultFormat, $"{minimum} - {maximum}", InvocationCount, message)); Asserted = true; }
public void Never(string message = "") { if (Asserted || InvocationCount == 0) { return; } AssertHandler.OnAssertFailed(string.Format(RubberduckUI.Assert_ParameterResultFormat, 0, InvocationCount, message)); Asserted = true; }
public void AtLeastOnce(string message = "") { if (Asserted || InvocationCount > 0) { return; } AssertHandler.OnAssertFailed(string.Format(RubberduckUI.Assert_ParameterResultFormat, 1, 0, message)); Asserted = true; }
/// <summary> /// Verifies that two specified objects are equal as considered equal under the loose terms of VBA equality. /// As such the assertion fails, if the objects are not equal, even after applying VBA Type promotions. /// </summary> /// <param name="expected">The expected value.</param> /// <param name="actual">The actual value.</param> /// <param name="message">An optional message to display if the assertion fails.</param> /// <remarks> /// contrary to <see cref="AssertClass.AreEqual"/> <paramref name="expected"/> and <paramref name="actual"/> are not required to be of the same type /// </remarks> public override void AreEqual(object expected, object actual, string message = "") { if (PermissiveComparer.Equals(expected, actual)) { AssertHandler.OnAssertSucceeded(); } else { AssertHandler.OnAssertFailed(message); } }
/// <summary> /// Verifies that the specified condition is <c>true</c>. The assertion fails if the condition is <c>false</c>. /// </summary> /// <param name="condition">Any Boolean value or expression.</param> /// <param name="message">An optional message to display if the assertion fails.</param> public void IsTrue(bool condition, string message = null) { if (condition) { AssertHandler.OnAssertSucceeded(); } else { AssertHandler.OnAssertFailed("IsTrue", message); } }
/// <summary> /// Verifies that the specified condition is <c>false</c>. The assertion fails if the condition is <c>true</c>. /// </summary> /// <param name="condition">Any Boolean value or expression.</param> /// <param name="message">An optional message to display if the assertion fails.</param> public void IsFalse(bool condition, string message = "") { if (!condition) { AssertHandler.OnAssertSucceeded(); } else { AssertHandler.OnAssertFailed(message); } }
/// <summary> /// Verifies that the specified object is <c>Nothing</c>. The assertion fails if it is not <c>Nothing</c>. /// </summary> /// <param name="value">The object to verify.</param> /// <param name="message">An optional message to display if the assertion fails.</param> public void IsNothing(object value, string message = "") { if (value == null) { AssertHandler.OnAssertSucceeded(); } else { AssertHandler.OnAssertFailed(message); } }
public void ParameterIsPassed(string parameter, int invocation = 1, string message = "") { var usage = GetUsageOrAssert(parameter, invocation, message); if (Asserted || !usage.HasValue || !usage.Value.IsMissing) { return; } AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_VerifyParameterNotPassed, parameter, invocation, message)); Asserted = true; }
public override void AreNotEqual(object expected, object actual, string message = "") { // vbNullString is marshalled as null. assume value semantics: expected = expected ?? string.Empty; actual = actual ?? string.Empty; if (PermissiveComparer.Equals(expected, actual)) { AssertHandler.OnAssertFailed(message); } AssertHandler.OnAssertSucceeded(); }
public void Parameter(string parameter, object value, int invocation = 1, string message = "") { var usage = GetUsageOrAssert(parameter, invocation, message); if (Asserted || !usage.HasValue || IsEasterEgg(value) || usage.Value.Value.Equals(value)) { return; } AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_ParameterResultFormat, value, usage.Value.Value, message)); Asserted = true; }
protected void OnCallBack(bool trackNoParams = false) { InvocationCount++; if (trackNoParams) { Verifier.AddUsage(string.Empty, null, string.Empty, InvocationCount); } if (Throws) { AssertHandler.RaiseVbaError(ErrorNumber, ErrorDescription); } }
private bool ReferenceOrValueTypesMatch(Type expectedType, Type actualType) { if (expectedType != null && !expectedType.IsCOMObject && (actualType == null || actualType.IsCOMObject)) { AssertHandler.OnAssertInconclusive(AssertMessages.Assert_ValueReferenceMismatch); return(false); } if (actualType != null && !actualType.IsCOMObject && (expectedType == null || expectedType.IsCOMObject)) { AssertHandler.OnAssertInconclusive(AssertMessages.Assert_ReferenceValueMismatch); return(false); } return(true); }
public void ParameterIsType(string parameter, string typeName, int invocation = 1, string message = "") { var usage = GetUsageOrAssert(parameter, invocation, message); if (Asserted || !usage.HasValue) { return; } //TODO AssertHandler.OnAssertInconclusive(AssertMessages.Assert_NotImplemented); //if (usage.Value.TypeName.ToLower().Equals(typeName.ToLower())) //{ // return; //} //AssertHandler.OnAssertFailed(string.Format(RubberduckUI.Assert_ParameterResultFormat, typeName, usage.Value.TypeName, message)); //Asserted = true; }
public override void AreNotEqual(object expected, object actual, string message = null) { // vbNullString is marshalled as null. assume value semantics: expected = expected ?? string.Empty; actual = actual ?? string.Empty; if (expected.GetType() != actual.GetType()) { if (!RunTypePromotions(ref expected, ref actual)) { AssertHandler.OnAssertFailed("AreNotEqual", message); } } if (!expected.Equals(actual)) { AssertHandler.OnAssertSucceeded(); } }
/// <summary> /// Verifies that two specified objects are not equal. The assertion fails if the objects are equal. /// </summary> /// <param name="expected">The expected value.</param> /// <param name="actual">The actual value.</param> /// <param name="message">An optional message to display if the assertion fails.</param> /// <remarks> /// <paramref name="expected"/> and <paramref name="actual"/> must be the same type. /// </remarks> public virtual void AreNotEqual(object expected, object actual, string message = "") { // vbNullString is marshaled as a null. assume value semantics: expected = expected ?? string.Empty; actual = actual ?? string.Empty; if (!ValueEquityAssertTypesMatch(expected, actual, false)) { return; } if (!expected.Equals(actual)) { AssertHandler.OnAssertSucceeded(); } else { AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_ParameterResultFormat, expected, actual, message).Trim()); } }
private UsageInfo?GetUsageOrAssert(string parameter, int invocation, string message = "", [CallerMemberName] string methodName = "") { if (invocation > InvocationCount || invocation < 1) { // ReSharper disable once ExplicitCallerInfoArgument AssertHandler.OnAssertFailed(string.Format(AssertMessages.Assert_VerifyNoInvocationFormat, parameter, _usages.Count, message), methodName); Asserted = true; return(null); } var uses = _usages.Where(u => u.Parameter.Equals(parameter.ToLower()) && u.Invocation == invocation).ToArray(); if (uses.Length != 1) { AssertHandler.OnAssertInconclusive(AssertMessages.Assert_VerifyInternalErrorMessage); Asserted = true; return(null); } return(uses[0]); }
/// <summary> /// Verifies that two specified object variables refer to different objects. The assertion fails if they refer to the same object. /// </summary> /// <param name="expected">The expected reference.</param> /// <param name="actual">The actual reference.</param> /// <param name="message">An optional message to display if the assertion fails.</param> public void AreNotSame(object expected, object actual, string message = null) { if (expected == null && actual == null) { AssertHandler.OnAssertFailed("AreNotSame", string.Concat("expected: Nothing; actual: Nothing. ", message)); return; } if (expected == null || actual == null) { AssertHandler.OnAssertSucceeded(); return; } if (!ReferenceEquals(expected, actual)) { AssertHandler.OnAssertSucceeded(); return; } AssertHandler.OnAssertFailed("AreNotSame", string.Concat("expected: ", expected.GetHashCode(), "; actual: ", actual.GetHashCode(), ". ", message)); }
/// <summary> /// Verifies that two specified objects are not equal. The assertion fails if the objects are equal. /// </summary> /// <param name="expected">The expected value.</param> /// <param name="actual">The actual value.</param> /// <param name="message">An optional message to display if the assertion fails.</param> /// <remarks> /// <paramref name="expected"/> and <paramref name="actual"/> must be the same type. /// </remarks> public void AreNotEqual(object expected, object actual, string message = null) { // vbNullString is marshaled as a null. assume value semantics: expected = expected ?? string.Empty; actual = actual ?? string.Empty; if (expected.GetType() != actual.GetType()) { AssertHandler.OnAssertInconclusive("[expected] and [actual] values are not the same type."); return; } if (!expected.Equals(actual)) { AssertHandler.OnAssertSucceeded(); } else { AssertHandler.OnAssertFailed("AreNotEqual", string.Concat("expected: ", expected.ToString(), "; actual: ", actual.ToString(), ". ", message)); } }
protected bool SequenceEquityParametersAreArrays(object expected, object actual, bool equals) { var expectedType = expected?.GetType(); var actualType = actual?.GetType(); if (expectedType == null && actualType == null) { AssertHandler.OnAssertInconclusive( string.Format(AssertMessages.Assert_UnexpectedNullArraysFormat, equals ? "Assert.AreSame" : "Assert.AreNotSame")); return(false); } if (!ReferenceOrValueTypesMatch(expectedType, actualType)) { return(false); } if (expectedType != null && !expectedType.IsArray && actualType != null && actualType.IsArray) { AssertHandler.OnAssertInconclusive(string.Format(AssertMessages.Assert_ParameterIsNotArrayFormat, "[Expected]")); return(false); } if (actualType != null && !actualType.IsArray && expectedType != null && expectedType.IsArray) { AssertHandler.OnAssertInconclusive(string.Format(AssertMessages.Assert_ParameterIsNotArrayFormat, "[Actual]")); return(false); } if (actualType != null && !actualType.IsArray && (expectedType == null || expectedType.IsArray)) { AssertHandler.OnAssertInconclusive(AssertMessages.Assert_NeitherParameterIsArray); return(false); } return(true); }
private bool IsEasterEgg(object value) { if (value.GetType() == typeof(AssertClass)) { AssertHandler.OnAssertInconclusive(AssertMessages.Assert_EasterEggAssertClassPassed); Asserted = true; return(true); } if (value.GetType() == typeof(IVerify)) { AssertHandler.OnAssertInconclusive(AssertMessages.Assert_EasterEggIVerifyPassed); Asserted = true; return(true); } if (value.GetType() == typeof(IFake)) { AssertHandler.OnAssertInconclusive(AssertMessages.Assert_EasterEggIFakePassed); Asserted = true; return(true); } return(false); }
private bool ReferenceEquityAssertTypesMatch(object expected, object actual, bool same) { var expectedType = expected?.GetType(); var actualType = actual?.GetType(); if ((expectedType == null && actualType == null) || ((expectedType == null || expectedType.IsCOMObject) && (actualType == null || actualType.IsCOMObject))) { return(true); } if (!ReferenceOrValueTypesMatch(expectedType, actualType)) { return(false); } if (expectedType != null && !expectedType.IsCOMObject && actualType != null && !actualType.IsCOMObject) { AssertHandler.OnAssertInconclusive( string.Format(AssertMessages.Assert_UnexpectedValueComparisonFormat, same ? "Assert.AreEqual" : "Assert.AreNotEqual")); return(false); } return(true); }
public void Succeed() { AssertHandler.OnAssertSucceeded(); }