Permalink
Browse files

Detect non controller derived controller suffixes

Finds classes named with a "Controller" suffix that do not derive from
IController.
  • Loading branch information...
1 parent 90dc4b6 commit ad49a16772e0af00878de78144533e437ea5fde4 @Haacked committed Jul 25, 2012
@@ -18,8 +18,6 @@ public void FindsControllersThatDoNotMeetConvention()
{
var badControllers = typeof (ControllerValidatorTests).GetUnconventionalControllers().ToList();
- foreach (var bad in badControllers)
- Console.Write(bad.Name);
Assert.Equal(4, badControllers.Count);
Assert.True(badControllers.Contains(typeof (Bad)));
@@ -29,22 +27,37 @@ public void FindsControllersThatDoNotMeetConvention()
}
}
- public class TheGetNonPublicControllers
+ public class TheGetNonPublicControllersMethod
{
[Fact]
public void FindsControllersThatDoNotMeetConvention()
{
var badControllers = typeof (ControllerValidatorTests).Assembly.GetNonPublicControllers().ToList();
- foreach (var bad in badControllers)
- Console.Write(bad.Name);
Assert.Equal(3, badControllers.Count);
Assert.True(badControllers.Contains(typeof (NotOk.NestedController)));
Assert.True(badControllers.Contains(typeof (InternalController)));
Assert.True(badControllers.Contains(typeof (PrivateController)));
}
}
+
+ public class TheGetNonControllersNamedWithControllerSuffixMethod
+ {
+ [Fact]
+ public void FindsClassesWithControllerSuffixButAreNotControllers()
+ {
+ var badControllers = typeof (ControllerValidatorTests).Assembly.GetNonControllersNamedWithControllerSuffix().ToList();
+
+ Assert.Equal(1, badControllers.Count);
+
+ Assert.True(badControllers.Contains(typeof (FakeController)));
+ }
+ }
+ }
+
+ public class FakeController
+ {
}
internal static class MediumTrustContext
@@ -13,6 +13,7 @@ public ActionResult Index(string id)
dynamic model = new ExpandoObject();
model.NonConventionalControllers = typeof (HomeController).GetUnconventionalControllers().ToList();
model.NonPublicControllers = typeof (HomeController).GetNonPublicControllers().ToList();
+ model.NonControllers = typeof (HomeController).GetNonControllersNamedWithControllerSuffix().ToList();
return View(model);
}
@@ -0,0 +1,6 @@
+namespace MvcHaack.ControllerInspector.DemoWeb.Controllers
+{
+ public class MistakeController
+ {
+ }
+}
@@ -62,6 +62,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Controllers\HomeController.cs" />
+ <Compile Include="Controllers\MistakeController.cs" />
<Compile Include="Controllers\TypoControler.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
@@ -19,7 +19,15 @@
<h2>Controllers that cannot be instantiated</h2>
<ul>
-@foreach (Type type in Model.NonPublicControllers)
+ @foreach (Type type in Model.NonPublicControllers)
+ {
+ <li>@type.Name</li>
+ }
+</ul>
+
+<h2>Controllers that do not derive from controller</h2>
+<ul>
+@foreach (Type type in Model.NonControllers)
{
<li>@type.Name</li>
}
@@ -37,17 +37,29 @@ public static IEnumerable<Type> GetNonPublicControllers(this Assembly assembly)
select t;
}
- private static bool IsPublicClass(this Type type)
+ public static IEnumerable<Type> GetNonControllersNamedWithControllerSuffix(this Type typeInAssembly)
+ {
+ return typeInAssembly.Assembly.GetNonControllersNamedWithControllerSuffix();
+ }
+
+ public static IEnumerable<Type> GetNonControllersNamedWithControllerSuffix(this Assembly assembly)
+ {
+ return from t in assembly.GetLoadableTypes()
+ where t.IsPublicClass() && !t.IsControllerType() && t.MeetsConvention()
+ select t;
+ }
+
+ static bool IsPublicClass(this Type type)
{
return (type != null && type.IsPublic && type.IsClass && !type.IsAbstract);
}
- internal static bool IsControllerType(this Type t)
+ static bool IsControllerType(this Type t)
{
return typeof (IController).IsAssignableFrom(t);
}
- public static bool MeetsConvention(this Type t)
+ static bool MeetsConvention(this Type t)
{
return t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase);
}

0 comments on commit ad49a16

Please sign in to comment.