-
Notifications
You must be signed in to change notification settings - Fork 724
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
Added ContainsAny and DoesNotContainAny to CollectionAssert #4211
Open
tbenne10
wants to merge
3
commits into
nunit:master
Choose a base branch
from
tbenne10:4187-CollectionAssert-DisjointSets
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/NUnitFramework/framework/Constraints/CollectionContainsAnyConstraint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NUnit.Framework.Internal; | ||
|
||
namespace NUnit.Framework.Constraints | ||
{ | ||
/// <summary> | ||
/// CollectionContainsAnyConstraint is used to determine whether | ||
/// one collection contains any element from another collection | ||
/// </summary> | ||
public class CollectionContainsAnyConstraint : CollectionItemsEqualConstraint | ||
{ | ||
private readonly IEnumerable _expected; | ||
private List<object>? _presentItems; | ||
|
||
/// <summary> | ||
/// Construct a CollectionContainsAnyConstraint | ||
/// </summary> | ||
/// <param name="expected">The collection that any element from the actual collection is expected to be in</param> | ||
public CollectionContainsAnyConstraint(IEnumerable expected) | ||
: base(expected) | ||
{ | ||
_expected = expected; | ||
} | ||
|
||
/// <summary> | ||
/// The display name of this Constraint for use by ToString(). | ||
/// The default value is the name of the constraint with | ||
/// trailing "Constraint" removed. Derived classes may set | ||
/// this to another name in their constructors. | ||
/// </summary> | ||
public override string DisplayName { get { return "ContainsAny"; } } | ||
|
||
/// <summary> | ||
/// The Description of what this constraint tests, for | ||
/// use in messages and in the ConstraintResult. | ||
/// </summary> | ||
public override string Description | ||
{ | ||
get { return "contains any from " + MsgUtils.FormatValue(_expected); } | ||
} | ||
|
||
/// <summary> | ||
/// Test whether the actual collection contains an element from | ||
/// the expected collection provided. | ||
/// </summary> | ||
protected override bool Matches(IEnumerable actual) | ||
{ | ||
CollectionTally tally = Tally(_expected); | ||
tally.TryRemove(actual); | ||
|
||
_presentItems = tally.Result.PresentItems; | ||
|
||
return _presentItems.Count > 0; | ||
} | ||
|
||
/// <summary> | ||
/// Test whether the constraint is satisfied by a given value. | ||
/// </summary> | ||
public override ConstraintResult ApplyTo<TActual>(TActual actual) | ||
{ | ||
IEnumerable enumerable = ConstraintUtils.RequireActual<IEnumerable>(actual, nameof(actual)); | ||
bool hasElementPresent = Matches(enumerable); | ||
return new CollectionContainsAnyConstraintResult(this, actual, hasElementPresent, _presentItems); | ||
} | ||
|
||
/// <summary> | ||
/// Flag the constraint to use the supplied predicate function | ||
/// </summary> | ||
/// <param name="comparison">The comparison function to use.</param> | ||
/// <returns>Self.</returns> | ||
public CollectionContainsAnyConstraint Using<TContainsAnyType, TExpectedType>(Func<TContainsAnyType, TExpectedType, bool> comparison) | ||
{ | ||
// internal code reverses the expected order of the arguments. | ||
Func<TExpectedType, TContainsAnyType, bool> invertedComparison = (actual, expected) => comparison.Invoke(expected, actual); | ||
base.Using(EqualityAdapter.For(invertedComparison)); | ||
return this; | ||
} | ||
|
||
#region Private CollectionContainsAnyConstraintResult Class | ||
|
||
private sealed class CollectionContainsAnyConstraintResult : ConstraintResult | ||
{ | ||
private readonly List<object>? _presentItems; | ||
|
||
public CollectionContainsAnyConstraintResult(IConstraint constraint, object actualValue, bool isSuccess, List<object>? extraItems) | ||
: base(constraint, actualValue, isSuccess) | ||
{ | ||
_presentItems = extraItems; | ||
} | ||
|
||
public override void WriteAdditionalLinesTo(MessageWriter writer) | ||
{ | ||
if (_presentItems?.Count > 0) | ||
{ | ||
string presentItemsMessage = "Present items: "; | ||
presentItemsMessage += MsgUtils.FormatCollection(_presentItems); | ||
|
||
writer.WriteMessageLine(presentItemsMessage); | ||
} | ||
} | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Thanks for adding this. I often also use the
Contains
class for this sort of check too. Would it be possible to also expose aContains.All(IEnumerable)
function there too?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.
Anytime - just want to clarify, do you use
Contains
with iteration to perform the checks you need? Just want to make sure we are on the same page since they perform different actions.The naming might have been a bit misleading here -
Contains.All(IEnumerable)
already exists but is instead named SupersetOf(IEnumerable) within this class.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.
@tbenne10 Thanks for asking, I appreciate you coming back to this PR after all this time.
You make a good point. I may've mistyped my earlier suggestion in haste. I'd suggest we disregard my suggestion around
Contains.All()
in the interest of keeping this PR simple. Having thought it through, I agree with @OsirisTerje 's suggestion that it might make sense to expose your new constraint as aDoes.ContainAny()
method, which could then be easily inverted to beDoes.Not.ContainAny()
using the already-existingNot
constraint.I think my original comment came from a place of concern around ambiguity of the name
Is.ContainedIn
in relation to some other API operations when both arguments are collections. If I understand your PR right, the following will pass the first assertion but fail the second and third because they apply fundamentally different checks, though it might not be clear simply by reading them that this will happen.