Skip to content

Instantly share code, notes, and snippets.

@mikehadlow
Created December 15, 2015 15:16
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 mikehadlow/e28dd552a6d343705092 to your computer and use it in GitHub Desktop.
Save mikehadlow/e28dd552a6d343705092 to your computer and use it in GitHub Desktop.
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