1
1
namespace TestStack . ConventionTests . Conventions
2
2
{
3
+ using System ;
4
+ using System . Collections . Generic ;
3
5
using System . Linq ;
6
+ using System . Reflection ;
7
+ using System . Text ;
4
8
using TestStack . ConventionTests . Helpers ;
9
+ using TestStack . ConventionTests . Internal ;
5
10
6
- public class AllMethodsAreVirtual : ConventionData
11
+ public class AllMethodsAreVirtual : IConvention < Types >
7
12
{
8
13
public AllMethodsAreVirtual ( )
9
14
{
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 ( ) )
12
22
{
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 ;
21
32
}
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 ; }
22
45
}
23
46
}
0 commit comments