File tree 4 files changed +50
-13
lines changed
src/CodeOfChaos.Testing.TUnit/Conditions/Library
4 files changed +50
-13
lines changed Original file line number Diff line number Diff line change @@ -24,9 +24,17 @@ protected override ValueTask<AssertionResult> GetResult(IServiceCollection? actu
24
24
}
25
25
26
26
private bool Predicate ( ServiceDescriptor descriptor ) {
27
+ if ( descriptor . ServiceKey == null && key == null )
28
+ return descriptor . IsKeyedService
29
+ && descriptor . ServiceType == serviceType
30
+ && descriptor . KeyedImplementationType == implementationType ;
31
+
32
+ if ( descriptor . ServiceKey == null || key == null )
33
+ return false ;
34
+
27
35
return descriptor . IsKeyedService
28
36
&& descriptor . ServiceType == serviceType
29
37
&& descriptor . KeyedImplementationType == implementationType
30
- && ReferenceEquals ( descriptor . ServiceKey , key ) ;
38
+ && ( descriptor . ServiceKey . Equals ( key ) || key . Equals ( descriptor . ServiceKey ) ) ;
31
39
}
32
- }
40
+ }
Original file line number Diff line number Diff line change 1
- // ---------------------------------------------------------------------------------------------------------------------
1
+
2
+ // ---------------------------------------------------------------------------------------------------------------------
2
3
// Imports
3
4
// ---------------------------------------------------------------------------------------------------------------------
4
5
using Microsoft . Extensions . DependencyInjection ;
@@ -21,9 +22,19 @@ protected override ValueTask<AssertionResult> GetResult(IServiceCollection? actu
21
22
return actualValue . Any ( descriptor =>
22
23
descriptor . IsKeyedService
23
24
&& descriptor . ServiceType == serviceType
24
- && ReferenceEquals ( descriptor . ServiceKey , key )
25
+ && CompareKeys ( descriptor . ServiceKey , key )
25
26
)
26
27
? AssertionResult . Passed
27
28
: FailWithMessage ( $ "No keyed service with type { serviceType . Name } and key { key } has been registered.") ;
28
29
}
29
- }
30
+
31
+ private static bool CompareKeys ( object ? descriptorKey , object ? expectedKey ) {
32
+ if ( descriptorKey == null && expectedKey == null )
33
+ return true ;
34
+
35
+ if ( descriptorKey == null || expectedKey == null )
36
+ return false ;
37
+
38
+ return descriptorKey . Equals ( expectedKey ) || expectedKey . Equals ( descriptorKey ) ;
39
+ }
40
+ }
Original file line number Diff line number Diff line change @@ -24,9 +24,17 @@ protected override ValueTask<AssertionResult> GetResult(IServiceCollection? actu
24
24
}
25
25
26
26
private bool Predicate ( ServiceDescriptor descriptor ) {
27
+ if ( descriptor . ServiceKey == null && key == null )
28
+ return descriptor . IsKeyedService
29
+ && descriptor . ServiceType == serviceType
30
+ && descriptor . KeyedImplementationType == implementationType ;
31
+
32
+ if ( descriptor . ServiceKey == null || key == null )
33
+ return false ;
34
+
27
35
return descriptor . IsKeyedService
28
36
&& descriptor . ServiceType == serviceType
29
37
&& descriptor . KeyedImplementationType == implementationType
30
- && ReferenceEquals ( descriptor . ServiceKey , key ) ;
38
+ && ( descriptor . ServiceKey . Equals ( key ) || key . Equals ( descriptor . ServiceKey ) ) ;
31
39
}
32
40
}
Original file line number Diff line number Diff line change 1
- // ---------------------------------------------------------------------------------------------------------------------
1
+
2
+ // ---------------------------------------------------------------------------------------------------------------------
2
3
// Imports
3
4
// ---------------------------------------------------------------------------------------------------------------------
4
5
using Microsoft . Extensions . DependencyInjection ;
@@ -18,12 +19,21 @@ protected override ValueTask<AssertionResult> GetResult(IServiceCollection? actu
18
19
if ( actualValue is null )
19
20
return AssertionResult . Fail ( $ "{ nameof ( IServiceCollection ) } is null") ;
20
21
21
- return actualValue . Any ( descriptor =>
22
- descriptor . IsKeyedService
23
- && descriptor . ServiceType == serviceType
24
- && ReferenceEquals ( descriptor . ServiceKey , key )
25
- )
22
+ return actualValue . Any ( Predicate )
26
23
? FailWithMessage ( $ "Found service of type { serviceType . Name } registered with key { key } ")
27
24
: AssertionResult . Passed ;
28
25
}
29
- }
26
+
27
+ private bool Predicate ( ServiceDescriptor descriptor ) {
28
+ if ( descriptor . ServiceKey == null && key == null )
29
+ return descriptor . IsKeyedService
30
+ && descriptor . ServiceType == serviceType ;
31
+
32
+ if ( descriptor . ServiceKey == null || key == null )
33
+ return false ;
34
+
35
+ return descriptor . IsKeyedService
36
+ && descriptor . ServiceType == serviceType
37
+ && ( descriptor . ServiceKey . Equals ( key ) || key . Equals ( descriptor . ServiceKey ) ) ;
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments