using System.Collections; using System.Linq; using MbUnit.Framework; using System.Collections.Generic; using System; namespace Machine.Specifications.Gallio { public static class GallioCollectionExtensionMethods { public static ArrayList ToArrayList(this IEnumerable enumerable) { ArrayList arrayList = new ArrayList(); foreach (object obj in enumerable) { arrayList.Add(obj); } return arrayList; } } public static class GallioShouldExtensionMethods { public static void ShouldBeFalse(this bool condition) { Assert.IsFalse(condition); } public static void ShouldBeTrue(this bool condition) { Assert.IsTrue(condition); } public static object ShouldEqual(this object actual, object expected) { Assert.AreEqual(expected, actual); return expected; } public static T ShouldEqual(this T actual, T expected) where T : IEquatable { Assert.IsTrue(actual.Equals(expected)); return expected; } public static object ShouldNotEqual(this object actual, object expected) { Assert.AreNotEqual(expected, actual); return expected; } public static void ShouldBeNull(this object anObject) { Assert.IsNull(anObject); } public static void ShouldNotBeNull(this object anObject) { Assert.IsNotNull(anObject); } public static object ShouldBeTheSameAs(this object actual, object expected) { Assert.AreSame(expected, actual); return expected; } public static object ShouldNotBeTheSameAs(this object actual, object expected) { Assert.AreNotSame(expected, actual); return expected; } public static void ShouldBeOfType(this object actual, Type expected) { Assert.IsInstanceOfType(expected, actual); } public static void ShouldBeOfType(this object actual) { Assert.IsInstanceOfType(typeof(T), actual); } public static void ShouldBe(this object actual, Type expected) { Assert.IsInstanceOfType(expected, actual); } public static void ShouldNotBeOfType(this object actual, Type expected) { Assert.IsNotInstanceOfType(expected, actual); } public static void ShouldContain(this IEnumerable actual, params T[] expected) { actual.ShouldContain(expected); } public static void ShouldContain(this IEnumerable actual, IEnumerable expected) { var actualList = new List(actual); foreach (var item in expected) Assert.Contains(actualList, item); } public static void ShouldNotContain(this IEnumerable collection, T expected) { Assert.DoesNotContain(collection, expected); } public static IComparable ShouldBeGreaterThan(this IComparable arg1, IComparable arg2) { Assert.GreaterThan(arg1, arg2); return arg2; } public static IComparable ShouldBeLessThan(this IComparable arg1, IComparable arg2) { Assert.LessThan(arg1, arg2); return arg2; } public static void ShouldBeEmpty(this IEnumerable collection) { Assert.IsEmpty(collection.ToArrayList()); } public static void ShouldBeEmpty(this string aString) { Assert.IsEmpty(aString); } public static void ShouldNotBeEmpty(this IEnumerable collection) { Assert.IsNotEmpty(collection.ToArrayList()); } public static void ShouldNotBeEmpty(this string aString) { Assert.IsNotEmpty(aString); } public static void ShouldContain(this string actual, string expected) { Assert.Contains(actual, expected); } public static string ShouldBeEqualIgnoringCase(this string actual, string expected) { Assert.IsTrue(expected.Equals(actual, StringComparison.InvariantCultureIgnoreCase)); return expected; } public static void ShouldStartWith(this string actual, string expected) { Assert.IsTrue(actual.StartsWith(expected)); } public static void ShouldEndWith(this string actual, string expected) { Assert.IsTrue(actual.EndsWith(expected)); } public static void ShouldBeSurroundedWith(this string actual, string expectedStartDelimiter, string expectedEndDelimiter) { actual.ShouldStartWith(expectedStartDelimiter); actual.ShouldEndWith(expectedEndDelimiter); } public static void ShouldBeSurroundedWith(this string actual, string expectedDelimiter) { actual.ShouldBeSurroundedWith(expectedDelimiter, expectedDelimiter); } public static void ShouldContainErrorMessage(this Exception exception, string expected) { exception.Message.ShouldContain(expected); } public static void ShouldContainOnly(this IEnumerable actual, params T[] expected) { actual.ShouldContainOnly(expected); } public static void ShouldContainOnly(this IEnumerable actual, IEnumerable expected) { actual.ShouldContain(expected); actual.Count().ShouldEqual(expected.Count()); } public static Exception ShouldBeThrownBy(this Type exceptionType, Action method) { Exception exception = method.GetException(); Assert.IsNotNull(exception); Assert.AreEqual(exceptionType, exception.GetType()); return exception; } static Exception GetException(this Action method) { Exception exception = null; try { method(); } catch (Exception e) { exception = e; } return exception; } } }