Last active
December 21, 2015 02:19
-
-
Save dotMorten/6234641 to your computer and use it in GitHub Desktop.
Unit test that checks that Task-returning methods are postfixed 'Async"
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
[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