← Back to team overview

nunit-core team mailing list archive

[Bug 674860] Re: Using() modifier missing on NUnit.Framework.Contains.Item()

 

** Changed in: nunitv2
       Status: Fix Committed => Fix Released

-- 
You received this bug notification because you are a member of NUnit
Developers, which is subscribed to NUnit V2.
https://bugs.launchpad.net/bugs/674860

Title:
  Using() modifier missing on NUnit.Framework.Contains.Item()

Status in NUnit V2 Test Framework:
  Fix Released

Bug description:
  There is a Using() modifier on NUnit.Framework.Is.EqualTo() (I'm using v2.5.8,) but I'd like to have one on NUnit.Framework.Contains.Item(), too. Here's what you'd have to do.

Make CollectionItemsEqualConstraint.comparer protected:
<    public abstract class CollectionItemsEqualConstraint : CollectionConstraint
<    {
<        private NUnitEqualityComparer comparer = NUnitEqualityComparer.Default;
-------
>    public abstract class CollectionItemsEqualConstraint : CollectionConstraint
>    {
>        protected NUnitEqualityComparer comparer = NUnitEqualityComparer.Default;

Add the Using modifier to CollectionContainsConstraint:
>        /// <summary>
>        /// Flag the constraint to use the supplied Comparison object.
>        /// </summary>
>        /// <param name="comparer">The Comparison<T> object to use.</param>
>        /// <returns>Self.</returns>
>        public CollectionContainsConstraint Using<T>(Comparison<T> comparer)
>        {
>            this.comparer.ExternalComparer = EqualityAdapter.For(comparer);
>            return this;
>        }

Variants go similar.

Narrow the return type of Contains.Item() so the new modifier becomes accessible:
>        /// <summary>
>        /// Creates a new CollectionContainsConstraint.
>        /// </summary>
>        /// <param name="item">The item that should be found.</param>
>        /// <returns>A new CollectionContainsConstraint</returns>
>        public static CollectionContainsConstraint Item(object item)
>        {
>            return new CollectionContainsConstraint(item);
>        }

Call the supplied comparer at the appropriate time. I suggest that this should be first after comparing for null and reference equality, since it is an explicit override in the test code:
>        public bool ObjectsEqual(object x, object y)
>        {
...
>            if (object.ReferenceEquals(x, y))
>                return true;
>
>            if (externalComparer != null)
>                return externalComparer.ObjectsEqual(x, y);
>
>            Type xType = x.GetType();
>            Type yType = y.GetType();
>
>            if (xType.IsArray && yType.IsArray && !compareAsCollection)
>                return ArraysEqual((Array)x, (Array)y);






References