"System.ArgumentException : Cannot compare" when the element is a list

Bug #709062 reported by Ngu Soon Hui
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
NUnit Framework
Fix Released
High
Charlie Poole
NUnit V2
Fix Released
Medium
Charlie Poole

Bug Description

Relevant discussion can be found here. https://groups.google.com/forum/?pli=1#!topic/nunit-discuss/IgGiVyo3fKw

This code throws me a System.ArgumentException : Cannot compare 2 exception.

        public class IntListEqualComparer: IEqualityComparer<List<int>>
        {
            public bool Equals(List<int> x, List<int> y)
            {
                return x.Count == y.Count;
            }

            public int GetHashCode(List<int> obj)
            {
                return obj.Count.GetHashCode();
            }
        }

        [Test]
        public void IntListListTest()
        {
            var list1 = new List<int>() {2, 3};
            var list2 = new List<int>() {2, 3};

            var list11 = new List<List<int>>() {list1};
            var list22 = new List<List<int>>() { list2 };
            var comparer = new IntListEqualComparer();
            Assert.That(list11, new CollectionEquivalentConstraint(list22).Using(comparer));
        }

It seems that Nunit can't compare if the elements are of the type List?

Here's the complete stack trace:

failed: System.ArgumentException : Cannot compare 2
 at NUnit.Framework.Constraints.EqualityAdapter.EqualityComparerAdapter`1.ObjectsEqual(Object x, Object y)
 at NUnit.Framework.Constraints.NUnitEqualityComparer.ObjectsEqual(Object x, Object y)
 at NUnit.Framework.Constraints.NUnitEqualityComparer.CollectionsEqual(ICollection x, ICollection y)
 at NUnit.Framework.Constraints.NUnitEqualityComparer.ObjectsEqual(Object x, Object y)
 at NUnit.Framework.Constraints.CollectionItemsEqualConstraint.CollectionTally.ItemsEqual(Object expected, Object actual)
 at NUnit.Framework.Constraints.CollectionItemsEqualConstraint.CollectionTally.TryRemove(Object o)
 at NUnit.Framework.Constraints.CollectionItemsEqualConstraint.CollectionTally.TryRemove(IEnumerable c)
 at NUnit.Framework.Constraints.CollectionEquivalentConstraint.doMatch(IEnumerable actual)
 at NUnit.Framework.Constraints.CollectionConstraint.Matches(Object actual)

Related branches

Changed in nunitv2:
status: New → Confirmed
Changed in nunitv2:
status: Confirmed → Triaged
importance: Undecided → Medium
milestone: none → 2.6.0
Changed in nunitv2:
assignee: nobody → Charlie Poole (charlie.poole)
status: Triaged → In Progress
Revision history for this message
Charlie Poole (charlie.poole) wrote :

This fixes the problem of NUnit using an external comparer designed for a particular type inappropriately.

In addition, it allows use of generic comparers when comparing lists and other ienumerables. Multiple Using clauses may now appear, and will be used on the appropriate types.

Changed in nunitv2:
status: In Progress → Fix Committed
Changed in nunit-3.0:
milestone: none → 2.9.6
Changed in nunit-3.0:
status: New → Triaged
importance: Undecided → High
Changed in nunitv2:
status: Fix Committed → Fix Released
Changed in nunit-3.0:
assignee: nobody → Charlie Poole (charlie.poole)
Changed in nunit-3.0:
status: Triaged → Fix Committed
Changed in nunit-3.0:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.