11namespace TestStack . ConventionTests . Conventions
22{
3+ using System ;
4+ using System . Collections . Generic ;
35 using System . Linq ;
6+ using System . Reflection ;
7+ using System . Text ;
48 using TestStack . ConventionTests . Helpers ;
9+ using TestStack . ConventionTests . Internal ;
510
6- public class AllMethodsAreVirtual : ConventionData
11+ public class AllMethodsAreVirtual : IConvention < Types >
712 {
813 public AllMethodsAreVirtual ( )
914 {
10- Must = t => t . NonVirtualMethods ( ) . None ( ) ;
11- ItemDescription = ( type , builder ) =>
15+ HeaderMessage = "The following methods are not virtual." ;
16+ }
17+
18+ public ConventionResult Execute ( Types data )
19+ {
20+ var types = data . ApplicableTypes ;
21+ if ( types . None ( ) )
1222 {
13- builder . Append ( type . FullName ) ;
14- builder . AppendLine ( " has non virtual method(s):" ) ;
15- foreach ( var nonVirtual in type . NonVirtualMethods ( ) )
16- {
17- builder . Append ( '\t ' ) ;
18- builder . AppendLine ( nonVirtual . Name ) ;
19- }
20- } ;
23+ return ConventionResult . Inconclusive ( "Put sensible 'inconclusive' message here" ) ;
24+ }
25+
26+ // do we want to encapsulate that in some way?
27+ // also notice how data gives us types, yet the convention acts upon methods.
28+ var invalid = types . ToLookup ( t => t , t => t . NonVirtualMethods ( ) ) . Where ( l => l . Any ( ) ) ;
29+ var result = ConventionResult . For ( invalid , HeaderMessage , DescribeTypeAndMethods ) ;
30+ result . HasExceptions = data . HasApprovedExceptions ;
31+ return result ;
2132 }
33+
34+ // I like how that's encapsulated in the reusable convention type, whereas previously it was part of the convention/test code
35+ void DescribeTypeAndMethods ( IGrouping < Type , IEnumerable < MethodInfo > > item , StringBuilder message )
36+ {
37+ message . AppendLine ( "\t " + item . Key ) ;
38+ foreach ( var method in item )
39+ {
40+ message . AppendLine ( "\t \t " + method ) ;
41+ }
42+ }
43+
44+ public string HeaderMessage { get ; set ; }
2245 }
2346}
0 commit comments