Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test project restructured and resolving changed #4

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace Windsor.ExtensionTests.Registration.Decorator
namespace Windsor.Extension.Tests.Registration.Decorator.Components
{
public class ExecutionStack
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;

namespace Windsor.ExtensionTests.Registration.Decorator
namespace Windsor.Extension.Tests.Registration.Decorator.Components.NonGeneric
{
public class Calculator1: ICalculator
public class Calculator1 : ICalculator
{
private readonly ExecutionStack executionStack;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Windsor.ExtensionTests.Registration.Decorator
namespace Windsor.Extension.Tests.Registration.Decorator.Components.NonGeneric
{
public class Calculator2 : ICalculator
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Windsor.ExtensionTests.Registration.Decorator
namespace Windsor.Extension.Tests.Registration.Decorator.Components.NonGeneric
{
internal class ExceptionDecorator : ICalculator
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Windsor.ExtensionTests.Registration.Decorator
namespace Windsor.Extension.Tests.Registration.Decorator.Components.NonGeneric
{
public interface ICalculator
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;

namespace Windsor.ExtensionTests.Registration.Decorator
namespace Windsor.Extension.Tests.Registration.Decorator.Components.NonGeneric
{
internal class LogDecorator: ICalculator
internal class LogDecorator : ICalculator
{
private readonly ICalculator decorated;
private readonly ExecutionStack executionStack;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace Windsor.Extension.Tests.Registration.Decorator.Components.SingleGeneric
{
public class GenericCalculator1 : IGenericCalculator<long>
{
private readonly ExecutionStack executionStack;

public GenericCalculator1(ExecutionStack executionStack)
{
this.executionStack = executionStack;
}

public long Sum(Guid executionId, long x, long y)
{
executionStack.PushInstance(executionId, this);
return x + y;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace Windsor.Extension.Tests.Registration.Decorator.Components.SingleGeneric
{
public class GenericCalculator2 : IGenericCalculator<int>
{
private readonly ExecutionStack executionStack;

public GenericCalculator2(ExecutionStack executionStack)
{
this.executionStack = executionStack;
}

public int Sum(Guid executionId, int x, int y)
{
executionStack.PushInstance(executionId, this);
return x + y;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace Windsor.Extension.Tests.Registration.Decorator.Components.SingleGeneric
{
public class GenericCalculator3 : IGenericCalculator<long>
{
private readonly ExecutionStack executionStack;

public GenericCalculator3(ExecutionStack executionStack)
{
this.executionStack = executionStack;
}

public long Sum(Guid executionId, long x, long y)
{
executionStack.PushInstance(executionId, this);
return x + y;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace Windsor.Extension.Tests.Registration.Decorator.Components.SingleGeneric
{
class GenericExceptionDecorator<T> : IGenericCalculator<T>
{
private readonly IGenericCalculator<T> decorated;
private readonly ExecutionStack executionStack;

public GenericExceptionDecorator(IGenericCalculator<T> decorated, ExecutionStack executionStack)
{
this.decorated = decorated;
this.executionStack = executionStack;
}

public T Sum(Guid executionId, T x, T y)
{
var sum = decorated.Sum(executionId, x, y);
executionStack.PushInstance(executionId, this);
return sum;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace Windsor.Extension.Tests.Registration.Decorator.Components.SingleGeneric
{
class GenericLogDecorator<T> : IGenericCalculator<T>
{
private readonly IGenericCalculator<T> decorated;
private readonly ExecutionStack executionStack;

public GenericLogDecorator(IGenericCalculator<T> decorated, ExecutionStack executionStack)
{
this.decorated = decorated;
this.executionStack = executionStack;
}

public T Sum(Guid executionId, T x, T y)
{
var result = decorated.Sum(executionId, x, y);
executionStack.PushInstance(executionId, this);
Console.WriteLine("Sum executed");
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Windsor.Extension.Tests.Registration.Decorator.Components.SingleGeneric
{
interface IGenericCalculator<T>
{
T Sum(Guid executionId, T x, T y);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
using System;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Windsor.Extension.Tests.Registration.Decorator.Components;
using Windsor.Extension.Tests.Registration.Decorator.Components.NonGeneric;
using Windsor.Extension.Tests.Registration.Decorator.Components.SingleGeneric;
using Xunit;

namespace Windsor.Extension.Tests.Registration.Decorator
{
public class DecorationRegistrationTests
{
private readonly IWindsorContainer container = new WindsorContainer();
private readonly ExecutionStack executionStack;

public DecorationRegistrationTests()
{
container = new WindsorContainer();
container.Register(Component.For<ExecutionStack>());
executionStack = container.Resolve<ExecutionStack>();
}

[Fact]
public void Single_Component_Registration_For_NonGenericClass_With_One_Decorator_Should_Be_Successful()
{
container.Register(
Component
.For<ICalculator>()
.ImplementedBy<Calculator1>()
.Decorated().By<LogDecorator>()
);

TestCalculator<Calculator1>(typeof(LogDecorator));
}

[Fact]
public void Single_Component_Registration_For_NonGenericClass_With_Multiple_Decorator_Should_Be_Successful()
{
container.Register(
Component
.For<ICalculator>()
.ImplementedBy<Calculator1>()
.Decorated().By<LogDecorator>()
.Decorated().By<ExceptionDecorator>()
);

TestCalculator<Calculator1>(typeof(ExceptionDecorator), typeof(LogDecorator));
}

[Fact]
public void Multi_Component_Registration_For_NonGenericClass_With_Single_Decorator_Should_Be_Successful()
{
container.Register(
Classes
.FromAssemblyContaining<DecorationRegistrationTests>()
.BasedOn<ICalculator>()
.WithService
.FromInterface()
.Decorated().By<ExceptionDecorator>()
);

TestCalculator<Calculator1>(typeof(ExceptionDecorator));
TestCalculator<Calculator2>(typeof(ExceptionDecorator));
}

[Fact]
public void Multi_Component_Registration_For_NonGenericClass_With_Multiple_Decorator_Should_Be_Successful()
{
container.Register(
Classes
.FromAssemblyContaining<DecorationRegistrationTests>()
.BasedOn<ICalculator>()
.WithService
.FromInterface()
.Decorated().By<ExceptionDecorator>()
.Decorated().By<LogDecorator>()
);

TestCalculator<Calculator1>(typeof(ExceptionDecorator), typeof(LogDecorator));
TestCalculator<Calculator2>(typeof(ExceptionDecorator), typeof(LogDecorator));
}

[Fact]
public void Single_Component_Registration_For_GenericClass_With_One_Decorator_Should_Be_Successful()
{
container.Register(
Component
.For<IGenericCalculator<int>>()
.ImplementedBy<GenericCalculator2>()
.Decorated().By<GenericLogDecorator<int>>()
);

TestSingleGenericCalculator<GenericCalculator2, int>(1, 3, typeof(GenericLogDecorator<int>));
}

[Fact]
public void Single_Component_Registration_For_GenericClass_With_Multiple_Decorator_Should_Be_Successful()
{
container.Register(
Component
.For<IGenericCalculator<long>>()
.ImplementedBy<GenericCalculator1>()
.Decorated().By<GenericLogDecorator<long>>()
.Decorated().By<GenericExceptionDecorator<long>>()
);

TestSingleGenericCalculator<GenericCalculator1, long>(1L, 3L, typeof(GenericExceptionDecorator<long>), typeof(GenericLogDecorator<long>));
}

[Fact]
public void Multi_Component_Registration_For_GenericClass_With_Single_Decorator_Should_Be_Successful()
{
container.Register(
Classes
.FromAssemblyContaining<DecorationRegistrationTests>()
.BasedOn<IGenericCalculator<long>>()
.WithService
.FromInterface()
.Decorated().By<GenericExceptionDecorator<long>>()
);

TestSingleGenericCalculator<GenericCalculator1, long>(1L, 3L, typeof(GenericExceptionDecorator<long>));
TestSingleGenericCalculator<GenericCalculator3, long>(1L, 3L, typeof(GenericExceptionDecorator<long>));
}

[Fact]
public void Multi_Component_Registration_For_GenericClass_With_Multiple_Decorator_Should_Be_Successful()
{
container.Register(
Classes
.FromAssemblyContaining<DecorationRegistrationTests>()
.BasedOn<IGenericCalculator<long>>()
.WithService
.FromInterface()
.Decorated().By<GenericExceptionDecorator<long>>()
.Decorated().By<GenericLogDecorator<long>>()
);

TestSingleGenericCalculator<GenericCalculator1, long>(1L, 3L, typeof(GenericExceptionDecorator<long>), typeof(GenericLogDecorator<long>));
TestSingleGenericCalculator<GenericCalculator3, long>(1L, 3L, typeof(GenericExceptionDecorator<long>), typeof(GenericLogDecorator<long>));
}

private void TestCalculator<TCalculator>(params Type[] decoratorTypes)
where TCalculator : ICalculator
{
var calculator = container.Resolve<ICalculator>(typeof(TCalculator).FullName);
Guid executionId = Guid.NewGuid();
calculator.Sum(executionId, 1, 2);

foreach (var decoratorType in decoratorTypes)
{
var instance = executionStack.PopInstance(executionId);
Assert.Equal(instance.GetType(), decoratorType);
}

Assert.Equal(typeof(TCalculator), executionStack.PopInstance(executionId).GetType());
}

private void TestSingleGenericCalculator<TGenericCalculator, TArg>(TArg first, TArg second, params Type[] decoratorTypes)
{
var calculator = container.Resolve<IGenericCalculator<TArg>>(typeof(TGenericCalculator).FullName);
Guid executionId = Guid.NewGuid();
calculator.Sum(executionId, first, second);

foreach (var decoratorType in decoratorTypes)
{
var instance = executionStack.PopInstance(executionId);
Assert.Equal(instance.GetType(), decoratorType);
}

Assert.Equal(typeof(TGenericCalculator), executionStack.PopInstance(executionId).GetType());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Windsor.Extension\Windsor.Extension.csproj" />
</ItemGroup>

</Project>
Loading