Skip to content

Commit

Permalink
Added failing/skipped tests for #967.
Browse files Browse the repository at this point in the history
  • Loading branch information
tillig committed Apr 1, 2019
1 parent b79b388 commit b5e637f
Showing 1 changed file with 65 additions and 17 deletions.
82 changes: 65 additions & 17 deletions test/Autofac.Specification.Test/Features/DecoratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,23 +282,17 @@ public void DecoratorAppliedOnlyOnceToComponentWithExternalRegistrySource()
}

[Fact]
public void DecoratorCanBeAppliedTwiceIntentionallyWithExternalRegistrySource()
public void DecoratorCanBeAppliedToServiceRegisteredInChildLifetimeScope()
{
// #965: A nested lifetime scope that has a registration lambda
// causes the decorator to be applied twice - once for the container
// level, and once for the scope level.
var builder = new ContainerBuilder();
builder.RegisterType<ImplementorA>().As<IDecoratedService>();
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
var container = builder.Build();

var scope = container.BeginLifetimeScope(b => { });
var service = scope.Resolve<IDecoratedService>();
var scope = container.BeginLifetimeScope(b => b.RegisterType<ImplementorA>().As<IDecoratedService>());
var instance = scope.Resolve<IDecoratedService>();

Assert.IsType<DecoratorA>(service);
Assert.IsType<DecoratorA>(service.Decorated);
Assert.IsType<ImplementorA>(service.Decorated.Decorated);
Assert.IsType<DecoratorA>(instance);
Assert.IsType<ImplementorA>(instance.Decorated);
}

[Fact]
Expand Down Expand Up @@ -338,17 +332,23 @@ public void DecoratorCanBeAppliedTwiceInChildLifetimeScope()
}

[Fact]
public void DecoratorCanBeAppliedToServiceRegisteredInChildLifetimeScope()
public void DecoratorCanBeAppliedTwiceIntentionallyWithExternalRegistrySource()
{
// #965: A nested lifetime scope that has a registration lambda
// causes the decorator to be applied twice - once for the container
// level, and once for the scope level.
var builder = new ContainerBuilder();
builder.RegisterType<ImplementorA>().As<IDecoratedService>();
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
var container = builder.Build();

var scope = container.BeginLifetimeScope(b => b.RegisterType<ImplementorA>().As<IDecoratedService>());
var instance = scope.Resolve<IDecoratedService>();
var scope = container.BeginLifetimeScope(b => { });
var service = scope.Resolve<IDecoratedService>();

Assert.IsType<DecoratorA>(instance);
Assert.IsType<ImplementorA>(instance.Decorated);
Assert.IsType<DecoratorA>(service);
Assert.IsType<DecoratorA>(service.Decorated);
Assert.IsType<ImplementorA>(service.Decorated.Decorated);
}

[Fact]
Expand Down Expand Up @@ -457,6 +457,34 @@ public void DecoratorInheritsDecoratedLifetimeWhenSingleInstance()
}
}

[Fact(Skip = "Issue #967")]
public void DecoratorParameterSupportsFunc()
{
var builder = new ContainerBuilder();
builder.RegisterType<ImplementorA>().As<IDecoratedService>();
builder.RegisterDecorator<DecoratorWithFunc, IDecoratedService>();
var container = builder.Build();

var instance = container.Resolve<IDecoratedService>();

Assert.IsType<DecoratorWithFunc>(instance);
Assert.IsType<ImplementorA>(instance.Decorated);
}

[Fact(Skip = "Issue #967")]
public void DecoratorParameterSupportsLazy()
{
var builder = new ContainerBuilder();
builder.RegisterType<ImplementorA>().As<IDecoratedService>();
builder.RegisterDecorator<DecoratorWithLazy, IDecoratedService>();
var container = builder.Build();

var instance = container.Resolve<IDecoratedService>();

Assert.IsType<DecoratorWithLazy>(instance);
Assert.IsType<ImplementorA>(instance.Decorated);
}

[Fact]
public void DecoratorRegisteredAsLambdaCanAcceptAdditionalParameters()
{
Expand Down Expand Up @@ -664,7 +692,7 @@ private abstract class Decorator : IDecoratedService
{
protected Decorator(IDecoratedService decorated)
{
Decorated = decorated;
this.Decorated = decorated;
}

public IDecoratedService Decorated { get; }
Expand Down Expand Up @@ -709,6 +737,26 @@ public DecoratorWithContextB(IDecoratedService decorated, IDecoratorContext cont
public IDecoratorContext Context { get; }
}

private class DecoratorWithFunc : IDecoratedService
{
public DecoratorWithFunc(Func<IDecoratedService> decorated)
{
this.Decorated = decorated();
}

public IDecoratedService Decorated { get; }
}

private class DecoratorWithLazy : IDecoratedService
{
public DecoratorWithLazy(Lazy<IDecoratedService> decorated)
{
this.Decorated = decorated.Value;
}

public IDecoratedService Decorated { get; }
}

private class DecoratorWithParameter : Decorator, IDecoratorWithParameter
{
public DecoratorWithParameter(IDecoratedService decorated, string parameter)
Expand Down

0 comments on commit b5e637f

Please sign in to comment.