/
CollectionTracker.cs
866 lines (728 loc) · 25.2 KB
/
CollectionTracker.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
#if XUNIT_NULLABLE
#nullable enable
#else
// In case this is source-imported with global nullable enabled but no XUNIT_NULLABLE
#pragma warning disable CS8601
#pragma warning disable CS8603
#pragma warning disable CS8604
#pragma warning disable CS8605
#pragma warning disable CS8618
#endif
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
#if XUNIT_NULLABLE
using System.Diagnostics.CodeAnalysis;
#endif
namespace Xunit.Sdk
{
/// <summary>
/// Base class for generic <see cref="CollectionTracker{T}"/>, which also includes some public
/// static functionality.
/// </summary>
#if XUNIT_VISIBILITY_INTERNAL
internal
#else
public
#endif
abstract class CollectionTracker : IDisposable
{
/// <summary>
/// Initializes a new instance of the <see cref="CollectionTracker"/> class.
/// </summary>
/// <param name="innerEnumerable"></param>
/// <exception cref="ArgumentNullException"></exception>
protected CollectionTracker(IEnumerable innerEnumerable)
{
if (innerEnumerable == null)
throw new ArgumentNullException(nameof(innerEnumerable));
InnerEnumerable = innerEnumerable;
}
static MethodInfo openGenericCompareTypedSetsMethod =
typeof(CollectionTracker)
.GetRuntimeMethods()
.Single(m => m.Name == nameof(CompareTypedSets));
/// <summary>
/// Gets the inner enumerable that this collection track is wrapping. This is mostly
/// provided for simplifying other APIs which require both the tracker and the collection
/// (for example, <see cref="AreCollectionsEqual"/>).
/// </summary>
protected internal IEnumerable InnerEnumerable { get; protected set; }
/// <summary>
/// Determine if two enumerable collections are equal. It contains logic that varies depending
/// on the collection type (supporting arrays, dictionaries, sets, and generic enumerables).
/// </summary>
/// <param name="x">First value to compare</param>
/// <param name="y">Second value to comare</param>
/// <param name="itemComparer">The comparer used for individual item comparisons</param>
/// <param name="isDefaultItemComparer">Pass <c>true</c> if the <paramref name="itemComparer"/> is the default item
/// comparer from <see cref="AssertEqualityComparer{T}"/>; pass <c>false</c>, otherwise.</param>
/// <param name="mismatchedIndex">The output mismatched item index when the collections are not equal</param>
/// <returns>Returns <c>true</c> if the collections are equal; <c>false</c>, otherwise.</returns>
public static bool AreCollectionsEqual(
#if XUNIT_NULLABLE
CollectionTracker? x,
CollectionTracker? y,
#else
CollectionTracker x,
CollectionTracker y,
#endif
IEqualityComparer itemComparer,
bool isDefaultItemComparer,
out int? mismatchedIndex)
{
Assert.GuardArgumentNotNull(nameof(itemComparer), itemComparer);
mismatchedIndex = null;
return
CheckIfDictionariesAreEqual(x, y) ??
CheckIfSetsAreEqual(x, y, isDefaultItemComparer ? null : itemComparer) ??
CheckIfArraysAreEqual(x, y, itemComparer, isDefaultItemComparer, out mismatchedIndex) ??
CheckIfEnumerablesAreEqual(x, y, itemComparer, isDefaultItemComparer, out mismatchedIndex);
}
static bool? CheckIfArraysAreEqual(
#if XUNIT_NULLABLE
CollectionTracker? x,
CollectionTracker? y,
#else
CollectionTracker x,
CollectionTracker y,
#endif
IEqualityComparer itemComparer,
bool isDefaultItemComparer,
out int? mismatchedIndex)
{
mismatchedIndex = null;
if (x == null || y == null)
return null;
var expectedArray = x.InnerEnumerable as Array;
var actualArray = y.InnerEnumerable as Array;
if (expectedArray == null || actualArray == null)
return null;
// If we have single-dimensional zero-based arrays, then we delegate to the enumerable
// version, since that's uses the trackers and gets us the mismatch pointer.
if (expectedArray.Rank == 1 && expectedArray.GetLowerBound(0) == 0 &&
actualArray.Rank == 1 && actualArray.GetLowerBound(0) == 0)
return CheckIfEnumerablesAreEqual(x, y, itemComparer, isDefaultItemComparer, out mismatchedIndex);
if (expectedArray.Rank != actualArray.Rank)
return false;
// Differing bounds, aka object[2,1] vs. object[1,2]
// You can also have non-zero-based arrays, so we don't just check lengths
for (var rank = 0; rank < expectedArray.Rank; rank++)
if (expectedArray.GetLowerBound(rank) != actualArray.GetLowerBound(rank) || expectedArray.GetUpperBound(rank) != actualArray.GetUpperBound(rank))
return false;
// Enumeration will flatten everything identically, so just enumerate at this point
var expectedEnumerator = x.GetSafeEnumerator();
var actualEnumerator = y.GetSafeEnumerator();
while (true)
{
var hasExpected = expectedEnumerator.MoveNext();
var hasActual = actualEnumerator.MoveNext();
if (!hasExpected || !hasActual)
return hasExpected == hasActual;
if (!itemComparer.Equals(expectedEnumerator.Current, actualEnumerator.Current))
return false;
}
}
static bool? CheckIfDictionariesAreEqual(
#if XUNIT_NULLABLE
CollectionTracker? x,
CollectionTracker? y)
#else
CollectionTracker x,
CollectionTracker y)
#endif
{
if (x == null || y == null)
return null;
var dictionaryX = x.InnerEnumerable as IDictionary;
var dictionaryY = y.InnerEnumerable as IDictionary;
if (dictionaryX == null || dictionaryY == null)
return null;
if (dictionaryX.Count != dictionaryY.Count)
return false;
var dictionaryYKeys = new HashSet<object>(dictionaryY.Keys.Cast<object>());
// We don't pass along the itemComparer from AreCollectionsEqual because we aren't directly
// comparing the KeyValuePair<> objects. Instead we rely on Contains() on the dictionary to
// match up keys, and then create type-appropriate comparers for the values.
foreach (var key in dictionaryX.Keys.Cast<object>())
{
if (!dictionaryYKeys.Contains(key))
return false;
var valueX = dictionaryX[key];
var valueY = dictionaryY[key];
if (valueX == null)
{
if (valueY != null)
return false;
}
else if (valueY == null)
return false;
else
{
var comparer = AssertEqualityComparer.GetDefaultComparer(valueX.GetType());
if (!comparer.Equals(valueX, valueY))
return false;
}
dictionaryYKeys.Remove(key);
}
return dictionaryYKeys.Count == 0;
}
static bool CheckIfEnumerablesAreEqual(
#if XUNIT_NULLABLE
CollectionTracker? x,
CollectionTracker? y,
#else
CollectionTracker x,
CollectionTracker y,
#endif
IEqualityComparer itemComparer,
bool isDefaultItemComparer,
out int? mismatchIndex)
{
mismatchIndex = null;
if (x == null)
return y == null;
if (y == null)
return false;
var enumeratorX = x.GetSafeEnumerator();
var enumeratorY = y.GetSafeEnumerator();
mismatchIndex = 0;
while (true)
{
var hasNextX = enumeratorX.MoveNext();
var hasNextY = enumeratorY.MoveNext();
if (!hasNextX || !hasNextY)
{
if (hasNextX == hasNextY)
{
mismatchIndex = null;
return true;
}
return false;
}
var xCurrent = enumeratorX.Current;
var yCurrent = enumeratorY.Current;
using (var xCurrentTracker = isDefaultItemComparer ? xCurrent.AsNonStringTracker() : null)
using (var yCurrentTracker = isDefaultItemComparer ? yCurrent.AsNonStringTracker() : null)
{
if (xCurrentTracker != null && yCurrentTracker != null)
{
int? _;
var innerCompare = AreCollectionsEqual(xCurrentTracker, yCurrentTracker, AssertEqualityComparer<object>.DefaultInnerComparer, true, out _);
if (innerCompare == false)
return false;
}
else if (!itemComparer.Equals(xCurrent, yCurrent))
return false;
mismatchIndex++;
}
}
}
static bool? CheckIfSetsAreEqual(
#if XUNIT_NULLABLE
CollectionTracker? x,
CollectionTracker? y,
IEqualityComparer? itemComparer)
#else
CollectionTracker x,
CollectionTracker y,
IEqualityComparer itemComparer)
#endif
{
if (x == null || y == null)
return null;
var elementTypeX = ArgumentFormatter.GetSetElementType(x.InnerEnumerable);
var elementTypeY = ArgumentFormatter.GetSetElementType(y.InnerEnumerable);
if (elementTypeX == null || elementTypeY == null)
return null;
if (elementTypeX != elementTypeY)
return false;
var genericCompareMethod = openGenericCompareTypedSetsMethod.MakeGenericMethod(elementTypeX);
#if XUNIT_NULLABLE
return (bool)genericCompareMethod.Invoke(null, new object?[] { x.InnerEnumerable, y.InnerEnumerable, itemComparer })!;
#else
return (bool)genericCompareMethod.Invoke(null, new object[] { x.InnerEnumerable, y.InnerEnumerable, itemComparer });
#endif
}
static bool CompareTypedSets<T>(
ISet<T> setX,
ISet<T> setY,
#if XUNIT_NULLABLE
IEqualityComparer<T>? itemComparer)
#else
IEqualityComparer<T> itemComparer)
#endif
{
if (setX.Count != setY.Count)
return false;
if (itemComparer != null)
{
setX = new HashSet<T>(setX, itemComparer);
setY = new HashSet<T>(setY, itemComparer);
}
return setX.SetEquals(setY);
}
/// <inheritdoc/>
public abstract void Dispose();
/// <summary>
/// Formats the collection when you have a mismatched index. The formatted result will be the section of the
/// collection surrounded by the mismatched item.
/// </summary>
/// <param name="mismatchedIndex">The index of the mismatched item</param>
/// <param name="pointerIndent">How many spaces into the output value the pointed-to item begins at</param>
/// <param name="depth">The optional printing depth (1 indicates a top-level value)</param>
/// <returns>The formatted collection</returns>
public abstract string FormatIndexedMismatch(
int? mismatchedIndex,
out int? pointerIndent,
int depth = 1);
/// <summary>
/// Formats the collection when you have a mismatched index. The formatted result will be the section of the
/// collection from <paramref name="startIndex"/> to <paramref name="endIndex"/>. These indices are usually
/// obtained by calling <see cref="GetMismatchExtents"/>.
/// </summary>
/// <param name="startIndex">The start index of the collection to print</param>
/// <param name="endIndex">The end index of the collection to print</param>
/// <param name="mismatchedIndex">The mismatched item index</param>
/// <param name="pointerIndent">How many spaces into the output value the pointed-to item begins at</param>
/// <param name="depth">The optional printing depth (1 indicates a top-level value)</param>
/// <returns>The formatted collection</returns>
public abstract string FormatIndexedMismatch(
int startIndex,
int endIndex,
int? mismatchedIndex,
out int? pointerIndent,
int depth = 1);
/// <summary>
/// Formats the beginning part of the collection.
/// </summary>
/// <param name="depth">The optional printing depth (1 indicates a top-level value)</param>
/// <returns>The formatted collection</returns>
public abstract string FormatStart(int depth = 1);
/// <summary>
/// Gets the extents to print when you find a mismatched index, in the form of
/// a <paramref name="startIndex"/> and <paramref name="endIndex"/>. If the mismatched
/// index is <c>null</c>, the extents will start at index 0.
/// </summary>
/// <param name="mismatchedIndex">The mismatched item index</param>
/// <param name="startIndex">The start index that should be used for printing</param>
/// <param name="endIndex">The end index that should be used for printing</param>
public abstract void GetMismatchExtents(
int? mismatchedIndex,
out int startIndex,
out int endIndex);
/// <summary>
/// Gets a safe version of <see cref="IEnumerator"/> that prevents double enumeration and does all
/// the necessary tracking required for collection formatting. Should should be the same value
/// returned by <see cref="CollectionTracker{T}.GetEnumerator"/>, except non-generic.
/// </summary>
protected internal abstract IEnumerator GetSafeEnumerator();
/// <summary>
/// Gets the full name of the type of the element at the given index, if known.
/// Since this uses the item cache produced by enumeration, it may return <c>null</c>
/// when we haven't enumerated enough to see the given element, or if we enumerated
/// so much that the item has left the cache, or if the item at the given index
/// is <c>null</c>. It will also return <c>null</c> when the <paramref name="index"/>
/// is <c>null</c>.
/// </summary>
/// <param name="index">The item index</param>
#if XUNIT_NULLABLE
public abstract string? TypeAt(int? index);
#else
public abstract string TypeAt(int? index);
#endif
/// <summary>
/// Wraps an untyped enumerable in an object-based <see cref="CollectionTracker{T}"/>.
/// </summary>
/// <param name="enumerable">The untyped enumerable to wrap</param>
public static CollectionTracker<object> Wrap(IEnumerable enumerable) =>
new CollectionTracker<object>(enumerable, enumerable.Cast<object>());
}
/// <summary>
/// A utility class that can be used to wrap enumerables to prevent double enumeration.
/// It offers the ability to safely print parts of the collection when failures are
/// encountered, as well as some static versions of the printing functionality.
/// </summary>
#if XUNIT_VISIBILITY_INTERNAL
internal
#else
public
#endif
sealed class CollectionTracker<T> : CollectionTracker, IEnumerable<T>
{
const int MAX_ENUMERABLE_LENGTH_HALF = ArgumentFormatter.MAX_ENUMERABLE_LENGTH / 2;
readonly IEnumerable<T> collection;
#pragma warning disable CA2213 // We move disposal to DisposeInternal, due to https://github.com/xunit/xunit/issues/2762
#if XUNIT_NULLABLE
Enumerator? enumerator;
#else
Enumerator enumerator;
#endif
#pragma warning restore CA2213
/// <summary>
/// INTERNAL CONSTRUCTOR. DO NOT CALL.
/// </summary>
internal CollectionTracker(
IEnumerable collection,
IEnumerable<T> castCollection) :
base(collection)
{
if (castCollection == null)
throw new ArgumentNullException(nameof(castCollection));
this.collection = castCollection;
}
CollectionTracker(IEnumerable<T> collection) :
base(collection)
{
this.collection = collection;
}
/// <summary>
/// Gets the number of iterations that have happened so far.
/// </summary>
public int IterationCount =>
enumerator == null ? 0 : enumerator.CurrentIndex + 1;
/// <inheritdoc/>
public override void Dispose() =>
enumerator?.DisposeInternal();
/// <inheritdoc/>
public override string FormatIndexedMismatch(
int? mismatchedIndex,
out int? pointerIndent,
int depth = 1)
{
if (depth == ArgumentFormatter.MAX_DEPTH)
{
pointerIndent = 1;
return ArgumentFormatter.EllipsisInBrackets;
}
int startIndex;
int endIndex;
GetMismatchExtents(mismatchedIndex, out startIndex, out endIndex);
return FormatIndexedMismatch(
#if XUNIT_NULLABLE
enumerator!.CurrentItems,
#else
enumerator.CurrentItems,
#endif
() => enumerator.MoveNext(),
startIndex,
endIndex,
mismatchedIndex,
out pointerIndent,
depth
);
}
/// <inheritdoc/>
public override string FormatIndexedMismatch(
int startIndex,
int endIndex,
int? mismatchedIndex,
out int? pointerIndent,
int depth = 1)
{
if (enumerator == null)
throw new InvalidOperationException("Called FormatIndexedMismatch with indices without calling GetMismatchExtents first");
return FormatIndexedMismatch(
enumerator.CurrentItems,
() => enumerator.MoveNext(),
startIndex,
endIndex,
mismatchedIndex,
out pointerIndent,
depth
);
}
#if XUNIT_SPAN
/// <summary>
/// Formats a span with a mismatched index.
/// </summary>
/// <param name="span">The span to be formatted</param>
/// <param name="mismatchedIndex">The mismatched index point</param>
/// <param name="pointerIndent">How many spaces into the output value the pointed-to item begins at</param>
/// <param name="depth">The optional printing depth (1 indicates a top-level value)</param>
/// <returns>The formatted span</returns>
public static string FormatIndexedMismatch(
ReadOnlySpan<T> span,
int? mismatchedIndex,
out int? pointerIndent,
int depth = 1)
{
if (depth == ArgumentFormatter.MAX_DEPTH)
{
pointerIndent = 1;
return ArgumentFormatter.EllipsisInBrackets;
}
var startIndex = Math.Max(0, (mismatchedIndex ?? 0) - MAX_ENUMERABLE_LENGTH_HALF);
var endIndex = Math.Min(span.Length - 1, startIndex + ArgumentFormatter.MAX_ENUMERABLE_LENGTH - 1);
startIndex = Math.Max(0, endIndex - ArgumentFormatter.MAX_ENUMERABLE_LENGTH + 1);
var moreItemsPastEndIndex = endIndex < span.Length - 1;
var items = new Dictionary<int, T>();
for (var idx = startIndex; idx <= endIndex; ++idx)
items[idx] = span[idx];
return FormatIndexedMismatch(
items,
() => moreItemsPastEndIndex,
startIndex,
endIndex,
mismatchedIndex,
out pointerIndent,
depth
);
}
#endif
static string FormatIndexedMismatch(
Dictionary<int, T> items,
Func<bool> moreItemsPastEndIndex,
int startIndex,
int endIndex,
int? mismatchedIndex,
out int? pointerIndent,
int depth)
{
pointerIndent = null;
var printedValues = new StringBuilder("[");
if (startIndex != 0)
printedValues.Append(ArgumentFormatter.Ellipsis + ", ");
for (var idx = startIndex; idx <= endIndex; ++idx)
{
if (idx != startIndex)
printedValues.Append(", ");
if (idx == mismatchedIndex)
pointerIndent = printedValues.Length;
printedValues.Append(ArgumentFormatter.Format(items[idx], depth));
}
if (moreItemsPastEndIndex())
printedValues.Append(", " + ArgumentFormatter.Ellipsis);
printedValues.Append(']');
return printedValues.ToString();
}
/// <inheritdoc/>
public override string FormatStart(int depth = 1)
{
if (depth == ArgumentFormatter.MAX_DEPTH)
return ArgumentFormatter.EllipsisInBrackets;
if (enumerator == null)
enumerator = new Enumerator(collection.GetEnumerator());
// Ensure we have already seen enough data to format
while (enumerator.CurrentIndex <= ArgumentFormatter.MAX_ENUMERABLE_LENGTH)
if (!enumerator.MoveNext())
break;
return FormatStart(enumerator.StartItems, enumerator.CurrentIndex, depth);
}
/// <summary>
/// Formats the beginning part of a collection.
/// </summary>
/// <param name="collection">The collection to be formatted</param>
/// <param name="depth">The optional printing depth (1 indicates a top-level value)</param>
/// <returns>The formatted collection</returns>
public static string FormatStart(
IEnumerable<T> collection,
int depth = 1)
{
Assert.GuardArgumentNotNull(nameof(collection), collection);
if (depth == ArgumentFormatter.MAX_DEPTH)
return ArgumentFormatter.EllipsisInBrackets;
var startItems = new List<T>();
var currentIndex = -1;
var spanEnumerator = collection.GetEnumerator();
// Ensure we have already seen enough data to format
while (currentIndex <= ArgumentFormatter.MAX_ENUMERABLE_LENGTH)
{
if (!spanEnumerator.MoveNext())
break;
startItems.Add(spanEnumerator.Current);
++currentIndex;
}
return FormatStart(startItems, currentIndex, depth);
}
#if XUNIT_SPAN
/// <summary>
/// Formats the beginning part of a span.
/// </summary>
/// <param name="span">The span to be formatted</param>
/// <param name="depth">The optional printing depth (1 indicates a top-level value)</param>
/// <returns>The formatted span</returns>
public static string FormatStart(
ReadOnlySpan<T> span,
int depth = 1)
{
if (depth == ArgumentFormatter.MAX_DEPTH)
return ArgumentFormatter.EllipsisInBrackets;
var startItems = new List<T>();
var currentIndex = -1;
var spanEnumerator = span.GetEnumerator();
// Ensure we have already seen enough data to format
while (currentIndex <= ArgumentFormatter.MAX_ENUMERABLE_LENGTH)
{
if (!spanEnumerator.MoveNext())
break;
startItems.Add(spanEnumerator.Current);
++currentIndex;
}
return FormatStart(startItems, currentIndex, depth);
}
#endif
static string FormatStart(
List<T> items,
int currentIndex,
int depth)
{
var printedValues = new StringBuilder("[");
var printLength = Math.Min(currentIndex + 1, ArgumentFormatter.MAX_ENUMERABLE_LENGTH);
for (var idx = 0; idx < printLength; ++idx)
{
if (idx != 0)
printedValues.Append(", ");
printedValues.Append(ArgumentFormatter.Format(items[idx], depth));
}
if (currentIndex >= ArgumentFormatter.MAX_ENUMERABLE_LENGTH)
printedValues.Append(", " + ArgumentFormatter.Ellipsis);
printedValues.Append(']');
return printedValues.ToString();
}
/// <inheritdoc/>
public IEnumerator<T> GetEnumerator()
{
if (enumerator != null)
throw new InvalidOperationException("Multiple enumeration is not supported");
enumerator = new Enumerator(collection.GetEnumerator());
return enumerator;
}
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
/// <inheritdoc/>
protected internal override IEnumerator GetSafeEnumerator() =>
GetEnumerator();
/// <inheritdoc/>
public override void GetMismatchExtents(
int? mismatchedIndex,
out int startIndex,
out int endIndex)
{
if (enumerator == null)
enumerator = new Enumerator(collection.GetEnumerator());
startIndex = Math.Max(0, (mismatchedIndex ?? 0) - MAX_ENUMERABLE_LENGTH_HALF);
endIndex = startIndex + ArgumentFormatter.MAX_ENUMERABLE_LENGTH - 1;
// Make sure our window starts with startIndex and ends with endIndex, as appropriate
while (enumerator.CurrentIndex < endIndex)
if (!enumerator.MoveNext())
break;
endIndex = enumerator.CurrentIndex;
startIndex = Math.Max(0, endIndex - ArgumentFormatter.MAX_ENUMERABLE_LENGTH + 1);
}
/// <inheritdoc/>
#if XUNIT_NULLABLE
public override string? TypeAt(int? index)
#else
public override string TypeAt(int? index)
#endif
{
if (enumerator == null || !index.HasValue)
return null;
#if XUNIT_NULLABLE
T? item;
#else
T item;
#endif
if (!enumerator.TryGetCurrentItemAt(index.Value, out item))
return null;
return item?.GetType().FullName;
}
/// <summary>
/// Wraps the given collection inside of a <see cref="CollectionTracker{T}"/>.
/// </summary>
/// <param name="collection">The collection to be wrapped</param>
public static CollectionTracker<T> Wrap(IEnumerable<T> collection) =>
new CollectionTracker<T>(collection);
sealed class Enumerator : IEnumerator<T>
{
int currentItemsLastInsertionIndex = -1;
T[] currentItemsRingBuffer = new T[ArgumentFormatter.MAX_ENUMERABLE_LENGTH];
readonly IEnumerator<T> innerEnumerator;
public Enumerator(IEnumerator<T> innerEnumerator)
{
this.innerEnumerator = innerEnumerator;
}
public T Current =>
innerEnumerator.Current;
#if XUNIT_NULLABLE
object? IEnumerator.Current =>
#else
object IEnumerator.Current =>
#endif
Current;
public int CurrentIndex { get; private set; } = -1;
public Dictionary<int, T> CurrentItems
{
get
{
var result = new Dictionary<int, T>();
if (CurrentIndex > -1)
{
var itemIndex = Math.Max(0, CurrentIndex - ArgumentFormatter.MAX_ENUMERABLE_LENGTH + 1);
var indexInRingBuffer = (currentItemsLastInsertionIndex - CurrentIndex + itemIndex) % ArgumentFormatter.MAX_ENUMERABLE_LENGTH;
if (indexInRingBuffer < 0)
indexInRingBuffer += ArgumentFormatter.MAX_ENUMERABLE_LENGTH;
while (itemIndex <= CurrentIndex)
{
result[itemIndex] = currentItemsRingBuffer[indexInRingBuffer];
++itemIndex;
indexInRingBuffer = (indexInRingBuffer + 1) % ArgumentFormatter.MAX_ENUMERABLE_LENGTH;
}
}
return result;
}
}
public List<T> StartItems { get; } = new List<T>();
public void Dispose()
{ }
public void DisposeInternal()
{
innerEnumerator.Dispose();
}
public bool MoveNext()
{
if (!innerEnumerator.MoveNext())
return false;
CurrentIndex++;
var current = innerEnumerator.Current;
// Keep (MAX_ENUMERABLE_LENGTH + 1) items here, so we can
// print the start of the collection when lengths differ
if (CurrentIndex <= ArgumentFormatter.MAX_ENUMERABLE_LENGTH)
StartItems.Add(current);
// Keep a ring buffer filled with the most recent MAX_ENUMERABLE_LENGTH items
// so we can print out the items when we've found a bad index
currentItemsLastInsertionIndex = (currentItemsLastInsertionIndex + 1) % ArgumentFormatter.MAX_ENUMERABLE_LENGTH;
currentItemsRingBuffer[currentItemsLastInsertionIndex] = current;
return true;
}
public void Reset()
{
innerEnumerator.Reset();
CurrentIndex = -1;
currentItemsLastInsertionIndex = -1;
StartItems.Clear();
}
public bool TryGetCurrentItemAt(
int index,
#if XUNIT_NULLABLE
[MaybeNullWhen(false)] out T item)
#else
out T item)
#endif
{
item = default(T);
if (index < 0 || index <= CurrentIndex - ArgumentFormatter.MAX_ENUMERABLE_LENGTH || index > CurrentIndex)
return false;
var indexInRingBuffer = (currentItemsLastInsertionIndex - CurrentIndex + index) % ArgumentFormatter.MAX_ENUMERABLE_LENGTH;
if (indexInRingBuffer < 0)
indexInRingBuffer += ArgumentFormatter.MAX_ENUMERABLE_LENGTH;
item = currentItemsRingBuffer[indexInRingBuffer];
return true;
}
}
}
}