Skip to content

Commit

Permalink
Cleanup all tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
seesharper committed Oct 27, 2024
1 parent 120b18e commit c65cb30
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 66 deletions.
1 change: 1 addition & 0 deletions packlocal.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dotnet pack "src/LightInject/LightInject.csproj" --configuration Release --output "build/Artifacts/NuGet" /property:Version=$1
54 changes: 54 additions & 0 deletions src/LightInject.Tests/KeyedMicrosoftTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,28 @@ public void ResolveKeyedTransientFromScopeServiceProvider()
Assert.NotSame(serviceA1, serviceB1);
}


[Fact]
public void ShouldHandleKeyedServiceWithEnumServiceKey()
{
var container = CreateContainer();
var rootScope = container.BeginScope();
container.Register<IKeyedService, KeyedServiceWithEnumServiceKey>(Key.A.ToString(), new PerRootScopeLifetime(rootScope));
var instance = rootScope.GetInstance<IKeyedService>(Key.A.ToString());
Assert.Equal(Key.A, ((KeyedServiceWithEnumServiceKey)instance).ServiceKey);
}

[Fact]
public void ShouldHandleKeyedServiceWithIntServiceKey()
{
var container = CreateContainer();
var rootScope = container.BeginScope();
container.Register<IKeyedService, KeyServiceWithIntServiceKey>("42", new PerRootScopeLifetime(rootScope));
var instance = rootScope.GetInstance<IKeyedService>("42");
Assert.Equal(42, ((KeyServiceWithIntServiceKey)instance).ServiceKey);
}


// [Fact]
// public void ResolveKeyedServiceThrowsIfNotSupported()
// {
Expand Down Expand Up @@ -800,6 +822,38 @@ public void Test()
// factory(null, 32);
// }


public interface IKeyedService
{
}

public enum Key
{
A,
B
}

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

public Key ServiceKey { get; }
}

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

public int ServiceKey { get; }
}


internal class ServiceFactoryAccessor
{
public ServiceFactoryAccessor(IServiceFactory serviceFactory)
Expand Down
65 changes: 65 additions & 0 deletions src/LightInject.Tests/ServiceRegistrationEqualsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using LightInject.SampleLibrary;
using Xunit;

namespace LightInject.Tests;

public class ServiceRegistrationEqualsTests
{
[Fact]
public void ShouldBeEqualWhenServiceTypeAndServiceNameAreTheSame()
{
var serviceRegistration1 = new ServiceRegistration
{
ServiceType = typeof(int),
ServiceName = string.Empty
};

var serviceRegistration2 = new ServiceRegistration
{
ServiceType = typeof(int),
ServiceName = string.Empty
};

Assert.Equal(serviceRegistration1, serviceRegistration2);
}

[Fact]
public void ShouldNotBeEqualIfImplementingTypeIsDifferent()
{
var serviceRegistration1 = new ServiceRegistration
{
ServiceType = typeof(IFoo),
ServiceName = string.Empty,
ImplementingType = typeof(Foo)
};

var serviceRegistration2 = new ServiceRegistration
{
ServiceType = typeof(IFoo),
ServiceName = string.Empty,
ImplementingType = typeof(AnotherFoo)
};

Assert.NotEqual(serviceRegistration1, serviceRegistration2);
}

[Fact]
public void ShouldNotBeEqualWithMixedImplementingTypeAndFactoryExpression()
{
var serviceRegistration1 = new ServiceRegistration
{
ServiceType = typeof(IFoo),
ServiceName = string.Empty,
ImplementingType = typeof(Foo)
};

var serviceRegistration2 = new ServiceRegistration
{
ServiceType = typeof(IFoo),
ServiceName = string.Empty,
FactoryExpression = (IServiceFactory f) => new Foo()
};

Assert.NotEqual(serviceRegistration1, serviceRegistration2);
}
}
Loading

0 comments on commit c65cb30

Please sign in to comment.