Created
October 1, 2009 18:56
-
-
Save bennage/199160 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using Xunit; | |
public class When_overriding_GetHashCode | |
{ | |
[Fact] | |
public void Equals_should_be_overridden() | |
{ | |
var dlls = new List<Assembly> | |
{ | |
Assembly.Load("some.assembly"), | |
Assembly.Load("another.assembly"), | |
}; | |
var missing = from assembly in dlls | |
from type in assembly.GetTypes() | |
let methods = type.GetMethods() | |
where methods.Any(x => x.Name == "GetHashCode" && x.DeclaringType == type) | |
&& !methods.Any(x => x.Name == "Equals" && x.DeclaringType == type) | |
select new {Type = type, Assembly = assembly}; | |
var messages = from item in missing | |
select | |
string.Format( | |
"The type {0} overrides GetHashCode() but not override Equals(), in assembly {1}.", | |
item.Type.Name, | |
item.Assembly.GetName()); | |
Assert.True(missing.Count() == 0, string.Join("\n", messages.ToArray())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment