-
Notifications
You must be signed in to change notification settings - Fork 290
implemented non-generic overload for Assert.ContainsSingle #6736
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
base: main
Are you sure you want to change the base?
implemented non-generic overload for Assert.ContainsSingle #6736
Conversation
|
@Youssef1313 do we need to hndle AssertSingleInterpolatedStringHandler for non-generic collection as well
|
…rpolationStringHandler based off Assert.HasCount
|
removed the |
Evangelink
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the 2 new APIs, we could instead call the generic version by doing .Cast<object>(). There is a potential small performance hit but I think it's ok.
WDYT @Youssef1313
test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs
Show resolved
Hide resolved
test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs
Show resolved
Hide resolved
Evangelink
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviving my earlier comment, I would personally use Cast<object> to avoid to have to maintain both implementations, the perf cost is IMO neglictible.
| public void ContainsSingle_InNonGenericCollection_NoMessage_WithEmptyCollection_ReturnsNoElement() | ||
| { | ||
| // Arrange | ||
| var collection = new ArrayList(); | ||
|
|
||
| // Act | ||
| Action action = () => Assert.ContainsSingle(collection); | ||
|
|
||
| // Assert | ||
| action.Should().Throw<AssertFailedException>(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove this test as it has no value compared to test below (besides test comment is wrong).
| public void ContainsSingle_InNonGenericCollection_AssertCustomMessage_WithEmptyCollection_ThrowsException() | ||
| { | ||
| // Arrange | ||
| var collection = new ArrayList(); | ||
|
|
||
| // Act | ||
| Action action = () => Assert.ContainsSingle(collection); | ||
|
|
||
| // Assert | ||
| action.Should().Throw<AssertFailedException>().WithMessage("Assert.ContainsSingle failed. Expected collection to contain exactly one element but found 0 element(s). 'collection' expression: 'collection'."); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add another test case like:
public void ContainsSingle_InNonGenericCollection_AssertCustomMessage_WithEmptyCollection_ThrowsException()
{
// Arrange
var collection = new ArrayList();
// Act
Action action = () => Assert.ContainsSingle(collection, "my custom message");
// Assert
action.Should().Throw<AssertFailedException>().WithMessage("Assert.ContainsSingle failed. Expected collection to contain exactly one element but found 0 element(s). 'collection' expression: 'collection'.");
Comment view }
|
@Evangelink I would personally avoid unsafe
{
var x = new int*[] { (int*)1 };
IEnumerable y = x;
Assert.ContainsSingle(y);
}It might also be worth adding such test :) |
|
Actually the scenario above might be invalid either way. |
| /// <returns>The item that matches the predicate.</returns> | ||
| public static object ContainsSingle(Func<object, bool> predicate, IEnumerable collection, string? message = "", [CallerArgumentExpression(nameof(predicate))] string predicateExpression = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") | ||
| { | ||
| var matchingElements = new List<object>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we creating a list unnecessary? We only care about first match and acount.
| /// Users shouldn't pass a value for this parameter. | ||
| /// </param> | ||
| /// <returns>The item.</returns> | ||
| public static object ContainsSingle(IEnumerable collection, string? message = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method can be written in terms of the predicate method, where the predicate is just static _ => true.
This Pull Request fixes Assert.ContainsSingle should accept non-generic collections #6732
This Implementation involves an overload being added to the
Assert.ContainsSinglewhich allows non-generic collections like ArrayList.Current Behaviour
Assert.ContainsSinglethrows compile time error because it only accepts generic collections.Expected Behaviour
With this Implementation, A
ssert.ContainsSinglewill allow non-generic collection as seen below;