You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure whether it's a bug or not, but consider the following code:
[Test]publicvoidDummy(){varitems=newSomeStuff[]{new("Name 1"),new("Name 2")};// OKAssert.That(items,Has.Exactly(2).Items);Assert.That(items,Has.ItemAt(0).Property(nameof(SomeStuff.Name)).EqualTo("Name 1"));Assert.That(items,Has.ItemAt(1).Property(nameof(SomeStuff.Name)).EqualTo("Name 2"));// OKAssert.That(items,Has.Exactly(2).Items.And.One.Property(nameof(SomeStuff.Name)).EqualTo("Name 1").And.One.Property(nameof(SomeStuff.Name)).EqualTo("Name 2"));// NOT OK 1Assert.That(items,Has.Exactly(2).Items.And.ItemAt(0).Property(nameof(SomeStuff.Name)).EqualTo("Name 1").And.ItemAt(1).Property(nameof(SomeStuff.Name)).EqualTo("Name 2"));// NOT OK 2Assert.That(items,Has.Exactly(2).Items.And.One.ItemAt(0).Property(nameof(SomeStuff.Name)).EqualTo("Name 1").And.One.ItemAt(1).Property(nameof(SomeStuff.Name)).EqualTo("Name 2"));}publicsealedrecordSomeStuff(stringName);
How would one chain AndConstraint and use the indexer in the same Assert.That call? The NOT OK 1 assert throws:
System.InvalidOperationException : Stack empty.
at System.Collections.Generic.Stack`1.ThrowForEmptyStack()
at System.Collections.Generic.Stack`1.Pop()
at NUnit.Framework.Constraints.ConstraintBuilder.ConstraintStack.Pop()
at NUnit.Framework.Constraints.BinaryOperator.Reduce(ConstraintStack stack)
at NUnit.Framework.Constraints.ConstraintBuilder.Resolve()
at NUnit.Framework.Constraints.Constraint.NUnit.Framework.Constraints.IResolveConstraint.Resolve()
at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression, String message, Object[] args)
at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression)
at SomeTests.Dummy() in /path/TestFile.cs:line 115
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
The NOT OK 2 assert throws:
System.ArgumentException : Default indexer accepting arguments < 0 > was not found on SomeTests+SomeStuff.
at NUnit.Framework.Constraints.IndexerConstraint.ApplyTo[TActual](TActual actual)
at NUnit.Framework.Constraints.ExactCountConstraint.ApplyTo[TActual](TActual actual)
at NUnit.Framework.Constraints.AndConstraint.ApplyTo[TActual](TActual actual)
at NUnit.Framework.Constraints.AndConstraint.ApplyTo[TActual](TActual actual)
at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression, String message, Object[] args)
at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression)
at SomeTests.Dummy() in /path/TestFile.cs:line 115
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
Namespaces, file paths and test names have been changed to protect the innocent.
The text was updated successfully, but these errors were encountered:
Thanks! This seems to be a bug.
I copied your repro into https://github.com/nunit/nunit.issues/tree/main/Issue4423, split it up and made a couple of more tests there. It looks like it is the ItemAt that give the problem.
It does return two different error message though:
and
I think the one that stems from .One.ItemAt is because One is returning the instance from the list to work on. At that point, it tries to use an indexer on an instance that has no indexer.
Make sense. It should however 1) give a better error message, or not sure if we can, make it a compiler error, and 2) add a nunit.analyzer warnings for this.
* Add check if RightContext is IndexerOperator to allow ItemAt on the right side of an And operator
* add test for IndexerOperator when on the left side is an AndOperator
I'm not sure whether it's a bug or not, but consider the following code:
How would one chain
AndConstraint
and use the indexer in the sameAssert.That
call? The NOT OK 1 assert throws:The NOT OK 2 assert throws:
Namespaces, file paths and test names have been changed to protect the innocent.
The text was updated successfully, but these errors were encountered: