Skip to content

Commit

Permalink
Test that Equals only enumerates once (#2463)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericstj committed Jan 18, 2022
1 parent 145b4fc commit 4415ae1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/xunit.v3.assert.tests/Asserts/CollectionAssertsTests.cs
Expand Up @@ -926,6 +926,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 @@ -1351,6 +1359,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
2 changes: 1 addition & 1 deletion src/xunit.v3.assert/Asserts
Submodule Asserts updated 1 files
+5 −8 EqualityAsserts.cs

0 comments on commit 4415ae1

Please sign in to comment.