Created
December 15, 2015 15:16
-
-
Save mikehadlow/e28dd552a6d343705092 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; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Xunit; | |
namespace MyProject.Tests | |
{ | |
public class AsyncNamingConventionTests | |
{ | |
[Fact] | |
public void AsyncMethodNamesShouldBePostfixedWithAsync() | |
{ | |
var webAssembly = typeof(Program).Assembly; | |
var incorrectlyNamedMethods = webAssembly | |
.GetTypes() | |
.SelectMany(x => x.GetMethods()) | |
.Where(x => x.ReturnType == typeof(Task) || x.ReturnType.IsGenericTask()) | |
.Where(x => !x.Name.EndsWith("Async")); | |
if(incorrectlyNamedMethods.Any()) | |
{ | |
foreach(var method in incorrectlyNamedMethods) | |
{ | |
Console.WriteLine($"{method.DeclaringType.Name}.cs(0,0): {method.Name}"); | |
} | |
Assert.True(false, "Async method names do not end with Async. See test output for details."); | |
} | |
} | |
} | |
public static class TypeExtension | |
{ | |
public static bool IsGenericTask(this Type type) | |
{ | |
return | |
type.IsGenericType && | |
type.GetGenericTypeDefinition() == typeof(Task<>); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment