Created
May 27, 2014 04:32
-
-
Save JayBazuzi/ffb7daa9450b2a647302 to your computer and use it in GitHub Desktop.
Asserts that this type correctly implements "value" equality semantics.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class ValueTypeAssertions | |
{ | |
class C | |
{ | |
} | |
/// <summary> | |
/// Asserts that this type correctly implements "value" equality semantics. | |
/// </summary> | |
/// <param name="a">An example of T</param> | |
/// <param name="aPrime">Another T which should be the "same" as a</param> | |
/// <param name="b">Another T which should be different from a</param> | |
public static void HasValueEquality<T>(T a, T aPrime, T b) | |
{ | |
Assert.IsTrue(a.Equals(aPrime)); | |
Assert.IsTrue(a.GetHashCode().Equals(aPrime.GetHashCode())); | |
Assert.IsFalse(a.Equals(b)); | |
Assert.IsFalse(a.GetHashCode().Equals(b.GetHashCode())); | |
Assert.IsFalse(a.Equals(null)); | |
Assert.IsFalse(a.Equals(new C())); | |
Assert.AreEqual(a, aPrime); | |
Assert.AreNotEqual(a, b); | |
} | |
} |
@bgeihsgt and @dennisdoomen: Here's another iteration. I split out equality from inequality, so you can deal with case-insensitive stuff:
new Foo("bar")
should equalnew Foo("BAR")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I see that, but FA usually takes two objects and executes an assertion on that, but your gist uses 4 objects. Value Equality in .NET means that two objects return
true
forEquals
, return the same value forGetHashCode
even thoughReferenceEquals
returnsfalse
. I could have an extension method that takes a factory lamba like @bgeihsgt just showed.