forked from nspec/NSpec
/
AssertionExtensions.cs
253 lines (203 loc) · 8.67 KB
/
AssertionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NSpec.Domain;
using NUnit.Framework;
namespace NSpec
{
public static class AssertionExtensions
{
public static void should<T>(this T o, Expression<Predicate<T>> predicate)
{
Assert.IsTrue(predicate.Compile()(o), Example.Parse(predicate.Body));
}
public static T should_not_be_null<T>(this T target) where T : class
{
Assert.IsNotNull(target);
return target;
}
public static Nullable<T> should_not_be_null<T>(this Nullable<T> target) where T : struct
{
Assert.IsTrue(target.HasValue);
return target;
}
public static T should_be_null<T>(this T target) where T : class
{
Assert.IsNull(target);
return target;
}
public static Nullable<T> should_be_null<T>(this Nullable<T> target) where T : struct
{
Assert.IsFalse(target.HasValue);
return target;
}
public static void should_not_be_default<T>(this T t)
{
Assert.AreNotEqual(default(T), t);
}
public static void is_not_null_or_empty(this string source)
{
Assert.IsNotNullOrEmpty(source);
}
public static void is_true(this bool actual) { actual.should_be_true(); }
public static void should_be_true(this bool actual)
{
Assert.IsTrue(actual);
}
public static void is_false(this bool actual) { actual.should_be_false(); }
public static void should_be_false(this bool actual)
{
Assert.IsFalse(actual);
}
public static void should_be(this object actual, object expected)
{
actual.Is(expected);
}
public static void Is(this object actual, object expected)
{
Assert.AreEqual(expected, actual);
}
public static void should_not_be(this object actual, object expected)
{
Assert.AreNotEqual(expected, actual);
}
public static void should_be(this string actual, string expected)
{
Assert.AreEqual(expected, actual);
}
public static void should_end_with(this string actual, string end)
{
StringAssert.EndsWith(end, actual);
}
public static void should_start_with(this string actual, string start)
{
StringAssert.StartsWith(start, actual);
}
public static void should_contain(this string actual, string expected)
{
StringAssert.Contains(expected, actual);
}
public static IEnumerable<T> should_not_contain<T>(this IEnumerable<T> collection, Func<T, bool> predicate)
{
Assert.IsTrue(!collection.Any(predicate), "collection contains an item it should not.".With(collection, predicate));
return collection;
}
public static IEnumerable<T> should_contain<T>(this IEnumerable<T> collection, Func<T, bool> predicate)
{
Assert.IsTrue(collection.Any(predicate), "collection does not contain an item it should.".With(collection, predicate));
return collection;
}
public static IEnumerable<T> should_contain<T>(this IEnumerable<T> collection, T t)
{
CollectionAssert.Contains(collection, t);
return collection;
}
public static IEnumerable<T> should_not_contain<T>(this IEnumerable<T> collection, T t)
{
CollectionAssert.DoesNotContain(collection, t);
return collection;
}
public static IEnumerable<T> should_not_be_empty<T>(this IEnumerable<T> collection)
{
CollectionAssert.IsNotEmpty(collection);
return collection;
}
public static string should_not_be_empty(this string target)
{
Assert.IsNotNullOrEmpty(target);
return target;
}
public static string should_be_empty(this string target)
{
Assert.IsNullOrEmpty(target);
return target;
}
public static IEnumerable<T> should_be_empty<T>(this IEnumerable<T> collection)
{
CollectionAssert.IsEmpty(collection);
return collection;
}
public static IEnumerable<T> should_be<T>(this IEnumerable<T> actual, params T[] expected)
{
CollectionAssert.AreEqual(expected.ToArray(), actual.ToArray());
return actual;
}
public static IEnumerable<T> should_be<T>(this IEnumerable<T> actual, IEnumerable<T> expected)
{
CollectionAssert.AreEqual(expected.ToArray(), actual.ToArray());
return actual;
}
public static T should_cast_to<T>(this object value)
{
Assert.IsInstanceOf<T>(value);
return (T)value;
}
public static void should_not_match(this string value, string pattern)
{
StringAssert.DoesNotMatch(pattern, value);
}
public static void should_be_same(this object actual, object expected)
{
Assert.AreSame(expected, actual);
}
public static void should_not_be_same(this object actual, object expected)
{
Assert.AreNotSame(expected, actual);
}
public static void is_greater_than(this IComparable arg1, IComparable arg2){ arg1.should_be_greater_than(arg2);}
public static void should_be_greater_than(this IComparable arg1, IComparable arg2)
{
Assert.Greater(arg1, arg2);
}
public static void is_greater_or_equal_to(this IComparable arg1, IComparable arg2){ arg1.should_be_greater_or_equal_to(arg2);}
public static void should_be_greater_or_equal_to(this IComparable arg1, IComparable arg2)
{
Assert.GreaterOrEqual(arg1, arg2);
}
public static void is_less_than(this IComparable arg1, IComparable arg2){ arg1.should_be_less_than(arg2);}
public static void should_be_less_than(this IComparable arg1, IComparable arg2)
{
Assert.Less(arg1, arg2);
}
public static void is_less_or_equal_to(this IComparable arg1, IComparable arg2){ arg1.should_be_less_or_equal_to(arg2);}
public static void should_be_less_or_equal_to(this IComparable arg1, IComparable arg2)
{
Assert.LessOrEqual(arg1, arg2);
}
public static void is_close_to(this float actual, float expected){ actual.should_be_close_to(expected);}
public static void should_be_close_to(this float actual, float expected)
{
actual.should_be_close_to(expected, 0.0000001f);
}
public static void is_close_to(this float actual, float expected, float tolerance){ actual.should_be_close_to(expected, tolerance);}
public static void should_be_close_to(this float actual, float expected, float tolerance)
{
Assert.LessOrEqual(Math.Abs(actual - expected), tolerance,
string.Format("should be close to {0} of {1} but was {2} ", tolerance, expected, actual));
}
public static void is_close_to(this double actual, double expected, double tolerance) { actual.should_be_close_to(expected, tolerance); }
public static void should_be_close_to(this double actual, double expected, double tolerance)
{
Assert.LessOrEqual(Math.Abs(actual - expected), tolerance,
string.Format("should be close to {0} of {1} but was {2}", tolerance, expected, actual));
}
public static void is_close_to(this double actual, double expected){ actual.should_be_close_to(expected);}
public static void should_be_close_to(this double actual, double expected)
{
actual.should_be_close_to(expected, 0.0000001f);
}
public static void is_close_to(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance){ actual.should_be_close_to(expected, tolerance);}
public static void should_be_close_to(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance)
{
Assert.LessOrEqual(Math.Abs(actual.Ticks - expected.Ticks), tolerance.Ticks,
string.Format("should be close to {0} ticks of {1} but was {2}", tolerance, expected, actual));
}
public static void is_close_to(this DateTime actual, DateTime expected, DateTime tolerance) { actual.should_be_close_to(expected, tolerance);}
public static void should_be_close_to(this DateTime actual, DateTime expected, DateTime tolerance)
{
Assert.LessOrEqual(Math.Abs((actual - expected).Ticks), tolerance.Ticks,
string.Format("should be close to {0} ticks of {1} but was {2}", tolerance.Ticks, expected, actual));
}
}
}