1
- using System ;
2
- using System . Collections . Generic ;
3
- using System . Linq ;
4
- using System . Reflection ;
5
- using AutoFixture ;
6
- using AutoFixture . Kernel ;
1
+ namespace Atc . Test . Customizations ;
7
2
8
- namespace Atc . Test . Customizations
3
+ /// <summary>
4
+ /// Responsible for registering customizations with the <see cref="AutoRegisterAttribute"/> specified.
5
+ /// </summary>
6
+ public class AutoRegisterCustomization : ICustomization
9
7
{
10
- /// <summary>
11
- /// Responsible for registering customizations with the <see cref="AutoRegisterAttribute"/> specified.
12
- /// </summary>
13
- public class AutoRegisterCustomization : ICustomization
14
- {
15
- private static Type [ ] ? autoRegisterTypes ;
8
+ private static Type [ ] ? autoRegisterTypes ;
16
9
17
- /// <inheritdoc/>
18
- public void Customize ( IFixture fixture )
10
+ /// <inheritdoc/>
11
+ public void Customize ( IFixture fixture )
12
+ {
13
+ if ( fixture is null )
19
14
{
20
- if ( fixture is null )
21
- {
22
- throw new ArgumentNullException ( nameof ( fixture ) ) ;
23
- }
15
+ throw new ArgumentNullException ( nameof ( fixture ) ) ;
16
+ }
24
17
25
- if ( autoRegisterTypes is null )
26
- {
27
- autoRegisterTypes = AppDomain . CurrentDomain
28
- . GetAssemblies ( )
29
- . SelectMany ( GetLoadableTypes )
30
- . Where ( HasAutoRegisterAttribute )
31
- . ToArray ( ) ;
32
- }
18
+ if ( autoRegisterTypes is null )
19
+ {
20
+ autoRegisterTypes = AppDomain . CurrentDomain
21
+ . GetAssemblies ( )
22
+ . SelectMany ( GetLoadableTypes )
23
+ . Where ( HasAutoRegisterAttribute )
24
+ . ToArray ( ) ;
25
+ }
33
26
34
- foreach ( var type in autoRegisterTypes )
27
+ foreach ( var type in autoRegisterTypes )
28
+ {
29
+ var customization = Activator . CreateInstance ( type ) ;
30
+ switch ( customization )
35
31
{
36
- var customization = Activator . CreateInstance ( type ) ;
37
- switch ( customization )
38
- {
39
- case ICustomization c :
40
- fixture . Customize ( c ) ;
41
- break ;
32
+ case ICustomization c :
33
+ fixture . Customize ( c ) ;
34
+ break ;
42
35
43
- case ISpecimenBuilder b :
44
- fixture . Customizations . Add ( b ) ;
45
- break ;
36
+ case ISpecimenBuilder b :
37
+ fixture . Customizations . Add ( b ) ;
38
+ break ;
46
39
47
- default :
48
- throw new NotSupportedException (
49
- $ "Invalid type { type . Name } . Only ICustomization and " +
50
- $ "ISpecimenBuilder is supported for the " +
51
- $ "AutoRegisterAttribute.") ;
52
- }
40
+ default :
41
+ throw new NotSupportedException (
42
+ $ "Invalid type { type . Name } . Only ICustomization and " +
43
+ $ "ISpecimenBuilder is supported for the " +
44
+ $ "AutoRegisterAttribute.") ;
53
45
}
54
46
}
47
+ }
55
48
56
- private static bool HasAutoRegisterAttribute ( Type type )
57
- => type . GetCustomAttributes (
58
- typeof ( AutoRegisterAttribute ) ,
59
- inherit : false ) . Length > 0 ;
49
+ private static bool HasAutoRegisterAttribute ( Type type )
50
+ => type . GetCustomAttributes (
51
+ typeof ( AutoRegisterAttribute ) ,
52
+ inherit : false ) . Length > 0 ;
60
53
61
- private static IEnumerable < Type > GetLoadableTypes ( Assembly assembly )
54
+ private static IEnumerable < Type > GetLoadableTypes ( Assembly assembly )
55
+ {
56
+ try
62
57
{
63
- try
64
- {
65
- return assembly . GetTypes ( ) ;
66
- }
67
- catch ( ReflectionTypeLoadException e )
68
- {
69
- return e . Types ? . OfType < Type > ( )
70
- ?? Enumerable . Empty < Type > ( ) ;
71
- }
58
+ return assembly . GetTypes ( ) ;
59
+ }
60
+ catch ( ReflectionTypeLoadException e )
61
+ {
62
+ return e . Types ? . OfType < Type > ( )
63
+ ?? Enumerable . Empty < Type > ( ) ;
72
64
}
73
65
}
74
66
}
0 commit comments