File tree Expand file tree Collapse file tree 4 files changed +90
-0
lines changed
TestStack.ConventionTests Expand file tree Collapse file tree 4 files changed +90
-0
lines changed Original file line number Diff line number Diff line change 1+ namespace TestAssembly . Controllers
2+ {
3+ public class TestApiController
4+ {
5+
6+ }
7+ }
Original file line number Diff line number Diff line change 1+ namespace TestStack . ConventionTests . Conventions
2+ {
3+ using System ;
4+ using System . Linq ;
5+ using TestStack . ConventionTests . ConventionData ;
6+
7+ public class ApiControllerConvention : IConvention < Types >
8+ {
9+ public void Execute ( Types data , IConventionResult result )
10+ {
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 ) ;
30+ }
31+
32+ static bool IsWebApiController ( Type arg )
33+ {
34+ var isController = arg . FullName == "System.Web.Http.ApiController" ;
35+ if ( arg . BaseType == null )
36+ return isController ;
37+ return isController || IsWebApiController ( arg . BaseType ) ;
38+ }
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ namespace TestStack . ConventionTests . Conventions
2+ {
3+ using System ;
4+ using System . Linq ;
5+ using TestStack . ConventionTests . ConventionData ;
6+
7+ public class MvcControllerConvention : IConvention < Types >
8+ {
9+ public void Execute ( Types data , IConventionResult result )
10+ {
11+ var controllers = data . TypesToVerify . Where ( IsMvcController ) ;
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+ "Mvc 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 ) ;
30+ }
31+
32+ static bool IsWebApiController ( Type arg )
33+ {
34+ var isController = arg . FullName == "System.Web.Http.ApiController" ;
35+ if ( arg . BaseType == null )
36+ return isController ;
37+ return isController || IsWebApiController ( arg . BaseType ) ;
38+ }
39+ }
40+ }
Original file line number Diff line number Diff line change 6161 <Compile Include =" ConventionData\TypeExtensions.cs" />
6262 <Compile Include =" ConventionFailedException.cs" />
6363 <Compile Include =" IConventionResultContext.cs" />
64+ <Compile Include =" Conventions\ApiControllerConvention.cs" />
65+ <Compile Include =" Conventions\MvcControllerConvention.cs" />
66+ <Compile Include =" IConventionResult.cs" />
6467 <Compile Include =" Internal\ConventionReportFailure.cs" />
6568 <Compile Include =" Internal\ConventionContext.cs" />
6669 <Compile Include =" Internal\ConventionResult.cs" />
You can’t perform that action at this time.
0 commit comments