Skip to content

Commit

Permalink
Port #2463 to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Jan 18, 2022
1 parent b88fee5 commit 19618ef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/xunit.assert/Asserts
Submodule Asserts updated 1 files
+5 −8 EqualityAsserts.cs
28 changes: 28 additions & 0 deletions test/test.xunit.assert/Asserts/CollectionAssertsTests.cs
Expand Up @@ -925,6 +925,14 @@ public static void Equivalence()

Assert.Equal(expected, actual);
}

[Fact]
public static void EnumeratesOnlyOnce()
{
var expected = new[] { 1, 2, 3, 4, 5 };
var actual = new RunOnceEnumerable<int>(expected);
Assert.Equal(expected, actual);
}
}

public class EqualDictionary
Expand Down Expand Up @@ -1350,6 +1358,26 @@ public static void PredicateTooManyMatches()
}
}

sealed class RunOnceEnumerable<T> : IEnumerable<T>
{
private readonly IEnumerable<T> _source;
private bool _called;

public RunOnceEnumerable(IEnumerable<T> source)
{
_source = source;
}

public IEnumerator<T> GetEnumerator()
{
Assert.False(_called, "GetEnumerator() was called more than once");
_called = true;
return _source.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

sealed class SpyEnumerator<T> : IEnumerable<T>, IEnumerator<T>
{
IEnumerator<T>? innerEnumerator;
Expand Down

0 comments on commit 19618ef

Please sign in to comment.