Skip to content

Commit

Permalink
Remove CollectionContainsConstraint
Browse files Browse the repository at this point in the history
There is some code duplication between
`DictionaryContainsKeyConstraint` and
`DictionaryContainsValueConstraint`, but
not much will be saved by extracting
`Matches` and `Using` to a helper class.

Fixes #1324
  • Loading branch information
mikkelbu committed Jun 10, 2017
1 parent 78e6776 commit aa19bd1
Show file tree
Hide file tree
Showing 27 changed files with 90 additions and 292 deletions.
4 changes: 2 additions & 2 deletions src/NUnitFramework/framework/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ static public void Inconclusive()
/// <param name="args">Array of objects to be used in formatting the message</param>
public static void Contains(object expected, ICollection actual, string message, params object[] args)
{
Assert.That(actual, new CollectionContainsConstraint(expected) ,message, args);
Assert.That(actual, new SomeItemsConstraint(new EqualConstraint(expected)) ,message, args);
}

/// <summary>
Expand All @@ -308,7 +308,7 @@ public static void Contains(object expected, ICollection actual, string message,
/// <param name="actual">The collection to be examined</param>
public static void Contains(object expected, ICollection actual)
{
Assert.That(actual, new CollectionContainsConstraint(expected) ,null, null);
Assert.That(actual, new SomeItemsConstraint(new EqualConstraint(expected)) ,null, null);
}

#endregion
Expand Down
12 changes: 6 additions & 6 deletions src/NUnitFramework/framework/AssertionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -686,21 +686,21 @@ public CollectionOrderedConstraint Ordered
#region Member

/// <summary>
/// Returns a new CollectionContainsConstraint checking for the
/// Returns a new <see cref="EqualConstraint"/> checking for the
/// presence of a particular object in the collection.
/// </summary>
public CollectionContainsConstraint Member(object expected)
public EqualConstraint Member(object expected)
{
return new CollectionContainsConstraint(expected);
return Has.Some.EqualTo(expected);
}

/// <summary>
/// Returns a new CollectionContainsConstraint checking for the
/// Returns a new <see cref="EqualConstraint"/> checking for the
/// presence of a particular object in the collection.
/// </summary>
public CollectionContainsConstraint Contains(object expected)
public EqualConstraint Contains(object expected)
{
return new CollectionContainsConstraint(expected);
return Has.Some.EqualTo(expected);
}

#endregion
Expand Down

This file was deleted.

17 changes: 8 additions & 9 deletions src/NUnitFramework/framework/Constraints/ConstraintExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

using System;
using System.Collections;
using System.Runtime.CompilerServices;

namespace NUnit.Framework.Constraints
{
Expand Down Expand Up @@ -702,25 +701,25 @@ public CollectionOrderedConstraint Ordered
#region Member

/// <summary>
/// Returns a new CollectionContainsConstraint checking for the
/// Returns a new <see cref="SomeItemsConstraint"/> checking for the
/// presence of a particular object in the collection.
/// </summary>
public CollectionContainsConstraint Member(object expected)
public SomeItemsConstraint Member(object expected)
{
return (CollectionContainsConstraint)this.Append(new CollectionContainsConstraint(expected));
return (SomeItemsConstraint)this.Append(new SomeItemsConstraint(new EqualConstraint(expected)));
}

#endregion

#region Contains

/// <summary>
/// Returns a new CollectionContainsConstraint checking for the
/// Returns a new <see cref="SomeItemsConstraint"/> checking for the
/// presence of a particular object in the collection.
/// </summary>
public CollectionContainsConstraint Contains(object expected)
public SomeItemsConstraint Contains(object expected)
{
return (CollectionContainsConstraint)this.Append(new CollectionContainsConstraint(expected));
return (SomeItemsConstraint)this.Append(new SomeItemsConstraint(new EqualConstraint(expected)));
}

/// <summary>
Expand All @@ -737,10 +736,10 @@ public ContainsConstraint Contains(string expected)
}

/// <summary>
/// Returns a new CollectionContainsConstraint checking for the
/// Returns a new <see cref="SomeItemsConstraint"/> checking for the
/// presence of a particular object in the collection.
/// </summary>
public CollectionContainsConstraint Contain(object expected)
public SomeItemsConstraint Contain(object expected)
{
return Contains(expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public override ConstraintResult ApplyTo<TActual>(TActual actual)
_realConstraint = constraint;
}
else
_realConstraint = new CollectionContainsConstraint(_expected);
_realConstraint = new SomeItemsConstraint(new EqualConstraint(_expected));

return _realConstraint.ApplyTo(actual);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace NUnit.Framework.Constraints
/// DictionaryContainsKeyConstraint is used to test whether a dictionary
/// contains an expected object as a key.
/// </summary>
public class DictionaryContainsKeyConstraint : CollectionContainsConstraint
public class DictionaryContainsKeyConstraint : CollectionItemsEqualConstraint
{
/// <summary>
/// Construct a DictionaryContainsKeyConstraint
Expand All @@ -39,6 +39,7 @@ public class DictionaryContainsKeyConstraint : CollectionContainsConstraint
public DictionaryContainsKeyConstraint(object expected)
: base(expected)
{
Expected = expected;
}

/// <summary>
Expand All @@ -58,6 +59,11 @@ public override string Description
get { return "dictionary containing key " + MsgUtils.FormatValue(Expected); }
}

/// <summary>
/// Gets the expected object
/// </summary>
protected object Expected { get; private set; }

/// <summary>
/// Test whether the expected key is contained in the dictionary
/// </summary>
Expand All @@ -68,7 +74,25 @@ protected override bool Matches(IEnumerable actual)
if (dictionary == null)
throw new ArgumentException("The actual value must be an IDictionary", "actual");

return base.Matches(dictionary.Keys);
foreach (object obj in dictionary.Keys)
if (ItemsEqual(obj, Expected))
return true;

return false;
}

/// <summary>
/// Flag the constraint to use the supplied predicate function
/// </summary>
/// <param name="comparison">The comparison function to use.</param>
/// <returns>Self.</returns>
public DictionaryContainsKeyConstraint Using<TCollectionType, TMemberType>(Func<TCollectionType, TMemberType, bool> comparison)
{
// reverse the order of the arguments to match expectations of PredicateEqualityComparer
Func<TMemberType, TCollectionType, bool> invertedComparison = (actual, expected) => comparison.Invoke(expected, actual);

base.Using(EqualityAdapter.For(invertedComparison));
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace NUnit.Framework.Constraints
/// DictionaryContainsValueConstraint is used to test whether a dictionary
/// contains an expected object as a value.
/// </summary>
public class DictionaryContainsValueConstraint : CollectionContainsConstraint
public class DictionaryContainsValueConstraint : CollectionItemsEqualConstraint
{
/// <summary>
/// Construct a DictionaryContainsValueConstraint
Expand All @@ -39,6 +39,7 @@ public class DictionaryContainsValueConstraint : CollectionContainsConstraint
public DictionaryContainsValueConstraint(object expected)
: base(expected)
{
Expected = expected;
}

/// <summary>
Expand All @@ -58,6 +59,11 @@ public override string Description
get { return "dictionary containing value " + MsgUtils.FormatValue(Expected); }
}

/// <summary>
/// Gets the expected object
/// </summary>
protected object Expected { get; private set; }

/// <summary>
/// Test whether the expected value is contained in the dictionary
/// </summary>
Expand All @@ -68,7 +74,25 @@ protected override bool Matches(IEnumerable actual)
if (dictionary == null)
throw new ArgumentException("The actual value must be an IDictionary", "actual");

return base.Matches(dictionary.Values);
foreach (object obj in dictionary.Values)
if (ItemsEqual(obj, Expected))
return true;

return false;
}

/// <summary>
/// Flag the constraint to use the supplied predicate function
/// </summary>
/// <param name="comparison">The comparison function to use.</param>
/// <returns>Self.</returns>
public DictionaryContainsValueConstraint Using<TCollectionType, TMemberType>(Func<TCollectionType, TMemberType, bool> comparison)
{
// reverse the order of the arguments to match expectations of PredicateEqualityComparer
Func<TMemberType, TCollectionType, bool> invertedComparison = (actual, expected) => comparison.Invoke(expected, actual);

base.Using(EqualityAdapter.For(invertedComparison));
return this;
}
}
}
8 changes: 3 additions & 5 deletions src/NUnitFramework/framework/Contains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System;
using System.Collections;
using NUnit.Framework.Constraints;

namespace NUnit.Framework
Expand All @@ -36,12 +34,12 @@ public class Contains
#region Item

/// <summary>
/// Returns a new CollectionContainsConstraint checking for the
/// Returns a new <see cref="EqualConstraint"/> checking for the
/// presence of a particular object in the collection.
/// </summary>
public static CollectionContainsConstraint Item(object expected)
public static EqualConstraint Item(object expected)
{
return new CollectionContainsConstraint(expected);
return Has.Some.EqualTo(expected);
}

#endregion
Expand Down
6 changes: 3 additions & 3 deletions src/NUnitFramework/framework/Does.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public static FileOrDirectoryExistsConstraint Exist
#region Contain

/// <summary>
/// Returns a new CollectionContainsConstraint checking for the
/// Returns a new <see cref="EqualConstraint"/> checking for the
/// presence of a particular object in the collection.
/// </summary>
public static CollectionContainsConstraint Contain(object expected)
public static EqualConstraint Contain(object expected)
{
return new CollectionContainsConstraint(expected);
return new ConstraintExpression().Some.EqualTo(expected);
}

/// <summary>
Expand Down
7 changes: 3 additions & 4 deletions src/NUnitFramework/framework/Has.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
// ***********************************************************************

using System;
using System.Collections;
using NUnit.Framework.Constraints;

namespace NUnit.Framework
Expand Down Expand Up @@ -193,12 +192,12 @@ public static ResolvableConstraintExpression Attribute<T>()
#region Member

/// <summary>
/// Returns a new CollectionContainsConstraint checking for the
/// Returns a new <see cref="EqualConstraint"/> checking for the
/// presence of a particular object in the collection.
/// </summary>
public static CollectionContainsConstraint Member(object expected)
public static EqualConstraint Member(object expected)
{
return new CollectionContainsConstraint(expected);
return Some.EqualTo(expected);
}

#endregion
Expand Down
1 change: 0 additions & 1 deletion src/NUnitFramework/framework/nunit.framework-2.0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@
<Compile Include="Constraints\BinaryConstraint.cs" />
<Compile Include="Constraints\BinarySerializableConstraint.cs" />
<Compile Include="Constraints\CollectionConstraint.cs" />
<Compile Include="Constraints\CollectionContainsConstraint.cs" />
<Compile Include="Constraints\CollectionEquivalentConstraint.cs" />
<Compile Include="Constraints\CollectionItemsEqualConstraint.cs" />
<Compile Include="Constraints\CollectionOrderedConstraint.cs" />
Expand Down
Loading

0 comments on commit aa19bd1

Please sign in to comment.