/// <summary> /// Verifies that an object is of the given type or a derived type. /// </summary> /// <param name="expectedType">The type the object should be</param> /// <param name="object">The object to be evaluated</param> /// <exception cref="IsAssignableFromException">Thrown when the object is not the given type</exception> public static void IsAssignableFrom(Type expectedType, object @object) { Assert.GuardArgumentNotNull("expectedType", expectedType); if (@object == null || !expectedType.GetTypeInfo().IsAssignableFrom(@object.GetType().GetTypeInfo())) { throw new IsAssignableFromException(expectedType, @object); } }
/// <summary> /// Verifies that two objects are equal, using a custom equatable comparer. /// </summary> /// <typeparam name="T">The type of the objects to be compared</typeparam> /// <param name="expected">The expected value</param> /// <param name="actual">The value to be compared against</param> /// <param name="comparer">The comparer used to compare the two objects</param> /// <exception cref="EqualException">Thrown when the objects are not equal</exception> public static void Equal <T>(T expected, T actual, IEqualityComparer <T> comparer) { Assert.GuardArgumentNotNull("comparer", comparer); if (!comparer.Equals(expected, actual)) { throw new EqualException(expected, actual); } }
/// <summary> /// Verifies that a string matches a regular expression. /// </summary> /// <param name="expectedRegex">The regex expected to match</param> /// <param name="actualString">The string to be inspected</param> /// <exception cref="MatchesException">Thrown when the string does not match the regex</exception> public static void Matches(Regex expectedRegex, string actualString) { Assert.GuardArgumentNotNull("expectedRegex", expectedRegex); if (actualString == null || !expectedRegex.IsMatch(actualString)) { throw new MatchesException(expectedRegex.ToString(), actualString); } }
/// <summary> /// Verifies that a set is a superset of another set. /// </summary> /// <typeparam name="T">The type of the object to be verified</typeparam> /// <param name="expectedSubset">The expected subset</param> /// <param name="actual">The set expected to be a superset</param> /// <exception cref="ContainsException">Thrown when the actual set is not a superset of the expected set</exception> public static void Superset <T>(ISet <T> expectedSubset, ISet <T> actual) { Assert.GuardArgumentNotNull("expectedSubset", expectedSubset); if (actual == null || !actual.IsSupersetOf(expectedSubset)) { throw new SupersetException(expectedSubset, actual); } }
/// <summary> /// Verifies that a collection is not empty. /// </summary> /// <param name="collection">The collection to be inspected</param> /// <exception cref="ArgumentNullException">Thrown when a null collection is passed</exception> /// <exception cref="NotEmptyException">Thrown when the collection is empty</exception> public static void NotEmpty(IEnumerable collection, string userMessage = null) { Assert.GuardArgumentNotNull("collection", collection); if (!collection.GetEnumerator().MoveNext()) { throw new NotEmptyException(userMessage); } }
/// <summary> /// Verifies that two objects are not equal, using a custom equality comparer. /// </summary> /// <typeparam name="T">The type of the objects to be compared</typeparam> /// <param name="expected">The expected object</param> /// <param name="actual">The actual object</param> /// <param name="comparer">The comparer used to examine the objects</param> /// <exception cref="NotEqualException">Thrown when the objects are equal</exception> public static void NotEqual <T>(T expected, T actual, IEqualityComparer <T> comparer) { Assert.GuardArgumentNotNull("comparer", comparer); if (comparer.Equals(expected, actual)) { throw new NotEqualException(ArgumentFormatter.Format(expected), ArgumentFormatter.Format(actual)); } }
/// <summary> /// Verifies that a string does not match a regular expression. /// </summary> /// <param name="expectedRegex">The regex expected not to match</param> /// <param name="actualString">The string to be inspected</param> /// <exception cref="DoesNotMatchException">Thrown when the string matches the regex</exception> public static void DoesNotMatch(Regex expectedRegex, string actualString) { Assert.GuardArgumentNotNull("expectedRegex", expectedRegex); if (actualString != null && expectedRegex.IsMatch(actualString)) { throw new DoesNotMatchException(expectedRegex.ToString(), actualString); } }
/// <summary> /// Verifies that a collection is empty. /// </summary> /// <param name="collection">The collection to be inspected</param> /// <exception cref="ArgumentNullException">Thrown when the collection is null</exception> /// <exception cref="EmptyException">Thrown when the collection is not empty</exception> public static void Empty(IEnumerable collection) { Assert.GuardArgumentNotNull("collection", collection); if (collection.GetEnumerator().MoveNext()) { throw new EmptyException(); } }
/// <summary> /// Verifies that a string does not match a regular expression. /// </summary> /// <param name="expectedRegexPattern">The regex pattern expected not to match</param> /// <param name="actualString">The string to be inspected</param> /// <exception cref="DoesNotMatchException">Thrown when the string matches the regex pattern</exception> public static void DoesNotMatch(string expectedRegexPattern, string actualString) { Assert.GuardArgumentNotNull("expectedRegexPattern", expectedRegexPattern); if (actualString != null && Regex.IsMatch(actualString, expectedRegexPattern)) { throw new DoesNotMatchException(expectedRegexPattern, actualString); } }
/// <summary> /// Verifies that a value is not within a given range, using a comparer. /// </summary> /// <typeparam name="T">The type of the value to be compared</typeparam> /// <param name="actual">The actual value to be evaluated</param> /// <param name="low">The (inclusive) low value of the range</param> /// <param name="high">The (inclusive) high value of the range</param> /// <param name="comparer">The comparer used to evaluate the value's range</param> /// <exception cref="NotInRangeException">Thrown when the value is in the given range</exception> public static void NotInRange <T>(T actual, T low, T high, IComparer <T> comparer) { Assert.GuardArgumentNotNull("comparer", comparer); if (comparer.Compare(low, actual) <= 0 && comparer.Compare(actual, high) <= 0) { throw new NotInRangeException(actual, low, high); } }
/// <summary> /// Verifies that an object is not exactly the given type. /// </summary> /// <param name="expectedType">The type the object should not be</param> /// <param name="object">The object to be evaluated</param> /// <exception cref="IsNotTypeException">Thrown when the object is the given type</exception> public static void IsNotType(Type expectedType, object @object) { Assert.GuardArgumentNotNull("expectedType", expectedType); if (@object != null && expectedType.Equals(@object.GetType())) { throw new IsNotTypeException(expectedType, @object); } }
/// <summary> /// Verifies that a string matches a regular expression. /// </summary> /// <param name="expectedRegexPattern">The regex pattern expected to match</param> /// <param name="actualString">The string to be inspected</param> /// <exception cref="MatchesException">Thrown when the string does not match the regex pattern</exception> public static void Matches(string expectedRegexPattern, string actualString) { Assert.GuardArgumentNotNull("expectedRegexPattern", expectedRegexPattern); if (actualString == null || !Regex.IsMatch(actualString, expectedRegexPattern)) { throw new MatchesException(expectedRegexPattern, actualString); } }
/// <summary> /// Verifies that the given collection contains only a single /// element of the given value. The collection may or may not /// contain other values. /// </summary> /// <param name="collection">The collection.</param> /// <param name="expected">The value to find in the collection.</param> /// <returns>The single item in the collection.</returns> /// <exception cref="SingleException">Thrown when the collection does not contain /// exactly one element.</exception> public static void Single(IEnumerable collection, object expected) { Assert.GuardArgumentNotNull("collection", collection); GetSingleResult(collection.Cast <object>(), item => object.Equals(item, expected), ArgumentFormatter.Format(expected), out Exception toThrow); if (toThrow != null) { throw toThrow; } }
/// <summary> /// Verifies that a dictionary contains a given key. /// </summary> /// <typeparam name="TKey">The type of the keys of the object to be verified.</typeparam> /// <typeparam name="TValue">The type of the values of the object to be verified.</typeparam> /// <param name="expected">The object expected to be in the collection.</param> /// <param name="collection">The collection to be inspected.</param> /// <returns>The value associated with <paramref name="expected"/>.</returns> /// <exception cref="ContainsException">Thrown when the object is not present in the collection</exception> public static TValue Contains <TKey, TValue>(TKey expected, IDictionary <TKey, TValue> collection) { Assert.GuardArgumentNotNull("expected", expected); Assert.GuardArgumentNotNull("collection", collection); if (!collection.TryGetValue(expected, out var value)) { throw new ContainsException(expected, collection.Keys); } return(value); }
/// <summary> /// Verifies that a collection does not contain a given object, using an equality comparer. /// </summary> /// <typeparam name="T">The type of the object to be compared</typeparam> /// <param name="expected">The object that is expected not to be in the collection</param> /// <param name="collection">The collection to be inspected</param> /// <param name="comparer">The comparer used to equate objects in the collection with the expected object</param> /// <exception cref="DoesNotContainException">Thrown when the object is present inside the container</exception> public static void DoesNotContain <T>(T expected, IEnumerable <T> collection, IEqualityComparer <T> comparer) { Assert.GuardArgumentNotNull("collection", collection); Assert.GuardArgumentNotNull("comparer", comparer); if (!collection.Contains(expected, comparer)) { return; } throw new DoesNotContainException(expected, collection); }
/// <summary> /// Verifies that the given collection contains no /// elements that match the given predicate. The collection may or may not /// contain other values. /// </summary> /// <param name="collection">The collection.</param> /// <param name="predicate">The item matching predicate.</param> /// <exception cref="NoneException">Thrown when the collection contains at least one matching element.</exception> public static void None <T>(IEnumerable <T> collection, Func <T, bool> predicate) { Assert.GuardArgumentNotNull("collection", collection); Assert.GuardArgumentNotNull("predicate", predicate); int count = collection.Count(predicate); if (count != 0) { throw new NoneException(count); } }
/// <summary> /// Verifies that a collection does not contain a given object, using an equality comparer. /// </summary> /// <typeparam name="T">The type of the object to be compared</typeparam> /// <param name="expected">The object that is expected not to be in the collection</param> /// <param name="collection">The collection to be inspected</param> /// <param name="comparer">The comparer used to equate objects in the collection with the expected object</param> /// <exception cref="DoesNotContainException">Thrown when the object is present inside the container</exception> public static void DoesNotContain <T>(T expected, IEnumerable <T> collection, IEqualityComparer <T> comparer) { Assert.GuardArgumentNotNull("collection", collection); Assert.GuardArgumentNotNull("comparer", comparer); foreach (var item in collection) { if (comparer.Equals(expected, item)) { throw new DoesNotContainException(expected, collection); } } }
/// <summary> /// Verifies that a collection does not contain a given object. /// </summary> /// <typeparam name="T">The type of the object to be compared</typeparam> /// <param name="collection">The collection to be inspected</param> /// <param name="filter">The filter used to find the item you're ensuring the collection does not contain</param> /// <exception cref="DoesNotContainException">Thrown when the object is present inside the container</exception> public static void DoesNotContain <T>(IEnumerable <T> collection, Predicate <T> filter) { Assert.GuardArgumentNotNull("collection", collection); Assert.GuardArgumentNotNull("filter", filter); foreach (var item in collection) { if (filter(item)) { throw new DoesNotContainException("(filter expression)", collection); } } }
/// <summary> /// Verifies that the given collection contains only a single /// element of the given type. /// </summary> /// <typeparam name="T">The collection type.</typeparam> /// <param name="collection">The collection.</param> /// <returns>The single item in the collection.</returns> /// <exception cref="SingleException">Thrown when the collection does not contain /// exactly one element.</exception> public static T Single <T>(IEnumerable <T> collection) { Assert.GuardArgumentNotNull("collection", collection); T result = GetSingleResult(collection, null, null, out Exception toThrow); if (toThrow != null) { throw toThrow; } return(result); }
/// <summary> /// Verifies that the given collection contains only a single /// element of the given type which matches the given predicate. The /// collection may or may not contain other values which do not /// match the given predicate. /// </summary> /// <typeparam name="T">The collection type.</typeparam> /// <param name="collection">The collection.</param> /// <param name="predicate">The item matching predicate.</param> /// <returns>The single item in the filtered collection.</returns> /// <exception cref="SingleException">Thrown when the filtered collection does /// not contain exactly one element.</exception> public static T Single <T>(IEnumerable <T> collection, Predicate <T> predicate) { Assert.GuardArgumentNotNull("collection", collection); Assert.GuardArgumentNotNull("predicate", predicate); T result = GetSingleResult(collection, predicate, "(filter expression)", out Exception toThrow); if (toThrow != null) { throw toThrow; } return(result); }
protected static Exception RecordException(Action testCode) { Assert.GuardArgumentNotNull("testCode", testCode); try { testCode(); return(null); } catch (Exception ex) { return(ex); } }
protected static async Task <Exception> RecordExceptionAsync(Func <Task> testCode) { Assert.GuardArgumentNotNull("testCode", testCode); try { await testCode(); return(null); } catch (Exception ex) { return(ex); } }
static Exception ThrowsAny(Type exceptionType, Exception exception) { Assert.GuardArgumentNotNull("exceptionType", exceptionType); if (exception == null) { throw new ThrowsException(exceptionType); } if (!exceptionType.GetTypeInfo().IsAssignableFrom(exception.GetType().GetTypeInfo())) { throw new ThrowsException(exceptionType, exception); } return(exception); }
static Exception Throws(Type exceptionType, Exception exception) { Assert.GuardArgumentNotNull("exceptionType", exceptionType); if (exception == null) { throw new ThrowsException(exceptionType); } if (!exceptionType.Equals(exception.GetType())) { throw new ThrowsException(exceptionType, exception); } return(exception); }
/// <summary> /// Verifies that a collection contains a given object, using an equality comparer. /// </summary> /// <typeparam name="T">The type of the object to be verified</typeparam> /// <param name="expected">The object expected to be in the collection</param> /// <param name="collection">The collection to be inspected</param> /// <param name="comparer">The comparer used to equate objects in the collection with the expected object</param> /// <exception cref="ContainsException">Thrown when the object is not present in the collection</exception> public static void Contains <T>(T expected, IEnumerable <T> collection, IEqualityComparer <T> comparer) { Assert.GuardArgumentNotNull("comparer", comparer); if (collection != null) { foreach (T item in collection) { if (comparer.Equals(expected, item)) { return; } } } throw new ContainsException(expected); }
/// <summary> /// Verifies that a collection is not empty. /// </summary> /// <param name="collection">The collection to be inspected</param> /// <exception cref="ArgumentNullException">Thrown when a null collection is passed</exception> /// <exception cref="NotEmptyException">Thrown when the collection is empty</exception> public static void NotEmpty(IEnumerable collection) { Assert.GuardArgumentNotNull("collection", collection); var enumerator = collection.GetEnumerator(); try { if (!enumerator.MoveNext()) { throw new NotEmptyException(); } } finally { (enumerator as IDisposable)?.Dispose(); } }
public static Exception Exception(Func <object> testCode) { Assert.GuardArgumentNotNull("testCode", testCode); try { var task = testCode() as Task; if (task != null) { task.GetAwaiter().GetResult(); } return(null); } catch (Exception ex) { return(ex); } }
protected static Exception RecordException(Func <object> testCode) { Assert.GuardArgumentNotNull("testCode", testCode); Task task; try { task = testCode() as Task; } catch (Exception ex) { return(ex); } if (task != null) { throw new InvalidOperationException("You must call Assert.ThrowsAsync, Assert.DoesNotThrowAsync, or Record.ExceptionAsync when testing async code."); } return(null); }
/// <summary> /// Verifies that the provided object raised <see cref="INotifyPropertyChanged.PropertyChanged"/> /// as a result of executing the given test code. /// </summary> /// <param name="object">The object which should raise the notification</param> /// <param name="propertyName">The property name for which the notification should be raised</param> /// <param name="testCode">The test code which should cause the notification to be raised</param> /// <exception cref="PropertyChangedException">Thrown when the notification is not raised</exception> public static void PropertyChanged(INotifyPropertyChanged @object, string propertyName, Action testCode) { Assert.GuardArgumentNotNull("object", @object); Assert.GuardArgumentNotNull("testCode", testCode); bool propertyChangeHappened = false; PropertyChangedEventHandler handler = (sender, args) => propertyChangeHappened |= propertyName.Equals(args.PropertyName, StringComparison.OrdinalIgnoreCase); @object.PropertyChanged += handler; try { testCode(); if (!propertyChangeHappened) { throw new PropertyChangedException(propertyName); } } finally { @object.PropertyChanged -= handler; } }
/// <summary> /// Verifies that the given collection contains only a single /// element of the given type which matches the given predicate. The /// collection may or may not contain other values which do not /// match the given predicate. /// </summary> /// <typeparam name="T">The collection type.</typeparam> /// <param name="collection">The collection.</param> /// <param name="predicate">The item matching predicate.</param> /// <returns>The single item in the filtered collection.</returns> /// <exception cref="SingleException">Thrown when the filtered collection does /// not contain exactly one element.</exception> public static T Single <T>(IEnumerable <T> collection, Predicate <T> predicate) { Assert.GuardArgumentNotNull("collection", collection); Assert.GuardArgumentNotNull("predicate", predicate); int count = 0; T result = default(T); foreach (T item in collection) { if (predicate(item)) { result = item; ++count; } } if (count != 1) { throw new SingleException(count); } return(result); }