Skip to content

Commit

Permalink
Adding post-build step to NSubstitute.csproj to ilmerge assemblies
Browse files Browse the repository at this point in the history
Needed to modify tests that referenced Castle types as it caused ambiguous matches afer internalising Castle references within NSubstitute.dll.
Removed Castle references from test project.
Updated CastleDynamicProxyFactory to create its own Castle ProxyGenerator instance so it can be tested without knowing about Castle types.

Closes #1.
  • Loading branch information
dtchepak committed May 4, 2010
1 parent 8e4fff4 commit c78947b
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 104 deletions.
11 changes: 2 additions & 9 deletions Source/NSubstitute.Specs/NSubstitute.Specs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@
<AssemblyOriginatorKeyFile>..\nsubstitute.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core">
<HintPath>..\..\ThirdParty\Castle.DynamicProxy-2.2\bin\net-35\Castle.Core.dll</HintPath>
<Aliases>CastleCore</Aliases>
</Reference>
<Reference Include="Castle.DynamicProxy2">
<HintPath>..\..\ThirdParty\Castle.DynamicProxy-2.2\bin\net-35\Castle.DynamicProxy2.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=2.5.0.9122, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\ThirdParty\NUnit-2.5.0.9122\bin\net-2.0\nunit.framework.dll</HintPath>
Expand Down Expand Up @@ -95,6 +88,7 @@
<Compile Include="CallResultsSpecs.cs" />
<Compile Include="CallSpecificationFactorySpecs.cs" />
<Compile Include="CallSpecificationSpecs.cs" />
<Compile Include="Proxies\CastleDynamicProxy\CastleDynamicProxyFactorySpecs.cs" />
<Compile Include="RoutePartsSpecs.cs" />
<Compile Include="Routes\CheckCallReceivedRouteSpecs.cs" />
<Compile Include="Routes\ConcernForRoute.cs" />
Expand All @@ -109,8 +103,6 @@
<Compile Include="Infrastructure\TemporaryChangeNotConfiguredProperlyException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PropertyHelperSpecs.cs" />
<Compile Include="Proxies\CastleDynamicProxy\CastleDynamicProxyFactorySpecs.cs" />
<Compile Include="Proxies\CastleDynamicProxy\CastleForwardingInterceptorSpecs.cs" />
<Compile Include="ReceivedExtensionSpec.cs" />
<Compile Include="Routes\Handlers\RecordCallHandlerSpecs.cs" />
<Compile Include="Routes\Handlers\ReturnConfiguredResultHandlerSpecs.cs" />
Expand Down Expand Up @@ -149,6 +141,7 @@
<ItemGroup>
<None Include="..\nsubstitute.snk" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
extern alias CastleCore;
using Castle.DynamicProxy;
using NSubstitute.Routes;
using NSubstitute.Specs.Infrastructure;
using NSubstitute.Specs.SampleStructures;
using Rhino.Mocks;
Expand All @@ -11,37 +8,26 @@ namespace NSubstitute.Specs.Proxies.CastleDynamicProxy
{
public class CastleDynamicProxyFactorySpecs
{
public abstract class Concern : ConcernFor<CastleDynamicProxyFactory>
public abstract class When_creating_a_proxy<TFoo> : ConcernFor<CastleDynamicProxyFactory> where TFoo : class, IFoo
{
protected ICallRouter _callRouter;
ICallRouter _callRouter;
TFoo _result;

public override void Context()
{
_callRouter = mock<ICallRouter>();
}

public override CastleDynamicProxyFactory CreateSubjectUnderTest()
{
return new CastleDynamicProxyFactory(new ProxyGenerator(), new CastleInterceptorFactory());
}

protected void AssertCallsMadeToResultsCallRouterAreForwardedToOriginalRouter(object result)
public override void Because()
{
var resultsCallRouter = (ICallRouter) result;
resultsCallRouter.SetRoute<CheckCallReceivedRoute>();
_callRouter.received(x => x.SetRoute<CheckCallReceivedRoute>());
_result = sut.GenerateProxy<TFoo>(_callRouter);
}
}

public class When_creating_a_proxy_for_an_interface : Concern
{
IFoo _result;

[Test]
public void Should_generate_a_proxy_that_forwards_to_call_router()
{
_result.Goo();
_callRouter.AssertWasCalled(x => x.Route(Arg<ICall>.Matches(arg => arg.GetMethodInfo().Name == "Goo")));
_callRouter.received(x => x.Route(Arg<ICall>.Matches(call => CallWasToMethodNamed(call, "Goo"))));
}

[Test]
Expand All @@ -51,34 +37,58 @@ public void Should_be_able_to_cast_proxy_to_its_call_router()
AssertCallsMadeToResultsCallRouterAreForwardedToOriginalRouter(_result);
}

public override void Because()
[Test]
public void Should_forward_call_with_original_arguments_to_router_and_return_value_from_route()
{
_result = sut.GenerateProxy<IFoo>(_callRouter);
const int aNumber = 5;
const string aString = "Some string";
const string returnFromRoute = "value from route";
_callRouter
.stub(x => x.Route(Arg<ICall>.Matches(
call => CallWasToMethodNamed(call, "Bar") && CallArgsWere(call, new object[] {aNumber, aString})
)
))
.Return(returnFromRoute);

var returnValue = _result.Bar(aNumber, aString);
Assert.That(returnValue, Is.EqualTo(returnFromRoute));
}
}

public class When_creating_a_proxy_for_a_class : Concern
{
Foo _result;
public override CastleDynamicProxyFactory CreateSubjectUnderTest()
{
return new CastleDynamicProxyFactory(new CastleInterceptorFactory());
}

[Test]
public void Should_generate_a_proxy_that_forwards_to_call_router()
private bool CallWasToMethodNamed(ICall call, string methodName)
{
_result.Goo();
_callRouter.AssertWasCalled(x => x.Route(Arg<ICall>.Matches(arg => arg.GetMethodInfo().Name == "Goo")));
return call.GetMethodInfo().Name == methodName;
}

[Test]
public void Should_be_able_to_cast_proxy_to_its_call_router()
private bool CallArgsWere(ICall call, object[] expectedArguments)
{
Assert.That(_result, Is.InstanceOf<ICallRouter>());
AssertCallsMadeToResultsCallRouterAreForwardedToOriginalRouter(_result);
var callArguments = call.GetArguments();
if (callArguments.Length != expectedArguments.Length) return false;
for (int i = 0; i < callArguments.Length; i++)
{
if (!callArguments[i].Equals(expectedArguments[i])) return false;
}
return true;
}

public override void Because()
protected void AssertCallsMadeToResultsCallRouterAreForwardedToOriginalRouter(object result)
{
_result = sut.GenerateProxy<Foo>(_callRouter);
var resultsCallRouter = (ICallRouter) result;
resultsCallRouter.SetRoute<IRoute>();
_callRouter.received(x => x.SetRoute<IRoute>());
}
}

public class When_creating_a_proxy_for_an_interface : When_creating_a_proxy<IFoo>
{
}

public class When_creating_a_proxy_for_a_class : When_creating_a_proxy<Foo>
{
}
}
}

This file was deleted.

2 changes: 2 additions & 0 deletions Source/NSubstitute.Specs/SampleStructures/Foo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace NSubstitute.Specs.SampleStructures
public class Foo : IFoo
{
public virtual void Goo() {}
public virtual string Bar(int aNumber, string aString) { return null; }

public event EventHandler Boo;

public void OnBoo(EventArgs e)
Expand Down
1 change: 1 addition & 0 deletions Source/NSubstitute.Specs/SampleStructures/IFoo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace NSubstitute.Specs.SampleStructures
public interface IFoo
{
void Goo();
string Bar(int aNumber, string aString);
event EventHandler Boo;
}
}
7 changes: 6 additions & 1 deletion Source/NSubstitute/NSubstitute.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
-->
<Target Name="AfterBuild">
<CreateItem Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)'=='.dll'">
<Output ItemName="AssembliesToMerge" TaskParameter="Include" />
</CreateItem>
<exec command="&quot;$(MSBuildProjectDirectory)\..\..\ThirdParty\Ilmerge\ILMerge.exe&quot; /internalize:&quot;$(MSBuildProjectDirectory)\ilmerge.exclude&quot; /ndebug /keyfile:$(AssemblyOriginatorKeyFile) /out:@(MainAssembly) &quot;@(IntermediateAssembly)&quot; @(AssembliesToMerge->'&quot;%(FullPath)&quot;', ' ')" />
<delete files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" />
</Target>
-->
</Project>
1 change: 0 additions & 1 deletion Source/NSubstitute/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("NSubstitute")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class CastleDynamicProxyFactory : IProxyFactory
readonly CastleInterceptorFactory _interceptorFactory;
readonly IgnoreCallRouterCallsHook _ignoreCallRouterCallsHook;

public CastleDynamicProxyFactory(ProxyGenerator proxyGenerator, CastleInterceptorFactory interceptorFactory)
public CastleDynamicProxyFactory(CastleInterceptorFactory interceptorFactory)
{
_proxyGenerator = proxyGenerator;
_proxyGenerator = new ProxyGenerator();
_interceptorFactory = interceptorFactory;
_ignoreCallRouterCallsHook = new IgnoreCallRouterCallsHook();
}
Expand Down
3 changes: 1 addition & 2 deletions Source/NSubstitute/SubstitutionContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using Castle.DynamicProxy;
using NSubstitute.Exceptions;
using NSubstitute.Proxies.CastleDynamicProxy;
using NSubstitute.Routes;
Expand All @@ -24,7 +23,7 @@ static SubstitutionContext()
{
var callRouterFactory = new CallRouterFactory();
var interceptorFactory = new CastleInterceptorFactory();
var proxyFactory = new CastleDynamicProxyFactory(new ProxyGenerator(), interceptorFactory);
var proxyFactory = new CastleDynamicProxyFactory(interceptorFactory);
_substituteFactory = new SubstituteFactory(this, callRouterFactory, proxyFactory);
_argumentSpecifications = new List<IArgumentSpecification>();
}
Expand Down
5 changes: 5 additions & 0 deletions Source/NSubstitute/ilmerge.exclude
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Castle.DynamicProxy.CompositionInvocation
Castle.DynamicProxy.AbstractInvocation
Castle.DynamicProxy.InheritanceInvocation
Castle.Core.Interceptor.IProxyTargetAccessor
Castle.DynamicProxy.Generators.AttributesToAvoidReplicating

0 comments on commit c78947b

Please sign in to comment.