Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test that Equals only enumerates once #2463

Merged
merged 3 commits into from Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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