← Back to team overview

nunit-core team mailing list archive

[Bug 532488] Re: constraints from ConstraintExpression/ConstraintBuilder are not reusable

 

After discussion on the mailing list and in the context of another bug,
Dale King pointed out that the source of the problem is that a
constraint expression is resolved by Assert.That on it's first use and a
pointer to the resolved constraint is what is actually used. What we
should do for a reusable constraint is to pre-resolve it and then save
that pointer.

Note that the problem does not arise with simple constraints like
Is.Null. It's only when the constraint has pending operators to be
resolved - like Is.Not.Null - that we see the issue.

Requiring the user to resolve the constraint first seems like too much.
The Resolve() method is an explicit interface implementation and so
requires a cast for it even to be called.  I'll experiment with a
wrapper class that allows reuse and add some info about reuse to the
docs.

** Changed in: nunitv2
       Status: New => Triaged

** Changed in: nunitv2
   Importance: Undecided => High

** Changed in: nunitv2
     Assignee: (unassigned) => Charlie Poole (charlie.poole)

** Changed in: nunitv2
    Milestone: None => 2.5.6

** Also affects: nunit-3.0
   Importance: Undecided
       Status: New

-- 
constraints from ConstraintExpression/ConstraintBuilder are not reusable
https://bugs.launchpad.net/bugs/532488
You received this bug notification because you are a member of NUnit
Developers, which is subscribed to NUnit V2.

Status in NUnit Test Framework: New
Status in NUnit V2 Test Framework: Triaged

Bug description:
Constraint that I receive from syntax helper (Is.Not.Null) is not reusable, but I guess this is more general problem.
I found this problem when upgrading nunit.framework.dll from v2.4.6 to v.2.5.3. In both cases tests were executed witn NUnit 2.5.3 console.
Worst thing about this problem seems that tests could actually pass when they should fail if you reuse constraint.

Here is simple example that demonstrates the problem:

using NUnit.Framework;
using NUnit.Framework.Constraints;

namespace NUnit_2_5_3_bug
{
    [TestFixture]
    public class FailToReuseConstraint
    {
        /// <summary>
        /// Constraint from <see cref="ConstraintExpression"/>.
        /// </summary>
        /// <remarks>
        /// Demonstrates that constraint received from syntax helper is not reusable.
        /// </remarks>
        [Test]
        public void FromSyntaxHelper()
        {
            Constraint expression = Is.Not.Null;
            Assert.That(this, expression, "one");
            Assert.That(this, expression, "two"); // this step fails on NUnit.2.5.3
        }

        /// <summary>
        /// How NUnit 2.4 did it (i think).
        /// </summary>
        [Test]
        public void NotConstraint()
        {
            Constraint expression = new NotConstraint(Is.Null);
            Assert.That(this, expression, "one");
            Assert.That(this, expression, "two");
        }
    }
}





References