Skip to content

Commit 19618ef

Browse files
committed
Port #2463 to v2
1 parent b88fee5 commit 19618ef

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/xunit.assert/Asserts

test/test.xunit.assert/Asserts/CollectionAssertsTests.cs

+28
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,14 @@ public static void Equivalence()
925925

926926
Assert.Equal(expected, actual);
927927
}
928+
929+
[Fact]
930+
public static void EnumeratesOnlyOnce()
931+
{
932+
var expected = new[] { 1, 2, 3, 4, 5 };
933+
var actual = new RunOnceEnumerable<int>(expected);
934+
Assert.Equal(expected, actual);
935+
}
928936
}
929937

930938
public class EqualDictionary
@@ -1350,6 +1358,26 @@ public static void PredicateTooManyMatches()
13501358
}
13511359
}
13521360

1361+
sealed class RunOnceEnumerable<T> : IEnumerable<T>
1362+
{
1363+
private readonly IEnumerable<T> _source;
1364+
private bool _called;
1365+
1366+
public RunOnceEnumerable(IEnumerable<T> source)
1367+
{
1368+
_source = source;
1369+
}
1370+
1371+
public IEnumerator<T> GetEnumerator()
1372+
{
1373+
Assert.False(_called, "GetEnumerator() was called more than once");
1374+
_called = true;
1375+
return _source.GetEnumerator();
1376+
}
1377+
1378+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
1379+
}
1380+
13531381
sealed class SpyEnumerator<T> : IEnumerable<T>, IEnumerator<T>
13541382
{
13551383
IEnumerator<T>? innerEnumerator;

0 commit comments

Comments
 (0)