How does Assert determine equality?

  • Thread starter Thread starter Ethan Strauss
  • Start date Start date
E

Ethan Strauss

This is mostly just curiousity. I was writting a unit test to verify that I
was creating a Regex correctly. This involved
Assert.AreEqual(expected, actual)
where expected and actual are both Regex objects. My code failed the test
:-(, but I think it because I had created two Regex objects which would find
the same Matches, but did not have identical patterns (the same set of
characters, but in different orders with []). I am not surprised that the two
different Regex objects are not evaluated for equivalence, but it brings up
the question of how Assert determines equality for Reference types? Does
anyone know?

Thanks,
Ethan
 
The default equality comparison for all reference types is identity
comparison -- are A and B actually the same instance. MSDN indicates that
Regex does not override Equals(), so it's an identity comparison. Contrast
that to String, which does override Equals() and compares 2 strings based on
their respective values.
 
Ethan,

The AreEqual method on the Assert class is going to call Equals at some
point. This is different from the AreSame method, which is going to check
for object reference equality.

With the RegEx class, there is no concept of value equality. The Equals
method is inherited from Object, and the implementation on Object is going
to perform a reference equality check. So even if you have two RegEx
instances with the same pattern, I don't believe that passing one to the
Equals method of the other will return true.

Unfortunately, there is no overload of the AreEqual method which will
take a IComparer<T> (or IComparer) where you could define your own equality
semantics. The best you could do in this situation is check for equality
yourself, and use the IsTrue and IsFalse methods, or the Fail/Inconclsive
methods to indicate pass or fail in your unit test.
 
Thanks for the answers. For my Regex I ended up doing a comparison of the
patterns strings and that works fine for my purpose, but I am surprised that
AreEqual does not do something moresuitable than a reference identity
comparison. I would think that would rarely be true for a meaningful test.
Ethan

Ethan Strauss said:
This is mostly just curiousity. I was writting a unit test to verify that
I
was creating a Regex correctly. This involved
Assert.AreEqual(expected, actual)
where expected and actual are both Regex objects. My code failed the test
:-(, but I think it because I had created two Regex objects which would
find
the same Matches, but did not have identical patterns (the same set of
characters, but in different orders with []). I am not surprised that the
two
different Regex objects are not evaluated for equivalence, but it brings
up
the question of how Assert determines equality for Reference types? Does
anyone know?

Thanks,
Ethan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top