Skip to content

Commit

Permalink
Merge pull request #418 from seesharper/tests/decorators
Browse files Browse the repository at this point in the history
Tests/decorators
  • Loading branch information
seesharper authored May 17, 2018
2 parents aea45c0 + b1f8d83 commit a01be40
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
21 changes: 15 additions & 6 deletions src/LightInject.Tests/DecoratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,11 @@ public void GetInstance_DeferredDecorator_ReturnsDecoratedInstance()
{
var container = CreateContainer();
container.Register<IFoo, Foo>();
var registration = new DecoratorRegistration();
registration.CanDecorate = serviceRegistration => true;
registration.ImplementingTypeFactory = (factory, serviceRegistration) => typeof(FooDecorator);
var registration = new DecoratorRegistration
{
CanDecorate = serviceRegistration => true,
ImplementingTypeFactory = (factory, serviceRegistration) => typeof(FooDecorator)
};
container.Decorate(registration);

var instance = container.GetInstance<IFoo>();
Expand Down Expand Up @@ -550,11 +552,18 @@ public void GetInstance_HalfClosedDecoratorWithMissingGenericArgument_DotNotAppl
container.Decorate(typeof(IFoo<,>), typeof(HalfClosedOpenGenericFooDecorator<,>));
var instance = container.GetInstance<IFoo<int, int>>();
Assert.IsType<OpenGenericFoo<int, int>>(instance);
}



}

[Fact]
public void GetInstance_DecoratorWithBaseGenericConstraint_AppliesDecorator()
{
var container = CreateContainer();
container.Register<IFoo<InheritedBar>, Foo<InheritedBar>>();
container.Decorate(typeof(IFoo<>), typeof(FooDecoratorWithBarBaseConstraint<>));
var instance = container.GetInstance<IFoo<InheritedBar>>();
Assert.IsType<FooDecoratorWithBarBaseConstraint<InheritedBar>>(instance);
}

private IFoo CreateFooWithDependency(IServiceFactory factory)
{
Expand Down
18 changes: 18 additions & 0 deletions src/LightInject.Tests/SampleServices/Foo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,24 @@ public HalfClosedOpenGenericFooDecorator(IFoo<string, T1> foo)
}
}

public class BarBase
{

}

public class InheritedBar :BarBase
{

}

public class FooDecoratorWithBarBaseConstraint<T> : IFoo<T> where T:BarBase
{
public FooDecoratorWithBarBaseConstraint(IFoo<T> foo)
{

}
}

public class Foo<T1, T2>
{
}
Expand Down
4 changes: 2 additions & 2 deletions src/LightInject.Tests/ServiceContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public void GetInstance_OneNamedService_ReturnsDefaultService()
}

[Fact]
public void issue_231()
public void Issue_231()
{
var container = CreateContainer();
container.Register<IFoo, Foo>("foo", new PerContainerLifetime());
Expand All @@ -315,7 +315,7 @@ public void issue_231()
}

[Fact]
public void issue_168()
public void Issue_168()
{
var serviceContainer = new ServiceContainer();
serviceContainer.Register<IBar, Bar>("bar");
Expand Down
36 changes: 14 additions & 22 deletions src/LightInject/LightInject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,8 +1095,7 @@ public static class RuntimeArgumentsLoader
/// <returns>An array containing the runtime arguments supplied when resolving the service.</returns>
public static object[] Load(object[] constants)
{
object[] arguments = constants[constants.Length - 1] as object[];
if (arguments == null)
if (!(constants[constants.Length - 1] is object[] arguments))
{
return new object[] { };
}
Expand Down Expand Up @@ -3183,15 +3182,13 @@ private TypeConstructionInfoBuilder CreateTypeConstructionInfoBuilder()

private Delegate GetConstructorDependencyDelegate(Type type, string serviceName)
{
Delegate dependencyDelegate;
GetConstructorDependencyFactories(type).TryGetValue(serviceName, out dependencyDelegate);
GetConstructorDependencyFactories(type).TryGetValue(serviceName, out Delegate dependencyDelegate);
return dependencyDelegate;
}

private Delegate GetPropertyDependencyExpression(Type type, string serviceName)
{
Delegate dependencyDelegate;
GetPropertyDependencyFactories(type).TryGetValue(serviceName, out dependencyDelegate);
GetPropertyDependencyFactories(type).TryGetValue(serviceName, out Delegate dependencyDelegate);
return dependencyDelegate;
}

Expand Down Expand Up @@ -3293,9 +3290,8 @@ private Action<IEmitter> CreateEmitMethodWrapper(Action<IEmitter> emitter, Type

private Action<IEmitter> GetRegisteredEmitMethod(Type serviceType, string serviceName)
{
Action<IEmitter> emitMethod;
var registrations = GetEmitMethods(serviceType);
registrations.TryGetValue(serviceName, out emitMethod);
registrations.TryGetValue(serviceName, out Action<IEmitter> emitMethod);
return emitMethod ?? CreateEmitMethodForUnknownService(serviceType, serviceName);
}

Expand Down Expand Up @@ -3864,8 +3860,7 @@ private ServiceRegistration GetOpenGenericServiceRegistration(Type openGenericSe
return null;
}

ServiceRegistration openGenericServiceRegistration;
services.TryGetValue(serviceName, out openGenericServiceRegistration);
services.TryGetValue(serviceName, out ServiceRegistration openGenericServiceRegistration);
if (openGenericServiceRegistration == null && string.IsNullOrEmpty(serviceName) && services.Count == 1)
{
return services.First().Value;
Expand Down Expand Up @@ -5165,8 +5160,10 @@ public ConstructionInfo Execute(Registration registration)
}

var implementingType = registration.ImplementingType;
var constructionInfo = new ConstructionInfo();
constructionInfo.ImplementingType = implementingType;
var constructionInfo = new ConstructionInfo
{
ImplementingType = implementingType
};
constructionInfo.PropertyDependencies.AddRange(GetPropertyDependencies(implementingType));
constructionInfo.Constructor = constructorSelector.Execute(implementingType);
constructionInfo.ConstructorDependencies.AddRange(GetConstructorDependencies(constructionInfo.Constructor));
Expand Down Expand Up @@ -5361,8 +5358,7 @@ public override int GetHashCode()
/// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
public override bool Equals(object obj)
{
var other = obj as ServiceRegistration;
if (other == null)
if (!(obj is ServiceRegistration other))
{
return false;
}
Expand Down Expand Up @@ -5632,8 +5628,7 @@ public object GetInstance(Func<object> createInstance, Scope scope)
/// </summary>
public void Dispose()
{
var disposable = singleton as IDisposable;
if (disposable != null)
if (singleton is IDisposable disposable)
{
disposable.Dispose();
}
Expand All @@ -5654,8 +5649,7 @@ public class PerRequestLifeTime : ILifetime
public object GetInstance(Func<object> createInstance, Scope scope)
{
var instance = createInstance();
var disposable = instance as IDisposable;
if (disposable != null)
if (instance is IDisposable disposable)
{
TrackInstance(scope, disposable);
}
Expand Down Expand Up @@ -5704,8 +5698,7 @@ public object GetInstance(Func<object> createInstance, Scope scope)

private static void RegisterForDisposal(Scope scope, object instance)
{
var disposable = instance as IDisposable;
if (disposable != null)
if (instance is IDisposable disposable)
{
scope.TrackInstance(disposable);
}
Expand All @@ -5724,8 +5717,7 @@ private void OnScopeCompleted(object sender, EventArgs e)
{
var scope = (Scope)sender;
scope.Completed -= OnScopeCompleted;
object removedInstance;
instances.TryRemove(scope, out removedInstance);
instances.TryRemove(scope, out object removedInstance);
}
}

Expand Down

0 comments on commit a01be40

Please sign in to comment.