11using System ;
2+ using System . IO ;
23using System . Linq ;
34using System . Reflection ;
45using ApprovalTests . Reporters ;
89
910namespace ConventionTests
1011{
11- [ TestFixture ]
12- public class ConventionTestsRunner
13- {
14- [ TestFixtureSetUp ]
15- public static void GlobalSetUp ( )
16- {
17- }
18-
19- public TestCaseData [ ] Conventions
20- {
21- get
22- {
23- var types = GetConventionTypes ( ) ;
24- var conventionTests = Array . ConvertAll ( types , BuildTestData ) ;
25- return conventionTests ;
26- }
27- }
28-
29- private TestCaseData BuildTestData ( Type t )
30- {
31- var convention = CreateConvention ( t ) ;
32- return new TestCaseData ( convention ) . SetName ( convention . Name ) ;
33- }
34-
35- private static IConventionTest CreateConvention ( Type t )
36- {
37- return ( IConventionTest ) Activator . CreateInstance ( t ) ;
38- }
39-
40- private static Type [ ] GetConventionTypes ( )
41- {
42- var types =
43- Assembly . GetExecutingAssembly ( ) . GetExportedTypes ( ) . Where (
44- IsConventionTest ) . ToArray ( ) ;
45- return types ;
46- }
47-
48- private static bool IsConventionTest ( Type type )
49- {
50- return type . IsClass && type . IsAbstract == false && typeof ( IConventionTest ) . IsAssignableFrom ( type ) ;
51- }
52-
53- [ Test ]
54- [ TestCaseSource ( "Conventions" ) ]
55- public void Run ( IConventionTest test )
56- {
57- test . Execute ( ) ;
58- }
59- }
12+ [ TestFixture ]
13+ public class ConventionTestsRunner
14+ {
15+ [ TestFixtureSetUp ]
16+ public static void GlobalSetUp ( )
17+ {
18+ }
19+
20+ public TestCaseData [ ] Conventions
21+ {
22+ get
23+ {
24+ var types = GetConventionTypes ( ) ;
25+ var conventionTests = Array . ConvertAll ( types , BuildTestData ) ;
26+ return conventionTests ;
27+ }
28+ }
29+
30+ private TestCaseData BuildTestData ( Type t )
31+ {
32+ var convention = CreateConvention ( t ) ;
33+ return new TestCaseData ( convention ) . SetName ( convention . Name ) ;
34+ }
35+
36+ private static IConventionTest CreateConvention ( Type t )
37+ {
38+ return ( IConventionTest ) Activator . CreateInstance ( t ) ;
39+ }
40+
41+ private static Type [ ] GetConventionTypes ( )
42+ {
43+ var types =
44+ Assembly . GetExecutingAssembly ( ) . GetExportedTypes ( ) . Where (
45+ IsConventionTest ) . ToArray ( ) ;
46+ return types ;
47+ }
48+
49+ private static bool IsConventionTest ( Type type )
50+ {
51+ return type . IsClass && type . IsAbstract == false && typeof ( IConventionTest ) . IsAssignableFrom ( type ) ;
52+ }
53+
54+ [ Test ]
55+ [ TestCaseSource ( "Conventions" ) ]
56+ public void Run ( IConventionTest test )
57+ {
58+ test . Execute ( ) ;
59+ }
60+ }
61+
62+ public static class ReflectionExtensions
63+ {
64+ public static Assembly TryLoadAssembly ( this AssemblyName name )
65+ {
66+ try
67+ {
68+ return Assembly . Load ( name ) ;
69+ }
70+ catch ( FileNotFoundException )
71+ {
72+ return null ;
73+ }
74+ catch ( FileLoadException )
75+ {
76+ return null ;
77+ }
78+ catch ( BadImageFormatException )
79+ {
80+ return null ;
81+ }
82+ catch ( ReflectionTypeLoadException )
83+ {
84+ return null ;
85+ }
86+ }
87+
88+ public static Type [ ] SafeGetTypes ( this Assembly assembly )
89+ {
90+ try
91+ {
92+ return assembly . GetTypes ( ) ;
93+ }
94+ catch ( ReflectionTypeLoadException ex )
95+ {
96+ return Array . FindAll ( ex . Types , x => x != null ) ;
97+ }
98+ }
99+ }
60100}
0 commit comments