Created
March 11, 2010 15:31
-
-
Save mendicantx/329241 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class AssertionExtensions | |
{ | |
public static void should_be_equal_to<T>(this T actual, T expected) | |
{ | |
Assert.That(actual, Is.EqualTo(expected)); | |
} | |
public static void should_be_equal_to(this double actual, double expected, int precision) | |
{ | |
Assert.That(Math.Round(actual, precision), Is.EqualTo(expected)); | |
} | |
public static void should_be_null<T>(this T item) | |
{ | |
Assert.That(item, Is.Null); | |
} | |
public static void should_be_true(this bool actual) | |
{ | |
Assert.IsTrue(actual); | |
} | |
public static void should_be_false(this bool actual) | |
{ | |
Assert.IsFalse(actual); | |
} | |
public static void should_begin_with(this string actual, string expected) | |
{ | |
StringAssert.StartsWith(expected, actual); | |
} | |
public static void should_contain(this string actual, string expected) | |
{ | |
StringAssert.Contains(expected, actual); | |
} | |
public static void should_not_contain(this string actual, string expected) | |
{ | |
StringAssert.DoesNotContain(expected, actual); | |
} | |
public static void should_end_with(this string actual, string expected) | |
{ | |
StringAssert.EndsWith(expected, actual); | |
} | |
public static void should_not_end_with(this string actual, string expected) | |
{ | |
StringAssert.DoesNotEndWith(expected, actual); | |
} | |
public static void should_contain<T>(this IEnumerable<T> items, Action<T> configuration) where T: new() | |
{ | |
var itemToFind = new T(); | |
configuration(itemToFind); | |
items.should_contain(itemToFind); | |
} | |
public static void should_contain<T>(this IEnumerable<T> items, T item_to_find) | |
{ | |
if (items.Count(x => x.Equals(item_to_find) || ReferenceEquals(x, item_to_find)) <= 0) | |
Assert.Fail("could not find {0}", item_to_find); | |
} | |
public static void should_contain<T>(this IEnumerable<T> items, Expression<Func<T, bool>> item_to_find) | |
{ | |
var pretty_print = item_to_find.ToString(); | |
if (items.Count(item_to_find.Compile()) <= 0) | |
Assert.Fail("could not find {0}", pretty_print); | |
} | |
public static void should_not_contain<T>(this IEnumerable<T> items, T item_to_find) | |
{ | |
if (items.Count(x => x.Equals(item_to_find)) > 0) | |
Assert.Fail("found {0}", item_to_find); | |
} | |
public static void should_not_contain<T>(this IEnumerable<T> items, Func<T, bool> item_to_find) | |
{ | |
var pretty_print = item_to_find.ToString(); | |
if (items.Count(item_to_find) != 0) | |
Assert.Fail("should not find {0}", pretty_print); | |
} | |
public static void should_be_empty<T>(this IEnumerable<T> items) | |
{ | |
Assert.That(items.Count(), Is.EqualTo(0)); | |
} | |
public static void should_not_be_null<T>(this T item) | |
{ | |
Assert.IsNotNull(item); | |
} | |
public static void should_be_greater_than<T>(this T item, T right) where T : IComparable | |
{ | |
Assert.Greater(item, right); | |
} | |
public static void should_be_less_than<T>(this T item, T right) where T : IComparable | |
{ | |
Assert.Less(item, right); | |
} | |
public static void should_be_within_a_percentage_of<T>(this T item, T value, double expectedPercentage) where T : IComparable | |
{ | |
double actualPercentage = (Convert.ToDouble(value) - Convert.ToDouble(item))/Convert.ToDouble(value); | |
Assert.Less(actualPercentage, expectedPercentage); | |
} | |
public static void should_be_an_instance_of<T>(this object item) | |
{ | |
Assert.That(item, Is.InstanceOf(typeof (T))); | |
} | |
public static void should_not_be_an_instance_of<T>(this object item) | |
{ | |
Assert.That(item, Is.Not.InstanceOf(typeof (T))); | |
} | |
public static void should_be_same_instance_as<T>(this T item, T otherItem) | |
{ | |
Assert.That(item, Is.SameAs(otherItem)); | |
} | |
public static void should_have_thrown<ExpectedException>(this Action action) where ExpectedException : Exception | |
{ | |
try | |
{ | |
action(); | |
Assert.Fail("No Exception was raised."); | |
} | |
catch (Exception e) | |
{ | |
if(!(e is ExpectedException)) | |
{ | |
throw; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment