/// <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()); }); }
/// <summary> /// Verifies that a type supports the runtime serialization protocol. /// </summary> /// <remarks> /// <para> /// Ensures that the type has the <see cref="SerializableAttribute" />. Also ensures that if it /// implements <see cref="ISerializable" /> then it also has a deserialization constructor with /// signature .ctor(SerializationInfo, StreamingContext). /// </para> /// </remarks> /// <param name="type">The type to verify.</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="type"/> is null.</exception> public static void IsSerializableType(Type type, string messageFormat, params object[] messageArgs) { if (type == null) { throw new ArgumentNullException("type"); } AssertionHelper.Verify(delegate { if (!type.IsDefined(typeof(SerializableAttribute), false)) { return(new AssertionFailureBuilder( "Expected the type to support binary serialization but it lacks the [Serializable] attribute.") .SetMessage(messageFormat, messageArgs) .AddRawLabeledValue("Type", type) .ToAssertionFailure()); } if (typeof(ISerializable).IsAssignableFrom(type) && !HasDeserializationConstructor(type)) { return(new AssertionFailureBuilder( "Expected the type to support binary serialization but it implements ISerializable and is missing a deserialization constructor with signature .ctor(SerializationInfo, StreamingContext).") .SetMessage(messageFormat, messageArgs) .AddRawLabeledValue("Type", type) .ToAssertionFailure()); } return(null); }); }
private static Attribute[] HasAttributesImpl(Type attributeType, int expectedCount, MemberInfo target, string messageFormat, params object[] messageArgs) { Attribute[] result = GenericCollectionUtils.ToArray(GetAttributes(attributeType, target)); AssertionHelper.Verify(() => { string message = null; if (result.Length == 0) { message = "Expected the searched attribute to decorate the target object; but none was found."; } else if (expectedCount > 0 && result.Length != expectedCount) { message = String.Format("Expected to find {0} attribute instance{1} but found {2}.", expectedCount, expectedCount > 1 ? "s" : String.Empty, result.Length); } if (message == null) { return(null); } return(new AssertionFailureBuilder(message) .AddRawLabeledValue("Target Object", target) .AddRawLabeledValue("Attribute Type", attributeType) .SetMessage(messageFormat, messageArgs) .ToAssertionFailure()); }); return(result); }
private Test CreateCollisionProbabilityTest() { return(new TestCase("CollisionProbabilityTest", () => AssertionHelper.Verify(() => { HashStoreResult result; try { result = GetResult(); } catch (NotEnoughHashesException exception) { return new AssertionFailureBuilder("Not enough hash code samples were provided to the hash code acceptance contract.") .AddException(exception) .SetStackTrace(Context.GetStackTraceData()) .ToAssertionFailure(); } var probability = result.CollisionProbability; TestLog.WriteLine("Statistical Population = {0}", result.StatisticalPopulation); TestLog.WriteLine("Actual Collision Probability = {0} {1} {2}", probability, ((probability <= collisionProbabilityLimit) ? "≤" : ">"), collisionProbabilityLimit); if (probability <= collisionProbabilityLimit) { return null; } return new AssertionFailureBuilder(String.Format("The actual collision probability {0} is higher than the specified limit of {1}.", probability, collisionProbabilityLimit)) .AddRawExpectedValue(collisionProbabilityLimit) .AddRawActualValue(probability) .SetStackTrace(Context.GetStackTraceData()) .ToAssertionFailure(); }))); }
public void Verify_WhenAssertionFuncReturnsNull_DoesNotSubmitAnyFailures() { var failures = AssertionHelper.Eval(() => { AssertionHelper.Verify(() => null); }); Assert.IsEmpty(failures); }
public void Verify_WhenAssertionFuncReturnsNonNull_SubmitsTheFailure() { var failure = new AssertionFailureBuilder("Boom").ToAssertionFailure(); var failures = AssertionHelper.Eval(() => { AssertionHelper.Verify(() => failure); }); Assert.AreElementsEqual(new[] { failure }, failures); }
/// <summary> /// Verifies that the sequence of values are sorted in the specified order. /// </summary> /// <typeparam name="T">The type of value.</typeparam> /// <param name="values">The sequence of values to be tested.</param> /// <param name="sortOrder">The expected sort order.</param> /// <param name="comparer">A comparison function to be used to compare two elements of the sequence, or null to use a default one.</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> public static void Sorted <T>(IEnumerable <T> values, SortOrder sortOrder, Comparison <T> comparer, string messageFormat, params object[] messageArgs) { AssertionHelper.Verify(() => { bool first = true; var previous = default(T); var sortInfo = SortOrderInfo.FromSortOrder(sortOrder); int delta; int index = 0; if (comparer == null) { comparer = ComparisonSemantics.Default.Compare; } foreach (T value in values) { if (!first) { try { delta = comparer(value, previous); } catch (InvalidOperationException exception) { return(new AssertionFailureBuilder( "Expected the elements to be sorted in a specific order but no implicit ordering comparison can be found for the subject type.") .SetMessage(messageFormat, messageArgs) .AddRawLabeledValue("Type", typeof(T)) .AddException(exception) .ToAssertionFailure()); } if (!sortInfo.VerifyDeltaSign(delta)) { return(new AssertionFailureBuilder( "Expected the elements to be sorted in a specific order but the sequence of values mismatches at one position at least.") .SetMessage(messageFormat, messageArgs) .AddRawLabeledValue("Expected Sort Order", sortInfo.Description) .AddRawLabeledValue("Sequence", values) .AddRawLabeledValue("Failing Position", index) .ToAssertionFailure()); } } previous = value; first = false; index++; } return(null); }); }
public void Verify_WhenAssertionFuncThrowsAssertionFailureException_ResubmitsTheFailure() { var failure = new AssertionFailureBuilder("Boom").ToAssertionFailure(); var failures = AssertionHelper.Eval(() => { AssertionHelper.Verify(() => { throw new AssertionFailureException(failure, false); }); }); Assert.AreElementsEqual(new[] { failure }, failures); }
public void Verify_WhenAssertionFuncThrowsSomeOtherException_WrapsTheExceptionAsAFailure() { var failures = AssertionHelper.Eval(() => { AssertionHelper.Verify(() => { throw new InvalidOperationException("Boom"); }); }); Assert.Count(1, failures); Assert.AreEqual("An exception occurred while verifying an assertion.", failures[0].Description); Assert.Count(1, failures[0].Exceptions); Assert.Contains(failures[0].Exceptions[0].ToString(), "Boom"); }
/// <summary> /// Verifies that an actual value is not null. /// </summary> /// <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> public static void IsNotNull(object actualValue, string messageFormat, params object[] messageArgs) { AssertionHelper.Verify(delegate { if (actualValue != null) { return(null); } return(new AssertionFailureBuilder("Expected value to be non-null.") .SetMessage(messageFormat, messageArgs) .AddRawActualValue(actualValue) .ToAssertionFailure()); }); }
/// <summary> /// Verifies that an actual value is not referentially identical to some unexpected value. /// </summary> /// <typeparam name="T">The type of value.</typeparam> /// <param name="unexpectedValue">The unexpected value.</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> public static void AreNotSame <T>(T unexpectedValue, T actualValue, string messageFormat, params object[] messageArgs) where T : class { AssertionHelper.Verify(delegate { if (!ComparisonSemantics.Default.Same(unexpectedValue, actualValue)) { return(null); } return(new AssertionFailureBuilder("Expected values to be referentially different.") .SetMessage(messageFormat, messageArgs) .AddRawLabeledValuesWithDiffs("Unexpected Value", unexpectedValue, "Actual Value", actualValue) .ToAssertionFailure()); }); }
public void Verify_WhenCalled_IncrementsAssertionCountEachTime() { AssertionHelper.Verify(() => null); int firstCount = TestContext.CurrentContext.AssertCount; try { AssertionHelper.Verify(() => new AssertionFailureBuilder("Boom").ToAssertionFailure()); } catch (AssertionFailureException) { } int secondCount = TestContext.CurrentContext.AssertCount; Assert.AreEqual(1, firstCount); Assert.AreEqual(2, secondCount); }
/// <summary> /// Verifies that the sequence of objects contains distinct values. /// </summary> /// <typeparam name="T">The type of value.</typeparam> /// <param name="values">The sequence of values to be tested.</param> /// <param name="comparer">The comparer to use, or null to use the default one.</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> public static void Distinct <T>(IEnumerable <T> values, EqualityComparison <T> comparer, string messageFormat, params object[] messageArgs) { AssertionHelper.Verify(() => { if (comparer == null) { comparer = ComparisonSemantics.Default.Equals; } var duplicates = new List <T>(); int i = 0; foreach (var value1 in values) { int j = 0; foreach (var value2 in values) { if ((i != j) && comparer(value1, value2) && !duplicates.Contains(value1)) { duplicates.Add(value1); } j++; } i++; } if (duplicates.Count > 0) { var builder = new AssertionFailureBuilder( "Expected the elements to be distinct but several instances represents the same value.") .SetMessage(messageFormat, messageArgs); foreach (var duplicate in duplicates) { builder.AddRawLabeledValue("Duplicated Value", duplicate); } return(builder.ToAssertionFailure()); } return(null); }); }
private static T XmlDeserialize <T>(Type type, string xml, string messageFormat, object[] messageArgs, GallioFunc <T> originalValueProvider) { T deserializedValue = default(T); AssertionHelper.Verify(delegate { if (xml == null) { return(new AssertionFailureBuilder("Could not deserialize the value because the xml is null.") .SetMessage(messageFormat, messageArgs) .ToAssertionFailure()); } // TODO: Allow the user to specify Xml serialization options. using (XmlReader reader = XmlReader.Create(new StringReader(xml), new XmlReaderSettings() { CheckCharacters = false, CloseInput = false, ProhibitDtd = true })) { try { var serializer = new XmlSerializer(type); deserializedValue = (T)serializer.Deserialize(reader); return(null); } catch (Exception ex) { var failureBuilder = new AssertionFailureBuilder("Could not deserialize the value.") .SetMessage(messageFormat, messageArgs) .AddException(ex); if (originalValueProvider != null) { failureBuilder.AddRawLabeledValue("Value", originalValueProvider()); } return(failureBuilder .AddRawLabeledValue("Xml", xml) .ToAssertionFailure()); } } }); return(deserializedValue); }
/// <summary> /// Verifies that an actual value does not approximately equal some unexpected value /// to within a specified delta. /// </summary> /// <remarks> /// <para> /// The values are considered approximately equal if the absolute value of their difference /// is less than or equal to the delta. /// </para> /// <para> /// This method works with any comparable type that also supports a subtraction operator /// including <see cref="Single" />, <see cref="Double" />, <see cref="Decimal" />, /// <see cref="Int32" />, <see cref="DateTime" /> (using a <see cref="TimeSpan" /> delta), /// and many others. /// </para> /// </remarks> /// <typeparam name="TValue">The type of values to be compared.</typeparam> /// <typeparam name="TDifference">The type of the difference produced when the values are /// subtracted, for numeric types this is the same as <typeparamref name="TValue"/> but it /// may differ for other types.</typeparam> /// <param name="unexpectedValue">The expected value.</param> /// <param name="actualValue">The actual value.</param> /// <param name="delta">The inclusive delta between the values.</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> public static void AreNotApproximatelyEqual <TValue, TDifference>(TValue unexpectedValue, TValue actualValue, TDifference delta, string messageFormat, params object[] messageArgs) { AssertionHelper.Verify(delegate { if (!ComparisonSemantics.Default.ApproximatelyEqual(unexpectedValue, actualValue, delta)) { return(null); } return(new AssertionFailureBuilder("Expected values not to be approximately equal to within a delta.") .SetMessage(messageFormat, messageArgs) .AddRawLabeledValue("Unexpected Value", unexpectedValue) .AddRawActualValue(actualValue) .AddRawLabeledValue("Delta", delta) .ToAssertionFailure()); }); }
private Test CreateSerializableAttributeTest(string name) { return(new TestCase(name, () => { AssertionHelper.Verify(() => { if (typeof(TException).IsDefined(typeof(SerializableAttribute), false)) { return null; } return new AssertionFailureBuilder("Expected the exception type to have the [Serializable] attribute.") .AddRawLabeledValue("Exception Type", typeof(TException)) .SetStackTrace(Context.GetStackTraceData()) .ToAssertionFailure(); }); })); }
/// <summary> /// Verifies that a block of code does not throw an exception of any type. /// </summary> /// <remarks> /// <para> /// The purpose of this assertion is to improve the readability of tests /// that only verify that an exception was not thrown. Using this assertion /// makes a positive and explicit statement that not throwing an exception /// is itself the primary behavior that is being verified. /// </para> /// </remarks> /// <param name="action">The action delegate to evaluate.</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="ArgumentNullException">Thrown if <paramref name="action"/> is null.</exception> /// <exception cref="AssertionException">Thrown if the verification failed unless the current <see cref="AssertionContext.AssertionFailureBehavior" /> indicates otherwise.</exception> public static void DoesNotThrow(Action action, string messageFormat, params object[] messageArgs) { if (action == null) { throw new ArgumentNullException("action"); } AssertionHelper.Verify(() => { try { AssertionContext context = AssertionContext.CurrentContext; if (context != null) { // We will intercept any assertion failure which could occur while the action is run, then report it // as is. The goal is to prevent zealous "Assert.DoesNotThrow" to report the failure a second time. // Basically, a failing assertion inside a DoesNotThrow action does not make Assert.DoesNotThrow // to fail redundantly. See issue 769 (http://code.google.com/p/mb-unit/issues/detail?id=769) AssertionFailure[] failures = context.CaptureFailures(action, AssertionFailureBehavior.Throw, false); if (failures.Length > 0) { return(failures[0]); } } else { action(); } return(null); } catch (Exception actualException) { return(new AssertionFailureBuilder("The block threw an exception but none was expected.") .SetMessage(messageFormat, messageArgs) .AddException(actualException) .ToAssertionFailure()); } }); }
/// <summary> /// Verifies that an object can be serialized to Xml using /// an <see cref="XmlSerializer" /> and returns the resulting Xml as a string. /// </summary> /// <remarks> /// <para> /// The assertion fails if <paramref name="value"/> is null. /// </para> /// </remarks> /// <param name="value">The value.</param> /// <returns>The serialized Xml.</returns> /// <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> public static string XmlSerialize(object value, string messageFormat, params object[] messageArgs) { StringBuilder xml = new StringBuilder(); AssertionHelper.Verify(delegate { if (value == null) { return(new AssertionFailureBuilder("Could not serialize the value because it is null.") .SetMessage(messageFormat, messageArgs) .ToAssertionFailure()); } // TODO: Allow the user to specify Xml serialization options. using (XmlWriter writer = XmlWriter.Create(xml, new XmlWriterSettings() { CheckCharacters = false, CloseOutput = false, OmitXmlDeclaration = true, Indent = false, NewLineChars = "\n" })) { try { var serializer = new XmlSerializer(value.GetType()); serializer.Serialize(writer, value); return(null); } catch (Exception ex) { return(new AssertionFailureBuilder("Could not serialize the value.") .SetMessage(messageFormat, messageArgs) .AddException(ex) .AddRawLabeledValue("Value", value) .ToAssertionFailure()); } } }); return(xml.ToString()); }
/// <summary> /// Verifies that an actual value does not equal some unexpected value according to a particular comparer. /// </summary> /// <typeparam name="T">The type of value.</typeparam> /// <param name="unexpectedValue">The unexpected value.</param> /// <param name="actualValue">The actual value.</param> /// <param name="comparer">The comparer to use, or null to use the default one.</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> public static void AreNotEqual <T>(T unexpectedValue, T actualValue, EqualityComparison <T> comparer, string messageFormat, params object[] messageArgs) { AssertionHelper.Verify(delegate { if (comparer == null) { comparer = ComparisonSemantics.Default.Equals; } if (!comparer(unexpectedValue, actualValue)) { return(null); } return(new AssertionFailureBuilder("Expected values to be non-equal.") .SetMessage(messageFormat, messageArgs) .AddRawLabeledValuesWithDiffs("Unexpected Value", unexpectedValue, "Actual Value", actualValue) .ToAssertionFailure()); }); }
/// <summary> /// Performs an action as a new step within the current context and associates it /// with the specified code reference. Verifies that the step produced the expected outcome. /// </summary> /// <remarks> /// <para> /// This method creates a new child context with a new nested <see cref="Model.Tree.TestStep" />, /// enters the child context, performs the action, then exits the child context. /// </para> /// <para> /// This method may be called recursively to create nested steps or concurrently /// to create parallel steps. /// </para> /// <para> /// This method verifies that the step produced the expected outcome. If a different outcome /// was obtained, then raises an assertion failure. /// </para> /// </remarks> /// <param name="name">The name of the step.</param> /// <param name="action">The action to perform.</param> /// <param name="timeout">The step execution timeout, or null if none.</param> /// <param name="isTestCase">True if the step represents an independent test case.</param> /// <param name="codeElement">The associated code element, or null if none.</param> /// <param name="expectedOutcome">The expected outcome of the step.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="name"/> or /// <paramref name="action"/> is null.</exception> /// <returns>The context of the step that ran.</returns> /// <exception cref="ArgumentException">Thrown if <paramref name="name"/> is the empty string.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="timeout"/> is negative.</exception> /// <exception cref="AssertionFailureException">Thrown if the expected outcome was not obtained.</exception> public TestContext RunStepAndVerifyOutcome(string name, GallioAction action, TimeSpan?timeout, bool isTestCase, ICodeElementInfo codeElement, TestOutcome expectedOutcome) { TestContext childContext = RunStep(name, action, timeout, isTestCase, codeElement); AssertionHelper.Verify(() => { TestOutcome actualOutcome = childContext.Outcome; if (actualOutcome == expectedOutcome) { return(null); } return(new AssertionFailureBuilder("The test step did not produce the expected outcome.") .AddLabeledValue("Expected Outcome", expectedOutcome.ToString()) .AddLabeledValue("Actual Outcome", actualOutcome.ToString()) .ToAssertionFailure()); }); return(childContext); }
/// <summary> /// Verifies that a type may be assigned to a variable of the specified type. /// </summary> /// <remarks> /// <para> /// This assertion will fail if <paramref name="actualType"/> is null. /// </para> /// </remarks> /// <seealso cref="Type.IsAssignableFrom"/> /// <param name="expectedType">The Type to compare with the object's Type.</param> /// <param name="actualType">The Type under examination.</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="ArgumentNullException">Thrown if <paramref name="expectedType"/> or <paramref name="actualType"/> is null.</exception> public static void IsAssignableFrom(Type expectedType, Type actualType, string messageFormat, params object[] messageArgs) { if (expectedType == null) { throw new ArgumentNullException("expectedType"); } AssertionHelper.Verify(() => { if (actualType != null && actualType.IsAssignableFrom(expectedType)) { return(null); } return(new AssertionFailureBuilder("Expected the actual type to be assignable to the expected type.") .SetMessage(messageFormat, messageArgs) .AddRawLabeledValue("Expected Type", expectedType) .AddRawLabeledValue("Actual Type", actualType) .ToAssertionFailure()); }); }
private Test CreateUniformDistributionTest() { return(new TestCase("UniformDistributionTest", () => AssertionHelper.Verify(() => { var result = GetResult(); var probability = result.UniformDistributionDeviationProbability; TestLog.WriteLine("Statistical Population = {0}", result.StatisticalPopulation); TestLog.WriteLine("Deviation Probability = {0} {1} {2}", probability, ((probability <= uniformDistributionQuality) ? "≤" : ">"), uniformDistributionQuality); if (probability <= uniformDistributionQuality) { return null; } return new AssertionFailureBuilder(String.Format("The distribution is not considered uniform because the actual probability of deviation {0} is higher than the specified limit of {1}.", probability, uniformDistributionQuality)) .AddRawExpectedValue(uniformDistributionQuality) .AddRawActualValue(probability) .SetStackTrace(Context.GetStackTraceData()) .ToAssertionFailure(); }))); }
/// <summary> /// Verifies that the specified sequence, collection, or array contains the expected number of elements. /// </summary> /// <remarks> /// <para> /// The assertion counts the elements according to the underlying type of the sequence. /// <list type="bullet"> /// <item>Uses <see cref="Array.Length"/> if the sequence is an array.</item> /// <item>Uses <see cref="ICollection.Count"/> or <see cref="ICollection{T}.Count"/> if the sequence is a collection such as <see cref="List{T}"/> or <see cref="Dictionary{K,V}"/>. It enumerates and counts the elements as well.</item> /// <item>Enumerates and counts the elements if the sequence is a simple <see cref="IEnumerable"/>.</item> /// </list> /// </para> /// </remarks> /// <param name="expectedCount">The expected number of elements.</param> /// <param name="values">The enumeration of elements to count.</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="ArgumentOutOfRangeException">Thrown if <paramref name="expectedCount"/> is negative.</exception> /// <exception cref="ArgumentNullException">Thrown if <paramref name="values"/> is null.</exception> public static void Count(int expectedCount, IEnumerable values, string messageFormat, params object[] messageArgs) { if (expectedCount < 0) { throw new ArgumentOutOfRangeException("expectedCount", "The expected count value must be greater than or equal to 0."); } AssertionHelper.Verify(() => { var counter = new EnumerableCounter(values); var failures = new List <ICountingStrategy>(); foreach (ICountingStrategy strategy in counter.Count()) { if (strategy.Count != expectedCount) { failures.Add(strategy); } } if (failures.Count == 0) { return(null); } var builder = new AssertionFailureBuilder(String.Format( "Expected the sequence to contain a certain number of elements but {0} counting strateg{1} failed.", failures.Count, failures.Count > 1 ? "ies have" : "y has")) .AddRawExpectedValue(expectedCount); foreach (var failure in failures) { builder.AddRawLabeledValue(String.Format("Actual Value ({0})", failure.Description), failure.Count); } return(builder .SetMessage(messageFormat, messageArgs) .ToAssertionFailure()); }); }
///<summary> /// Asserts that an item satisfies the condition specified by matcher. ///</summary> ///<param name="item">The item to test.</param> ///<param name="matcher">A <see cref="IMatcher{T}">matcher</see>.</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> ///<typeparam name="T">The static type accepted by the matcher.</typeparam> public static void That <T>(T item, IMatcher <T> matcher, string messageFormat, params object[] messageArgs) { AssertionHelper.Verify(() => { if (matcher.Matches(item)) { return(null); } var description = new StringDescription(); var mismatchDescription = new StringDescription(); matcher.DescribeTo(description); matcher.DescribeMismatch(item, mismatchDescription); return(new AssertionFailureBuilder("Expected " + description) .SetMessage(messageFormat, messageArgs) .AddLabeledValue("Expected", description.ToString()) .AddLabeledValue("But", mismatchDescription.ToString()) .ToAssertionFailure()); }); }
private static void AssertOrder <T>(T left, T right, Comparison <T> comparer, string exceptionMessage, Func <int, bool> orderingPredicate, string messageFormat, params object[] messageArgs) { AssertionHelper.Verify(() => { if (comparer == null) { comparer = ComparisonSemantics.Default.Compare; } if (orderingPredicate(comparer(left, right))) { return(null); } return(new AssertionFailureBuilder(exceptionMessage) .SetMessage(messageFormat, messageArgs) .AddRawLabeledValue("Left Value", left) .AddRawLabeledValue("Right Value", right) .ToAssertionFailure()); }); }
private static T Deserialize <T>(Stream stream, IFormatter formatter, string messageFormat, object[] messageArgs, GallioFunc <T> originalValueProvider) { if (formatter == null) { throw new ArgumentNullException("formatter"); } T deserializedValue = default(T); AssertionHelper.Verify(delegate { if (stream == null) { return(new AssertionFailureBuilder("Could not deserialize the value because the stream is null.") .SetMessage(messageFormat, messageArgs) .ToAssertionFailure()); } try { deserializedValue = (T)formatter.Deserialize(stream); } catch (Exception ex) { var failureBuilder = new AssertionFailureBuilder("Could not deserialize the value.") .SetMessage(messageFormat, messageArgs) .AddException(ex); if (originalValueProvider != null) { failureBuilder.AddRawLabeledValue("Value", originalValueProvider()); } return(failureBuilder.ToAssertionFailure()); } return(null); }); return(deserializedValue); }
/// <summary> /// Verifies that a test value is not between left and right values according to a particular comparer. /// </summary> /// <typeparam name="T">The type of value.</typeparam> /// <param name="actualValue">The actual value.</param> /// <param name="minimum">Inclusive minimum value.</param> /// <param name="maximum">Inclusive maximum value.</param> /// <param name="comparer">The comparer to use, or null to use the default one.</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> public static void NotBetween <T>(T actualValue, T minimum, T maximum, Comparison <T> comparer, string messageFormat, params object[] messageArgs) { AssertionHelper.Verify(delegate { if (comparer == null) { comparer = ComparisonSemantics.Default.Compare; } if (comparer(actualValue, minimum) < 0 || comparer(actualValue, maximum) > 0) { return(null); } return(new AssertionFailureBuilder("The actual value should not be between the minimum and maximum values.") .SetMessage(messageFormat, messageArgs) .AddRawActualValue(actualValue) .AddRawLabeledValue("Minimum Value", minimum) .AddRawLabeledValue("Maximum Value", maximum) .ToAssertionFailure()); }); }
/// <summary> /// Evaluates a block of code that contains multiple related assertions. /// </summary> /// <remarks> /// <para> /// While the action delegate runs, the behavior of assertions is change such that /// failures are captured but do not cause a <see cref="AssertionFailureException" /> /// to be throw. When the delegate returns, the previous assertion failure behavior /// is restored and any captured assertion failures are reported. The net effect /// of this change is that the test can continue to run even after an assertion failure /// occurs which can help to provide more information about the problem. /// </para> /// <para> /// A multiple assertion block is useful for verifying the state of a single /// component with many parts that require several assertions to check. /// This feature can accelerate debugging because more diagnostic information /// become available at once. /// </para> /// </remarks> /// <param name="action">The action to invoke.</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="ArgumentNullException">Thrown if <paramref name="action"/> is null.</exception> public static void Multiple(GallioAction action, string messageFormat, params object[] messageArgs) { if (action == null) { throw new ArgumentNullException("action"); } AssertionHelper.Verify(delegate { AssertionFailure[] failures = AssertionHelper.Eval(action, AssertionFailureBehavior.Log); if (failures.Length == 0) { return(null); } string description = failures.Length == 1 ? "There was 1 failure within the multiple assertion block." : String.Format("There were {0} failures within the multiple assertion block.", failures.Length); return(new AssertionFailureBuilder(description) .SetMessage(messageFormat, messageArgs) .ToAssertionFailure()); }); }
/// <summary> /// Verifies that a type supports the Xml serialization protocol used by <see cref="XmlSerializer" />. /// </summary> /// <remarks> /// <para> /// Ensures that an <see cref="XmlSerializer"/> for the type can be constructed without error. /// </para> /// </remarks> /// <param name="type">The type to verify.</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="type"/> is null.</exception> public static void IsXmlSerializableType(Type type, string messageFormat, params object[] messageArgs) { if (type == null) { throw new ArgumentNullException("type"); } AssertionHelper.Verify(delegate { try { new XmlSerializer(type); return(null); } catch (Exception ex) { return(new AssertionFailureBuilder("Expected the type to support Xml serialization but an exception was thrown while constructing an XmlSerializer.") .SetMessage(messageFormat, messageArgs) .AddException(ex) .AddRawLabeledValue("Type", type) .ToAssertionFailure()); } }); }
/// <summary> /// Verifies that an object can be serialized to a stream using the specified <see cref="IFormatter" /> /// and returns the resulting stream. /// </summary> /// <remarks> /// <para> /// The assertion fails if <paramref name="value"/> is null. /// </para> /// </remarks> /// <param name="value">The value.</param> /// <param name="formatter">The object serialization formatter.</param> /// <returns>The serialized stream.</returns> /// <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="ArgumentNullException">Thrown if <paramref name="formatter"/> is null.</exception> /// <exception cref="AssertionException">Thrown if the verification failed unless the current <see cref="AssertionContext.AssertionFailureBehavior" /> indicates otherwise.</exception> public static MemoryStream Serialize(object value, IFormatter formatter, string messageFormat, params object[] messageArgs) { if (formatter == null) { throw new ArgumentNullException("formatter"); } MemoryStream serializationStream = new MemoryStream(); AssertionHelper.Verify(delegate { if (value == null) { return(new AssertionFailureBuilder("Could not serialize the value because it is null.") .SetMessage(messageFormat, messageArgs) .ToAssertionFailure()); } try { formatter.Serialize(serializationStream, value); serializationStream.Position = 0; return(null); } catch (Exception ex) { return(new AssertionFailureBuilder("Could not serialize the value.") .SetMessage(messageFormat, messageArgs) .AddException(ex) .AddRawLabeledValue("Value", value) .ToAssertionFailure()); } }); return(serializationStream); }