diff --git a/lib/Should.dll b/lib/Should.dll deleted file mode 100644 index d8e3f6fc89..0000000000 Binary files a/lib/Should.dll and /dev/null differ diff --git a/src/UnitTests/Should/Should.Core/Assertions/Assert.cs b/src/UnitTests/Should/Should.Core/Assertions/Assert.cs new file mode 100644 index 0000000000..fc17b6ae93 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Assertions/Assert.cs @@ -0,0 +1,940 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using Should.Core.Exceptions; + +namespace Should.Core.Assertions +{ + /// + /// Contains various static methods that are used to verify that conditions are met during the + /// process of running tests. + /// + public class Assert + { + /// + /// Used by the Throws and DoesNotThrow methods. + /// + public delegate void ThrowsDelegate(); + + /// + /// Used by the Throws and DoesNotThrow methods. + /// + public delegate object ThrowsDelegateWithReturn(); + + /// + /// Initializes a new instance of the class. + /// + protected Assert() { } + + /// + /// Verifies that a collection contains a given object. + /// + /// The type of the object to be verified + /// The object expected to be in the collection + /// The collection to be inspected + /// Thrown when the object is not present in the collection + public static void Contains(T expected, + IEnumerable collection) + { + Contains(expected, collection, GetEqualityComparer()); + } + + /// + /// Verifies that a collection contains a given object, using an equality comparer. + /// + /// The type of the object to be verified + /// The object expected to be in the collection + /// The collection to be inspected + /// The comparer used to equate objects in the collection with the expected object + /// Thrown when the object is not present in the collection + public static void Contains(T expected, IEnumerable collection, IEqualityComparer comparer) + { + foreach (T item in collection) + if (comparer.Equals(expected, item)) + return; + + throw new ContainsException(expected); + } + + /// + /// Verifies that a string contains a given sub-string, using the current culture. + /// + /// The sub-string expected to be in the string + /// The string to be inspected + /// Thrown when the sub-string is not present inside the string + public static int Contains(string expectedSubString, + string actualString) + { + return Contains(expectedSubString, actualString, StringComparison.CurrentCulture); + } + + /// + /// Verifies that a string contains a given sub-string, using the given comparison type. + /// + /// The sub-string expected to be in the string + /// The string to be inspected + /// The type of string comparison to perform + /// Thrown when the sub-string is not present inside the string + public static int Contains(string expectedSubString, + string actualString, + StringComparison comparisonType) + { + int indexOf = actualString.IndexOf(expectedSubString, comparisonType); + + if (indexOf < 0) + throw new ContainsException(expectedSubString); + + return indexOf; + } + + /// + /// + /// + /// + /// + /// Thrown when the sub-string is not present at the start of the string + public static void StartsWith(string expectedStartString, string actualString) + { + if (actualString.StartsWith(expectedStartString) == false) + throw new StartsWithException(expectedStartString, actualString); + } + + /// + /// Verifies that a collection does not contain a given object. + /// + /// The type of the object to be compared + /// The object that is expected not to be in the collection + /// The collection to be inspected + /// Thrown when the object is present inside the container + public static void DoesNotContain(T expected, + IEnumerable collection) + { + DoesNotContain(expected, collection, GetEqualityComparer()); + } + + /// + /// Verifies that a collection does not contain a given object, using an equality comparer. + /// + /// The type of the object to be compared + /// The object that is expected not to be in the collection + /// The collection to be inspected + /// The comparer used to equate objects in the collection with the expected object + /// Thrown when the object is present inside the container + public static void DoesNotContain(T expected, IEnumerable collection, IEqualityComparer comparer) + { + foreach (T item in collection) + if (comparer.Equals(expected, item)) + throw new DoesNotContainException(expected); + } + + /// + /// Verifies that a string does not contain a given sub-string, using the current culture. + /// + /// The sub-string which is expected not to be in the string + /// The string to be inspected + /// Thrown when the sub-string is present inside the string + public static void DoesNotContain(string expectedSubString, + string actualString) + { + DoesNotContain(expectedSubString, actualString, StringComparison.CurrentCulture); + } + + /// + /// Verifies that a string does not contain a given sub-string, using the current culture. + /// + /// The sub-string which is expected not to be in the string + /// The string to be inspected + /// The type of string comparison to perform + /// Thrown when the sub-string is present inside the given string + public static void DoesNotContain(string expectedSubString, + string actualString, + StringComparison comparisonType) + { + if (actualString.IndexOf(expectedSubString, comparisonType) >= 0) + throw new DoesNotContainException(expectedSubString); + } + + ///// + ///// Verifies that a block of code does not throw any exceptions. + ///// + ///// A delegate to the code to be tested + //public static void DoesNotThrow(ThrowsDelegate testCode) + //{ + // Exception ex = Record.Exception(testCode); + + // if (ex != null) + // throw new DoesNotThrowException(ex); + //} + + /// + /// Verifies that a collection is empty. + /// + /// The collection to be inspected + /// Thrown when the collection is null + /// Thrown when the collection is not empty + public static void Empty(IEnumerable collection) + { + if (collection == null) throw new ArgumentNullException("collection", "cannot be null"); + +#pragma warning disable 168 + foreach (object @object in collection) + throw new EmptyException(); +#pragma warning restore 168 + } + + /// + /// Verifies that two objects are equal, using a default comparer. + /// + /// The type of the objects to be compared + /// The expected value + /// The value to be compared against + /// Thrown when the objects are not equal + public static void Equal(T expected, + T actual) + { + Equal(expected, actual, GetEqualityComparer()); + } + + /// + /// Verifies that two objects are equal, using a default comparer. + /// + /// The type of the objects to be compared + /// The expected value + /// The value to be compared against + /// The user message to be shown on failure + /// Thrown when the objects are not equal + public static void Equal(T expected, + T actual, + string userMessage) + { + Equal(expected, actual, GetEqualityComparer(), userMessage); + } + + /// + /// Verifies that two objects are equal, using a custom comparer. + /// + /// The type of the objects to be compared + /// The expected value + /// The value to be compared against + /// The comparer used to compare the two objects + /// Thrown when the objects are not equal + public static void Equal(T expected, + T actual, + IEqualityComparer comparer) + { + if (!comparer.Equals(expected, actual)) + throw new EqualException(expected, actual); + } + + /// + /// Verifies that two objects are equal, using a custom comparer. + /// + /// The type of the objects to be compared + /// The expected value + /// The value to be compared against + /// The comparer used to compare the two objects + /// Thrown when the objects are not equal + public static void Equal(T expected, + T actual, + IEqualityComparer comparer, + string userMessage) + { + if (!comparer.Equals(expected, actual)) + throw new EqualException(expected, actual, userMessage); + } + + /// + /// Verifies that two doubles are equal within a tolerance range. + /// + /// The expected value + /// The value to compare against + /// The +/- value for where the expected and actual are considered to be equal + public static void Equal(double expected, double actual, double tolerance) + { + var difference = Math.Abs(actual - expected); + if (difference > tolerance) + throw new EqualException(expected + " +/- " + tolerance, actual); + } + + /// + /// Verifies that two doubles are equal within a tolerance range. + /// + /// The expected value + /// The value to compare against + /// The +/- value for where the expected and actual are considered to be equal + /// The user message to be shown on failure + public static void Equal(double expected, double actual, double tolerance, string userMessage) + { + var difference = Math.Abs(actual - expected); + if (difference > tolerance) + throw new EqualException(expected + " +/- " + tolerance, actual, userMessage); + } + + /// + /// Verifies that two values are not equal, using a default comparer. + /// + /// The expected value + /// The actual value + /// The +/- value for where the expected and actual are considered to be equal + /// Thrown when the objects are equal + public static void NotEqual(double expected, double actual, double tolerance) + { + var difference = Math.Abs(actual - expected); + if (difference <= tolerance) + throw new NotEqualException(expected + " +/- " + tolerance, actual); + } + + /// + /// Verifies that two values are not equal, using a default comparer. + /// + /// The expected value + /// The actual value + /// The +/- value for where the expected and actual are considered to be equal + /// The user message to be shown on failure + /// Thrown when the objects are equal + public static void NotEqual(double expected, double actual, double tolerance, string userMessage) + { + var difference = Math.Abs(actual - expected); + if (difference <= tolerance) + throw new NotEqualException(expected + " +/- " + tolerance, actual, userMessage); + } + + /// + /// Verifies that two dates are equal within a tolerance range. + /// + /// The expected value + /// The value to compare against + /// The +/- value for where the expected and actual are considered to be equal + public static void Equal(DateTime expected, DateTime actual, TimeSpan tolerance) + { + var difference = Math.Abs((actual - expected).Ticks); + if (difference > tolerance.Ticks) + throw new EqualException(expected + " +/- " + tolerance, actual); + } + + /// + /// Verifies that two dates are not equal within a tolerance range. + /// + /// The expected value + /// The value to compare against + /// The +/- value for where the expected and actual are considered to be equal + public static void NotEqual(DateTime expected, DateTime actual, TimeSpan tolerance) + { + var difference = Math.Abs((actual - expected).Ticks); + if (difference <= tolerance.Ticks) + throw new NotEqualException(expected + " +/- " + tolerance, actual); + } + + /// + /// Verifies that two dates are equal within a tolerance range. + /// + /// The expected value + /// The value to compare against + /// The level of precision to use when making the comparison + public static void Equal(DateTime expected, DateTime actual, DatePrecision precision) + { + if (precision.Truncate(expected) != precision.Truncate(actual)) + throw new EqualException(precision.Truncate(actual), precision.Truncate(actual)); + } + + /// + /// Verifies that two doubles are not equal within a tolerance range. + /// + /// The expected value + /// The value to compare against + /// The level of precision to use when making the comparison + public static void NotEqual(DateTime expected, DateTime actual, DatePrecision precision) + { + if (precision.Truncate(expected) == precision.Truncate(actual)) + throw new NotEqualException(precision.Truncate(actual), precision.Truncate(actual)); + } + + /// Do not call this method. + [Obsolete("This is an override of Object.Equals(). Call Assert.Equal() instead.", true)] + public new static bool Equals(object a, + object b) + { + throw new InvalidOperationException("Assert.Equals should not be used"); + } + + /// + /// Verifies that the condition is false. + /// + /// The condition to be tested + /// Thrown if the condition is not false + public static void False(bool condition) + { + False(condition, null); + } + + /// + /// Verifies that the condition is false. + /// + /// The condition to be tested + /// The message to show when the condition is not false + /// Thrown if the condition is not false + public static void False(bool condition, + string userMessage) + { + if (condition) + throw new FalseException(userMessage); + } + + static IEqualityComparer GetEqualityComparer() + { + return new AssertEqualityComparer(); + } + + static IComparer GetComparer() + { + return new AssertComparer(); + } + + /// Verifies that an object is greater than the exclusive minimum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the exclusive minimum value of paramref name="value"/>. + public static void GreaterThan(T left, T right) + { + GreaterThan(left, right, GetComparer()); + } + + /// Verifies that an object is greater than the exclusive minimum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the exclusive minimum value of paramref name="value"/>. + /// An used to compare the objects. + public static void GreaterThan(T left, T right, IComparer comparer) + { + if (comparer.Compare(left, right) <= 0) + throw new GreaterThanException(left, right); + } + + /// Verifies that an object is greater than the inclusive minimum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the inclusive minimum value of paramref name="value"/>. + public static void GreaterThanOrEqual(T left, T right) + { + GreaterThanOrEqual(left, right, GetComparer()); + } + + /// Verifies that an object is greater than the inclusive minimum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the inclusive minimum value of paramref name="value"/>. + /// An used to compare the objects. + public static void GreaterThanOrEqual(T left, T right, IComparer comparer) + { + if (comparer.Compare(left, right) < 0) + throw new GreaterThanOrEqualException(left, right); + } + + /// + /// Verifies that a value is within a given range. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// Thrown when the value is not in the given range + public static void InRange(T actual, + T low, + T high) + { + InRange(actual, low, high, GetComparer()); + } + + /// + /// Verifies that a value is within a given range, using a comparer. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// The comparer used to evaluate the value's range + /// Thrown when the value is not in the given range + public static void InRange(T actual, + T low, + T high, + IComparer comparer) + { + if (comparer.Compare(low, actual) > 0 || comparer.Compare(actual, high) > 0) + throw new InRangeException(actual, low, high); + } + + /// + /// Verifies that an object is of the given type or a derived type. + /// + /// The type the object should be + /// The object to be evaluated + /// The object, casted to type T when successful + /// Thrown when the object is not the given type + public static T IsAssignableFrom(object @object) + { + IsAssignableFrom(typeof(T), @object); + return (T)@object; + } + + /// + /// Verifies that an object is of the given type or a derived type. + /// + /// The type the object should be + /// The object to be evaluated + /// Thrown when the object is not the given type + public static void IsAssignableFrom(Type expectedType, object @object) + { + if (@object == null || !expectedType.IsAssignableFrom(@object.GetType())) + throw new IsAssignableFromException(expectedType, @object); + } + + /// + /// Verifies that an object is of the given type or a derived type. + /// + /// The type the object should be + /// The object to be evaluated + /// The user message to show on failure + /// The object, casted to type T when successful + /// Thrown when the object is not the given type + public static T IsAssignableFrom(object @object, string userMessage) + { + IsAssignableFrom(typeof(T), @object, userMessage); + return (T)@object; + } + + /// + /// Verifies that an object is of the given type or a derived type. + /// + /// The type the object should be + /// The user message to show on failure + /// The object to be evaluated + /// Thrown when the object is not the given type + public static void IsAssignableFrom(Type expectedType, object @object, string userMessage) + { + if (@object == null || !expectedType.IsAssignableFrom(@object.GetType())) + throw new IsAssignableFromException(expectedType, @object, userMessage); + } + + /// + /// Verifies that an object is not exactly the given type. + /// + /// The type the object should not be + /// The object to be evaluated + /// Thrown when the object is the given type + public static void IsNotType(object @object) + { + IsNotType(typeof(T), @object); + } + + /// + /// Verifies that an object is not exactly the given type. + /// + /// The type the object should not be + /// The object to be evaluated + /// Thrown when the object is the given type + public static void IsNotType(Type expectedType, + object @object) + { + if (expectedType.Equals(@object.GetType())) + throw new IsNotTypeException(expectedType, @object); + } + + /// + /// Verifies that an object is exactly the given type (and not a derived type). + /// + /// The type the object should be + /// The object to be evaluated + /// The object, casted to type T when successful + /// Thrown when the object is not the given type + public static T IsType(object @object) + { + IsType(typeof(T), @object); + return (T)@object; + } + + /// + /// Verifies that an object is exactly the given type (and not a derived type). + /// + /// The type the object should be + /// The object to be evaluated + /// Thrown when the object is not the given type + public static void IsType(Type expectedType, + object @object) + { + if (@object == null || !expectedType.Equals(@object.GetType())) + throw new IsTypeException(expectedType, @object); + } + + /// Verifies that an object is less than the exclusive maximum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the exclusive maximum value of paramref name="value"/>. + public static void LessThan(T left, T right) + { + LessThan(left, right, GetComparer()); + } + + /// Verifies that an object is less than the exclusive maximum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the exclusive maximum value of paramref name="value"/>. + /// An used to compare the objects. + public static void LessThan(T left, T right, IComparer comparer) + { + if (comparer.Compare(left, right) >= 0) + throw new LessThanException(left, right); + } + + /// Verifies that an object is less than the inclusive maximum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the inclusive maximum value of paramref name="value"/>. + public static void LessThanOrEqual(T left, T right) + { + LessThanOrEqual(left, right, GetComparer()); + } + + /// Verifies that an object is less than the inclusive maximum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the inclusive maximum value of paramref name="value"/>. + /// An used to compare the objects. + public static void LessThanOrEqual(T left, T right, IComparer comparer) + { + if (comparer.Compare(left, right) > 0) + throw new LessThanOrEqualException(left, right); + } + + /// + /// Verifies that a collection is not empty. + /// + /// The collection to be inspected + /// Thrown when a null collection is passed + /// Thrown when the collection is empty + public static void NotEmpty(IEnumerable collection) + { + if (collection == null) throw new ArgumentNullException("collection", "cannot be null"); + +#pragma warning disable 168 + foreach (object @object in collection) + return; +#pragma warning restore 168 + + throw new NotEmptyException(); + } + + /// + /// Verifies that two objects are not equal, using a default comparer. + /// + /// The type of the objects to be compared + /// The expected object + /// The actual object + /// Thrown when the objects are equal + public static void NotEqual(T expected, + T actual) + { + NotEqual(expected, actual, GetEqualityComparer()); + } + + /// + /// Verifies that two objects are not equal, using a custom comparer. + /// + /// The type of the objects to be compared + /// The expected object + /// The actual object + /// The comparer used to examine the objects + /// Thrown when the objects are equal + public static void NotEqual(T expected, + T actual, + IEqualityComparer comparer) + { + if (comparer.Equals(expected, actual)) + throw new NotEqualException(expected, actual); + } + + /// + /// Verifies that a value is not within a given range, using the default comparer. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// Thrown when the value is in the given range + public static void NotInRange(T actual, + T low, + T high) + { + NotInRange(actual, low, high, GetComparer()); + } + + /// + /// Verifies that a value is not within a given range, using a comparer. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// The comparer used to evaluate the value's range + /// Thrown when the value is in the given range + public static void NotInRange(T actual, + T low, + T high, + IComparer comparer) + { + if (comparer.Compare(low, actual) <= 0 && comparer.Compare(actual, high) <= 0) + throw new NotInRangeException(actual, low, high); + } + + /// + /// Verifies that an object reference is not null. + /// + /// The object to be validated + /// Thrown when the object is not null + public static void NotNull(object @object) + { + if (@object == null) + throw new NotNullException(); + } + + /// + /// Verifies that an object reference is not null. + /// + /// The object to be validated + /// Thrown when the object is not null + public static void NotNull(object @object, string message) + { + if (@object == null) + throw new NotNullException(message); + } + + /// + /// Verifies that two objects are not the same instance. + /// + /// The expected object instance + /// The actual object instance + /// Thrown when the objects are the same instance + public static void NotSame(object expected, + object actual) + { + if (object.ReferenceEquals(expected, actual)) + throw new NotSameException(); + } + + /// + /// Verifies that an object reference is null. + /// + /// The object to be inspected + /// Thrown when the object reference is not null + public static void Null(object @object) + { + if (@object != null) + throw new NullException(@object); + } + + /// + /// Verifies that two objects are the same instance. + /// + /// The expected object instance + /// The actual object instance + /// Thrown when the objects are not the same instance + public static void Same(object expected, + object actual) + { + if (!object.ReferenceEquals(expected, actual)) + throw new SameException(expected, actual); + } + + /// + /// Verifies that the given collection contains only a single + /// element of the given type. + /// + /// The collection. + /// The single item in the collection. + /// Thrown when the collection does not contain + /// exactly one element. + public static object Single(IEnumerable collection) + { + if (collection == null) + throw new ArgumentNullException("collection"); + + int count = 0; + object result = null; + + foreach (object item in collection) + { + result = item; + ++count; + } + + if (count != 1) + throw new SingleException(count); + + return result; + } + + /// + /// Verifies that the given collection contains only a single + /// element of the given type. + /// + /// The collection type. + /// The collection. + /// The single item in the collection. + /// Thrown when the collection does not contain + /// exactly one element. + public static T Single(IEnumerable collection) + { + if (collection == null) + throw new ArgumentNullException("collection"); + + int count = 0; + T result = default(T); + + foreach (T item in collection) + { + result = item; + ++count; + } + + if (count != 1) + throw new SingleException(count); + + return result; + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// + /// The type of the exception expected to be thrown + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public static T Throws(ThrowsDelegate testCode) + where T : Exception + { + return (T)Throws(typeof(T), testCode); + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// + /// The type of the exception expected to be thrown + /// The message to be shown if the test fails + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public static T Throws(string userMessage, + ThrowsDelegate testCode) + where T : Exception + { + return (T)Throws(typeof(T), testCode); + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// Generally used to test property accessors. + /// + /// The type of the exception expected to be thrown + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public static T Throws(ThrowsDelegateWithReturn testCode) + where T : Exception + { + return (T)Throws(typeof(T), testCode); + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// Generally used to test property accessors. + /// + /// The type of the exception expected to be thrown + /// The message to be shown if the test fails + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public static T Throws(string userMessage, + ThrowsDelegateWithReturn testCode) + where T : Exception + { + return (T)Throws(typeof(T), testCode); + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// + /// The type of the exception expected to be thrown + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public static Exception Throws(Type exceptionType, + ThrowsDelegate testCode) + { + Exception exception = Record.Exception(testCode); + + if (exception == null) + throw new ThrowsException(exceptionType); + + if (!exceptionType.Equals(exception.GetType())) + throw new ThrowsException(exceptionType, exception); + + return exception; + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// Generally used to test property accessors. + /// + /// The type of the exception expected to be thrown + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public static Exception Throws(Type exceptionType, + ThrowsDelegateWithReturn testCode) + { + Exception exception = Record.Exception(testCode); + + if (exception == null) + throw new ThrowsException(exceptionType); + + if (!exceptionType.Equals(exception.GetType())) + throw new ThrowsException(exceptionType, exception); + + return exception; + } + + /// + /// Verifies that a block of code does not throw any exceptions. + /// + /// A delegate to the code to be tested + public static void DoesNotThrow(ThrowsDelegate testCode) + { + Exception ex = Record.Exception(testCode); + + if (ex != null) + throw new DoesNotThrowException(ex); + } + + /// + /// Verifies that an expression is true. + /// + /// The condition to be inspected + /// Thrown when the condition is false + public static void True(bool condition) + { + True(condition, null); + } + + /// + /// Verifies that an expression is true. + /// + /// The condition to be inspected + /// The message to be shown when the condition is false + /// Thrown when the condition is false + public static void True(bool condition, + string userMessage) + { + if (!condition) + throw new TrueException(userMessage); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Assertions/Assert.cs.orig b/src/UnitTests/Should/Should.Core/Assertions/Assert.cs.orig new file mode 100644 index 0000000000..275ed99eca --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Assertions/Assert.cs.orig @@ -0,0 +1,860 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using Should.Core.Exceptions; + +namespace Should.Core.Assertions +{ + /// + /// Contains various static methods that are used to verify that conditions are met during the + /// process of running tests. + /// + public class Assert + { + /// + /// Used by the Throws and DoesNotThrow methods. + /// + public delegate void ThrowsDelegate(); + + /// + /// Used by the Throws and DoesNotThrow methods. + /// + public delegate object ThrowsDelegateWithReturn(); + + /// + /// Initializes a new instance of the class. + /// + protected Assert() { } + + /// + /// Verifies that a collection contains a given object. + /// + /// The type of the object to be verified + /// The object expected to be in the collection + /// The collection to be inspected + /// Thrown when the object is not present in the collection + public static void Contains(T expected, + IEnumerable collection) + { + Contains(expected, collection, GetEqualityComparer()); + } + + /// + /// Verifies that a collection contains a given object, using an equality comparer. + /// + /// The type of the object to be verified + /// The object expected to be in the collection + /// The collection to be inspected + /// The comparer used to equate objects in the collection with the expected object + /// Thrown when the object is not present in the collection + public static void Contains(T expected, IEnumerable collection, IEqualityComparer comparer) + { + foreach (T item in collection) + if (comparer.Equals(expected, item)) + return; + + throw new ContainsException(expected); + } + + /// + /// Verifies that a string contains a given sub-string, using the current culture. + /// + /// The sub-string expected to be in the string + /// The string to be inspected + /// Thrown when the sub-string is not present inside the string + public static int Contains(string expectedSubString, + string actualString) + { + return Contains(expectedSubString, actualString, StringComparison.CurrentCulture); + } + + /// + /// Verifies that a string contains a given sub-string, using the given comparison type. + /// + /// The sub-string expected to be in the string + /// The string to be inspected + /// The type of string comparison to perform + /// Thrown when the sub-string is not present inside the string + public static int Contains(string expectedSubString, + string actualString, + StringComparison comparisonType) + { + int indexOf = actualString.IndexOf(expectedSubString, comparisonType); + + if (indexOf < 0) + throw new ContainsException(expectedSubString); + + return indexOf; + } + + /// + /// + /// + /// + /// + /// Thrown when the sub-string is not present at the start of the string + public static void StartsWith(string expectedStartString, string actualString) + { + if (actualString.StartsWith(expectedStartString) == false) + throw new StartsWithException(expectedStartString, actualString); + } + + /// + /// Verifies that a collection does not contain a given object. + /// + /// The type of the object to be compared + /// The object that is expected not to be in the collection + /// The collection to be inspected + /// Thrown when the object is present inside the container + public static void DoesNotContain(T expected, + IEnumerable collection) + { + DoesNotContain(expected, collection, GetEqualityComparer()); + } + + /// + /// Verifies that a collection does not contain a given object, using an equality comparer. + /// + /// The type of the object to be compared + /// The object that is expected not to be in the collection + /// The collection to be inspected + /// The comparer used to equate objects in the collection with the expected object + /// Thrown when the object is present inside the container + public static void DoesNotContain(T expected, IEnumerable collection, IEqualityComparer comparer) + { + foreach (T item in collection) + if (comparer.Equals(expected, item)) + throw new DoesNotContainException(expected); + } + + /// + /// Verifies that a string does not contain a given sub-string, using the current culture. + /// + /// The sub-string which is expected not to be in the string + /// The string to be inspected + /// Thrown when the sub-string is present inside the string + public static void DoesNotContain(string expectedSubString, + string actualString) + { + DoesNotContain(expectedSubString, actualString, StringComparison.CurrentCulture); + } + + /// + /// Verifies that a string does not contain a given sub-string, using the current culture. + /// + /// The sub-string which is expected not to be in the string + /// The string to be inspected + /// The type of string comparison to perform + /// Thrown when the sub-string is present inside the given string + public static void DoesNotContain(string expectedSubString, + string actualString, + StringComparison comparisonType) + { + if (actualString.IndexOf(expectedSubString, comparisonType) >= 0) + throw new DoesNotContainException(expectedSubString); + } + + ///// + ///// Verifies that a block of code does not throw any exceptions. + ///// + ///// A delegate to the code to be tested + //public static void DoesNotThrow(ThrowsDelegate testCode) + //{ + // Exception ex = Record.Exception(testCode); + + // if (ex != null) + // throw new DoesNotThrowException(ex); + //} + + /// + /// Verifies that a collection is empty. + /// + /// The collection to be inspected + /// Thrown when the collection is null + /// Thrown when the collection is not empty + public static void Empty(IEnumerable collection) + { + if (collection == null) throw new ArgumentNullException("collection", "cannot be null"); + +#pragma warning disable 168 + foreach (object @object in collection) + throw new EmptyException(); +#pragma warning restore 168 + } + + /// + /// Verifies that two objects are equal, using a default comparer. + /// + /// The type of the objects to be compared + /// The expected value + /// The value to be compared against + /// Thrown when the objects are not equal + public static void Equal(T expected, + T actual) + { + Equal(expected, actual, GetEqualityComparer()); + } + + /// + /// Verifies that two objects are equal, using a default comparer. + /// + /// The type of the objects to be compared + /// The expected value + /// The value to be compared against + /// The user message to be shown on failure + /// Thrown when the objects are not equal + public static void Equal(T expected, + T actual, + string userMessage) + { + Equal(expected, actual, GetEqualityComparer(), userMessage); + } + + /// + /// Verifies that two objects are equal, using a custom comparer. + /// + /// The type of the objects to be compared + /// The expected value + /// The value to be compared against + /// The comparer used to compare the two objects + /// Thrown when the objects are not equal + public static void Equal(T expected, + T actual, + IEqualityComparer comparer) + { + if (!comparer.Equals(expected, actual)) + throw new EqualException(expected, actual); + } + + /// + /// Verifies that two objects are equal, using a custom comparer. + /// + /// The type of the objects to be compared + /// The expected value + /// The value to be compared against + /// The comparer used to compare the two objects + /// Thrown when the objects are not equal + public static void Equal(T expected, + T actual, + IEqualityComparer comparer, + string userMessage) + { + if (!comparer.Equals(expected, actual)) + throw new EqualException(expected, actual, userMessage); + } + + /// + /// Verifies that two doubles are equal within a tolerance range. + /// + /// The expected value + /// The value to compare against + /// The +/- value for where the expected and actual are considered to be equal + public static void Equal(double expected, double actual, double tolerance) + { + var difference = Math.Abs(actual - expected); + if (difference > tolerance) + throw new EqualException(expected + " +/- " + tolerance, actual); + } + + /// + /// Verifies that two doubles are equal within a tolerance range. + /// + /// The expected value + /// The value to compare against + /// The +/- value for where the expected and actual are considered to be equal + /// The user message to be shown on failure + public static void Equal(double expected, double actual, double tolerance, string userMessage) + { + var difference = Math.Abs(actual - expected); + if (difference > tolerance) + throw new EqualException(expected + " +/- " + tolerance, actual, userMessage); + } + + /// + /// Verifies that two values are not equal, using a default comparer. + /// + /// The expected value + /// The actual value + /// The +/- value for where the expected and actual are considered to be equal + /// Thrown when the objects are equal + public static void NotEqual(double expected, double actual, double tolerance) + { + var difference = Math.Abs(actual - expected); + if (difference <= tolerance) + throw new NotEqualException(expected + " +/- " + tolerance, actual); + } + + /// + /// Verifies that two values are not equal, using a default comparer. + /// + /// The expected value + /// The actual value + /// The +/- value for where the expected and actual are considered to be equal + /// The user message to be shown on failure + /// Thrown when the objects are equal + public static void NotEqual(double expected, double actual, double tolerance, string userMessage) + { + var difference = Math.Abs(actual - expected); + if (difference <= tolerance) + throw new NotEqualException(expected + " +/- " + tolerance, actual, userMessage); + } + + /// + /// Verifies that two dates are equal within a tolerance range. + /// + /// The expected value + /// The value to compare against + /// The +/- value for where the expected and actual are considered to be equal + public static void Equal(DateTime expected, DateTime actual, TimeSpan tolerance) + { + var difference = Math.Abs((actual - expected).Ticks); + if (difference > tolerance.Ticks) + throw new EqualException(expected + " +/- " + tolerance, actual); + } + + /// + /// Verifies that two dates are not equal within a tolerance range. + /// + /// The expected value + /// The value to compare against + /// The +/- value for where the expected and actual are considered to be equal + public static void NotEqual(DateTime expected, DateTime actual, TimeSpan tolerance) + { + var difference = Math.Abs((actual - expected).Ticks); + if (difference <= tolerance.Ticks) + throw new NotEqualException(expected + " +/- " + tolerance, actual); + } + + /// + /// Verifies that two dates are equal within a tolerance range. + /// + /// The expected value + /// The value to compare against + /// The level of precision to use when making the comparison + public static void Equal(DateTime expected, DateTime actual, DatePrecision precision) + { + if (precision.Truncate(expected) != precision.Truncate(actual)) + throw new EqualException(precision.Truncate(actual), precision.Truncate(actual)); + } + + /// + /// Verifies that two doubles are not equal within a tolerance range. + /// + /// The expected value + /// The value to compare against + /// The level of precision to use when making the comparison + public static void NotEqual(DateTime expected, DateTime actual, DatePrecision precision) + { + if (precision.Truncate(expected) == precision.Truncate(actual)) + throw new NotEqualException(precision.Truncate(actual), precision.Truncate(actual)); + } + + /// Do not call this method. + [Obsolete("This is an override of Object.Equals(). Call Assert.Equal() instead.", true)] + public new static bool Equals(object a, + object b) + { + throw new InvalidOperationException("Assert.Equals should not be used"); + } + + /// + /// Verifies that the condition is false. + /// + /// The condition to be tested + /// Thrown if the condition is not false + public static void False(bool condition) + { + False(condition, null); + } + + /// + /// Verifies that the condition is false. + /// + /// The condition to be tested + /// The message to show when the condition is not false + /// Thrown if the condition is not false + public static void False(bool condition, + string userMessage) + { + if (condition) + throw new FalseException(userMessage); + } + + static IEqualityComparer GetEqualityComparer() + { + return new AssertEqualityComparer(); + } + + static IComparer GetComparer() + { + return new AssertComparer(); + } + + /// + /// Verifies that a value is within a given range. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// Thrown when the value is not in the given range + public static void InRange(T actual, + T low, + T high) + { + InRange(actual, low, high, GetComparer()); + } + + /// + /// Verifies that a value is within a given range, using a comparer. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// The comparer used to evaluate the value's range + /// Thrown when the value is not in the given range + public static void InRange(T actual, + T low, + T high, + IComparer comparer) + { + if (comparer.Compare(low, actual) > 0 || comparer.Compare(actual, high) > 0) + throw new InRangeException(actual, low, high); + } + + /// + /// Verifies that an object is of the given type or a derived type. + /// + /// The type the object should be + /// The object to be evaluated + /// The object, casted to type T when successful + /// Thrown when the object is not the given type + public static T IsAssignableFrom(object @object) + { + IsAssignableFrom(typeof(T), @object); + return (T)@object; + } + + /// + /// Verifies that an object is of the given type or a derived type. + /// + /// The type the object should be + /// The object to be evaluated + /// Thrown when the object is not the given type + public static void IsAssignableFrom(Type expectedType, object @object) + { + if (@object == null || !expectedType.IsAssignableFrom(@object.GetType())) + throw new IsAssignableFromException(expectedType, @object); + } + + /// + /// Verifies that an object is of the given type or a derived type. + /// + /// The type the object should be + /// The object to be evaluated + /// The user message to show on failure + /// The object, casted to type T when successful + /// Thrown when the object is not the given type + public static T IsAssignableFrom(object @object, string userMessage) + { + IsAssignableFrom(typeof(T), @object, userMessage); + return (T)@object; + } + + /// + /// Verifies that an object is of the given type or a derived type. + /// + /// The type the object should be + /// The user message to show on failure + /// The object to be evaluated + /// Thrown when the object is not the given type + public static void IsAssignableFrom(Type expectedType, object @object, string userMessage) + { + if (@object == null || !expectedType.IsAssignableFrom(@object.GetType())) + throw new IsAssignableFromException(expectedType, @object, userMessage); + } + + /// + /// Verifies that an object is not exactly the given type. + /// + /// The type the object should not be + /// The object to be evaluated + /// Thrown when the object is the given type + public static void IsNotType(object @object) + { + IsNotType(typeof(T), @object); + } + + /// + /// Verifies that an object is not exactly the given type. + /// + /// The type the object should not be + /// The object to be evaluated + /// Thrown when the object is the given type + public static void IsNotType(Type expectedType, + object @object) + { + if (expectedType.Equals(@object.GetType())) + throw new IsNotTypeException(expectedType, @object); + } + + /// + /// Verifies that an object is exactly the given type (and not a derived type). + /// + /// The type the object should be + /// The object to be evaluated + /// The object, casted to type T when successful + /// Thrown when the object is not the given type + public static T IsType(object @object) + { + IsType(typeof(T), @object); + return (T)@object; + } + + /// + /// Verifies that an object is exactly the given type (and not a derived type). + /// + /// The type the object should be + /// The object to be evaluated + /// Thrown when the object is not the given type + public static void IsType(Type expectedType, + object @object) + { + if (@object == null || !expectedType.Equals(@object.GetType())) + throw new IsTypeException(expectedType, @object); + } + + /// + /// Verifies that a collection is not empty. + /// + /// The collection to be inspected + /// Thrown when a null collection is passed + /// Thrown when the collection is empty + public static void NotEmpty(IEnumerable collection) + { + if (collection == null) throw new ArgumentNullException("collection", "cannot be null"); + +#pragma warning disable 168 + foreach (object @object in collection) + return; +#pragma warning restore 168 + + throw new NotEmptyException(); + } + + /// + /// Verifies that two objects are not equal, using a default comparer. + /// + /// The type of the objects to be compared + /// The expected object + /// The actual object + /// Thrown when the objects are equal + public static void NotEqual(T expected, + T actual) + { + NotEqual(expected, actual, GetEqualityComparer()); + } + + /// + /// Verifies that two objects are not equal, using a custom comparer. + /// + /// The type of the objects to be compared + /// The expected object + /// The actual object + /// The comparer used to examine the objects + /// Thrown when the objects are equal + public static void NotEqual(T expected, + T actual, + IEqualityComparer comparer) + { + if (comparer.Equals(expected, actual)) + throw new NotEqualException(expected, actual); + } + + /// + /// Verifies that a value is not within a given range, using the default comparer. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// Thrown when the value is in the given range + public static void NotInRange(T actual, + T low, + T high) + { + NotInRange(actual, low, high, GetComparer()); + } + + /// + /// Verifies that a value is not within a given range, using a comparer. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// The comparer used to evaluate the value's range + /// Thrown when the value is in the given range + public static void NotInRange(T actual, + T low, + T high, + IComparer comparer) + { + if (comparer.Compare(low, actual) <= 0 && comparer.Compare(actual, high) <= 0) + throw new NotInRangeException(actual, low, high); + } + + /// + /// Verifies that an object reference is not null. + /// + /// The object to be validated + /// Thrown when the object is not null + public static void NotNull(object @object) + { + if (@object == null) + throw new NotNullException(); + } + + /// + /// Verifies that an object reference is not null. + /// + /// The object to be validated + /// Thrown when the object is not null + public static void NotNull(object @object, string message) + { + if (@object == null) + throw new NotNullException(message); + } + + /// + /// Verifies that two objects are not the same instance. + /// + /// The expected object instance + /// The actual object instance + /// Thrown when the objects are the same instance + public static void NotSame(object expected, + object actual) + { + if (object.ReferenceEquals(expected, actual)) + throw new NotSameException(); + } + + /// + /// Verifies that an object reference is null. + /// + /// The object to be inspected + /// Thrown when the object reference is not null + public static void Null(object @object) + { + if (@object != null) + throw new NullException(@object); + } + + /// + /// Verifies that two objects are the same instance. + /// + /// The expected object instance + /// The actual object instance + /// Thrown when the objects are not the same instance + public static void Same(object expected, + object actual) + { + if (!object.ReferenceEquals(expected, actual)) + throw new SameException(expected, actual); + } + + /// + /// Verifies that the given collection contains only a single + /// element of the given type. + /// + /// The collection. + /// The single item in the collection. + /// Thrown when the collection does not contain + /// exactly one element. + public static object Single(IEnumerable collection) + { + if (collection == null) + throw new ArgumentNullException("collection"); + + int count = 0; + object result = null; + + foreach (object item in collection) + { + result = item; + ++count; + } + + if (count != 1) + throw new SingleException(count); + + return result; + } + + /// + /// Verifies that the given collection contains only a single + /// element of the given type. + /// + /// The collection type. + /// The collection. + /// The single item in the collection. + /// Thrown when the collection does not contain + /// exactly one element. + public static T Single(IEnumerable collection) + { + if (collection == null) + throw new ArgumentNullException("collection"); + + int count = 0; + T result = default(T); + + foreach (T item in collection) + { + result = item; + ++count; + } + + if (count != 1) + throw new SingleException(count); + + return result; + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// + /// The type of the exception expected to be thrown + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public static T Throws(ThrowsDelegate testCode) + where T : Exception + { + return (T)Throws(typeof(T), testCode); + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// + /// The type of the exception expected to be thrown + /// The message to be shown if the test fails + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public static T Throws(string userMessage, + ThrowsDelegate testCode) + where T : Exception + { + return (T)Throws(typeof(T), testCode); + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// Generally used to test property accessors. + /// + /// The type of the exception expected to be thrown + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public static T Throws(ThrowsDelegateWithReturn testCode) + where T : Exception + { + return (T)Throws(typeof(T), testCode); + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// Generally used to test property accessors. + /// + /// The type of the exception expected to be thrown + /// The message to be shown if the test fails + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public static T Throws(string userMessage, + ThrowsDelegateWithReturn testCode) + where T : Exception + { + return (T)Throws(typeof(T), testCode); + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// + /// The type of the exception expected to be thrown + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public static Exception Throws(Type exceptionType, + ThrowsDelegate testCode) + { + Exception exception = Record.Exception(testCode); + + if (exception == null) + throw new ThrowsException(exceptionType); + + if (!exceptionType.Equals(exception.GetType())) + throw new ThrowsException(exceptionType, exception); + + return exception; + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// Generally used to test property accessors. + /// + /// The type of the exception expected to be thrown + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public static Exception Throws(Type exceptionType, + ThrowsDelegateWithReturn testCode) + { + Exception exception = Record.Exception(testCode); + + if (exception == null) + throw new ThrowsException(exceptionType); + + if (!exceptionType.Equals(exception.GetType())) + throw new ThrowsException(exceptionType, exception); + + return exception; + } + + /// + /// Verifies that a block of code does not throw any exceptions. + /// + /// A delegate to the code to be tested + public static void DoesNotThrow(ThrowsDelegate testCode) + { + Exception ex = Record.Exception(testCode); + + if (ex != null) + throw new DoesNotThrowException(ex); + } + + /// + /// Verifies that an expression is true. + /// + /// The condition to be inspected + /// Thrown when the condition is false + public static void True(bool condition) + { + True(condition, null); + } + + /// + /// Verifies that an expression is true. + /// + /// The condition to be inspected + /// The message to be shown when the condition is false + /// Thrown when the condition is false + public static void True(bool condition, + string userMessage) + { + if (!condition) + throw new TrueException(userMessage); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Assertions/AssertComparer.cs b/src/UnitTests/Should/Should.Core/Assertions/AssertComparer.cs new file mode 100644 index 0000000000..7343481d52 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Assertions/AssertComparer.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; + +namespace Should.Core.Assertions +{ + internal class AssertComparer : IComparer + { + public int Compare(T x, T y) + { + Type type = typeof(T); + + // Null? + if (!type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition().IsAssignableFrom(typeof(Nullable<>)))) + { + if (Equals(x, default(T))) + { + return Equals(y, default(T)) ? 0 : 1; + } + + if (Equals(y, default(T))) + return -1; + } + + var xIsAssignableFromY = x.GetType().IsAssignableFrom(y.GetType()); + var yIsAssignableFromX = y.GetType().IsAssignableFrom(x.GetType()); + + if (!xIsAssignableFromY && !yIsAssignableFromX) + throw new InvalidOperationException(string.Format("Cannot compare objects of type {0} and {1} because neither is assignable from the other.", x.GetType().Name, y.GetType().Name)); + + // x Implements IComparable? + IComparable comparable1 = x as IComparable; + + if (comparable1 != null && xIsAssignableFromY) + return comparable1.CompareTo(y); + + // y Implements IComparable? + IComparable comparable2 = y as IComparable; + + if (comparable2 != null && yIsAssignableFromX) + return comparable2.CompareTo(x) * -1; + + // x Implements IComparable? + IComparable comparable3 = x as IComparable; + + if (comparable3 != null && xIsAssignableFromY) + return comparable3.CompareTo(y); + + // y Implements IComparable? + IComparable comparable4 = y as IComparable; + + if (comparable4 != null && yIsAssignableFromX) + return comparable4.CompareTo(x) *-1; + + if (new AssertEqualityComparer().Equals(x, y)) + { + return 0; + } + + if (xIsAssignableFromY) + { + var result = CompareUsingOperators(x, y, x.GetType()); + if (result.HasValue) + { + return result.Value; + } + } + + if (yIsAssignableFromX) + { + var result = CompareUsingOperators(x, y, y.GetType()); + if (result.HasValue) + { + return result.Value; + } + } + + throw new InvalidOperationException(string.Format("Cannot compare objects of type {0} and {1} because neither implements IComparable or IComparable nor overloads comparaison operators.", x.GetType().Name, y.GetType().Name)); + } + + //Note: Handles edge case of a class where operators are overloaded but niether IComparable or IComparable are implemented + private int? CompareUsingOperators(T x, T y, Type type) + { + var greaterThan = type.GetMethod("op_GreaterThan"); + if (greaterThan != null) + { + var lessThan = type.GetMethod("op_LessThan"); + return (bool)greaterThan.Invoke(null, new object[] { x, y }) + ? 1 + : (bool)lessThan.Invoke(null, new object[] { x, y }) ? -1 : 0; + } + var greaterThanOrEqual = type.GetMethod("op_GreaterThanOrEqual"); + if (greaterThanOrEqual != null) + { + var lessThanOrEqual = type.GetMethod("op_LessThanOrEqual"); + return (bool)greaterThanOrEqual.Invoke(null, new object[] { x, y }) + ? (bool)lessThanOrEqual.Invoke(null, new object[] { x, y }) ? 0 : 1 + : -1; + } + return null; + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Assertions/AssertEqualityComparer.cs b/src/UnitTests/Should/Should.Core/Assertions/AssertEqualityComparer.cs new file mode 100644 index 0000000000..50d1a179a8 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Assertions/AssertEqualityComparer.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Should.Core.Assertions +{ + internal class AssertEqualityComparer : IEqualityComparer + { + public bool Equals(T x, T y) + { + Type type = typeof(T); + + // Null? + if (!type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition().IsAssignableFrom(typeof(Nullable<>)))) + { + if (Object.Equals(x, default(T))) + return Object.Equals(y, default(T)); + + if (Object.Equals(y, default(T))) + return false; + } + + //x implements IEquitable and is assignable from y? + var xIsAssignableFromY = x.GetType().IsAssignableFrom(y.GetType()); + if (xIsAssignableFromY && x is IEquatable) + return ((IEquatable)x).Equals(y); + + //y implements IEquitable and is assignable from x? + var yIsAssignableFromX = y.GetType().IsAssignableFrom(x.GetType()); + if (yIsAssignableFromX && y is IEquatable) + return ((IEquatable)y).Equals(x); + + // Enumerable? + IEnumerable enumerableX = x as IEnumerable; + IEnumerable enumerableY = y as IEnumerable; + + if (enumerableX != null && enumerableY != null) + { + return new EnumerableEqualityComparer().Equals(enumerableX, enumerableY); + } + + // Last case, rely on Object.Equals + return Object.Equals(x, y); + } + + public int GetHashCode(T obj) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Assertions/Assertions.cs b/src/UnitTests/Should/Should.Core/Assertions/Assertions.cs new file mode 100644 index 0000000000..bcc9283385 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Assertions/Assertions.cs @@ -0,0 +1,503 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using Should.Core.Exceptions; + +namespace Should.Core.Assertions +{ + /// + /// A wrapper for Assert which is used by . + /// + public class Assertions + { + /// + /// Verifies that a collection contains a given object. + /// + /// The type of the object to be verified + /// The object expected to be in the collection + /// The collection to be inspected + /// Thrown when the object is not present in the collection + public void Contains(T expected, IEnumerable collection) + { + Assert.Contains(expected, collection); + } + + /// + /// Verifies that a collection contains a given object, using a comparer. + /// + /// The type of the object to be verified + /// The object expected to be in the collection + /// The collection to be inspected + /// The comparer used to equate objects in the collection with the expected object + /// Thrown when the object is not present in the collection + public void Contains(T expected, IEnumerable collection, IEqualityComparer comparer) + { + Assert.Contains(expected, collection, comparer); + } + + /// + /// Verifies that a string contains a given sub-string, using the current culture. + /// + /// The sub-string expected to be in the string + /// The string to be inspected + /// Thrown when the sub-string is not present inside the string + public void Contains(string expectedSubString, string actualString) + { + Assert.Contains(expectedSubString, actualString); + } + + /// + /// Verifies that a string contains a given sub-string, using the given comparison type. + /// + /// The sub-string expected to be in the string + /// The string to be inspected + /// The type of string comparison to perform + /// Thrown when the sub-string is not present inside the string + public void Contains(string expectedSubString, string actualString, StringComparison comparisonType) + { + Assert.Contains(expectedSubString, actualString, comparisonType); + } + + /// + /// Verifies that a collection does not contain a given object. + /// + /// The type of the object to be compared + /// The object that is expected not to be in the collection + /// The collection to be inspected + /// Thrown when the object is present inside the container + public void DoesNotContain(T expected, IEnumerable collection) + { + Assert.DoesNotContain(expected, collection); + } + + /// + /// Verifies that a collection does not contain a given object, using a comparer. + /// + /// The type of the object to be compared + /// The object that is expected not to be in the collection + /// The collection to be inspected + /// The comparer used to equate objects in the collection with the expected object + /// Thrown when the object is present inside the container + public void DoesNotContain(T expected, IEnumerable collection, IEqualityComparer comparer) + { + Assert.DoesNotContain(expected, collection, comparer); + } + + /// + /// Verifies that a string does not contain a given sub-string, using the current culture. + /// + /// The sub-string which is expected not to be in the string + /// The string to be inspected + /// Thrown when the sub-string is present inside the string + public void DoesNotContain(string expectedSubString, string actualString) + { + Assert.DoesNotContain(expectedSubString, actualString); + } + + /// + /// Verifies that a string does not contain a given sub-string, using the current culture. + /// + /// The sub-string which is expected not to be in the string + /// The string to be inspected + /// The type of string comparison to perform + /// Thrown when the sub-string is present inside the given string + public void DoesNotContain(string expectedSubString, string actualString, StringComparison comparisonType) + { + Assert.DoesNotContain(expectedSubString, actualString, comparisonType); + } + + ///// + ///// Verifies that a block of code does not throw any exceptions. + ///// + ///// A delegate to the code to be tested + //public void DoesNotThrow(Assert.ThrowsDelegate testCode) + //{ + // Assert.DoesNotThrow(testCode); + //} + + /// + /// Verifies that a collection is empty. + /// + /// The collection to be inspected + /// Thrown when the collection is null + /// Thrown when the collection is not empty + public void Empty(IEnumerable collection) + { + Assert.Empty(collection); + } + + /// + /// Verifies that two objects are equal, using a default comparer. + /// + /// The type of the objects to be compared + /// The expected value + /// The value to be compared against + /// Thrown when the objects are not equal + public void Equal(T expected, T actual) + { + Assert.Equal(expected, actual); + } + + /// + /// Verifies that two objects are equal, using a custom comparer. + /// + /// The type of the objects to be compared + /// The expected value + /// The value to be compared against + /// The comparer used to compare the two objects + /// Thrown when the objects are not equal + public void Equal(T expected, T actual, IEqualityComparer comparer) + { + Assert.Equal(expected, actual, comparer); + } + + /// Do not call this method. Call Assert.Equal() instead. + public override bool Equals(object obj) + { + throw new NotImplementedException(); + } + + /// + /// Verifies that the condition is false. + /// + /// The condition to be tested + /// Thrown if the condition is not false + public void False(bool condition) + { + Assert.False(condition); + } + + /// + /// Verifies that the condition is false. + /// + /// The condition to be tested + /// The message to show when the condition is not false + /// Thrown if the condition is not false + public void False(bool condition, string userMessage) + { + Assert.False(condition, userMessage); + } + + /// + /// Serves as a hash function for a particular type. + /// + /// A hash code for the current . + public override int GetHashCode() + { + return 42; + } + + /// Verifies that an object is greater than the exclusive minimum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the exclusive minimum value of paramref name="value"/>. + public static void GreaterThan(T value, T maxValue) + { + Assert.GreaterThan(value, maxValue); + } + + /// Verifies that an object is greater than the exclusive minimum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the exclusive minimum value of paramref name="value"/>. + /// An used to compare the objects. + public static void GreaterThan(T value, T minValue, IComparer comparer) + { + Assert.GreaterThan(value, minValue, comparer); + } + + /// Verifies that an object is greater than the inclusive minimum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the inclusive minimum value of paramref name="value"/>. + public static void GreaterThanOrEqual(T value, T minValue) + { + Assert.GreaterThanOrEqual(value, minValue); + } + + /// Verifies that an object is greater than the inclusive minimum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the inclusive minimum value of paramref name="value"/>. + /// An used to compare the objects. + public static void GreaterThanOrEqual(T value, T minValue, IComparer comparer) + { + Assert.GreaterThanOrEqual(value, minValue, comparer); + } + + /// + /// Verifies that a value is within a given range. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// Thrown when the value is not in the given range + public void InRange(T actual, T low, T high) + { + Assert.InRange(actual, low, high); + } + + /// + /// Verifies that a value is within a given range, using a comparer. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// The comparer used to evaluate the value's range + /// Thrown when the value is not in the given range + public void InRange(T actual, T low, T high, IComparer comparer) + { + Assert.InRange(actual, low, high, comparer); + } + + /// + /// Verifies that an object is not exactly the given type. + /// + /// The type the object should not be + /// The object to be evaluated + /// Thrown when the object is the given type + public void IsNotType(object @object) + { + Assert.IsNotType(@object); + } + + /// + /// Verifies that an object is not exactly the given type. + /// + /// The type the object should not be + /// The object to be evaluated + /// Thrown when the object is the given type + public void IsNotType(Type expectedType, object @object) + { + Assert.IsNotType(expectedType, @object); + } + + /// + /// Verifies that an object is exactly the given type (and not a derived type). + /// + /// The type the object should be + /// The object to be evaluated + /// The object, casted to type T when successful + /// Thrown when the object is not the given type + public T IsType(object @object) + { + return Assert.IsType(@object); + } + + /// + /// Verifies that an object is exactly the given type (and not a derived type). + /// + /// The type the object should be + /// The object to be evaluated + /// Thrown when the object is not the given type + public void IsType(Type expectedType, object @object) + { + Assert.IsType(expectedType, @object); + } + + /// Verifies that an object is less than the exclusive maximum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the exclusive maximum value of paramref name="value"/>. + public static void LessThan(T value, T maxValue) + { + Assert.LessThan(value, maxValue); + } + + /// Verifies that an object is less than the exclusive maximum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the exclusive maximum value of paramref name="value"/>. + /// An used to compare the objects. + public static void LessThan(T value, T maxValue, IComparer comparer) + { + Assert.LessThan(value, maxValue, comparer); + } + + /// Verifies that an object is less than the inclusive maximum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the inclusive maximum value of paramref name="value"/>. + public static void LessThanOrEqual(T value, T maxValue) + { + Assert.LessThanOrEqual(value, maxValue); + } + + /// Verifies that an object is less than the inclusive maximum value. + /// The type of the objects to be compared. + /// The object to be evaluated. + /// An object representing the inclusive maximum value of paramref name="value"/>. + /// An used to compare the objects. + public static void LessThanOrEqual(T value, T maxValue, IComparer comparer) + { + Assert.LessThanOrEqual(value, maxValue, comparer); + } + + /// + /// Verifies that a collection is not empty. + /// + /// The collection to be inspected + /// Thrown when a null collection is passed + /// Thrown when the collection is empty + public void NotEmpty(IEnumerable collection) + { + Assert.NotEmpty(collection); + } + + /// + /// Verifies that two objects are not equal, using a default comparer. + /// + /// The type of the objects to be compared + /// The expected object + /// The actual object + /// Thrown when the objects are equal + public void NotEqual(T expected, T actual) + { + Assert.NotEqual(expected, actual); + } + + /// + /// Verifies that two objects are not equal, using a custom comparer. + /// + /// The type of the objects to be compared + /// The expected object + /// The actual object + /// The comparer used to examine the objects + /// Thrown when the objects are equal + public void NotEqual(T expected, T actual, IEqualityComparer comparer) + { + Assert.NotEqual(expected, actual, comparer); + } + + /// + /// Verifies that a value is not within a given range, using the default comparer. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// Thrown when the value is in the given range + public void NotInRange(T actual, T low, T high) + { + Assert.NotInRange(actual, low, high); + } + + /// + /// Verifies that a value is not within a given range, using a comparer. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// The comparer used to evaluate the value's range + /// Thrown when the value is in the given range + public void NotInRange(T actual, T low, T high, IComparer comparer) + { + Assert.NotInRange(actual, low, high, comparer); + } + + /// + /// Verifies that an object reference is not null. + /// + /// The object to be validated + /// Thrown when the object is not null + public void NotNull(object @object) + { + Assert.NotNull(@object); + } + + /// + /// Verifies that two objects are not the same instance. + /// + /// The expected object instance + /// The actual object instance + /// Thrown when the objects are the same instance + public void NotSame(object expected, object actual) + { + Assert.NotSame(expected, actual); + } + + /// + /// Verifies that an object reference is null. + /// + /// The object to be inspected + /// Thrown when the object reference is not null + public void Null(object @object) + { + Assert.Null(@object); + } + + /// + /// Verifies that two objects are the same instance. + /// + /// The expected object instance + /// The actual object instance + /// Thrown when the objects are not the same instance + public void Same(object expected, object actual) + { + Assert.Same(expected, actual); + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// + /// The type of the exception expected to be thrown + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public T Throws(Assert.ThrowsDelegate testCode) + where T : Exception + { + return Assert.Throws(testCode); + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// + /// The type of the exception expected to be thrown + /// The message to be shown if the test fails + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public T Throws(string userMessage, Assert.ThrowsDelegate testCode) + where T : Exception + { + return Assert.Throws(userMessage, testCode); + } + + /// + /// Verifies that the exact exception is thrown (and not a derived exception type). + /// + /// The type of the exception expected to be thrown + /// A delegate to the code to be tested + /// The exception that was thrown, when successful + /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown + public Exception Throws(Type exceptionType, Assert.ThrowsDelegate testCode) + { + return Assert.Throws(exceptionType, testCode); + } + + /// + /// Verifies that an expression is true. + /// + /// The condition to be inspected + /// Thrown when the condition is false + public void True(bool condition) + { + Assert.True(condition); + } + + /// + /// Verifies that an expression is true. + /// + /// The condition to be inspected + /// The message to be shown when the condition is false + /// Thrown when the condition is false + public void True(bool condition, string userMessage) + { + Assert.True(condition, userMessage); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Assertions/EnumerableEqualityComparer.cs b/src/UnitTests/Should/Should.Core/Assertions/EnumerableEqualityComparer.cs new file mode 100644 index 0000000000..e7e1d1d3a0 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Assertions/EnumerableEqualityComparer.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Should.Core.Assertions +{ + internal class EnumerableEqualityComparer : IEqualityComparer + { + public int Position { get; set; } + + public bool Equals(IEnumerable x, IEnumerable y) + { + IEnumerator enumeratorX = x.GetEnumerator(); + IEnumerator enumeratorY = y.GetEnumerator(); + + Position = 0; + + while (true) + { + bool hasNextX = enumeratorX.MoveNext(); + bool hasNextY = enumeratorY.MoveNext(); + + if (!hasNextX || !hasNextY) + return hasNextX == hasNextY; + + if (enumeratorX.Current != null || enumeratorY.Current != null) + { + if (enumeratorX.Current != null && enumeratorY.Current == null) + return false; + + if (enumeratorX.Current == null) + return false; + + var xType = enumeratorX.Current.GetType(); + var yType = enumeratorY.Current.GetType(); + + if (xType.IsAssignableFrom(yType)) + { + if (!Equals(enumeratorX.Current, enumeratorY.Current, xType)) + return false; + } + else if (yType.IsAssignableFrom(xType)) + { + if (!Equals(enumeratorY.Current, enumeratorX.Current, yType)) + return false; + } + else + { + return false; + } + } + Position++; + } + } + + public int GetHashCode(IEnumerable obj) + { + throw new NotImplementedException(); + } + + private bool Equals(object a, object b, Type baseType) + { + var assertComparerType = typeof(AssertEqualityComparer<>).MakeGenericType(baseType); + var assertComparer = Activator.CreateInstance(assertComparerType); + var compareMethod = assertComparerType.GetMethod("Equals", new [] { baseType, baseType }); + return (bool)compareMethod.Invoke(assertComparer, new[] { a, b }); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Assertions/Record.cs b/src/UnitTests/Should/Should.Core/Assertions/Record.cs new file mode 100644 index 0000000000..84d6a556a4 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Assertions/Record.cs @@ -0,0 +1,47 @@ +using System; + +namespace Should.Core.Assertions +{ + /// + /// Allows the user to record actions for a test. + /// + public class Record + { + /// + /// Records any exception which is thrown by the given code. + /// + /// The code which may thrown an exception. + /// Returns the exception that was thrown by the code; null, otherwise. + public static Exception Exception(Assert.ThrowsDelegate code) + { + try + { + code(); + return null; + } + catch (Exception ex) + { + return ex; + } + } + + /// + /// Records any exception which is thrown by the given code that has + /// a return value. Generally used for testing property accessors. + /// + /// The code which may thrown an exception. + /// Returns the exception that was thrown by the code; null, otherwise. + public static Exception Exception(Assert.ThrowsDelegateWithReturn code) + { + try + { + code(); + return null; + } + catch (Exception ex) + { + return ex; + } + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/DatePrecision.cs b/src/UnitTests/Should/Should.Core/DatePrecision.cs new file mode 100644 index 0000000000..4c850b55c5 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/DatePrecision.cs @@ -0,0 +1,64 @@ +using System; + +namespace Should.Core +{ + public abstract class DatePrecision + { + public static DatePrecision Second = new SecondPrecision(); + public static DatePrecision Minute = new MinutePrecision(); + public static DatePrecision Hour = new HourPrecision(); + public static DatePrecision Date = new DayPrecision(); + public static DatePrecision Month = new MonthPrecision(); + public static DatePrecision Year = new YearPrecision(); + + public abstract DateTime Truncate(DateTime date); + + public class SecondPrecision : DatePrecision + { + public override DateTime Truncate(DateTime date) + { + return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second); + } + } + + public class MinutePrecision : DatePrecision + { + public override DateTime Truncate(DateTime date) + { + return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, 0); + } + } + + public class HourPrecision : DatePrecision + { + public override DateTime Truncate(DateTime date) + { + return new DateTime(date.Year, date.Month, date.Day, date.Hour, 0, 0); + } + } + + public class DayPrecision : DatePrecision + { + public override DateTime Truncate(DateTime date) + { + return new DateTime(date.Year, date.Month, date.Day, 0, 0, 0); + } + } + + public class MonthPrecision : DatePrecision + { + public override DateTime Truncate(DateTime date) + { + return new DateTime(date.Year, date.Month, 0, 0, 0, 0); + } + } + + public class YearPrecision : DatePrecision + { + public override DateTime Truncate(DateTime date) + { + return new DateTime(date.Year, 0, 0, 0, 0, 0); + } + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/AssertActualExpectedException.cs b/src/UnitTests/Should/Should.Core/Exceptions/AssertActualExpectedException.cs new file mode 100644 index 0000000000..229cc9d230 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/AssertActualExpectedException.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using Should.Core.Assertions; + +namespace Should.Core.Exceptions +{ + /// + /// Base class for exceptions that have actual and expected values + /// + public class AssertActualExpectedException : AssertException + { + readonly string actual; + readonly string differencePosition = ""; + readonly string expected; + + /// + /// Creates a new instance of the class. + /// + /// The expected value + /// The actual value + /// The user message to be shown + public AssertActualExpectedException(object expected, + object actual, + string userMessage) + : this(expected, actual, userMessage, false) { } + + /// + /// Creates a new instance of the class. + /// + /// The expected value + /// The actual value + /// The user message to be shown + /// Set to true to skip the check for difference position + public AssertActualExpectedException(object expected, + object actual, + string userMessage, + bool skipPositionCheck) + : base(userMessage) + { + if (!skipPositionCheck) + { + IEnumerable enumerableActual = actual as IEnumerable; + IEnumerable enumerableExpected = expected as IEnumerable; + + if (enumerableActual != null && enumerableExpected != null) + { + var comparer = new EnumerableEqualityComparer(); + comparer.Equals(enumerableActual, enumerableExpected); + + differencePosition = "Position: First difference is at position " + comparer.Position + Environment.NewLine; + } + } + + this.actual = actual == null ? null : ConvertToString(actual); + this.expected = expected == null ? null : ConvertToString(expected); + + if (actual != null && + expected != null && + actual.ToString() == expected.ToString() && + actual.GetType() != expected.GetType()) + { + this.actual += String.Format(" ({0})", actual.GetType().FullName); + this.expected += String.Format(" ({0})", expected.GetType().FullName); + } + } + + /// + /// Gets the actual value. + /// + public string Actual + { + get { return actual; } + } + + /// + /// Gets the expected value. + /// + public string Expected + { + get { return expected; } + } + + /// + /// Gets a message that describes the current exception. Includes the expected and actual values. + /// + /// The error message that explains the reason for the exception, or an empty string(""). + /// 1 + public override string Message + { + get + { + return string.Format("{0}{4}{1}Expected: {2}{4}Actual: {3}", + base.Message, + differencePosition, + FormatMultiLine(Expected ?? "(null)"), + FormatMultiLine(Actual ?? "(null)"), + Environment.NewLine); + } + } + + static string ConvertToString(object value) + { + Array valueArray = value as Array; + if (valueArray == null) + return value.ToString(); + + List valueStrings = new List(); + + foreach (object valueObject in valueArray) + valueStrings.Add(valueObject == null ? "(null)" : valueObject.ToString()); + + return value.GetType().FullName + " { " + String.Join(", ", valueStrings.ToArray()) + " }"; + } + + static string FormatMultiLine(string value) + { + return value.Replace(Environment.NewLine, Environment.NewLine + " "); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/AssertException.cs b/src/UnitTests/Should/Should.Core/Exceptions/AssertException.cs new file mode 100644 index 0000000000..386f89fdb7 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/AssertException.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace Should.Core.Exceptions +{ + /// + /// The base assert exception class + /// + public class AssertException : Exception + { + public static string FilterStackTraceAssemblyPrefix = "Should."; + + readonly string stackTrace; + + /// + /// Initializes a new instance of the class. + /// + public AssertException() { } + + /// + /// Initializes a new instance of the class. + /// + /// The user message to be displayed + public AssertException(string userMessage) + : base(userMessage) + { + this.UserMessage = userMessage; + } + + /// + /// Initializes a new instance of the class. + /// + /// The user message to be displayed + /// The inner exception + public AssertException(string userMessage, Exception innerException) + : base(userMessage, innerException) { } + + /// + /// Initializes a new instance of the class. + /// + /// The user message to be displayed + /// The stack trace to be displayed + protected AssertException(string userMessage, string stackTrace) + : base(userMessage) + { + this.stackTrace = stackTrace; + } + + /// + /// Gets a string representation of the frames on the call stack at the time the current exception was thrown. + /// + /// A string that describes the contents of the call stack, with the most recent method call appearing first. + public override string StackTrace + { + get { return FilterStackTrace(stackTrace ?? base.StackTrace); } + } + + /// + /// Gets the user message + /// + public string UserMessage { get; protected set; } + + /// + /// Filters the stack trace to remove all lines that occur within the testing framework. + /// + /// The original stack trace + /// The filtered stack trace + protected static string FilterStackTrace(string stackTrace) + { + if (stackTrace == null) + return null; + + List results = new List(); + + foreach (string line in SplitLines(stackTrace)) + { + string trimmedLine = line.TrimStart(); + if (!trimmedLine.StartsWith( "at " + FilterStackTraceAssemblyPrefix) ) + results.Add(line); + } + + return string.Join(Environment.NewLine, results.ToArray()); + } + + // Our own custom String.Split because Silverlight/CoreCLR doesn't support the version we were using + static IEnumerable SplitLines(string input) + { + while (true) + { + int idx = input.IndexOf(Environment.NewLine); + + if (idx < 0) + { + yield return input; + break; + } + + yield return input.Substring(0, idx); + input = input.Substring(idx + Environment.NewLine.Length); + } + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/ComparisonException.cs b/src/UnitTests/Should/Should.Core/Exceptions/ComparisonException.cs new file mode 100644 index 0000000000..28899c759e --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/ComparisonException.cs @@ -0,0 +1,35 @@ +using System; + +namespace Should.Core.Exceptions +{ + public abstract class ComparisonException : AssertException + { + public string Left { get; private set; } + public string Right { get; private set; } + + protected ComparisonException(object left, object right, string methodName, string operation) + : base(string.Format("Assert.{0}() Failure:\r\n\tExpected: {1} {2} {3}\r\n\tbut it was not", methodName, Format(right), operation, Format(left))) + { + Left = left != null ? left.ToString() : null; + Right = right != null ? right.ToString() : null; + } + + protected ComparisonException(object left, object right, string message) : base(message) + { + Left = left != null ? left.ToString() : null; + Right = right != null ? right.ToString() : null; + } + + public static string Format(object value) + { + if (value == null) + { + return "(null)"; + } + var type = value.GetType(); + return type == typeof(string) // || type == typeof(DateTime) || type == typeof(DateTime?) + ? string.Format("\"{0}\"", value) + : value.ToString(); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/ContainsException.cs b/src/UnitTests/Should/Should.Core/Exceptions/ContainsException.cs new file mode 100644 index 0000000000..fb2d8485ba --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/ContainsException.cs @@ -0,0 +1,15 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when a collection unexpectedly does not contain the expected value. + /// + public class ContainsException : AssertException + { + /// + /// Creates a new instance of the class. + /// + /// The expected object value + public ContainsException(object expected) + : base(string.Format("Assert.Contains() failure: Not found: {0}", expected)) { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/DoesNotContainException.cs b/src/UnitTests/Should/Should.Core/Exceptions/DoesNotContainException.cs new file mode 100644 index 0000000000..59de99770d --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/DoesNotContainException.cs @@ -0,0 +1,15 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when a collection unexpectedly contains the expected value. + /// + public class DoesNotContainException : AssertException + { + /// + /// Creates a new instance of the class. + /// + /// The expected object value + public DoesNotContainException(object expected) + : base(string.Format("Assert.DoesNotContain() failure: Found: {0}", expected)) { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/DoesNotThrowException.cs b/src/UnitTests/Should/Should.Core/Exceptions/DoesNotThrowException.cs new file mode 100644 index 0000000000..f2a5de3559 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/DoesNotThrowException.cs @@ -0,0 +1,34 @@ +using System; + +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when code unexpectedly throws an exception. + /// + public class DoesNotThrowException : AssertActualExpectedException + { + readonly string stackTrace; + + /// + /// Creates a new instance of the class. + /// + /// Actual exception + public DoesNotThrowException(Exception actual) + : base("(No exception)", + actual.GetType().FullName + (actual.Message == null ? "" : ": " + actual.Message), + "Assert.DoesNotThrow() failure", + true) + { + stackTrace = actual.StackTrace; + } + + /// + /// Gets a string representation of the frames on the call stack at the time the current exception was thrown. + /// + /// A string that describes the contents of the call stack, with the most recent method call appearing first. + public override string StackTrace + { + get { return FilterStackTrace(stackTrace ?? base.StackTrace); } + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/EmptyException.cs b/src/UnitTests/Should/Should.Core/Exceptions/EmptyException.cs new file mode 100644 index 0000000000..8be488136c --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/EmptyException.cs @@ -0,0 +1,14 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when a collection is unexpectedly not empty. + /// + public class EmptyException : AssertException + { + /// + /// Creates a new instance of the class. + /// + public EmptyException() + : base("Assert.Empty() failure") { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/EqualException.cs b/src/UnitTests/Should/Should.Core/Exceptions/EqualException.cs new file mode 100644 index 0000000000..109c93432b --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/EqualException.cs @@ -0,0 +1,26 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when two values are unexpectedly not equal. + /// + public class EqualException : AssertActualExpectedException + { + /// + /// Creates a new instance of the class. + /// + /// The expected object value + /// The actual object value + public EqualException(object expected, + object actual) + : this(expected, actual, "Assert.Equal() Failure") { } + + /// + /// Creates a new instance of the class. + /// + /// The expected object value + /// The actual object value + /// The user message to be shown on failure + public EqualException(object expected, object actual, string userMessage) + : base(expected, actual, userMessage) { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/FalseException.cs b/src/UnitTests/Should/Should.Core/Exceptions/FalseException.cs new file mode 100644 index 0000000000..fe493b2716 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/FalseException.cs @@ -0,0 +1,15 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when a value is unexpectedly true. + /// + public class FalseException : AssertException + { + /// + /// Creates a new instance of the class. + /// + /// The user message to be display, or null for the default message + public FalseException(string userMessage) + : base(userMessage ?? "Assert.False() Failure") { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/GreaterThanException.cs b/src/UnitTests/Should/Should.Core/Exceptions/GreaterThanException.cs new file mode 100644 index 0000000000..41ff4eb1dc --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/GreaterThanException.cs @@ -0,0 +1,20 @@ +namespace Should.Core.Exceptions +{ + /// Exception thrown when a value is not greater than the expected minimum. + public class GreaterThanException : ComparisonException + { + /// Initializes a new instance of the class. + /// The value being tested. + /// The exclusive minimum allowed value. + public GreaterThanException(object left, object right) + : base(right, left, "GreaterThan", ">") + { } + + /// Initializes a new instance of the class. + /// The value being tested. + /// The exclusive minimum allowed value. + public GreaterThanException(object left, object right, string message) + : base(left, right, message) + { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/GreaterThanOrEqualException.cs b/src/UnitTests/Should/Should.Core/Exceptions/GreaterThanOrEqualException.cs new file mode 100644 index 0000000000..c095562a0d --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/GreaterThanOrEqualException.cs @@ -0,0 +1,20 @@ +namespace Should.Core.Exceptions +{ + /// Exception thrown when a value is not greater than the expected minimum. + public class GreaterThanOrEqualException : ComparisonException + { + /// Initializes a new instance of the class. + /// The value being tested. + /// The exclusive minimum allowed value. + public GreaterThanOrEqualException(object left, object right) + : base(right, left, "GreaterThanOrEqual", ">=") + { } + + /// Initializes a new instance of the class. + /// The value being tested. + /// The exclusive minimum allowed value. + public GreaterThanOrEqualException(object left, object right, string message) + : base(left, right, message) + { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/InRangeException.cs b/src/UnitTests/Should/Should.Core/Exceptions/InRangeException.cs new file mode 100644 index 0000000000..ea612232a2 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/InRangeException.cs @@ -0,0 +1,65 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when a value is unexpectedly not in the given range. + /// + public class InRangeException : AssertException + { + readonly string actual; + readonly string high; + readonly string low; + + /// + /// Creates a new instance of the class. + /// + /// The actual object value + /// The low value of the range + /// The high value of the range + public InRangeException(object actual, + object low, + object high) + : base("Assert.InRange() Failure") + { + this.low = low == null ? null : low.ToString(); + this.high = high == null ? null : high.ToString(); + this.actual = actual == null ? null : actual.ToString(); + } + + /// + /// Gets the actual object value + /// + public string Actual + { + get { return actual; } + } + + /// + /// Gets the high value of the range + /// + public string High + { + get { return high; } + } + + /// + /// Gets the low value of the range + /// + public string Low + { + get { return low; } + } + + /// + /// Gets a message that describes the current exception. + /// + /// The error message that explains the reason for the exception, or an empty string(""). + public override string Message + { + get + { + return string.Format("{0}\r\nRange: ({1} - {2})\r\nActual: {3}", + base.Message, Low, High, Actual ?? "(null)"); + } + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/IsAssignableFromException.cs b/src/UnitTests/Should/Should.Core/Exceptions/IsAssignableFromException.cs new file mode 100644 index 0000000000..a5e8c39fb4 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/IsAssignableFromException.cs @@ -0,0 +1,26 @@ +using System; + +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when the value is unexpectedly not of the given type or a derived type. + /// + public class IsAssignableFromException : AssertActualExpectedException + { + /// + /// Creates a new instance of the class. + /// + /// The expected type + /// The actual object value + public IsAssignableFromException(Type expected, object actual) : this(expected, actual, "Assert.IsAssignableFrom() Failure") { } + + /// + /// Creates a new instance of the class. + /// + /// The expected type + /// The actual object value + /// A custom message to prepend to the default Assert.IsAssignableFrom() failure message + public IsAssignableFromException(Type expected, object actual, string userMessage) + : base(expected, actual == null ? null : actual.GetType(), userMessage) { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/IsNotTypeException.cs b/src/UnitTests/Should/Should.Core/Exceptions/IsNotTypeException.cs new file mode 100644 index 0000000000..ee62a92574 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/IsNotTypeException.cs @@ -0,0 +1,19 @@ +using System; + +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when the value is unexpectedly of the exact given type. + /// + public class IsNotTypeException : AssertActualExpectedException + { + /// + /// Creates a new instance of the class. + /// + /// The expected type + /// The actual object value + public IsNotTypeException(Type expected, + object actual) + : base(expected, actual == null ? null : actual.GetType(), "Assert.IsNotType() Failure") { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/IsTypeException.cs b/src/UnitTests/Should/Should.Core/Exceptions/IsTypeException.cs new file mode 100644 index 0000000000..98c54cc61b --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/IsTypeException.cs @@ -0,0 +1,19 @@ +using System; + +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when the value is unexpectedly not of the exact given type. + /// + public class IsTypeException : AssertActualExpectedException + { + /// + /// Creates a new instance of the class. + /// + /// The expected type + /// The actual object value + public IsTypeException(Type expected, + object actual) + : base(expected, actual == null ? null : actual.GetType(), "Assert.IsType() Failure") { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/LessThanException.cs b/src/UnitTests/Should/Should.Core/Exceptions/LessThanException.cs new file mode 100644 index 0000000000..f30011b3e7 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/LessThanException.cs @@ -0,0 +1,20 @@ +namespace Should.Core.Exceptions +{ + /// Exception thrown when a value is not less than the expected maximum. + public class LessThanException : ComparisonException + { + /// Initializes a new instance of the class. + /// The value being tested. + /// The exclusive maximum allowed value. + public LessThanException(object left, object right) + : base(right, left, "LessThan", "<") + { } + + /// Initializes a new instance of the class. + /// The value being tested. + /// The exclusive maximum allowed value. + public LessThanException(object left, object right, string message) + : base(left, right, message) + { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/LessThanOrEqualException.cs b/src/UnitTests/Should/Should.Core/Exceptions/LessThanOrEqualException.cs new file mode 100644 index 0000000000..13d8fcc11d --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/LessThanOrEqualException.cs @@ -0,0 +1,20 @@ +namespace Should.Core.Exceptions +{ + /// Exception thrown when a value is not less than or equal to the expected maximum. + public class LessThanOrEqualException : ComparisonException + { + /// Initializes a new instance of the class. + /// The value being tested. + /// The exclusive maximum allowed value. + public LessThanOrEqualException(object left, object right) + : base(right, left, "LessThanOrEqual", "<=") + { } + + /// Initializes a new instance of the class. + /// The value being tested. + /// The exclusive maximum allowed value. + public LessThanOrEqualException(object left, object right, string message) + : base(left, right, message) + { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/NotEmptyException.cs b/src/UnitTests/Should/Should.Core/Exceptions/NotEmptyException.cs new file mode 100644 index 0000000000..2bb0181e23 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/NotEmptyException.cs @@ -0,0 +1,14 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when a collection is unexpectedly empty. + /// + public class NotEmptyException : AssertException + { + /// + /// Creates a new instance of the class. + /// + public NotEmptyException() + : base("Assert.NotEmpty() failure") { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/NotEqualException.cs b/src/UnitTests/Should/Should.Core/Exceptions/NotEqualException.cs new file mode 100644 index 0000000000..2ef6a10db4 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/NotEqualException.cs @@ -0,0 +1,26 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when two values are unexpectedly equal. + /// + public class NotEqualException : AssertActualExpectedException + { + /// + /// Creates a new instance of the class. + /// + /// The expected object value + /// The actual object value + public NotEqualException(object expected, + object actual) + : this(expected, actual, "Assert.NotEqual() Failure") { } + + /// + /// Creates a new instance of the class. + /// + /// The expected object value + /// The actual object value + /// The user message to be shown on failure + public NotEqualException(object expected, object actual, string userMessage) + : base(expected, actual, userMessage) { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/NotInRangeException.cs b/src/UnitTests/Should/Should.Core/Exceptions/NotInRangeException.cs new file mode 100644 index 0000000000..2ebcee277d --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/NotInRangeException.cs @@ -0,0 +1,65 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when a value is unexpectedly in the given range. + /// + public class NotInRangeException : AssertException + { + readonly string actual; + readonly string high; + readonly string low; + + /// + /// Creates a new instance of the class. + /// + /// The actual object value + /// The low value of the range + /// The high value of the range + public NotInRangeException(object actual, + object low, + object high) + : base("Assert.NotInRange() Failure") + { + this.low = low == null ? null : low.ToString(); + this.high = high == null ? null : high.ToString(); + this.actual = actual == null ? null : actual.ToString(); + } + + /// + /// Gets the actual object value + /// + public string Actual + { + get { return actual; } + } + + /// + /// Gets the high value of the range + /// + public string High + { + get { return high; } + } + + /// + /// Gets the low value of the range + /// + public string Low + { + get { return low; } + } + + /// + /// Gets a message that describes the current exception. + /// + /// The error message that explains the reason for the exception, or an empty string(""). + public override string Message + { + get + { + return string.Format("{0}\r\nRange: ({1} - {2})\r\nActual: {3}", + base.Message, Low, High, Actual ?? "(null)"); + } + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/NotNullException.cs b/src/UnitTests/Should/Should.Core/Exceptions/NotNullException.cs new file mode 100644 index 0000000000..c4e19c1deb --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/NotNullException.cs @@ -0,0 +1,20 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when an object is unexpectedly null. + /// + public class NotNullException : AssertException + { + /// + /// Creates a new instance of the class. + /// + public NotNullException() + : this("Assert.NotNull() Failure") { } + + /// + /// Creates a new instance of the class with the given failure . + /// + public NotNullException(string message) + : base(message) { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/NotSameException.cs b/src/UnitTests/Should/Should.Core/Exceptions/NotSameException.cs new file mode 100644 index 0000000000..8adadfc524 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/NotSameException.cs @@ -0,0 +1,14 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when two values are unexpected the same instance. + /// + public class NotSameException : AssertException + { + /// + /// Creates a new instance of the class. + /// + public NotSameException() + : base("Assert.NotSame() Failure") { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/NullException.cs b/src/UnitTests/Should/Should.Core/Exceptions/NullException.cs new file mode 100644 index 0000000000..5b66c5171e --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/NullException.cs @@ -0,0 +1,15 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when an object reference is unexpectedly not null. + /// + public class NullException : AssertActualExpectedException + { + /// + /// Creates a new instance of the class. + /// + /// + public NullException(object actual) + : base(null, actual, "Assert.Null() Failure") { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/ParameterCountMismatchException.cs b/src/UnitTests/Should/Should.Core/Exceptions/ParameterCountMismatchException.cs new file mode 100644 index 0000000000..b74904e198 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/ParameterCountMismatchException.cs @@ -0,0 +1,13 @@ +using System; +using System.Runtime.Serialization; + +namespace Should.Core.Exceptions +{ + /// + /// Exception to be thrown from when the number of + /// parameter values does not the test method signature. + /// + public class ParamterCountMismatchException : Exception + { + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/SameException.cs b/src/UnitTests/Should/Should.Core/Exceptions/SameException.cs new file mode 100644 index 0000000000..f011c8daca --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/SameException.cs @@ -0,0 +1,17 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when two object references are unexpectedly not the same instance. + /// + public class SameException : AssertActualExpectedException + { + /// + /// Creates a new instance of the class. + /// + /// The expected object reference + /// The actual object reference + public SameException(object expected, + object actual) + : base(expected, actual, "Assert.Same() Failure", true) { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/SingleException.cs b/src/UnitTests/Should/Should.Core/Exceptions/SingleException.cs new file mode 100644 index 0000000000..52ddb72b60 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/SingleException.cs @@ -0,0 +1,17 @@ +using System; + +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when the collection did not contain exactly one element. + /// + public class SingleException : AssertException + { + /// + /// Initializes a new instance of the class. + /// + /// The numbers of items in the collection. + public SingleException(int count) + : base(String.Format("The collection contained {0} elements instead of 1.", count)) { } + } +} diff --git a/src/UnitTests/Should/Should.Core/Exceptions/StartsWithException.cs b/src/UnitTests/Should/Should.Core/Exceptions/StartsWithException.cs new file mode 100644 index 0000000000..8ed35826fe --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/StartsWithException.cs @@ -0,0 +1,16 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when a collection unexpectedly does not contain the expected value. + /// + public class StartsWithException : AssertException + { + /// + /// Creates a new instance of the class. + /// + /// The expected object value + /// The actual object value + public StartsWithException(object expectedStartString, object actual) + : base(string.Format("Assert.StartsWith() failure: '{0}' not found at the beginning of '{1}'", expectedStartString, actual)) { } + } +} diff --git a/src/UnitTests/Should/Should.Core/Exceptions/ThrowsException.cs b/src/UnitTests/Should/Should.Core/Exceptions/ThrowsException.cs new file mode 100644 index 0000000000..b840b85ded --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/ThrowsException.cs @@ -0,0 +1,50 @@ +using System; + +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when code unexpectedly fails to throw an exception. + /// + public class ThrowsException : AssertActualExpectedException + { + readonly string stackTrace = null; + + /// + /// Creates a new instance of the class. Call this constructor + /// when no exception was thrown. + /// + /// The type of the exception that was expected + public ThrowsException(Type expectedType) + : this(expectedType, "(No exception was thrown)", null, null) { } + + /// + /// Creates a new instance of the class. Call this constructor + /// when an exception of the wrong type was thrown. + /// + /// The type of the exception that was expected + /// The actual exception that was thrown + public ThrowsException(Type expectedType, + Exception actual) + : this(expectedType, actual.GetType().FullName, actual.Message, actual.StackTrace) { } + + ThrowsException(Type expected, + string actual, + string actualMessage, + string stackTrace) + : base(expected, + actual + (actualMessage == null ? "" : ": " + actualMessage), + "Assert.Throws() Failure") + { + this.stackTrace = stackTrace; + } + + /// + /// Gets a string representation of the frames on the call stack at the time the current exception was thrown. + /// + /// A string that describes the contents of the call stack, with the most recent method call appearing first. + public override string StackTrace + { + get { return FilterStackTrace(stackTrace ?? base.StackTrace); } + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/TimeoutException.cs b/src/UnitTests/Should/Should.Core/Exceptions/TimeoutException.cs new file mode 100644 index 0000000000..26e24b7f24 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/TimeoutException.cs @@ -0,0 +1,15 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when a test method exceeds the given timeout value + /// + public class TimeoutException : AssertException + { + /// + /// Creates a new instance of the class. + /// + /// The timeout value, in milliseconds + public TimeoutException(long timeout) + : base(string.Format("Test execution time exceeded: {0}ms", timeout)) { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/TraceAssertException.cs b/src/UnitTests/Should/Should.Core/Exceptions/TraceAssertException.cs new file mode 100644 index 0000000000..1496ae0571 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/TraceAssertException.cs @@ -0,0 +1,69 @@ +using System; + +namespace Should.Core.Exceptions +{ + /// + /// Exception that is thrown when a call to Debug.Assert() fails. + /// + public class TraceAssertException : AssertException + { + readonly string assertDetailedMessage; + readonly string assertMessage; + + /// + /// Creates a new instance of the class. + /// + /// The original assert message + public TraceAssertException(string assertMessage) + : this(assertMessage, "") { } + + /// + /// Creates a new instance of the class. + /// + /// The original assert message + /// The original assert detailed message + public TraceAssertException(string assertMessage, + string assertDetailedMessage) + { + this.assertMessage = assertMessage ?? ""; + this.assertDetailedMessage = assertDetailedMessage ?? ""; + } + + /// + /// Gets the original assert detailed message. + /// + public string AssertDetailedMessage + { + get { return assertDetailedMessage; } + } + + /// + /// Gets the original assert message. + /// + public string AssertMessage + { + get { return assertMessage; } + } + + /// + /// Gets a message that describes the current exception. + /// + public override string Message + { + get + { + string result = "Debug.Assert() Failure"; + + if (AssertMessage != "") + { + result += " : " + AssertMessage; + + if (AssertDetailedMessage != "") + result += Environment.NewLine + "Detailed Message:" + Environment.NewLine + AssertDetailedMessage; + } + + return result; + } + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/Should.Core/Exceptions/TrueException.cs b/src/UnitTests/Should/Should.Core/Exceptions/TrueException.cs new file mode 100644 index 0000000000..5698cf2d59 --- /dev/null +++ b/src/UnitTests/Should/Should.Core/Exceptions/TrueException.cs @@ -0,0 +1,15 @@ +namespace Should.Core.Exceptions +{ + /// + /// Exception thrown when a value is unexpectedly false. + /// + public class TrueException : AssertException + { + /// + /// Creates a new instance of the class. + /// + /// The user message to be displayed, or null for the default message + public TrueException(string userMessage) + : base(userMessage ?? "Assert.True() Failure") { } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/should/ActionAssertionExtensions.cs b/src/UnitTests/Should/should/ActionAssertionExtensions.cs new file mode 100644 index 0000000000..13989ee4bf --- /dev/null +++ b/src/UnitTests/Should/should/ActionAssertionExtensions.cs @@ -0,0 +1,24 @@ +using System; +using Should.Core.Assertions; + +namespace Should +{ + public static class ActionAssertionExtensions + { + /// Verifies that the throws the specified exception type. + /// The type of exception expected to be thrown. + /// The action which should throw the exception. + public static void ShouldThrow(this Action action) where T : Exception + { + ShouldThrow(new Assert.ThrowsDelegate(action)); + } + + /// Verifies that the throws the specified exception type. + /// The type of exception expected to be thrown. + /// A which represents the action which should throw the exception. + public static void ShouldThrow(this Assert.ThrowsDelegate @delegate) where T : Exception + { + Assert.Throws(@delegate); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/should/BooleanAssertionExtensions.cs b/src/UnitTests/Should/should/BooleanAssertionExtensions.cs new file mode 100644 index 0000000000..52484653fe --- /dev/null +++ b/src/UnitTests/Should/should/BooleanAssertionExtensions.cs @@ -0,0 +1,56 @@ +using System; +using Should.Core.Assertions; +using Should.Core.Exceptions; + +namespace Should +{ + /// + /// Extensions which provide assertions to classes derived from . + /// + public static class BooleanAssertionExtensions + { + /// + /// Verifies that the condition is false. + /// + /// The condition to be tested + /// Thrown if the condition is not false + public static void ShouldBeFalse(this bool condition) + { + Assert.False(condition); + } + + /// + /// Verifies that the condition is false. + /// + /// The condition to be tested + /// The message to show when the condition is not false + /// Thrown if the condition is not false + public static void ShouldBeFalse(this bool condition, + string userMessage) + { + Assert.False(condition, userMessage); + } + + /// + /// Verifies that an expression is true. + /// + /// The condition to be inspected + /// Thrown when the condition is false + public static void ShouldBeTrue(this bool condition) + { + Assert.True(condition); + } + + /// + /// Verifies that an expression is true. + /// + /// The condition to be inspected + /// The message to be shown when the condition is false + /// Thrown when the condition is false + public static void ShouldBeTrue(this bool condition, + string userMessage) + { + Assert.True(condition, userMessage); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/should/CollectionAssertionExtensions.cs b/src/UnitTests/Should/should/CollectionAssertionExtensions.cs new file mode 100644 index 0000000000..79af20fb5b --- /dev/null +++ b/src/UnitTests/Should/should/CollectionAssertionExtensions.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using Should.Core.Assertions; +using Should.Core.Exceptions; + +namespace Should +{ + /// + /// Extensions which provide assertions to classes derived from and . + /// + public static class CollectionAssertExtensions + { + /// + /// Verifies that a collection is empty. + /// + /// The collection to be inspected + /// Thrown when the collection is null + /// Thrown when the collection is not empty + public static void ShouldBeEmpty(this IEnumerable collection) + { + Assert.Empty(collection); + } + + /// + /// Verifies that a collection contains a given object. + /// + /// The type of the object to be verified + /// The collection to be inspected + /// The object expected to be in the collection + /// Thrown when the object is not present in the collection + public static void ShouldContain(this IEnumerable collection, + T expected) + { + Assert.Contains(expected, collection); + } + + /// + /// Verifies that a collection contains a given object, using a comparer. + /// + /// The type of the object to be verified + /// The collection to be inspected + /// The object expected to be in the collection + /// The comparer used to equate objects in the collection with the expected object + /// Thrown when the object is not present in the collection + public static void ShouldContain(this IEnumerable collection, + T expected, + IEqualityComparer comparer) + { + Assert.Contains(expected, collection, comparer); + } + + /// + /// Verifies that a collection is not empty. + /// + /// The collection to be inspected + /// Thrown when a null collection is passed + /// Thrown when the collection is empty + public static void ShouldNotBeEmpty(this IEnumerable collection) + { + Assert.NotEmpty(collection); + } + + /// + /// Verifies that a collection does not contain a given object. + /// + /// The type of the object to be compared + /// The object that is expected not to be in the collection + /// The collection to be inspected + /// Thrown when the object is present inside the container + public static void ShouldNotContain(this IEnumerable collection, + T expected) + { + Assert.DoesNotContain(expected, collection); + } + + /// + /// Verifies that a collection does not contain a given object, using a comparer. + /// + /// The type of the object to be compared + /// The object that is expected not to be in the collection + /// The collection to be inspected + /// The comparer used to equate objects in the collection with the expected object + /// Thrown when the object is present inside the container + public static void ShouldNotContain(this IEnumerable collection, + T expected, + IEqualityComparer comparer) + { + Assert.DoesNotContain(expected, collection, comparer); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/should/DateAssertionExtensions.cs b/src/UnitTests/Should/should/DateAssertionExtensions.cs new file mode 100644 index 0000000000..318a709da6 --- /dev/null +++ b/src/UnitTests/Should/should/DateAssertionExtensions.cs @@ -0,0 +1,61 @@ +using System; +using Should.Core; +using Should.Core.Assertions; +using Should.Core.Exceptions; + +namespace Should +{ + /// + /// Extensions which provide assertions to classes derived from . + /// + public static class DateAssertionExtensions + { + /// + /// Verifies that two values are equal within a given tolerance. + /// + /// The value to be compared against + /// The expected value + /// The +/- value for where the expected and actual are considered to be equal + /// Thrown when the objects are not equal + public static void ShouldEqual(this DateTime actual, DateTime expected, TimeSpan tolerance) + { + Assert.Equal(expected, actual, tolerance); + } + + /// + /// Verifies that two values are not equal within a given tolerance. + /// + /// The value to be compared against + /// The expected value + /// The +/- value for where the expected and actual are considered to be equal + /// Thrown when the objects are equal + public static void ShouldNotEqual(this DateTime actual, DateTime expected, TimeSpan tolerance) + { + Assert.NotEqual(expected, actual, tolerance); + } + + /// + /// Verifies that two values are equal within a given tolerance. + /// + /// The value to be compared against + /// The expected value + /// The level of precision to use when making the comparison + /// Thrown when the objects are not equal + public static void ShouldEqual(this DateTime actual, DateTime expected, DatePrecision precision) + { + Assert.Equal(expected, actual, precision); + } + + /// + /// Verifies that two values are not equal within a given tolerance. + /// + /// The value to be compared against + /// The expected value + /// The level of precision to use when making the comparison + /// Thrown when the objects are equal + public static void ShouldNotEqual(this DateTime actual, DateTime expected, DatePrecision precision) + { + Assert.NotEqual(expected, actual, precision); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/should/DoubleAssertionExtensions.cs b/src/UnitTests/Should/should/DoubleAssertionExtensions.cs new file mode 100644 index 0000000000..5d8d11872a --- /dev/null +++ b/src/UnitTests/Should/should/DoubleAssertionExtensions.cs @@ -0,0 +1,62 @@ +using System; +using Should.Core.Assertions; +using Should.Core.Exceptions; + +namespace Should +{ + /// + /// Extensions which provide assertions to classes derived from . + /// + public static class DoubleAssertionExtensions + { + /// + /// Verifies that two values are equal within a given tolerance. + /// + /// The value to be compared against + /// The expected value + /// The +/- value for where the expected and actual are considered to be equal + /// Thrown when the objects are not equal + public static void ShouldEqual(this double actual, double expected, double tolerance) + { + Assert.Equal(expected, actual, tolerance); + } + + /// + /// Verifies that two values are equal within a given tolerance. + /// + /// The value to be compared against + /// The expected value + /// The +/- value for where the expected and actual are considered to be equal + /// The user message to show on failure + /// Thrown when the objects are not equal + public static void ShouldEqual(this double actual, double expected, double tolerance, string message) + { + Assert.Equal(expected, actual, tolerance, message); + } + + /// + /// Verifies that two values are not equal within a given tolerance. + /// + /// The value to be compared against + /// The expected value + /// The +/- value for where the expected and actual are considered to be equal + /// Thrown when the objects are equal + public static void ShouldNotEqual(this double actual, double expected, double tolerance) + { + Assert.NotEqual(expected, actual, tolerance); + } + + /// + /// Verifies that two values are not equal within a given tolerance. + /// + /// The value to be compared against + /// The expected value + /// The +/- value for where the expected and actual are considered to be equal + /// The user message to show on failure + /// Thrown when the objects are equal + public static void ShouldNotEqual(this double actual, double expected, double tolerance, string message) + { + Assert.NotEqual(expected, actual, tolerance, message); + } + } +} diff --git a/src/UnitTests/Should/should/ObjectAssertExtensions.cs b/src/UnitTests/Should/should/ObjectAssertExtensions.cs new file mode 100644 index 0000000000..b2c6c916db --- /dev/null +++ b/src/UnitTests/Should/should/ObjectAssertExtensions.cs @@ -0,0 +1,380 @@ +using System; +using System.Collections.Generic; +using Should.Core.Assertions; +using Should.Core.Exceptions; + +namespace Should +{ + /// + /// Extensions which provide assertions to classes derived from . + /// + public static class ObjectAssertExtensions + { + /// Verifies that an object is greater than the exclusive minimum value. + /// The type of the objects to be compared. + /// The object to be evaluated (left side of the comparison). + /// The (exclusive) minimum value of the . (The right side of the comparison.) + public static void ShouldBeGreaterThan(this T @object, T value) + { + Assert.GreaterThan(@object, value); + } + + /// Verifies that an object is greater than the exclusive minimum value. + /// The type of the objects to be compared. + /// The object to be evaluated (left side of the comparison). + /// The (exclusive) minimum value of the . (The right side of the comparison.) + /// An used to compare the objects. + public static void ShouldBeGreaterThan(this T @object, T value, IComparer comparer) + { + Assert.GreaterThan(@object, value, comparer); + } + + /// Verifies that an object is greater than the inclusive minimum value. + /// The type of the objects to be compared. + /// The object to be evaluated (left side of the comparison). + /// The (inclusive) minimum value of the . (The right side of the comparison.) + public static void ShouldBeGreaterThanOrEqualTo(this T @object, T value) + { + Assert.GreaterThanOrEqual(@object, value); + } + + /// Verifies that an object is greater than the inclusive minimum value. + /// The type of the objects to be compared. + /// The object to be evaluated (left side of the comparison). + /// The (inclusive) minimum value of the . (The right side of the comparison.) + /// An used to compare the objects. + public static void ShouldBeGreaterThanOrEqualTo(this T @object, T value, IComparer comparer) + { + Assert.GreaterThanOrEqual(@object, value, comparer); + } + + /// + /// Verifies that a value is within a given range. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// Thrown when the value is not in the given range + public static void ShouldBeInRange(this T actual, + T low, + T high) + { + Assert.InRange(actual, low, high); + } + + /// + /// Verifies that a value is within a given range, using a comparer. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// The comparer used to evaluate the value's range + /// Thrown when the value is not in the given range + public static void ShouldBeInRange(this T actual, + T low, + T high, + IComparer comparer) + { + Assert.InRange(actual, low, high, comparer); + } + + /// Verifies that an object is less than the exclusive maximum value. + /// The type of the objects to be compared. + /// The object to be evaluated (left side of the comparison). + /// The (exclusive) maximum value of the . (The right side of the comparison.) + public static void ShouldBeLessThan(this T @object, T value) + { + Assert.LessThan(@object, value); + } + + /// Verifies that an object is less than the exclusive maximum value. + /// The type of the objects to be compared. + /// The object to be evaluated (left side of the comparison). + /// The (exclusive) maximum value of the . (The right side of the comparison.) + /// An used to compare the objects. + public static void ShouldBeLessThan(this T @object, T value, IComparer comparer) + { + Assert.LessThan(@object, value, comparer); + } + + /// Verifies that an object is less than the inclusive maximum value. + /// The type of the objects to be compared. + /// The object to be evaluated (left side of the comparison). + /// The (inclusive) maximum value of the . (The right side of the comparison.) + public static void ShouldBeLessThanOrEqualTo(this T @object, T value) + { + Assert.LessThanOrEqual(@object, value); + } + + /// Verifies that an object is less than the inclusive maximum value. + /// The type of the objects to be compared. + /// The object to be evaluated (left side of the comparison). + /// The (inclusive) maximum value of the . (The right side of the comparison.) + /// An used to compare the objects. + public static void ShouldBeLessThanOrEqualTo(this T @object, T value, IComparer comparer) + { + Assert.LessThanOrEqual(@object, value, comparer); + } + + /// + /// Verifies that an object reference is null. + /// + /// The object to be inspected + /// Thrown when the object reference is not null + public static void ShouldBeNull(this object @object) + { + Assert.Null(@object); + } + + /// + /// Verifies that two objects are the same instance. + /// + /// The actual object instance + /// The expected object instance + /// Thrown when the objects are not the same instance + public static void ShouldBeSameAs(this object actual, + object expected) + { + Assert.Same(expected, actual); + } + + /// + /// Verifies that an object is exactly the given type (and not a derived type). + /// + /// The type the object should be + /// The object to be evaluated + /// The object, casted to type T when successful + /// Thrown when the object is not the given type + public static T ShouldBeType(this object @object) + { + return Assert.IsType(@object); + } + + /// + /// Verifies that an object is exactly the given type (and not a derived type). + /// + /// The object to be evaluated + /// The type the object should be + /// Thrown when the object is not the given type + public static void ShouldBeType(this object @object, + Type expectedType) + { + Assert.IsType(expectedType, @object); + } + + /// + /// Verifies that an object is of the given type or a derived type + /// + /// The type the object should implement + /// The object to be evaluated + /// The object, casted to type T when successful + /// Thrown when the object is not the given type + public static T ShouldImplement(this object @object) + { + return Assert.IsAssignableFrom(@object); + } + + /// + /// Verifies that an object is of the given type or a derived type + /// + /// The object to be evaluated + /// The type the object should implement + /// Thrown when the object is not the given type + public static void ShouldImplement(this object @object, + Type expectedType) + { + Assert.IsAssignableFrom(expectedType, @object); + } + + /// + /// Verifies that an object is of the given type or a derived type + /// + /// The type the object should implement + /// The object to be evaluated + /// The user message to show on failure + /// The object, casted to type T when successful + /// Thrown when the object is not the given type + public static T ShouldImplement(this object @object, string userMessage) + { + return Assert.IsAssignableFrom(@object, userMessage); + } + + /// + /// Verifies that an object is of the given type or a derived type + /// + /// The object to be evaluated + /// The type the object should implement + /// The user message to show on failure + /// Thrown when the object is not the given type + public static void ShouldImplement(this object @object, + Type expectedType, + string userMessage) + { + Assert.IsAssignableFrom(expectedType, @object, userMessage); + } + + /// + /// Verifies that two objects are equal, using a default comparer. + /// + /// The type of the objects to be compared + /// The value to be compared against + /// The expected value + /// Thrown when the objects are not equal + public static void ShouldEqual(this T actual, + T expected) + { + Assert.Equal(expected, actual); + } + + /// + /// Verifies that two objects are equal, using a default comparer, with a custom error message + /// + /// The type of the objects to be compared + /// The value to be compared against + /// The expected value + /// The user message to show on failure + /// Thrown when the objects are not equal + public static void ShouldEqual(this T actual, + T expected, + string userMessage) + { + Assert.Equal(expected, actual, userMessage); + } + + /// + /// Verifies that two objects are equal, using a custom comparer. + /// + /// The type of the objects to be compared + /// The value to be compared against + /// The expected value + /// The comparer used to compare the two objects + /// Thrown when the objects are not equal + public static void ShouldEqual(this T actual, + T expected, + IEqualityComparer comparer) + { + Assert.Equal(expected, actual, comparer); + } + + /// + /// Verifies that a value is not within a given range, using the default comparer. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// Thrown when the value is in the given range + public static void ShouldNotBeInRange(this T actual, + T low, + T high) + { + Assert.NotInRange(actual, low, high); + } + + /// + /// Verifies that a value is not within a given range, using a comparer. + /// + /// The type of the value to be compared + /// The actual value to be evaluated + /// The (inclusive) low value of the range + /// The (inclusive) high value of the range + /// The comparer used to evaluate the value's range + /// Thrown when the value is in the given range + public static void ShouldNotBeInRange(this T actual, + T low, + T high, + IComparer comparer) + { + Assert.NotInRange(actual, low, high, comparer); + } + + /// + /// Verifies that an object reference is not null. + /// + /// The object to be validated + /// Thrown when the object is not null + public static T ShouldNotBeNull(this T @object) where T : class + { + Assert.NotNull(@object); + return @object; + } + + /// + /// Verifies that an object reference is not null. + /// + /// The object to be validated + /// The message to show on failure + /// Thrown when the object reference is null + public static T ShouldNotBeNull(this T @object, string message) where T : class + { + Assert.NotNull(@object, message); + return @object; + } + + + /// + /// Verifies that two objects are not the same instance. + /// + /// The actual object instance + /// The expected object instance + /// Thrown when the objects are the same instance + public static void ShouldNotBeSameAs(this object actual, + object expected) + { + Assert.NotSame(expected, actual); + } + + /// + /// Verifies that an object is not exactly the given type. + /// + /// The type the object should not be + /// The object to be evaluated + /// Thrown when the object is the given type + public static void ShouldNotBeType(this object @object) + { + Assert.IsNotType(@object); + } + + /// + /// Verifies that an object is not exactly the given type. + /// + /// The object to be evaluated + /// The type the object should not be + /// Thrown when the object is the given type + public static void ShouldNotBeType(this object @object, + Type expectedType) + { + Assert.IsNotType(expectedType, @object); + } + + /// + /// Verifies that two objects are not equal, using a default comparer. + /// + /// The type of the objects to be compared + /// The actual object + /// The expected object + /// Thrown when the objects are equal + public static void ShouldNotEqual(this T actual, + T expected) + { + Assert.NotEqual(expected, actual); + } + + /// + /// Verifies that two objects are not equal, using a custom comparer. + /// + /// The type of the objects to be compared + /// The actual object + /// The expected object + /// The comparer used to examine the objects + /// Thrown when the objects are equal + public static void ShouldNotEqual(this T actual, + T expected, + IEqualityComparer comparer) + { + Assert.NotEqual(expected, actual, comparer); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/Should/should/StringAssertionExtensions.cs b/src/UnitTests/Should/should/StringAssertionExtensions.cs new file mode 100644 index 0000000000..9432135f21 --- /dev/null +++ b/src/UnitTests/Should/should/StringAssertionExtensions.cs @@ -0,0 +1,70 @@ +using System; +using Should.Core.Assertions; +using Should.Core.Exceptions; + +namespace Should +{ + /// + /// Extensions which provide assertions to classes derived from . + /// + public static class StringAssertionExtensions + { + /// + /// Verifies that a string contains a given sub-string, using the current culture. + /// + /// The string to be inspected + /// The sub-string expected to be in the string + /// Thrown when the sub-string is not present inside the string + public static int ShouldContain(this string actualString, + string expectedSubString) + { + return Assert.Contains(expectedSubString, actualString); + } + + /// + /// Verifies that a string contains a given sub-string, using the given comparison type. + /// + /// The string to be inspected + /// The sub-string expected to be in the string + /// The type of string comparison to perform + /// Thrown when the sub-string is not present inside the string + public static void ShouldContain(this string actualString, + string expectedSubString, + StringComparison comparisonType) + { + Assert.Contains(expectedSubString, actualString, comparisonType); + } + + /// + /// Verifies that a string does not contain a given sub-string, using the current culture. + /// + /// The string to be inspected + /// The sub-string which is expected not to be in the string + /// Thrown when the sub-string is present inside the string + public static void ShouldNotContain(this string actualString, + string expectedSubString) + { + Assert.DoesNotContain(expectedSubString, actualString); + } + + /// + /// Verifies that a string does not contain a given sub-string, using the current culture. + /// + /// The string to be inspected + /// The sub-string which is expected not to be in the string + /// The type of string comparison to perform + /// Thrown when the sub-string is present inside the given string + public static void ShouldNotContain(this string actualString, + string expectedSubString, + StringComparison comparisonType) + { + Assert.DoesNotContain(expectedSubString, actualString, comparisonType); + } + + public static void ShouldStartWith(this string actualString, + string expectedStartString) + { + Assert.StartsWith(expectedStartString, actualString); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index bd2f97f935..fce6318124 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -84,9 +84,6 @@ False ..\..\lib\Rhino.Mocks.dll - - ..\..\lib\Should.dll - 3.5 @@ -190,6 +187,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -214,6 +256,7 @@ +