-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathEnumerableExtensionTests.cs
225 lines (198 loc) · 7.62 KB
/
EnumerableExtensionTests.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
using System;
using System.Collections.Generic;
using System.Linq;
using NFluent;
using ReClassNET.Extensions;
using Xunit;
namespace ReClass.NET_Tests.Extensions
{
public class EnumerableExtensionTest
{
public static TheoryData<IEnumerable<int>, bool> GetTestNoneData => new TheoryData<IEnumerable<int>, bool>
{
{ new int[0], true },
{ new int[1], false },
{ Enumerable.Empty<int>(), true }
};
[Theory]
[MemberData(nameof(GetTestNoneData))]
public void TestNone(IEnumerable<int> sut, bool expected)
{
Check.That(sut.None()).IsEqualTo(expected);
}
public static TheoryData<IEnumerable<int>, Func<int, bool>, bool> GetTestNoneWithSelectorData => new TheoryData<IEnumerable<int>, Func<int, bool>, bool>
{
{ new int[0], i => false, true },
{ new int[0], i => true, true },
{ new [] { 1 }, i => i == 1, false },
{ new [] { 1 }, i => i != 1, true },
{ new [] { 1, 3, 5 }, i => i % 2 == 0, true }
};
[Theory]
[MemberData(nameof(GetTestNoneWithSelectorData))]
public void TestNoneWithSelector(IEnumerable<int> sut, Func<int, bool> selector, bool expected)
{
Check.That(sut.None(selector)).IsEqualTo(expected);
}
public static TheoryData<IEnumerable<int>, Func<int, bool>> GetTestWhereNotData => new TheoryData<IEnumerable<int>, Func<int, bool>>
{
{ new int[0], i => false },
{ new int[0], i => true },
{ new [] { 1 }, i => i == 1 },
{ new [] { 1, 3, 5 }, i => i % 2 == 1 }
};
[Theory]
[MemberData(nameof(GetTestWhereNotData))]
public void TestWhereNot(IEnumerable<int> sut, Func<int, bool> selector)
{
Check.That(sut.WhereNot(selector)).IsEmpty();
}
public static TheoryData<IEnumerable<int>, int, int> GetTestFindIndexData => new TheoryData<IEnumerable<int>, int, int>
{
{ new int[0], 1, -1 },
{ new [] { 1 }, 1, 0 },
{ new [] { 1 }, 2, -1 },
{ new [] { 1, 3, 5 }, 1, 0 },
{ new [] { 1, 3, 5 }, 2, -1 },
{ new [] { 1, 3, 5 }, 3, 1 },
{ new [] { 1, 3, 5 }, 4, -1 },
{ new [] { 1, 3, 5 }, 5, 2 }
};
[Theory]
[MemberData(nameof(GetTestFindIndexData))]
public void TestFindIndex(IEnumerable<int> sut, int item, int expected)
{
Check.That(sut.FindIndex(i => i == item)).IsEqualTo(expected);
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(10)]
public void TestForEach(int expected)
{
var counter = 0;
Enumerable.Repeat(0, expected).ForEach(_ => ++counter);
Check.That(counter).IsEqualTo(expected);
}
public static TheoryData<IEnumerable<int>, Func<int, int>, IEnumerable<int>> GetTestDistinctByData => new TheoryData<IEnumerable<int>, Func<int, int>, IEnumerable<int>>
{
{ Enumerable.Empty<int>(), i => i, Enumerable.Empty<int>() },
{ new [] { 1 }, i => i, new [] { 1 } },
{ new [] { 1, 1, 1, 1 }, i => i, new [] { 1 } },
{ new [] { 1, 2, 3, 4 }, i => i, new [] { 1, 2, 3, 4 } },
{ new [] { 1, 1, 2, 2, 3, 3 }, i => i, new [] { 1, 2, 3 } },
{ new [] { 1, 1, 2, 2, 3, 4 }, i => i, new [] { 1, 2, 3, 4 } },
{ new [] { 1, 1, 2, 2, 3, 4 }, i => 0, new [] { 1 } }
};
[Theory]
[MemberData(nameof(GetTestDistinctByData))]
public void TestDistinctBy(IEnumerable<int> sut, Func<int, int> selector, IEnumerable<int> expected)
{
Check.That(sut.DistinctBy(selector)).IsEquivalentTo(expected);
}
public static TheoryData<IEnumerable<int>, IEnumerable<int>, bool> GetTestIsEquivalentToData => new TheoryData<IEnumerable<int>, IEnumerable<int>, bool>
{
{ Enumerable.Empty<int>(), Enumerable.Empty<int>(), true },
{ Enumerable.Empty<int>(), new int[0], true },
{ new [] { 1 }, new int[0], false },
{ new [] { 1 }, new [] { 2 }, false },
{ new [] { 1, 2, 3 }, new [] { 1, 2, 3 }, true },
{ new [] { 1, 2, 3 }, new [] { 3, 1, 2 }, true }
};
[Theory]
[MemberData(nameof(GetTestIsEquivalentToData))]
public void TestIsEquivalentTo(IEnumerable<int> sut, IEnumerable<int> other, bool expected)
{
Check.That(sut.IsEquivalentTo(other)).IsEqualTo(expected);
}
public static TheoryData<IEnumerable<int>, Func<int, bool>, IEnumerable<int>> GetTestTakeWhileInclusiveData => new TheoryData<IEnumerable<int>, Func<int, bool>, IEnumerable<int>>
{
{ Enumerable.Empty<int>(), i => false, Enumerable.Empty<int>() },
{ new [] { 1 }, i => false, new [] { 1 } },
{ new [] { 1 }, i => true, new [] { 1 } },
{ new [] { 1, 1 }, i => false, new [] { 1 } },
{ new [] { 1, 1 }, i => true, new [] { 1, 1 } },
{ new [] { 1, 2, 3, 4 }, i => i != 2, new [] { 1, 2 } },
{ new [] { 1, 2, 3, 4 }, i => i != 3, new [] { 1, 2, 3 } },
{ new [] { 4, 3, 2, 1 }, i => false, new [] { 4 } }
};
[Theory]
[MemberData(nameof(GetTestTakeWhileInclusiveData))]
public void TestTakeWhileInclusive(IEnumerable<int> sut, Func<int, bool> predicate, IEnumerable<int> expected)
{
Check.That(sut.TakeWhileInclusive(predicate)).IsEquivalentTo(expected);
}
public static TheoryData<IEnumerable<int>, Func<int, int, bool>, IEnumerable<IEnumerable<int>>> GetTestGroupWhileData => new TheoryData<IEnumerable<int>, Func<int, int, bool>, IEnumerable<IEnumerable<int>>>
{
{ Enumerable.Empty<int>(), (x, y) => false, Enumerable.Empty<IEnumerable<int>>() },
{ new [] { 1, 2, 3 }, (x, y) => x == y, new [] { new[] { 1 }, new[] { 2 }, new[] { 3 } } },
{ new [] { 1, 1, 2, 3, 3, 4 }, (x, y) => x == y, new [] { new[] { 1, 1 }, new[] { 2 }, new[] { 3, 3 }, new[] { 4 } } },
{ new [] { 1, 1, 2, 3, 3, 4 }, (x, y) => x != y, new [] { new[] { 1 }, new[] { 1, 2, 3 }, new[] { 3, 4 } } }
};
[Theory]
[MemberData(nameof(GetTestGroupWhileData))]
public void TestGroupWhile(IEnumerable<int> sut, Func<int, int, bool> predicate, IEnumerable<IEnumerable<int>> expected)
{
using (var expectedIt = expected.GetEnumerator())
{
using (var groupIt = sut.GroupWhile(predicate).GetEnumerator())
{
while (groupIt.MoveNext())
{
Check.That(expectedIt.MoveNext()).IsTrue();
Check.That(groupIt.Current).IsEquivalentTo(expectedIt.Current);
}
}
Check.That(expectedIt.MoveNext()).IsFalse();
}
}
public static TheoryData<IEnumerable<int>, Func<int, bool>, int> GetTestPredicateOrFirstData => new TheoryData<IEnumerable<int>, Func<int, bool>, int>
{
{ new [] { 1 }, i => false, 1 },
{ new [] { 1 }, i => true, 1 },
{ new [] { 1, 2, 3, 4 }, i => i == 2, 2 },
{ new [] { 1, 2, 3, 4 }, i => i == 4, 4 },
{ new [] { 1, 2, 3, 4 }, i => i == 5, 1 }
};
[Theory]
[MemberData(nameof(GetTestPredicateOrFirstData))]
public void TestPredicateOrFirst(IEnumerable<int> sut, Func<int, bool> predicate, int expected)
{
Check.That(sut.PredicateOrFirst(predicate)).IsEqualTo(expected);
}
[Fact]
public void TestPredicateOrFirstThrows()
{
Check.ThatCode(() => Enumerable.Empty<int>().PredicateOrFirst(i => true)).Throws<InvalidOperationException>();
}
public class Traversable
{
public IList<Traversable> Children { get; } = new List<Traversable>();
}
[Fact]
public void TestTraverse()
{
var traversable = new Traversable();
var child1 = new Traversable();
child1.Children.Add(new Traversable());
var child2 = new Traversable();
child2.Children.Add(new Traversable());
child2.Children.Add(new Traversable());
var child3 = new Traversable();
child3.Children.Add(new Traversable());
child3.Children.Add(new Traversable());
child3.Children.Add(new Traversable());
traversable.Children.Add(child1);
traversable.Children.Add(child2);
traversable.Children.Add(child3);
var expected = new[] { traversable }
.Append(child1)
.Append(child2)
.Append(child3)
.Concat(child1.Children)
.Concat(child2.Children)
.Concat(child3.Children);
Check.That(new[] { traversable }.Traverse(t => t.Children)).ContainsExactly(expected);
}
}
}