diff --git a/tests/Grace.Tests/Classes/Simple/CommandClasses.cs b/tests/Grace.Tests/Classes/Simple/CommandClasses.cs index fa5da1d0..117a896b 100644 --- a/tests/Grace.Tests/Classes/Simple/CommandClasses.cs +++ b/tests/Grace.Tests/Classes/Simple/CommandClasses.cs @@ -37,4 +37,44 @@ public override void DoSomething(double value) } } + + public class OtherCommand : ICommand + { + public void DoSomething(int value) + { + + } + } + + public class LoggingComand : ICommand + { + private ICommand _command; + + public LoggingComand(ICommand command) + { + _command = command; + } + + public void DoSomething(T value) + { + // Log + _command.DoSomething(value); + } + } + + public class ValidatingCommand : ICommand + { + private ICommand _command; + + public ValidatingCommand(ICommand command) + { + _command = command; + } + + public void DoSomething(T value) + { + // validate + _command.DoSomething(value); + } + } } diff --git a/tests/Grace.Tests/DependencyInjection/Decorator/ConditionDecoratorTests.cs b/tests/Grace.Tests/DependencyInjection/Decorator/ConditionDecoratorTests.cs index 0a185bc2..710c80a0 100644 --- a/tests/Grace.Tests/DependencyInjection/Decorator/ConditionDecoratorTests.cs +++ b/tests/Grace.Tests/DependencyInjection/Decorator/ConditionDecoratorTests.cs @@ -1,4 +1,8 @@ -using Grace.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Reflection; +using Grace.DependencyInjection; +using Grace.DependencyInjection.Conditions; using Grace.Tests.Classes.Simple; using Xunit; @@ -29,5 +33,65 @@ public void DecoratorCondition_InjectedType() Assert.NotNull(dependentService); Assert.IsType(dependentService.Value); } + + [Fact] + public void DecoratorCondition_Decoratoring() + { + var container = new DependencyInjectionContainer(); + + container.Configure(c => + { + c.Export().As>(); + c.Export().As>(); + c.ExportDecorator(typeof(ValidatingCommand<>)).As(typeof(ICommand<>)).When + .MeetsCondition(new WhenDecoratingCondition(typeof(BaseCommand<>))); + c.ExportDecorator(typeof(LoggingComand<>)).As(typeof(ICommand<>)).When + .MeetsCondition(new WhenDecoratingCondition(typeof(BaseCommand<>))); + }); + + var instances = container.Locate>>(); + + Assert.Equal(2, instances.Count); + Assert.IsType>(instances[0]); + Assert.IsType(instances[1]); + } + } + + public class WhenDecoratingCondition : ICompiledCondition + { + private Type _decoratedType; + + public WhenDecoratingCondition(Type decoratedType) + { + _decoratedType = decoratedType; + } + + /// + /// Test if strategy meets condition at configuration time + /// + /// strategy to test + /// static injection context + /// meets condition + public bool MeetsCondition(IActivationStrategy strategy, StaticInjectionContext staticInjectionContext) + { + if (_decoratedType.GetTypeInfo().IsGenericTypeDefinition) + { + var current = strategy.ActivationType; + + while (current != null && current != typeof(object)) + { + if (current.IsConstructedGenericType && current.GetGenericTypeDefinition() == _decoratedType) + { + return true; + } + + current = current.GetTypeInfo().BaseType; + } + + return false; + } + + return strategy.ActivationType.IsAssignableFrom(_decoratedType); + } } } diff --git a/tests/Grace.Tests/DependencyInjection/Registration/ExportTypeSetTests.cs b/tests/Grace.Tests/DependencyInjection/Registration/ExportTypeSetTests.cs index 3d1c476c..3a17af33 100644 --- a/tests/Grace.Tests/DependencyInjection/Registration/ExportTypeSetTests.cs +++ b/tests/Grace.Tests/DependencyInjection/Registration/ExportTypeSetTests.cs @@ -274,7 +274,7 @@ public void ExportTypeSet_Commands() var exports = container.StrategyCollectionContainer.GetAllStrategies().ToList(); - Assert.Equal(3, exports.Count); + Assert.Equal(6, exports.Count); } [Fact]