16
16
using System . Collections . Generic ;
17
17
using System . Globalization ;
18
18
using System . Linq ;
19
+ using System . Numerics ;
19
20
using OnixLabs . Core . Linq ;
20
21
using OnixLabs . Core . UnitTests . Data ;
21
22
using Xunit ;
@@ -32,7 +33,7 @@ public void AllEqualByShouldProduceExpectedResultTrue()
32
33
Record < Guid > element1 = new ( "abc" , 123 , Guid . NewGuid ( ) ) ;
33
34
Record < Guid > element2 = new ( "abc" , 123 , Guid . NewGuid ( ) ) ;
34
35
Record < Guid > element3 = new ( "abc" , 123 , Guid . NewGuid ( ) ) ;
35
- IEnumerable < Record < Guid > > elements = new [ ] { element1 , element2 , element3 } ;
36
+ IEnumerable < Record < Guid > > elements = [ element1 , element2 , element3 ] ;
36
37
37
38
// When
38
39
bool result = elements . AllEqualBy ( element => element . Text ) ;
@@ -48,7 +49,7 @@ public void AllEqualByShouldProduceExpectedResultFalse()
48
49
Record < Guid > element1 = new ( "abc" , 123 , Guid . NewGuid ( ) ) ;
49
50
Record < Guid > element2 = new ( "abc" , 123 , Guid . NewGuid ( ) ) ;
50
51
Record < Guid > element3 = new ( "xyz" , 123 , Guid . NewGuid ( ) ) ;
51
- IEnumerable < Record < Guid > > elements = new [ ] { element1 , element2 , element3 } ;
52
+ IEnumerable < Record < Guid > > elements = [ element1 , element2 , element3 ] ;
52
53
53
54
// When
54
55
bool result = elements . AllEqualBy ( element => element . Text ) ;
@@ -64,7 +65,7 @@ public void AnyEqualByShouldProduceExpectedResultTrue()
64
65
Record < Guid > element1 = new ( "abc" , 123 , Guid . NewGuid ( ) ) ;
65
66
Record < Guid > element2 = new ( "abc" , 123 , Guid . NewGuid ( ) ) ;
66
67
Record < Guid > element3 = new ( "xyz" , 123 , Guid . NewGuid ( ) ) ;
67
- IEnumerable < Record < Guid > > elements = new [ ] { element1 , element2 , element3 } ;
68
+ IEnumerable < Record < Guid > > elements = [ element1 , element2 , element3 ] ;
68
69
69
70
// When
70
71
bool result = elements . AnyEqualBy ( element => element . Text ) ;
@@ -80,7 +81,7 @@ public void AnyEqualByShouldProduceExpectedResultFalse()
80
81
Record < Guid > element1 = new ( "abc" , 123 , Guid . NewGuid ( ) ) ;
81
82
Record < Guid > element2 = new ( "def" , 123 , Guid . NewGuid ( ) ) ;
82
83
Record < Guid > element3 = new ( "xyz" , 123 , Guid . NewGuid ( ) ) ;
83
- IEnumerable < Record < Guid > > elements = new [ ] { element1 , element2 , element3 } ;
84
+ IEnumerable < Record < Guid > > elements = [ element1 , element2 , element3 ] ;
84
85
85
86
// When
86
87
bool result = elements . AnyEqualBy ( element => element . Text ) ;
@@ -93,11 +94,11 @@ public void AnyEqualByShouldProduceExpectedResultFalse()
93
94
public void CountNotShouldProduceExpectedResult ( )
94
95
{
95
96
// Given
97
+ const int expected = 2 ;
96
98
Record < Guid > element1 = new ( "abc" , 123 , Guid . NewGuid ( ) ) ;
97
99
Record < Guid > element2 = new ( "def" , 123 , Guid . NewGuid ( ) ) ;
98
100
Record < Guid > element3 = new ( "xyz" , 456 , Guid . NewGuid ( ) ) ;
99
- IEnumerable < Record < Guid > > elements = new [ ] { element1 , element2 , element3 } ;
100
- const int expected = 2 ;
101
+ IEnumerable < Record < Guid > > elements = [ element1 , element2 , element3 ] ;
101
102
102
103
// When
103
104
int actual = elements . CountNot ( element => element . Number == 456 ) ;
@@ -123,8 +124,8 @@ public void ForEachShouldProduceExpectedResult()
123
124
public void GetContentHashCodeShouldProduceExpectedResultEqual ( )
124
125
{
125
126
// Given
126
- IEnumerable < Element > enumerable1 = new [ ] { new Element ( 1 ) , new Element ( 2 ) , new Element ( 3 ) } ;
127
- IEnumerable < Element > enumerable2 = new [ ] { new Element ( 1 ) , new Element ( 2 ) , new Element ( 3 ) } ;
127
+ IEnumerable < Element > enumerable1 = [ new Element ( 1 ) , new Element ( 2 ) , new Element ( 3 ) ] ;
128
+ IEnumerable < Element > enumerable2 = [ new Element ( 1 ) , new Element ( 2 ) , new Element ( 3 ) ] ;
128
129
129
130
// When
130
131
int hashCode1 = enumerable1 . GetContentHashCode ( ) ;
@@ -138,8 +139,8 @@ public void GetContentHashCodeShouldProduceExpectedResultEqual()
138
139
public void GetContentHashCodeShouldProduceExpectedResultDifferent ( )
139
140
{
140
141
// Given
141
- IEnumerable < Element > enumerable1 = new [ ] { new Element ( 1 ) , new Element ( 2 ) , new Element ( 3 ) } ;
142
- IEnumerable < Element > enumerable2 = new [ ] { new Element ( 3 ) , new Element ( 2 ) , new Element ( 1 ) } ;
142
+ IEnumerable < Element > enumerable1 = [ new Element ( 1 ) , new Element ( 2 ) , new Element ( 3 ) ] ;
143
+ IEnumerable < Element > enumerable2 = [ new Element ( 3 ) , new Element ( 2 ) , new Element ( 1 ) ] ;
143
144
144
145
// When
145
146
int hashCode1 = enumerable1 . GetContentHashCode ( ) ;
@@ -153,7 +154,7 @@ public void GetContentHashCodeShouldProduceExpectedResultDifferent()
153
154
public void IsEmptyShouldProduceExpectedResultTrue ( )
154
155
{
155
156
// Given
156
- IEnumerable < Element > enumerable = Enumerable . Empty < Element > ( ) ;
157
+ IEnumerable < Element > enumerable = [ ] ;
157
158
158
159
// When
159
160
bool result = enumerable . IsEmpty ( ) ;
@@ -166,7 +167,7 @@ public void IsEmptyShouldProduceExpectedResultTrue()
166
167
public void IsEmptyShouldProduceExpectedResultFalse ( )
167
168
{
168
169
// Given
169
- IEnumerable < Element > enumerable = new [ ] { new Element ( ) } ;
170
+ IEnumerable < Element > enumerable = [ new Element ( ) ] ;
170
171
171
172
// When
172
173
bool result = enumerable . IsEmpty ( ) ;
@@ -179,7 +180,7 @@ public void IsEmptyShouldProduceExpectedResultFalse()
179
180
public void IsNotEmptyShouldProduceExpectedResultTrue ( )
180
181
{
181
182
// Given
182
- IEnumerable < Element > enumerable = new [ ] { new Element ( ) } ;
183
+ IEnumerable < Element > enumerable = [ new Element ( ) ] ;
183
184
184
185
// When
185
186
bool result = enumerable . IsNotEmpty ( ) ;
@@ -192,7 +193,7 @@ public void IsNotEmptyShouldProduceExpectedResultTrue()
192
193
public void IsNotEmptyShouldProduceExpectedResultFalse ( )
193
194
{
194
195
// Given
195
- IEnumerable < Element > enumerable = Enumerable . Empty < Element > ( ) ;
196
+ IEnumerable < Element > enumerable = [ ] ;
196
197
197
198
// When
198
199
bool result = enumerable . IsNotEmpty ( ) ;
@@ -205,7 +206,7 @@ public void IsNotEmptyShouldProduceExpectedResultFalse()
205
206
public void IsSingleShouldProduceExpectedResultTrue ( )
206
207
{
207
208
// Given
208
- IEnumerable < Element > enumerable = new [ ] { new Element ( ) } ;
209
+ IEnumerable < Element > enumerable = [ new Element ( ) ] ;
209
210
210
211
// When
211
212
bool result = enumerable . IsSingle ( ) ;
@@ -218,7 +219,7 @@ public void IsSingleShouldProduceExpectedResultTrue()
218
219
public void IsSingleShouldProduceExpectedResultFalseWhenEmpty ( )
219
220
{
220
221
// Given
221
- IEnumerable < Element > enumerable = Enumerable . Empty < Element > ( ) ;
222
+ IEnumerable < Element > enumerable = [ ] ;
222
223
223
224
// When
224
225
bool result = enumerable . IsSingle ( ) ;
@@ -231,7 +232,7 @@ public void IsSingleShouldProduceExpectedResultFalseWhenEmpty()
231
232
public void IsSingleShouldProduceExpectedResultFalseWhenMoreThanOneElement ( )
232
233
{
233
234
// Given
234
- IEnumerable < Element > enumerable = new [ ] { new Element ( ) , new Element ( ) } ;
235
+ IEnumerable < Element > enumerable = [ new Element ( ) , new Element ( ) ] ;
235
236
236
237
// When
237
238
bool result = enumerable . IsSingle ( ) ;
@@ -244,7 +245,7 @@ public void IsSingleShouldProduceExpectedResultFalseWhenMoreThanOneElement()
244
245
public void IsCountEvenShouldProduceExpectedResultTrue ( )
245
246
{
246
247
// Given
247
- IEnumerable < Element > enumerable = new [ ] { new Element ( ) , new Element ( ) } ;
248
+ IEnumerable < Element > enumerable = [ new Element ( ) , new Element ( ) ] ;
248
249
249
250
// When
250
251
bool result = enumerable . IsCountEven ( ) ;
@@ -257,7 +258,7 @@ public void IsCountEvenShouldProduceExpectedResultTrue()
257
258
public void IsCountEvenShouldProduceExpectedResultFalse ( )
258
259
{
259
260
// Given
260
- IEnumerable < Element > enumerable = new [ ] { new Element ( ) } ;
261
+ IEnumerable < Element > enumerable = [ new Element ( ) ] ;
261
262
262
263
// When
263
264
bool result = enumerable . IsCountEven ( ) ;
@@ -270,7 +271,7 @@ public void IsCountEvenShouldProduceExpectedResultFalse()
270
271
public void IsCountOddShouldProduceExpectedResultTrue ( )
271
272
{
272
273
// Given
273
- IEnumerable < Element > enumerable = new [ ] { new Element ( ) } ;
274
+ IEnumerable < Element > enumerable = [ new Element ( ) ] ;
274
275
275
276
// When
276
277
bool result = enumerable . IsCountOdd ( ) ;
@@ -283,7 +284,7 @@ public void IsCountOddShouldProduceExpectedResultTrue()
283
284
public void IsCountOddShouldProduceExpectedResultFalse ( )
284
285
{
285
286
// Given
286
- IEnumerable < Element > enumerable = new [ ] { new Element ( ) , new Element ( ) } ;
287
+ IEnumerable < Element > enumerable = [ new Element ( ) , new Element ( ) ] ;
287
288
288
289
// When
289
290
bool result = enumerable . IsCountOdd ( ) ;
@@ -296,7 +297,7 @@ public void IsCountOddShouldProduceExpectedResultFalse()
296
297
public void JoinToStringShouldProduceExpectedResultWithDefaultSeparator ( )
297
298
{
298
299
// Given
299
- IEnumerable < object > enumerable = new object [ ] { 1 , 2 , 3 , 4.5 , true , false } ;
300
+ IEnumerable < object > enumerable = [ 1 , 2 , 3 , 4.5 , true , false ] ;
300
301
const string expected = "1, 2, 3, 4.5, True, False" ;
301
302
302
303
// When
@@ -310,7 +311,7 @@ public void JoinToStringShouldProduceExpectedResultWithDefaultSeparator()
310
311
public void JoinToStringShouldProduceExpectedResultWithCustomSeparator ( )
311
312
{
312
313
// Given
313
- IEnumerable < object > enumerable = new object [ ] { 1 , 2 , 3 , 4.5 , true , false } ;
314
+ IEnumerable < object > enumerable = [ 1 , 2 , 3 , 4.5 , true , false ] ;
314
315
const string expected = "1 *$ 2 *$ 3 *$ 4.5 *$ True *$ False" ;
315
316
316
317
// When
@@ -327,7 +328,7 @@ public void NoneShouldProduceExpectedResultTrue()
327
328
Record < Guid > element1 = new ( "abc" , 123 , Guid . NewGuid ( ) ) ;
328
329
Record < Guid > element2 = new ( "def" , 456 , Guid . NewGuid ( ) ) ;
329
330
Record < Guid > element3 = new ( "xyz" , 789 , Guid . NewGuid ( ) ) ;
330
- IEnumerable < Record < Guid > > elements = new [ ] { element1 , element2 , element3 } ;
331
+ IEnumerable < Record < Guid > > elements = [ element1 , element2 , element3 ] ;
331
332
332
333
// When
333
334
bool result = elements . None ( element => element . Number == 0 ) ;
@@ -343,7 +344,7 @@ public void NoneShouldProduceExpectedResultFalseAny()
343
344
Record < Guid > element1 = new ( "abc" , 123 , Guid . NewGuid ( ) ) ;
344
345
Record < Guid > element2 = new ( "def" , 456 , Guid . NewGuid ( ) ) ;
345
346
Record < Guid > element3 = new ( "xyz" , 0 , Guid . NewGuid ( ) ) ;
346
- IEnumerable < Record < Guid > > elements = new [ ] { element1 , element2 , element3 } ;
347
+ IEnumerable < Record < Guid > > elements = [ element1 , element2 , element3 ] ;
347
348
348
349
// When
349
350
bool result = elements . None ( element => element . Number == 0 ) ;
@@ -359,7 +360,7 @@ public void NoneShouldProduceExpectedResultFalseAll()
359
360
Record < Guid > element1 = new ( "abc" , 0 , Guid . NewGuid ( ) ) ;
360
361
Record < Guid > element2 = new ( "def" , 0 , Guid . NewGuid ( ) ) ;
361
362
Record < Guid > element3 = new ( "xyz" , 0 , Guid . NewGuid ( ) ) ;
362
- IEnumerable < Record < Guid > > elements = new [ ] { element1 , element2 , element3 } ;
363
+ IEnumerable < Record < Guid > > elements = [ element1 , element2 , element3 ] ;
363
364
364
365
// When
365
366
bool result = elements . None ( element => element . Number == 0 ) ;
@@ -371,12 +372,15 @@ public void NoneShouldProduceExpectedResultFalseAll()
371
372
[ Fact ( DisplayName = "IEnumerable.Sum should produce the expected result" ) ]
372
373
public void SumShouldProduceExpectedResult ( )
373
374
{
375
+ // Required as Sum() already exists for concrete number types.
376
+ static T SumProxy < T > ( IEnumerable < T > enumerable ) where T : INumberBase < T > => enumerable . Sum ( ) ;
377
+
374
378
// Given
375
379
IEnumerable < decimal > elements = [ 12.34m , 34.56m , 56.78m ] ;
376
380
const decimal expected = 103.68m ;
377
381
378
382
// When
379
- decimal actual = elements . Sum ( ) ;
383
+ decimal actual = SumProxy ( elements ) ;
380
384
381
385
// Then
382
386
Assert . Equal ( expected , actual ) ;
@@ -406,8 +410,8 @@ public void WhereNotShouldProduceExpectedResult()
406
410
Record < Guid > element1 = new ( "abc" , 123 , Guid . NewGuid ( ) ) ;
407
411
Record < Guid > element2 = new ( "def" , 456 , Guid . NewGuid ( ) ) ;
408
412
Record < Guid > element3 = new ( "xyz" , 789 , Guid . NewGuid ( ) ) ;
409
- IEnumerable < Record < Guid > > elements = new [ ] { element1 , element2 , element3 } ;
410
- IEnumerable < Record < Guid > > expected = new [ ] { element2 , element3 } ;
413
+ IEnumerable < Record < Guid > > elements = [ element1 , element2 , element3 ] ;
414
+ IEnumerable < Record < Guid > > expected = [ element2 , element3 ] ;
411
415
412
416
// When
413
417
IEnumerable < Record < Guid > > actual = elements . WhereNot ( element => element . Number == 123 ) ;
@@ -422,8 +426,8 @@ public void WhereNotNullShouldProduceExpectedResult()
422
426
// Given
423
427
Record < Guid > element1 = new ( "abc" , 123 , Guid . NewGuid ( ) ) ;
424
428
Record < Guid > element2 = new ( "def" , 456 , Guid . NewGuid ( ) ) ;
425
- IEnumerable < Record < Guid > > elements = new [ ] { element1 , element2 , null } ;
426
- IEnumerable < Record < Guid > > expected = new [ ] { element1 , element2 } ;
429
+ IEnumerable < Record < Guid > ? > elements = [ element1 , element2 , null ] ;
430
+ IEnumerable < Record < Guid > > expected = [ element1 , element2 ] ;
427
431
428
432
// When
429
433
IEnumerable < Record < Guid > > actual = elements . WhereNotNull ( ) ;
0 commit comments