Skip to content

Instantly share code, notes, and snippets.

@ElemarJR
Last active December 13, 2015 23:08
Show Gist options
  • Select an option

  • Save ElemarJR/4989116 to your computer and use it in GitHub Desktop.

Select an option

Save ElemarJR/4989116 to your computer and use it in GitHub Desktop.
[Test]
public void ExpressionsWithChildrenShouldOverrideVisitMethod()
{
var expressionTypes = typeof(IExpression)
.Assembly.GetTypes()
.Where(t => typeof(IExpression).IsAssignableFrom(t))
.Where(t => !t.IsInterface);
var withChildren = expressionTypes
.Where(t => t.GetFields(BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.NonPublic |
BindingFlags.Public)
.Any(f => typeof(IExpression).IsAssignableFrom(f.FieldType)));
var withChildrenAndNoVisitOverride = withChildren
.Where(t => t.GetMethod("Visit").DeclaringType != t)
.Select(t => t.Name)
.ToArray()
;
if (withChildrenAndNoVisitOverride.Length > 0)
{
string names = withChildrenAndNoVisitOverride.Length > 1 ?
withChildrenAndNoVisitOverride.Aggregate((a, b) => string.Format("{0}, {1}", a, b)) :
withChildrenAndNoVisitOverride.First();
Assert.Fail(string.Format("Expressions with children ({0}) should override the Visit method",
names));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment