cbilson / machine.specifications forked from machine/machine.specifications

This URL has Read+Write access

cbilson (author)
Sun Jul 05 10:02:55 -0700 2009
commit  e922df9b5367072167694a6a887f6ece1aea518c
tree    196a8f8a5f3cc61d2d6b4af2f07021be3e01c707
parent  c54f6b760bbf4a2429311469098b1e7972d78fce
e922df9b » cbilson 2009-07-05 added Gallio/MbUnit example 1 using System.Collections;
2 using System.Linq;
3 using MbUnit.Framework;
4 using System.Collections.Generic;
5 using System;
6
7 namespace Machine.Specifications.Gallio
8 {
9 public static class GallioCollectionExtensionMethods
10 {
11 public static ArrayList ToArrayList(this IEnumerable enumerable)
12 {
13 ArrayList arrayList = new ArrayList();
14 foreach (object obj in enumerable)
15 {
16 arrayList.Add(obj);
17 }
18 return arrayList;
19 }
20 }
21 public static class GallioShouldExtensionMethods
22 {
23 public static void ShouldBeFalse(this bool condition)
24 {
25 Assert.IsFalse(condition);
26 }
27
28 public static void ShouldBeTrue(this bool condition)
29 {
30 Assert.IsTrue(condition);
31 }
32
33 public static object ShouldEqual(this object actual, object expected)
34 {
35 Assert.AreEqual(expected, actual);
36 return expected;
37 }
38
39 public static T ShouldEqual<T>(this T actual, T expected) where T : IEquatable<T>
40 {
41 Assert.IsTrue(actual.Equals(expected));
42 return expected;
43 }
44
45 public static object ShouldNotEqual(this object actual, object expected)
46 {
47 Assert.AreNotEqual(expected, actual);
48 return expected;
49 }
50
51 public static void ShouldBeNull(this object anObject)
52 {
53 Assert.IsNull(anObject);
54 }
55
56 public static void ShouldNotBeNull(this object anObject)
57 {
58 Assert.IsNotNull(anObject);
59 }
60
61 public static object ShouldBeTheSameAs(this object actual, object expected)
62 {
63 Assert.AreSame(expected, actual);
64 return expected;
65 }
66
67 public static object ShouldNotBeTheSameAs(this object actual, object expected)
68 {
69 Assert.AreNotSame(expected, actual);
70 return expected;
71 }
72
73 public static void ShouldBeOfType(this object actual, Type expected)
74 {
75 Assert.IsInstanceOfType(expected, actual);
76 }
77
78 public static void ShouldBeOfType<T>(this object actual)
79 {
80 Assert.IsInstanceOfType(typeof(T), actual);
81 }
82
83 public static void ShouldBe(this object actual, Type expected)
84 {
85 Assert.IsInstanceOfType(expected, actual);
86 }
87
88 public static void ShouldNotBeOfType(this object actual, Type expected)
89 {
90 Assert.IsNotInstanceOfType(expected, actual);
91 }
92
93 public static void ShouldContain<T>(this IEnumerable<T> actual, params T[] expected)
94 {
95 actual.ShouldContain(expected);
96 }
97
98 public static void ShouldContain<T>(this IEnumerable<T> actual, IEnumerable<T> expected)
99 {
100 var actualList = new List<T>(actual);
101 foreach (var item in expected)
102 Assert.Contains(actualList, item);
103 }
104
105 public static void ShouldNotContain<T>(this IEnumerable<T> collection, T expected)
106 {
107 Assert.DoesNotContain(collection, expected);
108 }
109
110 public static IComparable ShouldBeGreaterThan(this IComparable arg1, IComparable arg2)
111 {
112 Assert.GreaterThan(arg1, arg2);
113 return arg2;
114 }
115
116 public static IComparable ShouldBeLessThan(this IComparable arg1, IComparable arg2)
117 {
118 Assert.LessThan(arg1, arg2);
119 return arg2;
120 }
121
122 public static void ShouldBeEmpty(this IEnumerable collection)
123 {
124 Assert.IsEmpty(collection.ToArrayList());
125 }
126
127 public static void ShouldBeEmpty(this string aString)
128 {
129 Assert.IsEmpty(aString);
130 }
131
132 public static void ShouldNotBeEmpty(this IEnumerable collection)
133 {
134 Assert.IsNotEmpty(collection.ToArrayList());
135 }
136
137 public static void ShouldNotBeEmpty(this string aString)
138 {
139 Assert.IsNotEmpty(aString);
140 }
141
142 public static void ShouldContain(this string actual, string expected)
143 {
144 Assert.Contains(actual, expected);
145 }
146
147 public static string ShouldBeEqualIgnoringCase(this string actual, string expected)
148 {
149 Assert.IsTrue(expected.Equals(actual, StringComparison.InvariantCultureIgnoreCase));
150 return expected;
151 }
152
153 public static void ShouldStartWith(this string actual, string expected)
154 {
155 Assert.IsTrue(actual.StartsWith(expected));
156 }
157
158 public static void ShouldEndWith(this string actual, string expected)
159 {
160 Assert.IsTrue(actual.EndsWith(expected));
161 }
162
163 public static void ShouldBeSurroundedWith(this string actual, string expectedStartDelimiter,
164 string expectedEndDelimiter)
165 {
166 actual.ShouldStartWith(expectedStartDelimiter);
167 actual.ShouldEndWith(expectedEndDelimiter);
168 }
169
170 public static void ShouldBeSurroundedWith(this string actual, string expectedDelimiter)
171 {
172 actual.ShouldBeSurroundedWith(expectedDelimiter, expectedDelimiter);
173 }
174
175 public static void ShouldContainErrorMessage(this Exception exception, string expected)
176 {
177 exception.Message.ShouldContain(expected);
178 }
179
180 public static void ShouldContainOnly<T>(this IEnumerable<T> actual, params T[] expected)
181 {
182 actual.ShouldContainOnly(expected);
183 }
184
185 public static void ShouldContainOnly<T>(this IEnumerable<T> actual, IEnumerable<T> expected)
186 {
187 actual.ShouldContain(expected);
188 actual.Count().ShouldEqual(expected.Count());
189 }
190
191 public static Exception ShouldBeThrownBy(this Type exceptionType, Action method)
192 {
193 Exception exception = method.GetException();
194
195 Assert.IsNotNull(exception);
196 Assert.AreEqual(exceptionType, exception.GetType());
197 return exception;
198 }
199
200 static Exception GetException(this Action method)
201 {
202 Exception exception = null;
203
204 try
205 {
206 method();
207 }
208 catch (Exception e)
209 {
210 exception = e;
211 }
212
213 return exception;
214 }
215 }
216 }