Skip to content

Commit

Permalink
100% coverage and all tests passed for MS.DI
Browse files Browse the repository at this point in the history
  • Loading branch information
seesharper committed Nov 7, 2024
1 parent 9d0cda7 commit 8fe8b45
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 107 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"coverage-gutters.lcovname": "../build/Artifacts/TestCoverage/coverage.info"
"coverage-gutters.lcovname": "../build/Artifacts/TestCoverage/coverage.info",
"cSpell.words": [
"Xunit"
]
}
2 changes: 1 addition & 1 deletion build/build.csx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#load "nuget:Dotnet.Build, 0.22.0"
#load "nuget:Dotnet.Build, 0.23.0"
#load "nuget:dotnet-steps, 0.0.2"

[StepDescription("Runs the tests with test coverage")]
Expand Down
2 changes: 1 addition & 1 deletion omnisharp.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
},
"script": {
"enableScriptNuGetReferences": true,
"defaultTargetFramework": "net7.0"
"defaultTargetFramework": "net8.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ public async Task ShouldDisposeAsyncDisposableFromRootScope()
Assert.Contains(asyncDisposable, disposedObjects);
Assert.Single(disposedObjects);
}

[Fact]
public async Task ShouldHandleDisposeAsyncOnServiceProvider()
{

}
}

public class AsyncDisposable : IAsyncDisposable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Specification;
using Xunit;
using Xunit.Sdk;

namespace LightInject.Microsoft.DependencyInjection.Tests;

Expand All @@ -12,7 +14,6 @@ protected override IServiceProvider CreateServiceProvider(IServiceCollection ser
return serviceCollection.CreateLightInjectServiceProvider(new ContainerOptions() { EnableCurrentScope = false });
}


[Fact]
public void ShouldHandleKeyedServiceWithEnumKey()
{
Expand All @@ -22,7 +23,6 @@ public void ShouldHandleKeyedServiceWithEnumKey()
provider.GetKeyedService<IKeyedService>(Key.A);
}


[Fact]
public void ShouldHandleKeyedServiceWithStringServiceKey()
{
Expand All @@ -33,7 +33,7 @@ public void ShouldHandleKeyedServiceWithStringServiceKey()
Assert.Equal("A", ((KeyedServiceWithStringServiceKey)instance).ServiceKey);
}

[Fact]
[Fact]
public void ShouldHandleRequiredKeyedServiceWithStringServiceKeyUsingFactory()
{
var serviceCollection = new ServiceCollection();
Expand Down Expand Up @@ -83,7 +83,33 @@ public void ShouldHandlePassingServiceKeyAsEnum()
Assert.Equal(Key.A, ((KeyedServiceWithEnumServiceKey)instance).ServiceKey);
}


[Fact]
public void ShouldThrowExceptionWhenUsingInvalidKeyType()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IKeyedService>(new StringBuilder(), (sp, key) => new KeyedServiceWithEnumServiceKey((Key)key));
var provider = CreateServiceProvider(serviceCollection);
Assert.Throws<InvalidOperationException>(() => provider.GetKeyedService<IKeyedService>(new StringBuilder()));
}

[Fact]
public void ShouldHandleConvertibleType()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IKeyedService>(1L, (sp, key) => new KeyedServiceWithLongServiceKey((long)key));
var provider = CreateServiceProvider(serviceCollection);
var instance = provider.GetKeyedService<IKeyedService>(1L);
Assert.Equal(1L, ((KeyedServiceWithLongServiceKey)instance).ServiceKey);
}

[Fact]
public void ShouldHandlePassingNullAsServiceKeyForRequiredService()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IKeyedService, KeyedService>(Key.A);
var provider = CreateServiceProvider(serviceCollection);
Assert.Throws<InvalidOperationException>(() => provider.GetRequiredKeyedService<IKeyedService>(null));
}
}

public interface IKeyedService
Expand All @@ -95,38 +121,28 @@ public class KeyedService : IKeyedService

}

public class KeyedServiceWithStringServiceKey : IKeyedService
public class KeyedServiceWithStringServiceKey([ServiceKey] string serviceKey) : IKeyedService
{
public KeyedServiceWithStringServiceKey([ServiceKey] string serviceKey)
{
ServiceKey = serviceKey;
}

public string ServiceKey { get; }
public string ServiceKey { get; } = serviceKey;
}

public class KeyedServiceWithEnumServiceKey : IKeyedService
public class KeyedServiceWithEnumServiceKey([ServiceKey] Key serviceKey) : IKeyedService
{
public KeyedServiceWithEnumServiceKey([ServiceKey] Key serviceKey)
{
ServiceKey = serviceKey;
}

public Key ServiceKey { get; }
public Key ServiceKey { get; } = serviceKey;
}


public class KeyServiceWithIntKey : IKeyedService
public class KeyServiceWithIntKey([ServiceKey] int serviceKey) : IKeyedService
{
public KeyServiceWithIntKey([ServiceKey] int serviceKey)
{
ServiceKey = serviceKey;
}

public int ServiceKey { get; }
public int ServiceKey { get; } = serviceKey;
}


public class KeyedServiceWithLongServiceKey([ServiceKey] long serviceKey) : IKeyedService
{
public long ServiceKey { get; } = serviceKey;
}

public enum Key
{
A,
Expand Down
Loading

0 comments on commit 8fe8b45

Please sign in to comment.