/
ShouldBeEnumerableTestExtensions.cs
214 lines (176 loc) · 8.84 KB
/
ShouldBeEnumerableTestExtensions.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using JetBrains.Annotations;
namespace Shouldly
{
[DebuggerStepThrough]
[ShouldlyMethods]
public static class ShouldBeEnumerableTestExtensions
{
public static void ShouldContain<T>(this IEnumerable<T> actual, T expected)
{
ShouldContain(actual, expected, () => null);
}
public static void ShouldContain<T>(this IEnumerable<T> actual, T expected, string customMessage)
{
ShouldContain(actual, expected, () => customMessage);
}
public static void ShouldContain<T>(this IEnumerable<T> actual, T expected, Func<string> customMessage)
{
if (!actual.Contains(expected))
throw new ShouldAssertException(new ExpectedActualShouldlyMessage(expected, actual, customMessage).ToString());
}
public static void ShouldNotContain<T>(this IEnumerable<T> actual, T expected)
{
ShouldNotContain(actual, expected, () => null);
}
public static void ShouldNotContain<T>(this IEnumerable<T> actual, T expected, string customMessage)
{
ShouldNotContain(actual, expected, () => customMessage);
}
public static void ShouldNotContain<T>(this IEnumerable<T> actual, T expected, Func<string> customMessage)
{
if (actual.Contains(expected))
throw new ShouldAssertException(new ExpectedActualShouldlyMessage(expected, actual, customMessage).ToString());
}
public static void ShouldContain<T>(this IEnumerable<T> actual, [InstantHandle] Expression<Func<T, bool>> elementPredicate)
{
ShouldContain(actual, elementPredicate, () => null);
}
public static void ShouldContain<T>(this IEnumerable<T> actual, [InstantHandle] Expression<Func<T, bool>> elementPredicate, string customMessage)
{
ShouldContain(actual, elementPredicate, () => customMessage);
}
public static void ShouldContain<T>(this IEnumerable<T> actual, [InstantHandle] Expression<Func<T, bool>> elementPredicate, Func<string> customMessage)
{
var condition = elementPredicate.Compile();
if (!actual.Any(condition))
throw new ShouldAssertException(new ExpectedShouldlyMessage(elementPredicate.Body, customMessage).ToString());
}
public static void ShouldNotContain<T>(this IEnumerable<T> actual, [InstantHandle] Expression<Func<T, bool>> elementPredicate)
{
ShouldNotContain(actual, elementPredicate, () => null);
}
public static void ShouldNotContain<T>(this IEnumerable<T> actual, [InstantHandle] Expression<Func<T, bool>> elementPredicate, string customMessage)
{
ShouldNotContain(actual, elementPredicate, () => customMessage);
}
public static void ShouldNotContain<T>(this IEnumerable<T> actual, [InstantHandle] Expression<Func<T, bool>> elementPredicate, Func<string> customMessage)
{
var condition = elementPredicate.Compile();
if (actual.Any(condition))
throw new ShouldAssertException(new ExpectedShouldlyMessage(elementPredicate.Body, customMessage).ToString());
}
public static void ShouldAllBe<T>(this IEnumerable<T> actual, [InstantHandle] Expression<Func<T, bool>> elementPredicate)
{
ShouldAllBe(actual, elementPredicate, () => null);
}
public static void ShouldAllBe<T>(this IEnumerable<T> actual, [InstantHandle] Expression<Func<T, bool>> elementPredicate, string customMessage)
{
ShouldAllBe(actual, elementPredicate, () => customMessage);
}
public static void ShouldAllBe<T>(this IEnumerable<T> actual, [InstantHandle] Expression<Func<T, bool>> elementPredicate, Func<string> customMessage)
{
var condition = elementPredicate.Compile();
var actualResults = actual.Where(part => !condition(part));
if (actualResults.Any())
throw new ShouldAssertException(new ExpectedActualShouldlyMessage(elementPredicate.Body, actualResults, customMessage).ToString());
}
public static void ShouldBeEmpty<T>(this IEnumerable<T> actual)
{
ShouldBeEmpty(actual, () => null);
}
public static void ShouldBeEmpty<T>(this IEnumerable<T> actual, string customMessage)
{
ShouldBeEmpty(actual, () => customMessage);
}
public static void ShouldBeEmpty<T>(this IEnumerable<T> actual, Func<string> customMessage)
{
if (actual == null || (actual != null && actual.Count() != 0))
throw new ShouldAssertException(new ExpectedShouldlyMessage(actual, customMessage).ToString());
}
public static void ShouldNotBeEmpty<T>(this IEnumerable<T> actual)
{
ShouldNotBeEmpty(actual, () => null);
}
public static void ShouldNotBeEmpty<T>(this IEnumerable<T> actual, string customMessage)
{
ShouldNotBeEmpty(actual, () => customMessage);
}
public static void ShouldNotBeEmpty<T>(this IEnumerable<T> actual, Func<string> customMessage)
{
if (actual == null || actual != null && !actual.Any())
throw new ShouldAssertException(new ExpectedShouldlyMessage(actual, customMessage).ToString());
}
public static void ShouldContain(this IEnumerable<float> actual, float expected, double tolerance)
{
ShouldContain(actual, expected, tolerance, () => null);
}
public static void ShouldContain(this IEnumerable<float> actual, float expected, double tolerance, string customMessage)
{
ShouldContain(actual, expected, tolerance, () => customMessage);
}
public static void ShouldContain(this IEnumerable<float> actual, float expected, double tolerance, Func<string> customMessage)
{
if (!actual.Any(a => Math.Abs(expected - a) < tolerance))
throw new ShouldAssertException(new ExpectedActualToleranceShouldlyMessage(expected, actual, tolerance, customMessage).ToString());
}
public static void ShouldContain(this IEnumerable<double> actual, double expected, double tolerance)
{
ShouldContain(actual, expected, tolerance, () => null);
}
public static void ShouldContain(this IEnumerable<double> actual, double expected, double tolerance, string customMessage)
{
ShouldContain(actual, expected, tolerance, () => customMessage);
}
public static void ShouldContain(this IEnumerable<double> actual, double expected, double tolerance, Func<string> customMessage)
{
if (!actual.Any(a => Math.Abs(expected - a) < tolerance))
throw new ShouldAssertException(new ExpectedActualToleranceShouldlyMessage(expected, actual, tolerance, customMessage).ToString());
}
public static void ShouldBeSubsetOf<T>(this IEnumerable<T> actual, IEnumerable<T> expected)
{
ShouldBeSubsetOf(actual, expected, () => null);
}
public static void ShouldBeSubsetOf<T>(this IEnumerable<T> actual, IEnumerable<T> expected, string customMessage)
{
ShouldBeSubsetOf(actual, expected, () => customMessage);
}
public static void ShouldBeSubsetOf<T>(this IEnumerable<T> actual, IEnumerable<T> expected, Func<string> customMessage)
{
if (actual.Equals(expected))
return;
var missing = actual.Except(expected);
if (missing.Any())
throw new ShouldAssertException(new ExpectedActualShouldlyMessage(expected, missing, customMessage).ToString());
}
public static void ShouldBeUnique<T>(this IEnumerable<T> actual)
{
ShouldBeUnique(actual, () => null);
}
public static void ShouldBeUnique<T>(this IEnumerable<T> actual, string customMessage)
{
ShouldBeUnique(actual, () => customMessage);
}
public static void ShouldBeUnique<T>(this IEnumerable<T> actual, Func<string> customMessage)
{
var duplicates = GetDuplicates(actual);
if (duplicates.Any())
throw new ShouldAssertException(new ExpectedActualShouldlyMessage(new List<T>(), duplicates, customMessage).ToString());
}
static List<object> GetDuplicates<T>(IEnumerable<T> items)
{
var list = new List<object>();
var duplicates = new List<object>();
foreach (object o1 in items)
{
duplicates.AddRange(list.Where(o2 => o1 != null && o1.Equals(o2)));
list.Add(o1);
}
return duplicates;
}
}
}