Skip to content

Commit 2ac3039

Browse files
author
Jake Ginnivan
committed
Simplified ApiControllerConvention by inheiriting from MvcControllerConvention
1 parent c5d702d commit 2ac3039

File tree

5 files changed

+24
-34
lines changed

5 files changed

+24
-34
lines changed

TestStack.ConventionTests.Tests/MvcConventions.api_controller_conventions.approved.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Failed: 'WebApi controllers must be suffixed with Controller' for 'TestAssembly'
2-
--------------------------------------------------------------------------------
1+
Failed: 'Api Controllers must be suffixed with Controller' for 'TestAssembly'
2+
-----------------------------------------------------------------------------
33

44
TestAssembly.Controllers.BarApiControler
55

TestStack.ConventionTests.Tests/MvcConventions.controller_conventions.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Failed: 'Mvc controllers must be suffixed with Controller' for 'TestAssembly'
1+
Failed: 'Mvc Controllers must be suffixed with Controller' for 'TestAssembly'
22
-----------------------------------------------------------------------------
33

44
TestAssembly.Controllers.FooControler
Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,20 @@
11
namespace TestStack.ConventionTests.Conventions
22
{
33
using System;
4+
using System.Collections.Generic;
45
using System.Linq;
56
using TestStack.ConventionTests.ConventionData;
67

7-
public class ApiControllerConvention : IConvention<Types>
8+
public class ApiControllerConvention : MvcControllerConvention
89
{
9-
public void Execute(Types data, IConventionResult result)
10+
protected override IEnumerable<Type> GetControllers(Types data)
1011
{
11-
var controllers = data.TypesToVerify.Where(IsWebApiController);
12-
var typesWhichDoNotEndInController = controllers.Where(c => !c.Name.EndsWith("Controller"));
13-
14-
var typesWhichEndInController = data.TypesToVerify.Where(t => t.Name.EndsWith("Controller"));
15-
var controllersWhichDoNotInheritFromController =
16-
typesWhichEndInController.Where(t => !IsMvcController(t) && !IsWebApiController(t));
17-
18-
result.IsSymmetric(
19-
"WebApi controllers must be suffixed with Controller", typesWhichDoNotEndInController,
20-
"Types named *Controller must inherit from ApiController or Controller",
21-
controllersWhichDoNotInheritFromController);
22-
}
23-
24-
static bool IsMvcController(Type arg)
25-
{
26-
var isController = arg.FullName == "System.Web.Mvc.Controller";
27-
if (arg.BaseType == null)
28-
return isController;
29-
return isController || IsMvcController(arg.BaseType);
12+
return data.TypesToVerify.Where(IsWebApiController);
3013
}
3114

32-
static bool IsWebApiController(Type arg)
15+
protected override string GetControllerTypeName()
3316
{
34-
var isController = arg.FullName == "System.Web.Http.ApiController";
35-
if (arg.BaseType == null)
36-
return isController;
37-
return isController || IsWebApiController(arg.BaseType);
17+
return "Api Controller";
3818
}
3919
}
4020
}

TestStack.ConventionTests/Conventions/MvcControllerConvention.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
namespace TestStack.ConventionTests.Conventions
22
{
33
using System;
4+
using System.Collections.Generic;
45
using System.Linq;
56
using TestStack.ConventionTests.ConventionData;
67

78
public class MvcControllerConvention : IConvention<Types>
89
{
9-
public void Execute(Types data, IConventionResult result)
10+
public void Execute(Types data, IConventionResultContext result)
1011
{
11-
var controllers = data.TypesToVerify.Where(IsMvcController);
12+
var controllers = GetControllers(data);
1213
var typesWhichDoNotEndInController = controllers.Where(c => !c.Name.EndsWith("Controller"));
1314

1415
var typesWhichEndInController = data.TypesToVerify.Where(t => t.Name.EndsWith("Controller"));
1516
var controllersWhichDoNotInheritFromController =
1617
typesWhichEndInController.Where(t => !IsMvcController(t) && !IsWebApiController(t));
1718

1819
result.IsSymmetric(
19-
"Mvc controllers must be suffixed with Controller", typesWhichDoNotEndInController,
20+
GetControllerTypeName() + "s must be suffixed with Controller", typesWhichDoNotEndInController,
2021
"Types named *Controller must inherit from ApiController or Controller",
2122
controllersWhichDoNotInheritFromController);
2223
}
2324

25+
protected virtual string GetControllerTypeName()
26+
{
27+
return "Mvc Controller";
28+
}
29+
30+
protected virtual IEnumerable<Type> GetControllers(Types data)
31+
{
32+
return data.TypesToVerify.Where(IsMvcController);
33+
}
34+
2435
static bool IsMvcController(Type arg)
2536
{
2637
var isController = arg.FullName == "System.Web.Mvc.Controller";
@@ -29,7 +40,7 @@ static bool IsMvcController(Type arg)
2940
return isController || IsMvcController(arg.BaseType);
3041
}
3142

32-
static bool IsWebApiController(Type arg)
43+
protected static bool IsWebApiController(Type arg)
3344
{
3445
var isController = arg.FullName == "System.Web.Http.ApiController";
3546
if (arg.BaseType == null)

TestStack.ConventionTests/TestStack.ConventionTests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
<Compile Include="IConventionResultContext.cs" />
6464
<Compile Include="Conventions\ApiControllerConvention.cs" />
6565
<Compile Include="Conventions\MvcControllerConvention.cs" />
66-
<Compile Include="IConventionResult.cs" />
6766
<Compile Include="Internal\ConventionReportFailure.cs" />
6867
<Compile Include="Internal\ConventionContext.cs" />
6968
<Compile Include="Internal\ConventionResult.cs" />

0 commit comments

Comments
 (0)