Skip to content

Commit

Permalink
adding unit test for conditional decorators for issue #149
Browse files Browse the repository at this point in the history
  • Loading branch information
ipjohnson committed Jan 31, 2018
1 parent 4b64311 commit 301b084
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
40 changes: 40 additions & 0 deletions tests/Grace.Tests/Classes/Simple/CommandClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,44 @@ public override void DoSomething(double value)

}
}

public class OtherCommand : ICommand<int>
{
public void DoSomething(int value)
{

}
}

public class LoggingComand<T> : ICommand<T>
{
private ICommand<T> _command;

public LoggingComand(ICommand<T> command)
{
_command = command;
}

public void DoSomething(T value)
{
// Log
_command.DoSomething(value);
}
}

public class ValidatingCommand<T> : ICommand<T>
{
private ICommand<T> _command;

public ValidatingCommand(ICommand<T> command)
{
_command = command;
}

public void DoSomething(T value)
{
// validate
_command.DoSomething(value);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -29,5 +33,65 @@ public void DecoratorCondition_InjectedType()
Assert.NotNull(dependentService);
Assert.IsType<BasicServiceDecorator>(dependentService.Value);
}

[Fact]
public void DecoratorCondition_Decoratoring()
{
var container = new DependencyInjectionContainer();

container.Configure(c =>
{
c.Export<CommandA>().As<ICommand<int>>();
c.Export<OtherCommand>().As<ICommand<int>>();
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<List<ICommand<int>>>();

Assert.Equal(2, instances.Count);
Assert.IsType<LoggingComand<int>>(instances[0]);
Assert.IsType<OtherCommand>(instances[1]);
}
}

public class WhenDecoratingCondition : ICompiledCondition
{
private Type _decoratedType;

public WhenDecoratingCondition(Type decoratedType)
{
_decoratedType = decoratedType;
}

/// <summary>
/// Test if strategy meets condition at configuration time
/// </summary>
/// <param name="strategy">strategy to test</param>
/// <param name="staticInjectionContext">static injection context</param>
/// <returns>meets condition</returns>
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 301b084

Please sign in to comment.