-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from JSkimming/extension-tests
Added the ProxyGeneratorExtensions tests
- Loading branch information
Showing
5 changed files
with
248 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
test/Castle.Core.AsyncInterceptor.Tests/InterfaceProxies/ClassWithVirtualMethodToProxy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
test/Castle.Core.AsyncInterceptor.Tests/ProxyGeneratorExtensionsShould.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |