Skip to content

Instantly share code, notes, and snippets.

@dotMorten
Last active December 21, 2015 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotMorten/6234641 to your computer and use it in GitHub Desktop.
Save dotMorten/6234641 to your computer and use it in GitHub Desktop.
Unit test that checks that Task-returning methods are postfixed 'Async"
[TestMethod]
[TestCategory("Naming guidelines")]
public void TasksReturningMethodsArePostfixedAsync()
{
var asm = typeof(SomeTypeInMyAssembly).Assembly;
StringBuilder sb = new StringBuilder();
foreach (var type in asm.GetExportedTypes())
{
foreach (var mi in type.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.DeclaredOnly))
{
if (mi.IsPublic || mi.IsFamilyOrAssembly || mi.IsFamily) //public or protected internal or protected
{
if (mi.ReturnType == typeof(Task) ||
mi.ReturnType.IsGenericType && mi.ReturnType.Name.StartsWith(typeof(Task).FullName + "`"))
{
if (!mi.Name.EndsWith("Async"))
sb.AppendFormat("{0}.{1}\n", type.FullName, mi.Name);
}
}
}
}
Assert.IsTrue(sb.Length == 0,
"Naming violation. The following Task-returning methods are not postfixed 'Async':\n" + sb.ToString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment