1
- using System ;
2
- using System . Collections . Generic ;
3
- using System . Linq ;
4
- using System . Reflection ;
5
- using System . Threading ;
6
- using System . Threading . Tasks ;
7
- using Xunit . Abstractions ;
8
- using Xunit . Sdk ;
9
-
10
- namespace Xunit . Extensions . AssemblyFixture
11
- {
12
- class TestCollectionRunner : XunitTestCollectionRunner
13
- {
14
- readonly Dictionary < Type , object > assemblyFixtureMappings ;
15
- readonly IMessageSink diagnosticMessageSink ;
16
-
17
- public TestCollectionRunner ( Dictionary < Type , object > assemblyFixtureMappings ,
18
- ITestCollection testCollection ,
19
- IEnumerable < IXunitTestCase > testCases ,
20
- IMessageSink diagnosticMessageSink ,
21
- IMessageBus messageBus ,
22
- ITestCaseOrderer testCaseOrderer ,
23
- ExceptionAggregator aggregator ,
24
- CancellationTokenSource cancellationTokenSource )
25
- : base ( testCollection , testCases , diagnosticMessageSink , messageBus , testCaseOrderer , aggregator , cancellationTokenSource )
26
- {
27
- this . assemblyFixtureMappings = assemblyFixtureMappings ;
28
- this . diagnosticMessageSink = diagnosticMessageSink ;
29
- }
30
-
31
- protected override Task < RunSummary > RunTestClassAsync ( ITestClass testClass , IReflectionTypeInfo @class , IEnumerable < IXunitTestCase > testCases )
32
- {
33
- foreach ( var fixtureType in @class . Type . GetTypeInfo ( ) . ImplementedInterfaces
34
- . Where ( i => i . GetTypeInfo ( ) . IsGenericType && i . GetGenericTypeDefinition ( ) == typeof ( IAssemblyFixture < > ) )
35
- . Select ( i => i . GetTypeInfo ( ) . GenericTypeArguments . Single ( ) )
36
- // First pass at filtering out before locking
37
- . Where ( i => ! assemblyFixtureMappings . ContainsKey ( i ) ) )
38
- {
39
- // ConcurrentDictionary's GetOrAdd does not lock around the value factory call, so we need
40
- // to do it ourselves.
41
- lock ( assemblyFixtureMappings )
42
- if ( ! assemblyFixtureMappings . ContainsKey ( fixtureType ) )
43
- Aggregator . Run ( ( ) => assemblyFixtureMappings . Add ( fixtureType , CreateAssemblyFixtureInstance ( fixtureType ) ) ) ;
44
- }
45
-
46
- // Don't want to use .Concat + .ToDictionary because of the possibility of overriding types,
47
- // so instead we'll just let collection fixtures override assembly fixtures.
48
- var combinedFixtures = new Dictionary < Type , object > ( assemblyFixtureMappings ) ;
49
- foreach ( var kvp in CollectionFixtureMappings )
50
- combinedFixtures [ kvp . Key ] = kvp . Value ;
51
-
52
- // We've done everything we need, so let the built-in types do the rest of the heavy lifting
53
- return new XunitTestClassRunner ( testClass , @class , testCases , diagnosticMessageSink , MessageBus , TestCaseOrderer , new ExceptionAggregator ( Aggregator ) , CancellationTokenSource , combinedFixtures ) . RunAsync ( ) ;
54
- }
55
-
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Reflection ;
5
+ using System . Threading ;
6
+ using System . Threading . Tasks ;
7
+ using Xunit . Abstractions ;
8
+ using Xunit . Sdk ;
9
+
10
+ namespace Xunit . Extensions . AssemblyFixture
11
+ {
12
+ class TestCollectionRunner : XunitTestCollectionRunner
13
+ {
14
+ readonly Dictionary < Type , object > assemblyFixtureMappings ;
15
+ readonly IMessageSink diagnosticMessageSink ;
16
+
17
+ public TestCollectionRunner ( Dictionary < Type , object > assemblyFixtureMappings ,
18
+ ITestCollection testCollection ,
19
+ IEnumerable < IXunitTestCase > testCases ,
20
+ IMessageSink diagnosticMessageSink ,
21
+ IMessageBus messageBus ,
22
+ ITestCaseOrderer testCaseOrderer ,
23
+ ExceptionAggregator aggregator ,
24
+ CancellationTokenSource cancellationTokenSource )
25
+ : base ( testCollection , testCases , diagnosticMessageSink , messageBus , testCaseOrderer , aggregator , cancellationTokenSource )
26
+ {
27
+ this . assemblyFixtureMappings = assemblyFixtureMappings ;
28
+ this . diagnosticMessageSink = diagnosticMessageSink ;
29
+ }
30
+
31
+ protected override Task < RunSummary > RunTestClassAsync ( ITestClass testClass , IReflectionTypeInfo @class , IEnumerable < IXunitTestCase > testCases )
32
+ {
33
+ foreach ( var fixtureType in @class . Type . GetTypeInfo ( ) . ImplementedInterfaces
34
+ . Where ( i => i . GetTypeInfo ( ) . IsGenericType && i . GetGenericTypeDefinition ( ) == typeof ( IAssemblyFixture < > ) )
35
+ . Select ( i => i . GetTypeInfo ( ) . GenericTypeArguments . Single ( ) )
36
+ // First pass at filtering out before locking
37
+ . Where ( i => ! assemblyFixtureMappings . ContainsKey ( i ) ) )
38
+ {
39
+ // ConcurrentDictionary's GetOrAdd does not lock around the value factory call, so we need
40
+ // to do it ourselves.
41
+ lock ( assemblyFixtureMappings )
42
+ if ( ! assemblyFixtureMappings . ContainsKey ( fixtureType ) )
43
+ Aggregator . Run ( ( ) => assemblyFixtureMappings . Add ( fixtureType , CreateAssemblyFixtureInstance ( fixtureType ) ) ) ;
44
+ }
45
+
46
+ // Don't want to use .Concat + .ToDictionary because of the possibility of overriding types,
47
+ // so instead we'll just let collection fixtures override assembly fixtures.
48
+ var combinedFixtures = new Dictionary < Type , object > ( assemblyFixtureMappings ) ;
49
+ foreach ( var kvp in CollectionFixtureMappings )
50
+ combinedFixtures [ kvp . Key ] = kvp . Value ;
51
+
52
+ // We've done everything we need, so let the built-in types do the rest of the heavy lifting
53
+ return new XunitTestClassRunner ( testClass , @class , testCases , diagnosticMessageSink , MessageBus , TestCaseOrderer , new ExceptionAggregator ( Aggregator ) , CancellationTokenSource , combinedFixtures ) . RunAsync ( ) ;
54
+ }
55
+
56
56
private object CreateAssemblyFixtureInstance ( Type fixtureType ) {
57
57
var constructors = fixtureType . GetConstructors ( ) ;
58
58
@@ -63,8 +63,8 @@ private object CreateAssemblyFixtureInstance(Type fixtureType) {
63
63
if ( constructors [ 0 ] . GetParameters ( ) . Length == 0 )
64
64
return Activator . CreateInstance ( fixtureType ) ;
65
65
66
- return Activator . CreateInstance ( fixtureType , diagnosticMessageSink ) ;
67
- }
68
-
69
- }
70
- }
66
+ return Activator . CreateInstance ( fixtureType , diagnosticMessageSink ) ;
67
+ }
68
+
69
+ }
70
+ }
0 commit comments