Permalink
Browse files

Adding test for non-public controllers

These are controllers that meet the naming and inheritance
requirements, but are either nested or non-public.
  • Loading branch information...
1 parent 21ea7f5 commit 90dc4b6101fb0eb70b5a13b0f188f7ed2a451828 @Haacked committed Jul 21, 2012
@@ -11,12 +11,12 @@ namespace ControllerInspectorTests
{
public class ControllerValidatorTests
{
- public class TheGetControllersThatViolateConventionMethod
+ public class TheGetUnconventionalControllersMethod
{
[Fact]
public void FindsControllersThatDoNotMeetConvention()
{
- var badControllers = typeof (ControllerValidatorTests).GetControllersThatViolateConvention().ToList();
+ var badControllers = typeof (ControllerValidatorTests).GetUnconventionalControllers().ToList();
foreach (var bad in badControllers)
Console.Write(bad.Name);
@@ -28,6 +28,23 @@ public void FindsControllersThatDoNotMeetConvention()
Assert.True(badControllers.Contains(typeof (NotOk)));
}
}
+
+ public class TheGetNonPublicControllers
+ {
+ [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)));
+ }
+ }
}
internal static class MediumTrustContext
@@ -91,5 +108,16 @@ public class OkController : NotInstantiable
public class NotOk : NotInstantiable
{
+ public class NestedController : Controller
+ {
+ }
+ }
+
+ internal class PrivateController : Controller
+ {
+ }
+
+ internal class InternalController : Controller
+ {
}
}
@@ -10,11 +10,9 @@ public class HomeController : Controller
{
public ActionResult Index(string id)
{
- var nonConventionalControllers = typeof (HomeController).Assembly.GetControllersThatViolateConvention();
-
dynamic model = new ExpandoObject();
- model.NonConventionalControllers = nonConventionalControllers.ToList();
-
+ model.NonConventionalControllers = typeof (HomeController).GetUnconventionalControllers().ToList();
+ model.NonPublicControllers = typeof (HomeController).GetNonPublicControllers().ToList();
return View(model);
}
@@ -16,4 +16,8 @@ public class BadNestedController : Controller
{
}
}
+
+ internal class InternalController : Controller
+ {
+ }
}
@@ -16,3 +16,11 @@
<li>@type.Name</li>
}
</ul>
+
+<h2>Controllers that cannot be instantiated</h2>
+<ul>
+@foreach (Type type in Model.NonPublicControllers)
+{
+ <li>@type.Name</li>
+}
+</ul>
@@ -10,18 +10,30 @@ public static class ControllerValidator
{
public static bool AllControllersMeetConvention(this Assembly assembly)
{
- return assembly.GetControllersThatViolateConvention().Any();
+ return assembly.GetUnconventionalControllers().Any();
}
- public static IEnumerable<Type> GetControllersThatViolateConvention(this Type typeInAssembly)
+ public static IEnumerable<Type> GetUnconventionalControllers(this Type typeInAssembly)
{
- return typeInAssembly.Assembly.GetControllersThatViolateConvention();
+ return typeInAssembly.Assembly.GetUnconventionalControllers();
}
- public static IEnumerable<Type> GetControllersThatViolateConvention(this Assembly assembly)
+ public static IEnumerable<Type> GetUnconventionalControllers(this Assembly assembly)
{
return from t in assembly.GetLoadableTypes()
- where t.IsControllerType() && !t.MeetsConvention()
+ where t.IsPublicClass() && t.IsControllerType() && !t.MeetsConvention()
+ select t;
+ }
+
+ public static IEnumerable<Type> GetNonPublicControllers(this Type typeInAssembly)
+ {
+ return typeInAssembly.Assembly.GetNonPublicControllers();
+ }
+
+ public static IEnumerable<Type> GetNonPublicControllers(this Assembly assembly)
+ {
+ return from t in assembly.GetLoadableTypes()
+ where !t.IsPublicClass() && t.IsControllerType() && t.MeetsConvention()
select t;
}
@@ -30,12 +42,9 @@ private static bool IsPublicClass(this Type type)
return (type != null && type.IsPublic && type.IsClass && !type.IsAbstract);
}
-
internal static bool IsControllerType(this Type t)
{
- return
- t.IsPublicClass() &&
- typeof (IController).IsAssignableFrom(t);
+ return typeof (IController).IsAssignableFrom(t);
}
public static bool MeetsConvention(this Type t)

0 comments on commit 90dc4b6

Please sign in to comment.