c# - NUnit CollectionAssert.AreEqual(expected,actual) vs Assert.IsTrue(expected.SequenceEqual(actual)) -
c# - NUnit CollectionAssert.AreEqual(expected,actual) vs Assert.IsTrue(expected.SequenceEqual(actual)) -
i have objects placed queue. of objects in queue implement same base of operations interface, requires implement iequatable<>.
i want verify right objects beingness placed in queue in right order.
when write test makes assertion collectionassert.areequal(expected,actual)
, test fails, stating that, while queue expected length, differs @ index 0.
however, when write test assertion assert.istrue(expected.sequenceequal(actual))
, test passes.
it seems me these 2 assertions should same, apparently quite different.
nunit's website isn't detailed collection assert methods, , linq documentation, while more detailed, doesn't help without beingness able compare nunit doing collection assert method.
how these 2 assertions differ?
edit - adding detail
here code under test 2 tests expect have same result.
public enum argumenttypeenum { spiceworksdbname, destinationdbname, destinationservername } public interface iargument : iequatable<iargument> { } public interface icommandargument : iargument { argumenttypeenum argumenttype { get; } } internal interface ivalueargument : iargument { string argumentvalue { get; } } public class commandargument : icommandargument { public commandargument(argumenttypeenum argumenttype) { argumenttype = argumenttype; } public argumenttypeenum argumenttype { get; private set; } public bool equals(iargument other) { if (other == null) { homecoming false; } var otherascommandarg = other commandargument; if (otherascommandarg == null) { homecoming false; } homecoming argumenttype == otherascommandarg.argumenttype; } } public class valueargument : ivalueargument { public valueargument(string argumentvalue) { argumentvalue = argumentvalue; } public string argumentvalue { get; private set; } public bool equals(iargument other) { if (other == null) { homecoming false; } var otherasvaluearg = other valueargument; if (otherasvaluearg == null) { homecoming false; } homecoming argumentvalue == otherasvaluearg.argumentvalue; } } public class tokenizer { public queue<iargument> tokenizearguments(string[] args) { var tokenqueue = new queue<iargument>(); args.tolist().foreach((arg) => { if (arg.startswith("-")) { switch (arg) { case "-sd": tokenqueue.enqueue(new commandargument(argumenttypeenum.spiceworksdbname)); break; case "-dd": tokenqueue.enqueue(new commandargument(argumenttypeenum.destinationdbname)); break; case "-ds": tokenqueue.enqueue(new commandargument(argumenttypeenum.destinationservername)); break; default: tokenqueue.enqueue(new valueargument(arg)); break; } } else { tokenqueue.enqueue(new valueargument(arg)); } }); homecoming tokenqueue; } } [testfixture] public class tokenizerspecs { ///passes [test] public void tokenizer_multiplecommandsandvalues_tokenizesasexpected1() { var args = new[] { "-sd", @"\\some\directory\foo.db", "-dd", "some database name", "-ds", "server.name", "somerandomarg", "-unreconized" }; var t = new tokenizer(); var actualresult = t.tokenizearguments(args); var expectedresult = new queue<iargument>(); expectedresult.enqueue(new commandargument(argumenttypeenum.spiceworksdbname)); expectedresult.enqueue(new valueargument(@"\\some\directory\foo.db")); expectedresult.enqueue(new commandargument(argumenttypeenum.destinationdbname)); expectedresult.enqueue(new valueargument("some database name")); expectedresult.enqueue(new commandargument(argumenttypeenum.destinationservername)); expectedresult.enqueue(new valueargument("server.name")); expectedresult.enqueue(new valueargument("somerandomarg")); expectedresult.enqueue(new valueargument("-unreconized")); assert.istrue(expectedresult.sequenceequal(actualresult)); } ///fails [test] public void tokenizer_multiplecommandsandvalues_tokenizesasexpected2() { var args = new[] { "-sd", @"\\some\directory\foo.db", "-dd", "some database name", "-ds", "server.name", "somerandomarg", "-unreconized" }; var t = new tokenizer(); var actualresult = t.tokenizearguments(args); var expectedresult = new queue<iargument>(); expectedresult.enqueue(new commandargument(argumenttypeenum.spiceworksdbname)); expectedresult.enqueue(new valueargument(@"\\some\directory\foo.db")); expectedresult.enqueue(new commandargument(argumenttypeenum.destinationdbname)); expectedresult.enqueue(new valueargument("some database name")); expectedresult.enqueue(new commandargument(argumenttypeenum.destinationservername)); expectedresult.enqueue(new valueargument("server.name")); expectedresult.enqueue(new valueargument("somerandomarg")); expectedresult.enqueue(new valueargument("-unreconized")); collectionassert.areequal(expectedresult, actualresult); } }
i've taken @ nunit's source code , seems you've got bug here. comparing done nunitequalitycomparer.enumerablesequal
subsequently compares each element nunitequalitycomparer.objectsequal
. can simple test using object:
var n = new nunitequalitycomparer(); var tolerance = tolerance.zero; var equal = n.areequal(new commandargument(argumenttypeenum.spiceworksdbname), new commandargument(argumenttypeenum.spiceworksdbname), ref tolerance);
this test returns false
!
somehow, objectsequal
doesn't end in object's equals
method. require debugging against source code find out why, test sufficiently proves bug involved.
c# linq collections nunit assertions
Comments
Post a Comment