Skip to content

Commit 3fe6dc3

Browse files
committed
Fix: Correct key comparison logic in service conditions
1 parent 0240921 commit 3fe6dc3

4 files changed

+50
-13
lines changed

src/CodeOfChaos.Testing.TUnit/Conditions/Library/ContainsKeyedServiceImplementationCondition.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ protected override ValueTask<AssertionResult> GetResult(IServiceCollection? actu
2424
}
2525

2626
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+
2735
return descriptor.IsKeyedService
2836
&& descriptor.ServiceType == serviceType
2937
&& descriptor.KeyedImplementationType == implementationType
30-
&& ReferenceEquals(descriptor.ServiceKey, key);
38+
&& (descriptor.ServiceKey.Equals(key) || key.Equals(descriptor.ServiceKey));
3139
}
32-
}
40+
}

src/CodeOfChaos.Testing.TUnit/Conditions/Library/ContainsKeyedServiceTypeCondition.cs

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// ---------------------------------------------------------------------------------------------------------------------
1+

2+
// ---------------------------------------------------------------------------------------------------------------------
23
// Imports
34
// ---------------------------------------------------------------------------------------------------------------------
45
using Microsoft.Extensions.DependencyInjection;
@@ -21,9 +22,19 @@ protected override ValueTask<AssertionResult> GetResult(IServiceCollection? actu
2122
return actualValue.Any(descriptor =>
2223
descriptor.IsKeyedService
2324
&& descriptor.ServiceType == serviceType
24-
&& ReferenceEquals(descriptor.ServiceKey, key)
25+
&& CompareKeys(descriptor.ServiceKey, key)
2526
)
2627
? AssertionResult.Passed
2728
: FailWithMessage($"No keyed service with type {serviceType.Name} and key {key} has been registered.");
2829
}
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+
}

src/CodeOfChaos.Testing.TUnit/Conditions/Library/DoesNotContainKeyedServiceImplementationCondition.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ protected override ValueTask<AssertionResult> GetResult(IServiceCollection? actu
2424
}
2525

2626
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+
2735
return descriptor.IsKeyedService
2836
&& descriptor.ServiceType == serviceType
2937
&& descriptor.KeyedImplementationType == implementationType
30-
&& ReferenceEquals(descriptor.ServiceKey, key);
38+
&& (descriptor.ServiceKey.Equals(key) || key.Equals(descriptor.ServiceKey));
3139
}
3240
}

src/CodeOfChaos.Testing.TUnit/Conditions/Library/DoesNotContainKeyedServiceTypeCondition.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// ---------------------------------------------------------------------------------------------------------------------
1+

2+
// ---------------------------------------------------------------------------------------------------------------------
23
// Imports
34
// ---------------------------------------------------------------------------------------------------------------------
45
using Microsoft.Extensions.DependencyInjection;
@@ -18,12 +19,21 @@ protected override ValueTask<AssertionResult> GetResult(IServiceCollection? actu
1819
if (actualValue is null)
1920
return AssertionResult.Fail($"{nameof(IServiceCollection)} is null");
2021

21-
return actualValue.Any(descriptor =>
22-
descriptor.IsKeyedService
23-
&& descriptor.ServiceType == serviceType
24-
&& ReferenceEquals(descriptor.ServiceKey, key)
25-
)
22+
return actualValue.Any(Predicate)
2623
? FailWithMessage($"Found service of type {serviceType.Name} registered with key {key}")
2724
: AssertionResult.Passed;
2825
}
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+
}

0 commit comments

Comments
 (0)