shekharpro / mb-unit

Automatically exported from code.google.com/p/mb-unit
0 stars 0 forks source link

Assert.AreEqual<T> implementation improvement #426

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
There's what I consider to be a glitch in the way overloaded methods are 
resolved when generics are involved. Your current implementation of 
Assert.AreEqual<T> seems to always resolve to the object.Equals method. 
Try changing the implementation to:

public static bool AreEqual<T>(T left, T right) {
  return typeof(IEquatable<T>).IsAssignableFrom(typeof(T)) 
    ? ((IEquatable<T>) left).Equals(right) 
    : left.Equals(right);
}

This will use the IEquatable<T> overload of the Equals method if the class 
implements the interface; otherwise object.Equals will be used.

And, yes I know the simple workaround is to override object.Equals in the 
class.

Original issue reported on code.google.com by stephen....@merricksystems.com on 17 Apr 2009 at 1:57

GoogleCodeExporter commented 8 years ago
Thank you Stephen,

MbUnit 3.0.7 will include a structure equality comparer (see issue #424) that 
will 
make possible to define custom equality strategies on complex objects that do 
not 
implement IEquatable.

In the meantime, another solution (rather than actually implementing 
IEquatable) is 
to use the Assert.Equal<T> overload which takes a Func<T, T, bool> as 
additional 
argument.

Original comment by Yann.Tre...@gmail.com on 18 Apr 2009 at 8:18

GoogleCodeExporter commented 8 years ago
StructualEqualityComparer<T> implemented in MbUnit v3.0.7.

Original comment by Yann.Tre...@gmail.com on 25 Apr 2009 at 8:51