Skip to content

Commit

Permalink
Merge pull request #2 from JSkimming/extension-tests
Browse files Browse the repository at this point in the history
Added the ProxyGeneratorExtensions tests
  • Loading branch information
JSkimming authored Jun 15, 2016
2 parents e6e9aa4 + ad3971f commit f7edd23
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 107 deletions.
107 changes: 1 addition & 106 deletions src/Castle.Core.AsyncInterceptor/ProxyGeneratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static IInterceptor[] ToInterceptors(this IEnumerable<IAsyncInterceptor>
}

/// <summary>
/// See the <see cref="ProxyGenerator.CreateInterfaceProxyWithTargetInterface{TInterface}(TInterface,IInterceptor[])"/> documentation.
/// See the <see cref="ProxyGenerator.CreateInterfaceProxyWithTarget{TInterface}(TInterface,IInterceptor[])"/> documentation.
/// </summary>
public static TInterface CreateInterfaceProxyWithTarget<TInterface>(
this ProxyGenerator proxyGenerator,
Expand Down Expand Up @@ -221,111 +221,6 @@ public static object CreateInterfaceProxyWithTargetInterface(
interceptors.ToInterceptors());
}

/// <summary>
/// See the <see cref="ProxyGenerator.CreateInterfaceProxyWithoutTarget{TInterface}(IInterceptor)"/> documentation.
/// </summary>
public static TInterface CreateInterfaceProxyWithoutTarget<TInterface>(
this ProxyGenerator proxyGenerator,
IAsyncInterceptor interceptor)
where TInterface : class
{
return proxyGenerator.CreateInterfaceProxyWithoutTarget<TInterface>(interceptor.ToInterceptor());
}

/// <summary>
/// See the <see cref="ProxyGenerator.CreateInterfaceProxyWithoutTarget{TInterface}(IInterceptor[])"/> documentation.
/// </summary>
public static TInterface CreateInterfaceProxyWithoutTarget<TInterface>(
this ProxyGenerator proxyGenerator,
params IAsyncInterceptor[] interceptors)
where TInterface : class
{
return proxyGenerator.CreateInterfaceProxyWithoutTarget<TInterface>(interceptors.ToInterceptors());
}

/// <summary>
/// See the <see cref="ProxyGenerator.CreateInterfaceProxyWithoutTarget{TInterface}(ProxyGenerationOptions, IInterceptor[])"/> documentation.
/// </summary>
public static TInterface CreateInterfaceProxyWithoutTarget<TInterface>(
this ProxyGenerator proxyGenerator,
ProxyGenerationOptions options,
params IAsyncInterceptor[] interceptors)
where TInterface : class
{
return proxyGenerator.CreateInterfaceProxyWithoutTarget<TInterface>(
options,
interceptors.ToInterceptors());
}

/// <summary>
/// See the <see cref="ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type, IInterceptor)"/> documentation.
/// </summary>
public static object CreateInterfaceProxyWithoutTarget(
this ProxyGenerator proxyGenerator,
Type interfaceToProxy,
IAsyncInterceptor interceptor)
{
return proxyGenerator.CreateInterfaceProxyWithoutTarget(interfaceToProxy, interceptor.ToInterceptor());
}

/// <summary>
/// See the <see cref="ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type, IInterceptor[])"/> documentation.
/// </summary>
public static object CreateInterfaceProxyWithoutTarget(
this ProxyGenerator proxyGenerator,
Type interfaceToProxy,
params IAsyncInterceptor[] interceptors)
{
return proxyGenerator.CreateInterfaceProxyWithoutTarget(interfaceToProxy, interceptors.ToInterceptors());
}

/// <summary>
/// See the <see cref="ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type, Type[], IInterceptor[])"/> documentation.
/// </summary>
public static object CreateInterfaceProxyWithoutTarget(
this ProxyGenerator proxyGenerator,
Type interfaceToProxy,
Type[] additionalInterfacesToProxy,
params IAsyncInterceptor[] interceptors)
{
return proxyGenerator.CreateInterfaceProxyWithoutTarget(
interfaceToProxy,
additionalInterfacesToProxy,
interceptors.ToInterceptors());
}

/// <summary>
/// See the <see cref="ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type, ProxyGenerationOptions, IInterceptor[])"/> documentation.
/// </summary>
public static object CreateInterfaceProxyWithoutTarget(
this ProxyGenerator proxyGenerator,
Type interfaceToProxy,
ProxyGenerationOptions options,
params IAsyncInterceptor[] interceptors)
{
return proxyGenerator.CreateInterfaceProxyWithoutTarget(
interfaceToProxy,
options,
interceptors.ToInterceptors());
}

/// <summary>
/// See the <see cref="ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type, Type[], ProxyGenerationOptions, IInterceptor[])"/> documentation.
/// </summary>
public static object CreateInterfaceProxyWithoutTarget(
this ProxyGenerator proxyGenerator,
Type interfaceToProxy,
Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options,
params IAsyncInterceptor[] interceptors)
{
return proxyGenerator.CreateInterfaceProxyWithoutTarget(
interfaceToProxy,
additionalInterfacesToProxy,
options,
interceptors.ToInterceptors());
}

/// <summary>
/// See the <see cref="ProxyGenerator.CreateClassProxyWithTarget{TClass}(TClass, IInterceptor[])"/> documentation.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Castle.DynamicProxy.InterfaceProxies

public class ClassWithInterfaceToProxy : IInterfaceToProxy
{
private readonly ICollection<string> _log;
private readonly List<string> _log;

public ClassWithInterfaceToProxy(List<string> log)
{
Expand All @@ -21,6 +21,8 @@ public ClassWithInterfaceToProxy(List<string> log)
_log = log;
}

public IReadOnlyList<string> Log => _log;

public void SynchronousVoidMethod()
{
_log.Add($"{nameof(SynchronousVoidMethod)}:Start");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2016 James Skimming. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Castle.DynamicProxy.InterfaceProxies
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

public class ClassWithVirtualMethodToProxy
{
private readonly List<string> _log;

protected ClassWithVirtualMethodToProxy()
: this(new List<string>())
{
}

public ClassWithVirtualMethodToProxy(List<string> log)
{
if (log == null)
throw new ArgumentNullException(nameof(log));

_log = log;
}

public IReadOnlyList<string> Log => _log;

public virtual async Task<Guid> AsynchronousResultMethod()
{
_log.Add($"{nameof(AsynchronousResultMethod)}:Start");
await Task.Delay(10);
_log.Add($"{nameof(AsynchronousResultMethod)}:End");
return Guid.NewGuid();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
namespace Castle.DynamicProxy.InterfaceProxies
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

public interface IInterfaceToProxy
{
IReadOnlyList<string> Log { get; }

void SynchronousVoidMethod();

Guid SynchronousResultMethod();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
// Copyright (c) 2016 James Skimming. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Castle.DynamicProxy
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Castle.DynamicProxy.InterfaceProxies;
using Xunit;

public class ProxyGeneratorExtensionsShould
{
private static readonly ProxyGenerator Generator = new ProxyGenerator();

public static IEnumerable<object[]> InterfaceProxyFactories()
{
Func<ProxyGenerator, List<string>, IInterfaceToProxy>[] proxyFactories =
{
(gen, log) => gen.CreateInterfaceProxyWithTarget<IInterfaceToProxy>(
new ClassWithInterfaceToProxy(log),
new TestAsyncInterceptor(log)),
(gen, log) => gen.CreateInterfaceProxyWithTarget<IInterfaceToProxy>(
new ClassWithInterfaceToProxy(log),
ProxyGenerationOptions.Default,
new TestAsyncInterceptor(log)),
(gen, log) => (IInterfaceToProxy) gen.CreateInterfaceProxyWithTarget(
typeof(IInterfaceToProxy),
new ClassWithInterfaceToProxy(log),
new TestAsyncInterceptor(log)),
(gen, log) => (IInterfaceToProxy) gen.CreateInterfaceProxyWithTarget(
typeof(IInterfaceToProxy),
new ClassWithInterfaceToProxy(log),
ProxyGenerationOptions.Default,
new TestAsyncInterceptor(log)),
(gen, log) => (IInterfaceToProxy) gen.CreateInterfaceProxyWithTarget(
typeof(IInterfaceToProxy),
default(Type[]),
new ClassWithInterfaceToProxy(log),
new TestAsyncInterceptor(log)),
(gen, log) => (IInterfaceToProxy) gen.CreateInterfaceProxyWithTarget(
typeof(IInterfaceToProxy),
default(Type[]),
new ClassWithInterfaceToProxy(log),
ProxyGenerationOptions.Default,
new TestAsyncInterceptor(log)),
(gen, log) => (IInterfaceToProxy) gen.CreateInterfaceProxyWithTargetInterface(
typeof(IInterfaceToProxy),
new ClassWithInterfaceToProxy(log),
new TestAsyncInterceptor(log)),
(gen, log) => gen.CreateInterfaceProxyWithTargetInterface<IInterfaceToProxy>(
new ClassWithInterfaceToProxy(log),
new TestAsyncInterceptor(log)),
(gen, log) => gen.CreateInterfaceProxyWithTargetInterface<IInterfaceToProxy>(
new ClassWithInterfaceToProxy(log),
ProxyGenerationOptions.Default,
new TestAsyncInterceptor(log)),
(gen, log) => (IInterfaceToProxy) gen.CreateInterfaceProxyWithTargetInterface(
typeof(IInterfaceToProxy),
default(Type[]),
new ClassWithInterfaceToProxy(log),
new TestAsyncInterceptor(log)),
(gen, log) => (IInterfaceToProxy) gen.CreateInterfaceProxyWithTargetInterface(
typeof(IInterfaceToProxy),
new ClassWithInterfaceToProxy(log),
ProxyGenerationOptions.Default,
new TestAsyncInterceptor(log)),
(gen, log) => (IInterfaceToProxy) gen.CreateInterfaceProxyWithTargetInterface(
typeof(IInterfaceToProxy),
default(Type[]),
new ClassWithInterfaceToProxy(log),
ProxyGenerationOptions.Default,
new TestAsyncInterceptor(log)),
};

return proxyFactories.Select(p => new object[] { p, new List<string>() });
}

[Theory]
[MemberData(nameof(InterfaceProxyFactories))]
public async Task ExtendInterfaceProxyGenerator(
Func<ProxyGenerator, List<string>, IInterfaceToProxy> proxyFactory,
List<string> log)
{
// Act
IInterfaceToProxy proxy = proxyFactory(Generator, log);
Guid result = await proxy.AsynchronousResultMethod();

// Assert
const string methodName = nameof(IInterfaceToProxy.AsynchronousResultMethod);
Assert.NotEqual(Guid.Empty, result);
Assert.Equal(4, log.Count);
Assert.Equal($"{methodName}:InterceptStart", log[0]);
Assert.Equal($"{methodName}:InterceptEnd", log[3]);
}

public static IEnumerable<object[]> ClassProxyFactories()
{
Func<ProxyGenerator, List<string>, ClassWithVirtualMethodToProxy>[] proxyFactories =
{
(gen, log) => gen.CreateClassProxyWithTarget(
new ClassWithVirtualMethodToProxy(log),
new TestAsyncInterceptor(log)),
(gen, log) => gen.CreateClassProxyWithTarget(
new ClassWithVirtualMethodToProxy(log),
ProxyGenerationOptions.Default,
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxyWithTarget(
typeof(ClassWithVirtualMethodToProxy),
default(Type[]),
new ClassWithVirtualMethodToProxy(log),
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxyWithTarget(
typeof(ClassWithVirtualMethodToProxy),
new ClassWithVirtualMethodToProxy(log),
ProxyGenerationOptions.Default,
new object[] { log },
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxyWithTarget(
typeof(ClassWithVirtualMethodToProxy),
new ClassWithVirtualMethodToProxy(log),
new object[] { log },
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxyWithTarget(
typeof(ClassWithVirtualMethodToProxy),
new ClassWithVirtualMethodToProxy(log),
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxyWithTarget(
typeof(ClassWithVirtualMethodToProxy),
new ClassWithVirtualMethodToProxy(log),
ProxyGenerationOptions.Default,
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxyWithTarget(
typeof(ClassWithVirtualMethodToProxy),
default(Type[]),
new ClassWithVirtualMethodToProxy(log),
ProxyGenerationOptions.Default,
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxyWithTarget(
typeof(ClassWithVirtualMethodToProxy),
default(Type[]),
new ClassWithVirtualMethodToProxy(log),
ProxyGenerationOptions.Default,
new object[] { log },
new TestAsyncInterceptor(log)),
(gen, log) => gen.CreateClassProxy<ClassWithVirtualMethodToProxy>(new TestAsyncInterceptor(log)),
(gen, log) => gen.CreateClassProxy<ClassWithVirtualMethodToProxy>(
ProxyGenerationOptions.Default,
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxy(
typeof(ClassWithVirtualMethodToProxy),
default(Type[]),
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxy(
typeof(ClassWithVirtualMethodToProxy),
ProxyGenerationOptions.Default,
new object[] { log },
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxy(
typeof(ClassWithVirtualMethodToProxy),
new object[] { log },
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxy(
typeof(ClassWithVirtualMethodToProxy),
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxy(
typeof(ClassWithVirtualMethodToProxy),
ProxyGenerationOptions.Default,
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxy(
typeof(ClassWithVirtualMethodToProxy),
default(Type[]),
ProxyGenerationOptions.Default,
new TestAsyncInterceptor(log)),
(gen, log) => (ClassWithVirtualMethodToProxy) gen.CreateClassProxy(
typeof(ClassWithVirtualMethodToProxy),
default(Type[]),
ProxyGenerationOptions.Default,
new object[] { log },
new TestAsyncInterceptor(log)),
};

return proxyFactories.Select(p => new object[] { p, new List<string>() });
}

[Theory]
[MemberData(nameof(ClassProxyFactories))]
public async Task ExtendClassProxyGenerator(
Func<ProxyGenerator, List<string>, ClassWithVirtualMethodToProxy> proxyFactory,
List<string> log)
{
// Act
ClassWithVirtualMethodToProxy proxy = proxyFactory(Generator, log);
Guid result = await proxy.AsynchronousResultMethod();

// Assert
const string methodName = nameof(ClassWithVirtualMethodToProxy.AsynchronousResultMethod);
Assert.NotEqual(Guid.Empty, result);
Assert.Equal($"{methodName}:InterceptStart", log.First());
Assert.Equal($"{methodName}:InterceptEnd", log.Last());
}
}
}

0 comments on commit f7edd23

Please sign in to comment.