Created
March 7, 2018 10:07
-
-
Save davkean/d07e62021593f3328f7583a0dce21881 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Globalization; | |
using System.Linq; | |
using System.Reflection; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
using Xunit.Sdk; | |
namespace Xunit | |
{ | |
/// <summary> | |
/// Contains various static methods that are used to verify that conditions are met during the | |
/// process of running tests. | |
/// </summary> | |
public class Assert | |
{ | |
/// <summary> | |
/// Represents a raised event after the fact. | |
/// </summary> | |
/// <typeparam name="T">The type of the event arguments.</typeparam> | |
public class RaisedEvent<T> where T : EventArgs | |
{ | |
/// <summary> | |
/// The sender of the event. | |
/// </summary> | |
public object Sender | |
{ | |
get; | |
} | |
/// <summary> | |
/// The event arguments. | |
/// </summary> | |
public T Arguments | |
{ | |
get; | |
} | |
/// <summary> | |
/// Creates a new instance of the <see cref="T:Xunit.Assert.RaisedEvent`1" /> class. | |
/// </summary> | |
/// <param name="sender">The sender of the event.</param> | |
/// <param name="args">The event arguments</param> | |
public RaisedEvent(object sender, T args) | |
{ | |
this.Sender = sender; | |
this.Arguments = args; | |
} | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="T:Xunit.Assert" /> class. | |
/// </summary> | |
protected Assert() | |
{ | |
} | |
/// <summary>Do not call this method.</summary> | |
[Obsolete("This is an override of Object.Equals(). Call Assert.Equal() instead.", true)] | |
[EditorBrowsable()] | |
public new static bool Equals(object a, object b) | |
{ | |
throw new InvalidOperationException("Assert.Equals should not be used"); | |
} | |
/// <summary>Do not call this method.</summary> | |
[Obsolete("This is an override of Object.ReferenceEquals(). Call Assert.Same() instead.", true)] | |
[EditorBrowsable()] | |
public new static bool ReferenceEquals(object a, object b) | |
{ | |
throw new InvalidOperationException("Assert.ReferenceEquals should not be used"); | |
} | |
/// <summary> | |
/// Verifies that the condition is false. | |
/// </summary> | |
/// <param name="condition">The condition to be tested</param> | |
/// <exception cref="T:Xunit.Sdk.FalseException">Thrown if the condition is not false</exception> | |
public static void False(bool condition) | |
{ | |
Assert.False((bool?)condition, (string)null); | |
} | |
/// <summary> | |
/// Verifies that the condition is false. | |
/// </summary> | |
/// <param name="condition">The condition to be tested</param> | |
/// <exception cref="T:Xunit.Sdk.FalseException">Thrown if the condition is not false</exception> | |
public static void False(bool? condition) | |
{ | |
Assert.False(condition, null); | |
} | |
/// <summary> | |
/// Verifies that the condition is false. | |
/// </summary> | |
/// <param name="condition">The condition to be tested</param> | |
/// <param name="userMessage">The message to show when the condition is not false</param> | |
/// <exception cref="T:Xunit.Sdk.FalseException">Thrown if the condition is not false</exception> | |
public static void False(bool condition, string userMessage) | |
{ | |
Assert.False((bool?)condition, userMessage); | |
} | |
/// <summary> | |
/// Verifies that the condition is false. | |
/// </summary> | |
/// <param name="condition">The condition to be tested</param> | |
/// <param name="userMessage">The message to show when the condition is not false</param> | |
/// <exception cref="T:Xunit.Sdk.FalseException">Thrown if the condition is not false</exception> | |
public static void False(bool? condition, string userMessage) | |
{ | |
if ((!condition) ?? false) | |
{ | |
return; | |
} | |
throw new FalseException(userMessage, condition); | |
} | |
/// <summary> | |
/// Verifies that an expression is true. | |
/// </summary> | |
/// <param name="condition">The condition to be inspected</param> | |
/// <exception cref="T:Xunit.Sdk.TrueException">Thrown when the condition is false</exception> | |
public static void True(bool condition) | |
{ | |
Assert.True((bool?)condition, (string)null); | |
} | |
/// <summary> | |
/// Verifies that an expression is true. | |
/// </summary> | |
/// <param name="condition">The condition to be inspected</param> | |
/// <exception cref="T:Xunit.Sdk.TrueException">Thrown when the condition is false</exception> | |
public static void True(bool? condition) | |
{ | |
Assert.True(condition, null); | |
} | |
/// <summary> | |
/// Verifies that an expression is true. | |
/// </summary> | |
/// <param name="condition">The condition to be inspected</param> | |
/// <param name="userMessage">The message to be shown when the condition is false</param> | |
/// <exception cref="T:Xunit.Sdk.TrueException">Thrown when the condition is false</exception> | |
public static void True(bool condition, string userMessage) | |
{ | |
Assert.True((bool?)condition, userMessage); | |
} | |
/// <summary> | |
/// Verifies that an expression is true. | |
/// </summary> | |
/// <param name="condition">The condition to be inspected</param> | |
/// <param name="userMessage">The message to be shown when the condition is false</param> | |
/// <exception cref="T:Xunit.Sdk.TrueException">Thrown when the condition is false</exception> | |
public static void True(bool? condition, string userMessage) | |
{ | |
if (condition ?? false) | |
{ | |
return; | |
} | |
throw new TrueException(userMessage, condition); | |
} | |
/// <summary> | |
/// Verifies that all items in the collection pass when executed against | |
/// action. | |
/// </summary> | |
/// <typeparam name="T">The type of the object to be verified</typeparam> | |
/// <param name="collection">The collection</param> | |
/// <param name="action">The action to test each item against</param> | |
/// <exception cref="T:Xunit.Sdk.AllException">Thrown when the collection contains at least one non-matching element</exception> | |
public static void All<T>(IEnumerable<T> collection, Action<T> action) | |
{ | |
//IL_0016: Unknown result type (might be due to invalid IL) | |
//IL_001b: Unknown result type (might be due to invalid IL) | |
//IL_0038: Unknown result type (might be due to invalid IL) | |
//IL_004c: Expected Ref, but got Unknown | |
//IL_005d: Unknown result type (might be due to invalid IL) | |
//IL_005e: Expected Ref, but got Unknown | |
//IL_0069: Unknown result type (might be due to invalid IL) | |
//IL_006a: Expected Ref, but got Unknown | |
Assert.GuardArgumentNotNull("collection", collection); | |
Assert.GuardArgumentNotNull("action", action); | |
Stack val = new Stack(); | |
T[] array = collection.ToArray(); | |
for (int i = 0; i < array.Length; i++) | |
{ | |
try | |
{ | |
action(array[i]); | |
} | |
catch (Exception item) | |
{ | |
((Stack)(?)val).Push((!0)new Tuple<int, object, Exception>(i, (object)array[i], item)); | |
} | |
} | |
if (((Stack)(?) val).get_Count() <= 0) | |
{ | |
return; | |
} | |
throw new AllException(array.Length, (Tuple<int, object, Exception>[])((Stack)(?) val).ToArray()); | |
} | |
/// <summary> | |
/// Verifies that a collection contains exactly a given number of elements, which meet | |
/// the criteria provided by the element inspectors. | |
/// </summary> | |
/// <typeparam name="T">The type of the object to be verified</typeparam> | |
/// <param name="collection">The collection to be inspected</param> | |
/// <param name="elementInspectors">The element inspectors, which inspect each element in turn. The | |
/// total number of element inspectors must exactly match the number of elements in the collection.</param> | |
public static void Collection<T>(IEnumerable<T> collection, params Action<T>[] elementInspectors) | |
{ | |
T[] array = collection.ToArray(); | |
int num = elementInspectors.Length; | |
int num2 = array.Length; | |
if (num != num2) | |
{ | |
throw new CollectionException(collection, num, num2, -1, null); | |
} | |
for (int i = 0; i < num2; i++) | |
{ | |
try | |
{ | |
elementInspectors[i](array[i]); | |
} | |
catch (Exception innerException) | |
{ | |
throw new CollectionException(collection, num, num2, i, innerException); | |
} | |
} | |
} | |
/// <summary> | |
/// Verifies that a collection contains a given object. | |
/// </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> | |
/// <exception cref="T:Xunit.Sdk.ContainsException">Thrown when the object is not present in the collection</exception> | |
public static void Contains<T>(T expected, IEnumerable<T> collection) | |
{ | |
ICollection<T> collection2 = collection as ICollection<T>; | |
if (collection2 != null && collection2.Contains(expected)) | |
{ | |
return; | |
} | |
Assert.Contains(expected, collection, Assert.GetEqualityComparer<T>(null)); | |
} | |
/// <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="T:Xunit.Sdk.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); | |
Assert.GuardArgumentNotNull("collection", collection); | |
if (!collection.Contains(expected, comparer)) | |
{ | |
throw new ContainsException(expected, collection); | |
} | |
} | |
/// <summary> | |
/// Verifies that a collection contains a given object. | |
/// </summary> | |
/// <typeparam name="T">The type of the object to be verified</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 contains</param> | |
/// <exception cref="T:Xunit.Sdk.ContainsException">Thrown when the object is not present in the collection</exception> | |
public static void Contains<T>(IEnumerable<T> collection, Predicate<T> filter) | |
{ | |
Assert.GuardArgumentNotNull("collection", collection); | |
Assert.GuardArgumentNotNull("filter", filter); | |
foreach (T item in collection) | |
{ | |
if (filter(item)) | |
{ | |
return; | |
} | |
} | |
throw new ContainsException("(filter expression)", 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="expected">The object that is expected not to be in the collection</param> | |
/// <param name="collection">The collection to be inspected</param> | |
/// <exception cref="T:Xunit.Sdk.DoesNotContainException">Thrown when the object is present inside the container</exception> | |
public static void DoesNotContain<T>(T expected, IEnumerable<T> collection) | |
{ | |
ICollection<T> collection2 = collection as ICollection<T>; | |
if (collection2 != null && collection2.Contains(expected)) | |
{ | |
throw new DoesNotContainException(expected, collection); | |
} | |
Assert.DoesNotContain(expected, collection, Assert.GetEqualityComparer<T>(null)); | |
} | |
/// <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="T:Xunit.Sdk.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)) | |
{ | |
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="T:Xunit.Sdk.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 (T item in collection) | |
{ | |
if (filter(item)) | |
{ | |
throw new DoesNotContainException("(filter expression)", collection); | |
} | |
} | |
} | |
/// <summary> | |
/// Verifies that a collection is empty. | |
/// </summary> | |
/// <param name="collection">The collection to be inspected</param> | |
/// <exception cref="T:System.ArgumentNullException">Thrown when the collection is null</exception> | |
/// <exception cref="T:Xunit.Sdk.EmptyException">Thrown when the collection is not empty</exception> | |
public static void Empty(IEnumerable collection) | |
{ | |
Assert.GuardArgumentNotNull("collection", collection); | |
IEnumerator enumerator = collection.GetEnumerator(); | |
try | |
{ | |
if (enumerator.MoveNext()) | |
{ | |
throw new EmptyException(collection); | |
} | |
} | |
finally | |
{ | |
IDisposable disposable = enumerator as IDisposable; | |
if (disposable != null) | |
{ | |
disposable.Dispose(); | |
} | |
} | |
} | |
/// <summary> | |
/// Verifies that two sequences are equivalent, using a default 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> | |
/// <exception cref="T:Xunit.Sdk.EqualException">Thrown when the objects are not equal</exception> | |
public static void Equal<T>(IEnumerable<T> expected, IEnumerable<T> actual) | |
{ | |
Assert.Equal(expected, actual, Assert.GetEqualityComparer<IEnumerable<T>>(null)); | |
} | |
/// <summary> | |
/// Verifies that two sequences are equivalent, 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="T:Xunit.Sdk.EqualException">Thrown when the objects are not equal</exception> | |
public static void Equal<T>(IEnumerable<T> expected, IEnumerable<T> actual, IEqualityComparer<T> comparer) | |
{ | |
Assert.Equal(expected, actual, Assert.GetEqualityComparer<IEnumerable<T>>(new AssertEqualityComparerAdapter<T>(comparer))); | |
} | |
/// <summary> | |
/// Verifies that a collection is not empty. | |
/// </summary> | |
/// <param name="collection">The collection to be inspected</param> | |
/// <exception cref="T:System.ArgumentNullException">Thrown when a null collection is passed</exception> | |
/// <exception cref="T:Xunit.Sdk.NotEmptyException">Thrown when the collection is empty</exception> | |
public static void NotEmpty(IEnumerable collection) | |
{ | |
Assert.GuardArgumentNotNull("collection", collection); | |
IEnumerator enumerator = collection.GetEnumerator(); | |
try | |
{ | |
if (!enumerator.MoveNext()) | |
{ | |
throw new NotEmptyException(); | |
} | |
} | |
finally | |
{ | |
IDisposable disposable = enumerator as IDisposable; | |
if (disposable != null) | |
{ | |
disposable.Dispose(); | |
} | |
} | |
} | |
/// <summary> | |
/// Verifies that two sequences are not equivalent, using a default 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> | |
/// <exception cref="T:Xunit.Sdk.NotEqualException">Thrown when the objects are equal</exception> | |
public static void NotEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual) | |
{ | |
Assert.NotEqual(expected, actual, Assert.GetEqualityComparer<IEnumerable<T>>(null)); | |
} | |
/// <summary> | |
/// Verifies that two sequences are not equivalent, 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 compare the two objects</param> | |
/// <exception cref="T:Xunit.Sdk.NotEqualException">Thrown when the objects are equal</exception> | |
public static void NotEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual, IEqualityComparer<T> comparer) | |
{ | |
Assert.NotEqual(expected, actual, Assert.GetEqualityComparer<IEnumerable<T>>(new AssertEqualityComparerAdapter<T>(comparer))); | |
} | |
/// <summary> | |
/// Verifies that the given collection contains only a single | |
/// element of the given type. | |
/// </summary> | |
/// <param name="collection">The collection.</param> | |
/// <returns>The single item in the collection.</returns> | |
/// <exception cref="T:Xunit.Sdk.SingleException">Thrown when the collection does not contain | |
/// exactly one element.</exception> | |
public static object Single(IEnumerable collection) | |
{ | |
return Assert.Single(collection.Cast<object>()); | |
} | |
/// <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="T:Xunit.Sdk.SingleException">Thrown when the collection does not contain | |
/// exactly one element.</exception> | |
public static void Single(IEnumerable collection, object expected) | |
{ | |
Assert.Single(collection.Cast<object>(), (object item) => object.Equals(item, expected)); | |
} | |
/// <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="T:Xunit.Sdk.SingleException">Thrown when the collection does not contain | |
/// exactly one element.</exception> | |
public static T Single<T>(IEnumerable<T> collection) | |
{ | |
return Assert.Single(collection, (Predicate<T>)((T item) => true)); | |
} | |
/// <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="T:Xunit.Sdk.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 num = 0; | |
T result = default(T); | |
foreach (T item in collection) | |
{ | |
if (predicate(item)) | |
{ | |
result = item; | |
num++; | |
} | |
} | |
if (num != 1) | |
{ | |
throw new SingleException(num); | |
} | |
return result; | |
} | |
private static IComparer<T> GetComparer<T>() where T : IComparable | |
{ | |
return new AssertComparer<T>(); | |
} | |
private static IEqualityComparer<T> GetEqualityComparer<T>(IEqualityComparer innerComparer = null) | |
{ | |
return new AssertEqualityComparer<T>(innerComparer); | |
} | |
/// <summary> | |
/// Verifies that two objects are equal, using a default 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> | |
/// <exception cref="T:Xunit.Sdk.EqualException">Thrown when the objects are not equal</exception> | |
public static void Equal<T>(T expected, T actual) | |
{ | |
Assert.Equal(expected, actual, Assert.GetEqualityComparer<T>(null)); | |
} | |
/// <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="T:Xunit.Sdk.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)) | |
{ | |
return; | |
} | |
throw new EqualException(expected, actual); | |
} | |
/// <summary> | |
/// Verifies that two <see cref="T:System.Double" /> values are equal, within the number of decimal | |
/// places given by <paramref name="precision" />. | |
/// </summary> | |
/// <param name="expected">The expected value</param> | |
/// <param name="actual">The value to be compared against</param> | |
/// <param name="precision">The number of decimal places (valid values: 0-15)</param> | |
/// <exception cref="T:Xunit.Sdk.EqualException">Thrown when the values are not equal</exception> | |
public static void Equal(double expected, double actual, int precision) | |
{ | |
double num = Math.Round(expected, precision); | |
double num2 = Math.Round(actual, precision); | |
if (Assert.GetEqualityComparer<double>(null).Equals(num, num2)) | |
{ | |
return; | |
} | |
throw new EqualException(string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", new object[2] | |
{ | |
num, | |
expected | |
}), string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", new object[2] | |
{ | |
num2, | |
actual | |
})); | |
} | |
/// <summary> | |
/// Verifies that two <see cref="T:System.Decimal" /> values are equal, within the number of decimal | |
/// places given by <paramref name="precision" />. | |
/// </summary> | |
/// <param name="expected">The expected value</param> | |
/// <param name="actual">The value to be compared against</param> | |
/// <param name="precision">The number of decimal places (valid values: 0-28)</param> | |
/// <exception cref="T:Xunit.Sdk.EqualException">Thrown when the values are not equal</exception> | |
public static void Equal(decimal expected, decimal actual, int precision) | |
{ | |
decimal num = Math.Round(expected, precision); | |
decimal num2 = Math.Round(actual, precision); | |
if (Assert.GetEqualityComparer<decimal>(null).Equals(num, num2)) | |
{ | |
return; | |
} | |
throw new EqualException(string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", new object[2] | |
{ | |
num, | |
expected | |
}), string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", new object[2] | |
{ | |
num2, | |
actual | |
})); | |
} | |
/// <summary> | |
/// Verifies that two objects are strictly equal, using the type's default 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> | |
/// <exception cref="T:Xunit.Sdk.EqualException">Thrown when the objects are not equal</exception> | |
public static void StrictEqual<T>(T expected, T actual) | |
{ | |
Assert.Equal(expected, actual, EqualityComparer<T>.Default); | |
} | |
/// <summary> | |
/// Verifies that two objects are not equal, using a default 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> | |
/// <exception cref="T:Xunit.Sdk.NotEqualException">Thrown when the objects are equal</exception> | |
public static void NotEqual<T>(T expected, T actual) | |
{ | |
Assert.NotEqual(expected, actual, Assert.GetEqualityComparer<T>(null)); | |
} | |
/// <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="T:Xunit.Sdk.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)) | |
{ | |
return; | |
} | |
throw new NotEqualException(ArgumentFormatter.Format(expected), ArgumentFormatter.Format(actual)); | |
} | |
/// <summary> | |
/// Verifies that two <see cref="T:System.Double" /> values are not equal, within the number of decimal | |
/// places given by <paramref name="precision" />. | |
/// </summary> | |
/// <param name="expected">The expected value</param> | |
/// <param name="actual">The value to be compared against</param> | |
/// <param name="precision">The number of decimal places (valid values: 0-15)</param> | |
/// <exception cref="T:Xunit.Sdk.EqualException">Thrown when the values are equal</exception> | |
public static void NotEqual(double expected, double actual, int precision) | |
{ | |
double num = Math.Round(expected, precision); | |
double num2 = Math.Round(actual, precision); | |
if (!Assert.GetEqualityComparer<double>(null).Equals(num, num2)) | |
{ | |
return; | |
} | |
throw new NotEqualException(string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", new object[2] | |
{ | |
num, | |
expected | |
}), string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", new object[2] | |
{ | |
num2, | |
actual | |
})); | |
} | |
/// <summary> | |
/// Verifies that two <see cref="T:System.Decimal" /> values are not equal, within the number of decimal | |
/// places given by <paramref name="precision" />. | |
/// </summary> | |
/// <param name="expected">The expected value</param> | |
/// <param name="actual">The value to be compared against</param> | |
/// <param name="precision">The number of decimal places (valid values: 0-28)</param> | |
/// <exception cref="T:Xunit.Sdk.EqualException">Thrown when the values are equal</exception> | |
public static void NotEqual(decimal expected, decimal actual, int precision) | |
{ | |
decimal num = Math.Round(expected, precision); | |
decimal num2 = Math.Round(actual, precision); | |
if (!Assert.GetEqualityComparer<decimal>(null).Equals(num, num2)) | |
{ | |
return; | |
} | |
throw new NotEqualException(string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", new object[2] | |
{ | |
num, | |
expected | |
}), string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", new object[2] | |
{ | |
num2, | |
actual | |
})); | |
} | |
/// <summary> | |
/// Verifies that two objects are strictly not equal, using the type's default 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> | |
/// <exception cref="T:Xunit.Sdk.NotEqualException">Thrown when the objects are equal</exception> | |
public static void NotStrictEqual<T>(T expected, T actual) | |
{ | |
Assert.NotEqual(expected, actual, EqualityComparer<T>.Default); | |
} | |
/// <summary> | |
/// Verifies that a event with the exact event args is raised. | |
/// </summary> | |
/// <typeparam name="T">The type of the event arguments to expect</typeparam> | |
/// <param name="attach">Code to attach the event handler</param> | |
/// <param name="detach">Code to detach the event handler</param> | |
/// <param name="testCode">A delegate to the code to be tested</param> | |
/// <returns>The event sender and arguments wrapped in an object</returns> | |
/// <exception cref="T:Xunit.Sdk.RaisesException">Thrown when the expected event was not raised.</exception> | |
public static RaisedEvent<T> Raises<T>(Action<EventHandler<T>> attach, Action<EventHandler<T>> detach, Action testCode) where T : EventArgs | |
{ | |
RaisedEvent<T> raisedEvent = Assert.RaisesInternal(attach, detach, testCode); | |
if (raisedEvent == null) | |
{ | |
throw new RaisesException(typeof(T)); | |
} | |
if (!raisedEvent.Arguments.GetType().Equals(typeof(T))) | |
{ | |
throw new RaisesException(typeof(T), raisedEvent.Arguments.GetType()); | |
} | |
return raisedEvent; | |
} | |
/// <summary> | |
/// Verifies that an event with the exact or a derived event args is raised. | |
/// </summary> | |
/// <typeparam name="T">The type of the event arguments to expect</typeparam> | |
/// <param name="attach">Code to attach the event handler</param> | |
/// <param name="detach">Code to detach the event handler</param> | |
/// <param name="testCode">A delegate to the code to be tested</param> | |
/// <returns>The event sender and arguments wrapped in an object</returns> | |
/// <exception cref="T:Xunit.Sdk.RaisesException">Thrown when the expected event was not raised.</exception> | |
public static RaisedEvent<T> RaisesAny<T>(Action<EventHandler<T>> attach, Action<EventHandler<T>> detach, Action testCode) where T : EventArgs | |
{ | |
RaisedEvent<T> raisedEvent = Assert.RaisesInternal(attach, detach, testCode); | |
if (raisedEvent == null) | |
{ | |
throw new RaisesException(typeof(T)); | |
} | |
return raisedEvent; | |
} | |
/// <summary> | |
/// Verifies that a event with the exact event args (and not a derived type) is raised. | |
/// </summary> | |
/// <typeparam name="T">The type of the event arguments to expect</typeparam> | |
/// <param name="attach">Code to attach the event handler</param> | |
/// <param name="detach">Code to detach the event handler</param> | |
/// <param name="testCode">A delegate to the code to be tested</param> | |
/// <returns>The event sender and arguments wrapped in an object</returns> | |
/// <exception cref="T:Xunit.Sdk.RaisesException">Thrown when the expected event was not raised.</exception> | |
public static async Task<RaisedEvent<T>> RaisesAsync<T>(Action<EventHandler<T>> attach, Action<EventHandler<T>> detach, Func<Task> testCode) where T : EventArgs | |
{ | |
RaisedEvent<T> raisedEvent = await Assert.RaisesAsyncInternal<T>(attach, detach, testCode); | |
if (raisedEvent == null) | |
{ | |
throw new RaisesException(typeof(T)); | |
} | |
if (!raisedEvent.Arguments.GetType().Equals(typeof(T))) | |
{ | |
throw new RaisesException(typeof(T), raisedEvent.Arguments.GetType()); | |
} | |
return raisedEvent; | |
} | |
/// <summary> | |
/// Verifies that an event with the exact or a derived event args is raised. | |
/// </summary> | |
/// <typeparam name="T">The type of the event arguments to expect</typeparam> | |
/// <param name="attach">Code to attach the event handler</param> | |
/// <param name="detach">Code to detach the event handler</param> | |
/// <param name="testCode">A delegate to the code to be tested</param> | |
/// <returns>The event sender and arguments wrapped in an object</returns> | |
/// <exception cref="T:Xunit.Sdk.RaisesException">Thrown when the expected event was not raised.</exception> | |
public static async Task<RaisedEvent<T>> RaisesAnyAsync<T>(Action<EventHandler<T>> attach, Action<EventHandler<T>> detach, Func<Task> testCode) where T : EventArgs | |
{ | |
RaisedEvent<T> obj = await Assert.RaisesAsyncInternal<T>(attach, detach, testCode); | |
if (obj == null) | |
{ | |
throw new RaisesException(typeof(T)); | |
} | |
return obj; | |
} | |
private static RaisedEvent<T> RaisesInternal<T>(Action<EventHandler<T>> attach, Action<EventHandler<T>> detach, Action testCode) where T : EventArgs | |
{ | |
RaisedEvent<T> raisedEvent = null; | |
EventHandler<T> obj = delegate (object s, T args) | |
{ | |
raisedEvent = new RaisedEvent<T>(s, args); | |
}; | |
attach(obj); | |
testCode(); | |
detach(obj); | |
return (RaisedEvent<T>)raisedEvent; | |
} | |
private static async Task<RaisedEvent<T>> RaisesAsyncInternal<T>(Action<EventHandler<T>> attach, Action<EventHandler<T>> detach, Func<Task> testCode) where T : EventArgs | |
{ | |
RaisedEvent<T> raisedEvent = null; | |
EventHandler<T> handler = delegate (object s, T args) | |
{ | |
raisedEvent = new RaisedEvent<T>(s, args); | |
}; | |
attach(handler); | |
await testCode(); | |
detach(handler); | |
return (RaisedEvent<T>)raisedEvent; | |
} | |
/// <summary> | |
/// Verifies that the exact exception is thrown (and not a derived exception type). | |
/// </summary> | |
/// <typeparam name="T">The type of the exception expected to be thrown</typeparam> | |
/// <param name="testCode">A delegate to the code to be tested</param> | |
/// <returns>The exception that was thrown, when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception> | |
public static T Throws<T>(Action testCode) where T : Exception | |
{ | |
return (T)Assert.Throws(typeof(T), Assert.RecordException(testCode)); | |
} | |
/// <summary> | |
/// Verifies that the exact exception is thrown (and not a derived exception type). | |
/// Generally used to test property accessors. | |
/// </summary> | |
/// <typeparam name="T">The type of the exception expected to be thrown</typeparam> | |
/// <param name="testCode">A delegate to the code to be tested</param> | |
/// <returns>The exception that was thrown, when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception> | |
public static T Throws<T>(Func<object> testCode) where T : Exception | |
{ | |
return (T)Assert.Throws(typeof(T), Assert.RecordException(testCode)); | |
} | |
/// <summary /> | |
[EditorBrowsable()] | |
[Obsolete("You must call Assert.ThrowsAsync<T> (and await the result) when testing async code.", true)] | |
public static T Throws<T>(Func<Task> testCode) where T : Exception | |
{ | |
throw new NotImplementedException(); | |
} | |
/// <summary> | |
/// Verifies that the exact exception is thrown (and not a derived exception type). | |
/// </summary> | |
/// <typeparam name="T">The type of the exception expected to be thrown</typeparam> | |
/// <param name="testCode">A delegate to the task to be tested</param> | |
/// <returns>The exception that was thrown, when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception> | |
public static async Task<T> ThrowsAsync<T>(Func<Task> testCode) where T : Exception | |
{ | |
Type typeFromHandle = typeof(T); | |
return (T)Assert.Throws(typeFromHandle, await Assert.RecordExceptionAsync(testCode)); | |
} | |
/// <summary> | |
/// Verifies that the exact exception or a derived exception type is thrown. | |
/// </summary> | |
/// <typeparam name="T">The type of the exception expected to be thrown</typeparam> | |
/// <param name="testCode">A delegate to the code to be tested</param> | |
/// <returns>The exception that was thrown, when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception> | |
public static T ThrowsAny<T>(Action testCode) where T : Exception | |
{ | |
return (T)Assert.ThrowsAny(typeof(T), Assert.RecordException(testCode)); | |
} | |
/// <summary> | |
/// Verifies that the exact exception or a derived exception type is thrown. | |
/// Generally used to test property accessors. | |
/// </summary> | |
/// <typeparam name="T">The type of the exception expected to be thrown</typeparam> | |
/// <param name="testCode">A delegate to the code to be tested</param> | |
/// <returns>The exception that was thrown, when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception> | |
public static T ThrowsAny<T>(Func<object> testCode) where T : Exception | |
{ | |
return (T)Assert.ThrowsAny(typeof(T), Assert.RecordException(testCode)); | |
} | |
/// <summary> | |
/// Verifies that the exact exception or a derived exception type is thrown. | |
/// </summary> | |
/// <typeparam name="T">The type of the exception expected to be thrown</typeparam> | |
/// <param name="testCode">A delegate to the task to be tested</param> | |
/// <returns>The exception that was thrown, when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception> | |
public static async Task<T> ThrowsAnyAsync<T>(Func<Task> testCode) where T : Exception | |
{ | |
Type typeFromHandle = typeof(T); | |
return (T)Assert.ThrowsAny(typeFromHandle, await Assert.RecordExceptionAsync(testCode)); | |
} | |
/// <summary> | |
/// Verifies that the exact exception is thrown (and not a derived exception type). | |
/// </summary> | |
/// <param name="exceptionType">The type of the exception expected to be thrown</param> | |
/// <param name="testCode">A delegate to the code to be tested</param> | |
/// <returns>The exception that was thrown, when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception> | |
public static Exception Throws(Type exceptionType, Action testCode) | |
{ | |
return Assert.Throws(exceptionType, Assert.RecordException(testCode)); | |
} | |
/// <summary> | |
/// Verifies that the exact exception is thrown (and not a derived exception type). | |
/// Generally used to test property accessors. | |
/// </summary> | |
/// <param name="exceptionType">The type of the exception expected to be thrown</param> | |
/// <param name="testCode">A delegate to the code to be tested</param> | |
/// <returns>The exception that was thrown, when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception> | |
public static Exception Throws(Type exceptionType, Func<object> testCode) | |
{ | |
return Assert.Throws(exceptionType, Assert.RecordException(testCode)); | |
} | |
/// <summary> | |
/// Verifies that the exact exception is thrown (and not a derived exception type). | |
/// </summary> | |
/// <param name="exceptionType">The type of the exception expected to be thrown</param> | |
/// <param name="testCode">A delegate to the task to be tested</param> | |
/// <returns>The exception that was thrown, when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception> | |
public static async Task<Exception> ThrowsAsync(Type exceptionType, Func<Task> testCode) | |
{ | |
return Assert.Throws(exceptionType, await Assert.RecordExceptionAsync(testCode)); | |
} | |
private static Exception Throws(Type exceptionType, Exception exception) | |
{ | |
Assert.GuardArgumentNotNull("exceptionType", exceptionType); | |
if (exception == null) | |
{ | |
throw new ThrowsException(exceptionType); | |
} | |
if (!exceptionType.Equals(((object)exception).GetType())) | |
{ | |
throw new ThrowsException(exceptionType, exception); | |
} | |
return exception; | |
} | |
private static Exception ThrowsAny(Type exceptionType, Exception exception) | |
{ | |
Assert.GuardArgumentNotNull("exceptionType", exceptionType); | |
if (exception == null) | |
{ | |
throw new ThrowsException(exceptionType); | |
} | |
if (!exceptionType.GetTypeInfo().IsAssignableFrom(((object)exception).GetType().GetTypeInfo())) | |
{ | |
throw new ThrowsException(exceptionType, exception); | |
} | |
return exception; | |
} | |
/// <summary> | |
/// Verifies that the exact exception is thrown (and not a derived exception type), where the exception | |
/// derives from <see cref="T:System.ArgumentException" /> and has the given parameter name. | |
/// </summary> | |
/// <param name="paramName">The parameter name that is expected to be in the exception</param> | |
/// <param name="testCode">A delegate to the code to be tested</param> | |
/// <returns>The exception that was thrown, when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception> | |
public static T Throws<T>(string paramName, Action testCode) where T : ArgumentException | |
{ | |
T val = Assert.Throws<T>(testCode); | |
Assert.Equal(paramName, val.ParamName); | |
return val; | |
} | |
/// <summary> | |
/// Verifies that the exact exception is thrown (and not a derived exception type), where the exception | |
/// derives from <see cref="T:System.ArgumentException" /> and has the given parameter name. | |
/// </summary> | |
/// <param name="paramName">The parameter name that is expected to be in the exception</param> | |
/// <param name="testCode">A delegate to the code to be tested</param> | |
/// <returns>The exception that was thrown, when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception> | |
public static T Throws<T>(string paramName, Func<object> testCode) where T : ArgumentException | |
{ | |
T val = Assert.Throws<T>(testCode); | |
Assert.Equal(paramName, val.ParamName); | |
return val; | |
} | |
/// <summary /> | |
[EditorBrowsable()] | |
[Obsolete("You must call Assert.ThrowsAsync<T> (and await the result) when testing async code.", true)] | |
public static T Throws<T>(string paramName, Func<Task> testCode) where T : ArgumentException | |
{ | |
throw new NotImplementedException(); | |
} | |
/// <summary> | |
/// Verifies that the exact exception is thrown (and not a derived exception type), where the exception | |
/// derives from <see cref="T:System.ArgumentException" /> and has the given parameter name. | |
/// </summary> | |
/// <param name="paramName">The parameter name that is expected to be in the exception</param> | |
/// <param name="testCode">A delegate to the task to be tested</param> | |
/// <returns>The exception that was thrown, when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception> | |
public static async Task<T> ThrowsAsync<T>(string paramName, Func<Task> testCode) where T : ArgumentException | |
{ | |
T val = await Assert.ThrowsAsync<T>(testCode); | |
Assert.Equal(paramName, val.ParamName); | |
return val; | |
} | |
/// <summary /> | |
internal static void GuardArgumentNotNull(string argName, object argValue) | |
{ | |
if (argValue != null) | |
{ | |
return; | |
} | |
throw new ArgumentNullException(argName); | |
} | |
/// <summary> | |
/// Verifies that two objects are not the same instance. | |
/// </summary> | |
/// <param name="expected">The expected object instance</param> | |
/// <param name="actual">The actual object instance</param> | |
/// <exception cref="T:Xunit.Sdk.NotSameException">Thrown when the objects are the same instance</exception> | |
public static void NotSame(object expected, object actual) | |
{ | |
if (expected != actual) | |
{ | |
return; | |
} | |
throw new NotSameException(); | |
} | |
/// <summary> | |
/// Verifies that two objects are the same instance. | |
/// </summary> | |
/// <param name="expected">The expected object instance</param> | |
/// <param name="actual">The actual object instance</param> | |
/// <exception cref="T:Xunit.Sdk.SameException">Thrown when the objects are not the same instance</exception> | |
public static void Same(object expected, object actual) | |
{ | |
if (expected == actual) | |
{ | |
return; | |
} | |
throw new SameException(expected, actual); | |
} | |
/// <summary> | |
/// Verifies that an object reference is not null. | |
/// </summary> | |
/// <param name="object">The object to be validated</param> | |
/// <exception cref="T:Xunit.Sdk.NotNullException">Thrown when the object is not null</exception> | |
public static void NotNull(object @object) | |
{ | |
if (@object != null) | |
{ | |
return; | |
} | |
throw new NotNullException(); | |
} | |
/// <summary> | |
/// Verifies that an object reference is null. | |
/// </summary> | |
/// <param name="object">The object to be inspected</param> | |
/// <exception cref="T:Xunit.Sdk.NullException">Thrown when the object reference is not null</exception> | |
public static void Null(object @object) | |
{ | |
if (@object == null) | |
{ | |
return; | |
} | |
throw new NullException(@object); | |
} | |
/// <summary> | |
/// Verifies that the provided object raised <see cref="E:System.ComponentModel.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="T:Xunit.Sdk.PropertyChangedException">Thrown when the notification is not raised</exception> | |
public unsafe static void PropertyChanged(INotifyPropertyChanged @object, string propertyName, Action testCode) | |
{ | |
//IL_0012: Unknown result type (might be due to invalid IL) | |
//IL_0013: Expected O, but got Unknown | |
//IL_0031: Unknown result type (might be due to invalid IL) | |
//IL_0036: Unknown result type (might be due to invalid IL) | |
//IL_0037: Unknown result type (might be due to invalid IL) | |
//IL_0038: Unknown result type (might be due to invalid IL) | |
//IL_0039: Expected Ref, but got Unknown | |
//IL_005a: Unknown result type (might be due to invalid IL) | |
//IL_005b: Unknown result type (might be due to invalid IL) | |
//IL_005c: Expected Ref, but got Unknown | |
Assert.GuardArgumentNotNull("object", (object)(?)@object); | |
Assert.GuardArgumentNotNull("testCode", testCode); | |
bool propertyChangeHappened = false; | |
<> c__DisplayClass69_0 <> c__DisplayClass69_; | |
PropertyChangedEventHandler val = new PropertyChangedEventHandler((object)<> c__DisplayClass69_, (IntPtr)(void*)/*OpCode not supported: LdFtn*/); | |
((INotifyPropertyChanged)(?)@object).add_PropertyChanged(val); | |
try | |
{ | |
testCode(); | |
if (!propertyChangeHappened) | |
{ | |
throw new PropertyChangedException(propertyName); | |
} | |
} | |
finally | |
{ | |
((INotifyPropertyChanged)(?)@object).remove_PropertyChanged(val); | |
} | |
} | |
/// <summary /> | |
[EditorBrowsable()] | |
[Obsolete("You must call Assert.PropertyChangedAsync (and await the result) when testing async code.", true)] | |
public static void PropertyChanged(INotifyPropertyChanged @object, string propertyName, Func<Task> testCode) | |
{ | |
throw new NotImplementedException(); | |
} | |
/// <summary> | |
/// Verifies that the provided object raised <see cref="E:System.ComponentModel.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="T:Xunit.Sdk.PropertyChangedException">Thrown when the notification is not raised</exception> | |
public unsafe static async Task PropertyChangedAsync(INotifyPropertyChanged @object, string propertyName, Func<Task> testCode) | |
{ | |
//IL_0002: Unknown result type (might be due to invalid IL) | |
//IL_0003: Unknown result type (might be due to invalid IL) | |
Assert.GuardArgumentNotNull("object", (object)(?)@object); | |
Assert.GuardArgumentNotNull("testCode", testCode); | |
bool propertyChangeHappened = false; | |
<> c__DisplayClass71_0 <> c__DisplayClass71_; | |
PropertyChangedEventHandler handler = new PropertyChangedEventHandler((object)<> c__DisplayClass71_, (IntPtr)(void*)/*OpCode not supported: LdFtn*/); | |
((INotifyPropertyChanged)(?)@object).add_PropertyChanged(handler); | |
try | |
{ | |
await testCode(); | |
if (!propertyChangeHappened) | |
{ | |
throw new PropertyChangedException(propertyName); | |
} | |
} | |
finally | |
{ | |
((INotifyPropertyChanged)(?)@object).remove_PropertyChanged(handler); | |
} | |
} | |
/// <summary> | |
/// Verifies that a value is within a given range. | |
/// </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> | |
/// <exception cref="T:Xunit.Sdk.InRangeException">Thrown when the value is not in the given range</exception> | |
public static void InRange<T>(T actual, T low, T high) where T : IComparable | |
{ | |
Assert.InRange(actual, low, high, Assert.GetComparer<T>()); | |
} | |
/// <summary> | |
/// Verifies that a value is 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="T:Xunit.Sdk.InRangeException">Thrown when the value is not in the given range</exception> | |
public static void InRange<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) | |
{ | |
return; | |
} | |
throw new InRangeException(actual, low, high); | |
} | |
/// <summary> | |
/// Verifies that a value is not within a given range, using the default 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> | |
/// <exception cref="T:Xunit.Sdk.NotInRangeException">Thrown when the value is in the given range</exception> | |
public static void NotInRange<T>(T actual, T low, T high) where T : IComparable | |
{ | |
Assert.NotInRange(actual, low, high, Assert.GetComparer<T>()); | |
} | |
/// <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="T:Xunit.Sdk.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) | |
{ | |
return; | |
} | |
if (comparer.Compare(actual, high) > 0) | |
{ | |
return; | |
} | |
throw new NotInRangeException(actual, low, high); | |
} | |
/// <summary> | |
/// Records any exception which is thrown by the given code. | |
/// </summary> | |
/// <param name="testCode">The code which may thrown an exception.</param> | |
/// <returns>Returns the exception that was thrown by the code; null, otherwise.</returns> | |
protected static Exception RecordException(Action testCode) | |
{ | |
Assert.GuardArgumentNotNull("testCode", testCode); | |
try | |
{ | |
testCode(); | |
return null; | |
} | |
catch (Exception result) | |
{ | |
return result; | |
} | |
} | |
/// <summary> | |
/// Records any exception which is thrown by the given code that has | |
/// a return value. Generally used for testing property accessors. | |
/// </summary> | |
/// <param name="testCode">The code which may thrown an exception.</param> | |
/// <returns>Returns the exception that was thrown by the code; null, otherwise.</returns> | |
protected static Exception RecordException(Func<object> testCode) | |
{ | |
Assert.GuardArgumentNotNull("testCode", testCode); | |
Task task; | |
try | |
{ | |
task = (testCode() as Task); | |
} | |
catch (Exception result) | |
{ | |
return result; | |
} | |
if (task != null) | |
{ | |
throw new InvalidOperationException("You must call Assert.ThrowsAsync, Assert.DoesNotThrowAsync, or Record.ExceptionAsync when testing async code."); | |
} | |
return null; | |
} | |
/// <summary /> | |
[EditorBrowsable()] | |
[Obsolete("You must call Record.ExceptionAsync (and await the result) when testing async code.", true)] | |
protected static Exception RecordException(Func<Task> testCode) | |
{ | |
throw new NotImplementedException(); | |
} | |
/// <summary> | |
/// Records any exception which is thrown by the given task. | |
/// </summary> | |
/// <param name="testCode">The task which may thrown an exception.</param> | |
/// <returns>Returns the exception that was thrown by the code; null, otherwise.</returns> | |
protected static async Task<Exception> RecordExceptionAsync(Func<Task> testCode) | |
{ | |
Assert.GuardArgumentNotNull("testCode", testCode); | |
try | |
{ | |
await testCode(); | |
return null; | |
} | |
catch (Exception result) | |
{ | |
return result; | |
} | |
} | |
/// <summary> | |
/// Verifies that a set is a proper subset of another set. | |
/// </summary> | |
/// <typeparam name="T">The type of the object to be verified</typeparam> | |
/// <param name="expectedSuperset">The expected superset</param> | |
/// <param name="actual">The set expected to be a proper subset</param> | |
/// <exception cref="T:Xunit.Sdk.ContainsException">Thrown when the actual set is not a proper subset of the expected set</exception> | |
public static void ProperSubset<T>(ISet expectedSuperset, ISet actual) | |
{ | |
//IL_0005: Unknown result type (might be due to invalid IL) | |
//IL_0006: Expected O, but got Unknown | |
//IL_000b: Unknown result type (might be due to invalid IL) | |
//IL_000e: Unknown result type (might be due to invalid IL) | |
//IL_000f: Unknown result type (might be due to invalid IL) | |
//IL_0010: Expected O, but got Unknown | |
//IL_0010: Expected Ref, but got Unknown | |
//IL_0017: Unknown result type (might be due to invalid IL) | |
//IL_0018: Unknown result type (might be due to invalid IL) | |
//IL_0019: Expected O, but got Unknown | |
//IL_0019: Expected O, but got Unknown | |
Assert.GuardArgumentNotNull("expectedSuperset", (object)(?)expectedSuperset); | |
if (actual && ((ISet)(?)actual).IsProperSubsetOf((IEnumerable < !0 >)(?)expectedSuperset)) | |
{ | |
return; | |
} | |
throw new ProperSubsetException((IEnumerable)(?)expectedSuperset, (IEnumerable)(?)actual); | |
} | |
/// <summary> | |
/// Verifies that a set is a proper 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 proper superset</param> | |
/// <exception cref="T:Xunit.Sdk.ContainsException">Thrown when the actual set is not a proper superset of the expected set</exception> | |
public static void ProperSuperset<T>(ISet expectedSubset, ISet actual) | |
{ | |
//IL_0005: Unknown result type (might be due to invalid IL) | |
//IL_0006: Expected O, but got Unknown | |
//IL_000b: Unknown result type (might be due to invalid IL) | |
//IL_000e: Unknown result type (might be due to invalid IL) | |
//IL_000f: Unknown result type (might be due to invalid IL) | |
//IL_0010: Expected O, but got Unknown | |
//IL_0010: Expected Ref, but got Unknown | |
//IL_0017: Unknown result type (might be due to invalid IL) | |
//IL_0018: Unknown result type (might be due to invalid IL) | |
//IL_0019: Expected O, but got Unknown | |
//IL_0019: Expected O, but got Unknown | |
Assert.GuardArgumentNotNull("expectedSubset", (object)(?)expectedSubset); | |
if (actual && ((ISet)(?)actual).IsProperSupersetOf((IEnumerable < !0 >)(?)expectedSubset)) | |
{ | |
return; | |
} | |
throw new ProperSupersetException((IEnumerable)(?)expectedSubset, (IEnumerable)(?)actual); | |
} | |
/// <summary> | |
/// Verifies that a set is a subset of another set. | |
/// </summary> | |
/// <typeparam name="T">The type of the object to be verified</typeparam> | |
/// <param name="expectedSuperset">The expected superset</param> | |
/// <param name="actual">The set expected to be a subset</param> | |
/// <exception cref="T:Xunit.Sdk.ContainsException">Thrown when the actual set is not a subset of the expected set</exception> | |
public static void Subset<T>(ISet expectedSuperset, ISet actual) | |
{ | |
//IL_0005: Unknown result type (might be due to invalid IL) | |
//IL_0006: Expected O, but got Unknown | |
//IL_000b: Unknown result type (might be due to invalid IL) | |
//IL_000e: Unknown result type (might be due to invalid IL) | |
//IL_000f: Unknown result type (might be due to invalid IL) | |
//IL_0010: Expected O, but got Unknown | |
//IL_0010: Expected Ref, but got Unknown | |
//IL_0017: Unknown result type (might be due to invalid IL) | |
//IL_0018: Unknown result type (might be due to invalid IL) | |
//IL_0019: Expected O, but got Unknown | |
//IL_0019: Expected O, but got Unknown | |
Assert.GuardArgumentNotNull("expectedSuperset", (object)(?)expectedSuperset); | |
if (actual && ((ISet)(?)actual).IsSubsetOf((IEnumerable < !0 >)(?)expectedSuperset)) | |
{ | |
return; | |
} | |
throw new SubsetException((IEnumerable)(?)expectedSuperset, (IEnumerable)(?)actual); | |
} | |
/// <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="T:Xunit.Sdk.ContainsException">Thrown when the actual set is not a superset of the expected set</exception> | |
public static void Superset<T>(ISet expectedSubset, ISet actual) | |
{ | |
//IL_0005: Unknown result type (might be due to invalid IL) | |
//IL_0006: Expected O, but got Unknown | |
//IL_000b: Unknown result type (might be due to invalid IL) | |
//IL_000e: Unknown result type (might be due to invalid IL) | |
//IL_000f: Unknown result type (might be due to invalid IL) | |
//IL_0010: Expected O, but got Unknown | |
//IL_0010: Expected Ref, but got Unknown | |
//IL_0017: Unknown result type (might be due to invalid IL) | |
//IL_0018: Unknown result type (might be due to invalid IL) | |
//IL_0019: Expected O, but got Unknown | |
//IL_0019: Expected O, but got Unknown | |
Assert.GuardArgumentNotNull("expectedSubset", (object)(?)expectedSubset); | |
if (actual && ((ISet)(?)actual).IsSupersetOf((IEnumerable < !0 >)(?)expectedSubset)) | |
{ | |
return; | |
} | |
throw new SupersetException((IEnumerable)(?)expectedSubset, (IEnumerable)(?)actual); | |
} | |
/// <summary> | |
/// Verifies that a string contains a given sub-string, using the current culture. | |
/// </summary> | |
/// <param name="expectedSubstring">The sub-string expected to be in the string</param> | |
/// <param name="actualString">The string to be inspected</param> | |
/// <exception cref="T:Xunit.Sdk.ContainsException">Thrown when the sub-string is not present inside the string</exception> | |
public static void Contains(string expectedSubstring, string actualString) | |
{ | |
Assert.Contains(expectedSubstring, actualString, StringComparison.CurrentCulture); | |
} | |
/// <summary> | |
/// Verifies that a string contains a given sub-string, using the given comparison type. | |
/// </summary> | |
/// <param name="expectedSubstring">The sub-string expected to be in the string</param> | |
/// <param name="actualString">The string to be inspected</param> | |
/// <param name="comparisonType">The type of string comparison to perform</param> | |
/// <exception cref="T:Xunit.Sdk.ContainsException">Thrown when the sub-string is not present inside the string</exception> | |
public static void Contains(string expectedSubstring, string actualString, StringComparison comparisonType) | |
{ | |
if (actualString != null && actualString.IndexOf(expectedSubstring, comparisonType) >= 0) | |
{ | |
return; | |
} | |
throw new ContainsException(expectedSubstring, actualString); | |
} | |
/// <summary> | |
/// Verifies that a string does not contain a given sub-string, using the current culture. | |
/// </summary> | |
/// <param name="expectedSubstring">The sub-string which is expected not to be in the string</param> | |
/// <param name="actualString">The string to be inspected</param> | |
/// <exception cref="T:Xunit.Sdk.DoesNotContainException">Thrown when the sub-string is present inside the string</exception> | |
public static void DoesNotContain(string expectedSubstring, string actualString) | |
{ | |
Assert.DoesNotContain(expectedSubstring, actualString, StringComparison.CurrentCulture); | |
} | |
/// <summary> | |
/// Verifies that a string does not contain a given sub-string, using the current culture. | |
/// </summary> | |
/// <param name="expectedSubstring">The sub-string which is expected not to be in the string</param> | |
/// <param name="actualString">The string to be inspected</param> | |
/// <param name="comparisonType">The type of string comparison to perform</param> | |
/// <exception cref="T:Xunit.Sdk.DoesNotContainException">Thrown when the sub-string is present inside the given string</exception> | |
public static void DoesNotContain(string expectedSubstring, string actualString, StringComparison comparisonType) | |
{ | |
if (actualString == null) | |
{ | |
return; | |
} | |
if (actualString.IndexOf(expectedSubstring, comparisonType) < 0) | |
{ | |
return; | |
} | |
throw new DoesNotContainException(expectedSubstring, actualString); | |
} | |
/// <summary> | |
/// Verifies that a string starts with a given string, using the current culture. | |
/// </summary> | |
/// <param name="expectedStartString">The string expected to be at the start of the string</param> | |
/// <param name="actualString">The string to be inspected</param> | |
/// <exception cref="T:Xunit.Sdk.ContainsException">Thrown when the string does not start with the expected string</exception> | |
public static void StartsWith(string expectedStartString, string actualString) | |
{ | |
Assert.StartsWith(expectedStartString, actualString, StringComparison.CurrentCulture); | |
} | |
/// <summary> | |
/// Verifies that a string starts with a given string, using the given comparison type. | |
/// </summary> | |
/// <param name="expectedStartString">The string expected to be at the start of the string</param> | |
/// <param name="actualString">The string to be inspected</param> | |
/// <param name="comparisonType">The type of string comparison to perform</param> | |
/// <exception cref="T:Xunit.Sdk.ContainsException">Thrown when the string does not start with the expected string</exception> | |
public static void StartsWith(string expectedStartString, string actualString, StringComparison comparisonType) | |
{ | |
if (expectedStartString != null && actualString != null && actualString.StartsWith(expectedStartString, comparisonType)) | |
{ | |
return; | |
} | |
throw new StartsWithException(expectedStartString, actualString); | |
} | |
/// <summary> | |
/// Verifies that a string ends with a given string, using the current culture. | |
/// </summary> | |
/// <param name="expectedEndString">The string expected to be at the end of the string</param> | |
/// <param name="actualString">The string to be inspected</param> | |
/// <exception cref="T:Xunit.Sdk.ContainsException">Thrown when the string does not end with the expected string</exception> | |
public static void EndsWith(string expectedEndString, string actualString) | |
{ | |
Assert.EndsWith(expectedEndString, actualString, StringComparison.CurrentCulture); | |
} | |
/// <summary> | |
/// Verifies that a string ends with a given string, using the given comparison type. | |
/// </summary> | |
/// <param name="expectedEndString">The string expected to be at the end of the string</param> | |
/// <param name="actualString">The string to be inspected</param> | |
/// <param name="comparisonType">The type of string comparison to perform</param> | |
/// <exception cref="T:Xunit.Sdk.ContainsException">Thrown when the string does not end with the expected string</exception> | |
public static void EndsWith(string expectedEndString, string actualString, StringComparison comparisonType) | |
{ | |
if (expectedEndString != null && actualString != null && actualString.EndsWith(expectedEndString, comparisonType)) | |
{ | |
return; | |
} | |
throw new EndsWithException(expectedEndString, actualString); | |
} | |
/// <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="T:Xunit.Sdk.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)) | |
{ | |
return; | |
} | |
throw new MatchesException(expectedRegexPattern, actualString); | |
} | |
/// <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="T:Xunit.Sdk.MatchesException">Thrown when the string does not match the regex</exception> | |
public static void Matches(Regex expectedRegex, string actualString) | |
{ | |
//IL_0005: Unknown result type (might be due to invalid IL) | |
//IL_0006: Expected O, but got Unknown | |
//IL_000e: Unknown result type (might be due to invalid IL) | |
//IL_0010: Expected Ref, but got Unknown | |
//IL_0017: Unknown result type (might be due to invalid IL) | |
//IL_0018: Expected O, but got Unknown | |
Assert.GuardArgumentNotNull("expectedRegex", (object)(?)expectedRegex); | |
if (actualString != null && ((Regex)(?)expectedRegex).IsMatch(actualString)) | |
{ | |
return; | |
} | |
throw new MatchesException(((object)(?)expectedRegex).ToString(), actualString); | |
} | |
/// <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="T:Xunit.Sdk.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) | |
{ | |
return; | |
} | |
if (!Regex.IsMatch(actualString, expectedRegexPattern)) | |
{ | |
return; | |
} | |
throw new DoesNotMatchException(expectedRegexPattern, actualString); | |
} | |
/// <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="T:Xunit.Sdk.DoesNotMatchException">Thrown when the string matches the regex</exception> | |
public static void DoesNotMatch(Regex expectedRegex, string actualString) | |
{ | |
//IL_0005: Unknown result type (might be due to invalid IL) | |
//IL_0006: Expected O, but got Unknown | |
//IL_000e: Unknown result type (might be due to invalid IL) | |
//IL_0010: Expected Ref, but got Unknown | |
//IL_0017: Unknown result type (might be due to invalid IL) | |
//IL_0018: Expected O, but got Unknown | |
Assert.GuardArgumentNotNull("expectedRegex", (object)(?)expectedRegex); | |
if (actualString == null) | |
{ | |
return; | |
} | |
if (!((Regex)(?)expectedRegex).IsMatch(actualString)) | |
{ | |
return; | |
} | |
throw new DoesNotMatchException(((object)(?)expectedRegex).ToString(), actualString); | |
} | |
/// <summary> | |
/// Verifies that two strings are equivalent. | |
/// </summary> | |
/// <param name="expected">The expected string value.</param> | |
/// <param name="actual">The actual string value.</param> | |
/// <exception cref="T:Xunit.Sdk.EqualException">Thrown when the strings are not equivalent.</exception> | |
public static void Equal(string expected, string actual) | |
{ | |
Assert.Equal(expected, actual, false, false, false); | |
} | |
/// <summary> | |
/// Verifies that two strings are equivalent. | |
/// </summary> | |
/// <param name="expected">The expected string value.</param> | |
/// <param name="actual">The actual string value.</param> | |
/// <param name="ignoreCase">If set to <c>true</c>, ignores cases differences. The invariant culture is used.</param> | |
/// <param name="ignoreLineEndingDifferences">If set to <c>true</c>, treats \r\n, \r, and \n as equivalent.</param> | |
/// <param name="ignoreWhiteSpaceDifferences">If set to <c>true</c>, treats spaces and tabs (in any non-zero quantity) as equivalent.</param> | |
/// <exception cref="T:Xunit.Sdk.EqualException">Thrown when the strings are not equivalent.</exception> | |
public static void Equal(string expected, string actual, bool ignoreCase = false, bool ignoreLineEndingDifferences = false, bool ignoreWhiteSpaceDifferences = false) | |
{ | |
int num = -1; | |
int num2 = -1; | |
int num3 = 0; | |
int num4 = 0; | |
if (expected == null) | |
{ | |
if (actual == null) | |
{ | |
return; | |
} | |
} | |
else if (actual != null) | |
{ | |
num = 0; | |
num2 = 0; | |
num3 = expected.Length; | |
num4 = actual.Length; | |
while (num < num3 && num2 < num4) | |
{ | |
char c = expected[num]; | |
char c2 = actual[num2]; | |
if (ignoreLineEndingDifferences && Assert.IsLineEnding(c) && Assert.IsLineEnding(c2)) | |
{ | |
num = Assert.SkipLineEnding(expected, num); | |
num2 = Assert.SkipLineEnding(actual, num2); | |
} | |
else if (ignoreWhiteSpaceDifferences && Assert.IsWhiteSpace(c) && Assert.IsWhiteSpace(c2)) | |
{ | |
num = Assert.SkipWhitespace(expected, num); | |
num2 = Assert.SkipWhitespace(actual, num2); | |
} | |
else | |
{ | |
if (ignoreCase) | |
{ | |
c = char.ToUpperInvariant(c); | |
c2 = char.ToUpperInvariant(c2); | |
} | |
if (c != c2) | |
{ | |
break; | |
} | |
num++; | |
num2++; | |
} | |
} | |
} | |
if (num >= num3 && num2 >= num4) | |
{ | |
return; | |
} | |
throw new EqualException(expected, actual, num, num2); | |
} | |
private static bool IsLineEnding(char c) | |
{ | |
if (c != '\r') | |
{ | |
return c == '\n'; | |
} | |
return true; | |
} | |
private static bool IsWhiteSpace(char c) | |
{ | |
if (c != ' ') | |
{ | |
return c == '\t'; | |
} | |
return true; | |
} | |
private static int SkipLineEnding(string value, int index) | |
{ | |
if (value[index] == '\r') | |
{ | |
index++; | |
} | |
if (index < value.Length && value[index] == '\n') | |
{ | |
index++; | |
} | |
return index; | |
} | |
private static int SkipWhitespace(string value, int index) | |
{ | |
while (index < value.Length) | |
{ | |
char c = value[index]; | |
if (c != '\t' && c != ' ') | |
{ | |
return index; | |
} | |
index++; | |
} | |
return index; | |
} | |
/// <summary> | |
/// Verifies that an object is of the given type or a derived type. | |
/// </summary> | |
/// <typeparam name="T">The type the object should be</typeparam> | |
/// <param name="object">The object to be evaluated</param> | |
/// <returns>The object, casted to type T when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.IsAssignableFromException">Thrown when the object is not the given type</exception> | |
public static T IsAssignableFrom<T>(object @object) | |
{ | |
Assert.IsAssignableFrom(typeof(T), @object); | |
return (T)@object; | |
} | |
/// <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="T:Xunit.Sdk.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())) | |
{ | |
return; | |
} | |
throw new IsAssignableFromException(expectedType, @object); | |
} | |
/// <summary> | |
/// Verifies that an object is not exactly the given type. | |
/// </summary> | |
/// <typeparam name="T">The type the object should not be</typeparam> | |
/// <param name="object">The object to be evaluated</param> | |
/// <exception cref="T:Xunit.Sdk.IsNotTypeException">Thrown when the object is the given type</exception> | |
public static void IsNotType<T>(object @object) | |
{ | |
Assert.IsNotType(typeof(T), @object); | |
} | |
/// <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="T:Xunit.Sdk.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) | |
{ | |
return; | |
} | |
if (!expectedType.Equals(@object.GetType())) | |
{ | |
return; | |
} | |
throw new IsNotTypeException(expectedType, @object); | |
} | |
/// <summary> | |
/// Verifies that an object is exactly the given type (and not a derived type). | |
/// </summary> | |
/// <typeparam name="T">The type the object should be</typeparam> | |
/// <param name="object">The object to be evaluated</param> | |
/// <returns>The object, casted to type T when successful</returns> | |
/// <exception cref="T:Xunit.Sdk.IsTypeException">Thrown when the object is not the given type</exception> | |
public static T IsType<T>(object @object) | |
{ | |
Assert.IsType(typeof(T), @object); | |
return (T)@object; | |
} | |
/// <summary> | |
/// Verifies that an object is exactly the given type (and not 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="T:Xunit.Sdk.IsTypeException">Thrown when the object is not the given type</exception> | |
public static void IsType(Type expectedType, object @object) | |
{ | |
Assert.GuardArgumentNotNull("expectedType", expectedType); | |
if (@object == null) | |
{ | |
throw new IsTypeException(expectedType.FullName, null); | |
} | |
Type type = @object.GetType(); | |
if ((object)expectedType == type) | |
{ | |
return; | |
} | |
string text = expectedType.FullName; | |
string text2 = type.FullName; | |
if (text == text2) | |
{ | |
text += string.Format(" ({0})", new object[1] | |
{ | |
expectedType.GetTypeInfo().get_Assembly().GetName() | |
.FullName | |
}); | |
text2 += string.Format(" ({0})", new object[1] | |
{ | |
type.GetTypeInfo().get_Assembly().GetName() | |
.FullName | |
}); | |
} | |
throw new IsTypeException(text, text2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment