Skip to content

Testing for equality using a predicate throws exception for collections #3286

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

Closed
PiotrKlecha opened this issue Jun 19, 2019 · 0 comments · Fixed by #3287
Closed

Testing for equality using a predicate throws exception for collections #3286

PiotrKlecha opened this issue Jun 19, 2019 · 0 comments · Fixed by #3287

Comments

@PiotrKlecha
Copy link
Contributor

Attempting to test collections for equality using a predicate that compares items fails with an exception.

The following example tests:

        [Test]
        public void UsesProvidedPredicateForItemComparison()
        {
            var expected = new[] { "yeti", "łysy", "rysiu" };
            var actual = new[] { "YETI", "Łysy", "RySiU" };

            Assert.That(actual, Is.EqualTo(expected).Using<string>((x, y) => StringUtil.StringsEqual(x, y, true)));
        }

        [Test]
        public void UsesProvidedPredicateForItemComparisonDifferentTypes()
        {
            var expected = new[] { 1, 2, 3 };
            var actual = new[] { "1", "2", "3" };

            Assert.That(actual, Is.EqualTo(expected).Using<string, int>((s, i) => i.ToString() == s));
        }

both throw an exception:
System.InvalidCastException : Unable to cast object of type 'System.String[]' to type 'System.String'.
w NUnit.Framework.Constraints.EqualityAdapter.PredicateEqualityAdapter`2.AreEqual(Object x, Object y)
w NUnit.Framework.Constraints.NUnitEqualityComparer.AreEqual(Object x, Object y, Tolerance& tolerance, Boolean topLevelComparison)
w NUnit.Framework.Constraints.EqualConstraint.ApplyTo[TActual](TActual actual)
w NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression, String message, Object[] args)
w NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression)

Similar comparisons performed using a dedicated IComparer or IEqualityComparer pass correctly.

jnm2 added a commit that referenced this issue Jun 24, 2019
Fix PredicateEqualityAdapter condition (#3286).
@mikkelbu mikkelbu added this to the 3.13 milestone Jun 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants