如何在IEqualityComparer上实现单元测试?



我有一个类和一个实现IEqualityComparer的类的比较器:

class Foo
{
public int Int { get; set; }
public string Str { get; set; }
public Foo(int i, string s)
{
Int = i;
Str = s;
}
private sealed class FooEqualityComparer : IEqualityComparer<Foo>
{
public bool Equals(Foo x, Foo y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
if (x.GetType() != y.GetType()) return false;
return x.Int == y.Int && string.Equals(x.Str, y.Str);
}
public int GetHashCode(Foo obj)
{
unchecked
{
return (obj.Int * 397) ^ (obj.Str != null ? obj.Str.GetHashCode() : 0);
}
}
}
public static IEqualityComparer<Foo> Comparer { get; } = new FooEqualityComparer();
}

例如,EqualsGetHashCode两种方法通过比较器的实例在List.Except中使用。

我的问题是:如何在这个比较器上正确实现单元测试?我想检测是否有人在Foo中添加公共属性而不修改比较器,因为在这种情况下,比较器变得无效。

如果我做这样的事情:

Assert.That(new Foo(42, "answer"), Is.EqualTo(new Foo(42, "answer")));

这无法检测到添加了新属性,并且此属性在两个对象中不同。

有什么办法可以做到这一点吗?

如果可能的话,我们可以向属性添加一个属性来说明此属性在比较中不相关吗?

您可以使用反射来获取类型的属性,例如:

var knownPropNames = new string[]
{
"Int", 
"Str", 
};
var props = typeof(Foo).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var unknownProps = props
.Where(x => !knownPropNames.Contains(x.Name))
.Select(x => x.Name)
.ToArray();
// Use assertion instead of Console.WriteLine
Console.WriteLine("Unknown props: {0}", string.Join("; ", unknownProps));

这样,您可以实现在添加任何属性时失败的测试。当然,您必须在开始时向数组添加新属性。由于从性能的角度来看,使用反射是一项昂贵的操作,因此如果您需要比较大量对象,我建议在测试中使用它,而不是在比较器本身中使用它。

另请注意BindingFlags参数的使用,以便您可以将属性限制为仅公共属性和实例级别的属性。

此外,还可以定义用于标记不相关属性的自定义属性。例如:

[AttributeUsage(AttributeTargets.Property)]
public class ComparerIgnoreAttribute : Attribute {}

您可以将其应用于属性:

[ComparerIgnore]
public decimal Dec { get; set; }

此外,还必须扩展发现未知属性的代码:

var unknownProps = props
.Where(x => !knownPropNames.Contains(x.Name) 
&& !x.GetCustomAttributes(typeof(ComparerIgnoreAttribute)).Any())
.Select(x => x.Name)
.ToArray();

基本上,您可以通过反射检查要签入Equals的所有属性。要过滤掉其中一些属性,请使用这些属性的属性:

class Foo
{
[MyAttribute]
public string IgnoredProperty { get; set; }
public string MyProperty { get; set; }
}

现在在您的比较器中检查该特定属性。然后通过PropertyInfo.GetValue比较剩余列表中包含的每个属性

class MyComparer : IEqualityComparer<Foo>
{
public bool Equals(Foo x, Foo y)
{
var properties = this.GetType().GetProperties()
.Where(x => "Attribute.IsDefined(x, typeof(MyAttribute));
var equal = true;
foreach(var p in properties)
equal &= p.GetValue(x, null) == p.GetValue(y, null);
return equal;
}
}

但是,您应该在GetHashCode中进行一些良好的预检查,以避免对这种慢速方法进行不必要的调用。

编辑:正如您提到的ReSharper,我假设当您提供要在运行时验证的实际属性时,即使R#也不知道实现GetHashCode的好方法。您将需要一些属性,这些属性将完全适用于您的类型,并且这些属性可以很好地了解可能被视为相等的内容。然而,所有附加属性只应进入昂贵的Equals方法。

EDIT2:正如评论中提到的,在Equals甚至GetHashCode内进行反思是一个坏主意,因为它通常很慢并且通常可以避免。如果您知道在编译时要检查 eqality 的属性,则应明确地将它们包含在这两个方法中,因为这样做可以为您提供更高的安全性。当你发现自己真的需要这个,因为你有很多属性,你可能有一些基本问题,因为你的类做得太多了。

我想您可以在比较器内检查属性计数。像这样:

private sealed class FooEqualityComparer : IEqualityComparer<Foo>
{
private List<bool> comparisonResults = new List<bool>();
private List<Func<Foo, Foo, bool>> conditions = new List<Func<Foo, Foo, bool>>{
(x, y) => x.Int == y.Int,
(x, y) => string.Equals(x.Str, y.Str)
};
private int propertiesCount = typeof(Foo)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
//.Where(someLogicToExclde(e.g attribute))
.Count();
public bool Equals(Foo x, Foo y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
if (x.GetType() != y.GetType()) return false;   
//has new property which is not presented in the conditions list and not excluded
if (conditions.Count() != propertiesCount) return false;    
foreach(var func in conditions)
if(!func(x, y)) return false;//returns false on first mismatch
return true;//only if all conditions are satisfied
}
public int GetHashCode(Foo obj)
{
unchecked
{
return (obj.Int * 397) ^ (obj.Str != null ? obj.Str.GetHashCode() : 0);
}
}
}

最新更新