Skip to content

Commit

Permalink
Merge pull request #4356 from manfred-brands/anyOf_Improvements
Browse files Browse the repository at this point in the history
Allows Is.AnyOf to be called with arrays or other collections
  • Loading branch information
OsirisTerje committed Apr 23, 2023
2 parents 40b402b + 67cc2ea commit fda616f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/NUnitFramework/framework/Constraints/AnyOfConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ namespace NUnit.Framework.Constraints
/// </summary>
public class AnyOfConstraint : Constraint
{
private readonly object[] _expected;
private readonly ICollection _expected;
private readonly NUnitEqualityComparer _comparer = new NUnitEqualityComparer();

/// <summary>
/// Construct a <see cref="AnyOfConstraint"/>
/// </summary>
/// <param name="expected">Collection of expected values</param>
public AnyOfConstraint(object[] expected) : base(expected)
public AnyOfConstraint(ICollection expected) : base(expected)
{
Guard.ArgumentNotNull(expected, nameof(expected));
Guard.ArgumentValid(expected.Length > 0,
Guard.ArgumentValid(expected.Count > 0,
$"{nameof(AnyOfConstraint)} requires non-empty expected collection!", nameof(expected));

_expected = expected;
Expand All @@ -33,6 +33,10 @@ public AnyOfConstraint(object[] expected) : base(expected)
/// </summary>
public override string Description => "any of " + MsgUtils.FormatValue(_expected);

/// <inheritdoc/>
protected override string GetStringRepresentation()
=> GetStringRepresentation(_expected);

/// <summary>
/// Test whether item is present in expected collection
/// </summary>
Expand Down
13 changes: 10 additions & 3 deletions src/NUnitFramework/framework/Constraints/Constraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using NUnit.Framework.Internal;
using System;
using System.Collections;

namespace NUnit.Framework.Constraints
{
Expand Down Expand Up @@ -139,16 +140,16 @@ public override string ToString()
}

/// <summary>
/// Returns the string representation of this constraint
/// Returns the string representation of this constraint and the passed in arguments
/// </summary>
protected virtual string GetStringRepresentation()
protected string GetStringRepresentation(IEnumerable arguments)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.Append("<");
sb.Append(DisplayName.ToLower());

foreach (object arg in Arguments)
foreach (object arg in arguments)
{
sb.Append(" ");
sb.Append(Displayable(arg));
Expand All @@ -166,6 +167,12 @@ static string Displayable(object o)
}
}

/// <summary>
/// Returns the string representation of this constraint
/// </summary>
protected virtual string GetStringRepresentation()
=> GetStringRepresentation(Arguments);

#endregion

#region Operator Overloads
Expand Down
9 changes: 9 additions & 0 deletions src/NUnitFramework/framework/Is.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,15 @@ public static AnyOfConstraint AnyOf(params object[] expected)
return new AnyOfConstraint(expected);
}

/// <summary>
/// Returns a constraint that tests if an item is equal to any of parameters
/// </summary>
/// <param name="expected">Expected values</param>
public static AnyOfConstraint AnyOf(ICollection expected)
{
return new AnyOfConstraint(expected);
}

#endregion

}
Expand Down
27 changes: 27 additions & 0 deletions src/NUnitFramework/tests/Constraints/AnyOfConstraintTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.Collections.Generic;

namespace NUnit.Framework.Constraints
{
Expand Down Expand Up @@ -32,5 +33,31 @@ public void ItemIsPresent_WithEqualityComparer()
var anyOf = new AnyOfConstraint(new[] { "A", "B", "C" }).Using(comparer);
Assert.That(anyOf.ApplyTo("1. A").Status, Is.EqualTo(ConstraintStatus.Success));
}

[Test]
public void ValidMemberInParams()
{
Assert.That(42, Is.AnyOf(0, -1, 42, 100));
}

[Test]
public void ValidMemberInArray()
{
var array = new[] { 0, -1, 42, 100 };
Assert.That(42, Is.AnyOf(array));
}

[Test]
public void ValidMemberInList()
{
var list = new List<int>() { 0, -1, 42, 100 };
Assert.That(42, Is.AnyOf(list));
}

[Test]
public void MissingMember()
{
Assert.That(42, Is.Not.AnyOf(0, -1, 100));
}
}
}

0 comments on commit fda616f

Please sign in to comment.