Skip to content

Commit 437b383

Browse files
committed
Add tests for IInvocation.[Method]InvocationTarget
... for default interface methods. These tend to currently fail with `ArgumentNullException`.
1 parent 7a06b94 commit 437b383

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

src/Castle.Core.Tests/DynamicProxy.Tests/DefaultInterfaceMembersTestCase.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ public void Can_proceed_to_method_default_implementation_in_proxied_interface()
100100
Assert.AreEqual(expected, actual);
101101
}
102102

103+
[Test]
104+
public void Invocation_InvocationTarget_is_proxy_for_intercepted_method_with_default_implementation()
105+
{
106+
var interceptor = new KeepDataInterceptor();
107+
var proxy = generator.CreateInterfaceProxyWithoutTarget<IHaveMethodWithDefaultImplementation>(interceptor);
108+
proxy.MethodWithDefaultImplementation();
109+
Assert.AreSame(
110+
expected: proxy,
111+
actual: interceptor.Invocation.InvocationTarget);
112+
}
113+
114+
[Test]
115+
public void Invocation_MethodInvocationTarget_is_intercepted_method_with_default_implementation()
116+
{
117+
var interceptor = new KeepDataInterceptor();
118+
var proxy = generator.CreateInterfaceProxyWithoutTarget<IHaveMethodWithDefaultImplementation>(interceptor);
119+
proxy.MethodWithDefaultImplementation();
120+
Assert.AreSame(
121+
expected: typeof(IHaveMethodWithDefaultImplementation).GetMethod(nameof(IHaveMethodWithDefaultImplementation.MethodWithDefaultImplementation)),
122+
actual: interceptor.Invocation.MethodInvocationTarget);
123+
}
124+
103125
#endregion
104126

105127
#region Generic methods with default implementation
@@ -296,6 +318,28 @@ public void Can_proceed_to_method_default_implementation_from_additional_interfa
296318
Assert.AreEqual(expected, actual);
297319
}
298320

321+
[Test]
322+
public void Invocation_InvocationTarget_is_proxy_for_intercepted_method_with_default_implementation_from_additional_interface()
323+
{
324+
var interceptor = new KeepDataInterceptor();
325+
var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IEmpty), new[] { typeof(IHaveMethodWithDefaultImplementation) }, interceptor);
326+
((IHaveMethodWithDefaultImplementation)proxy).MethodWithDefaultImplementation();
327+
Assert.AreSame(
328+
expected: proxy,
329+
actual: interceptor.Invocation.InvocationTarget);
330+
}
331+
332+
[Test]
333+
public void Invocation_MethodInvocationTarget_is_intercepted_method_with_default_implementation_from_additional_interface()
334+
{
335+
var interceptor = new KeepDataInterceptor();
336+
var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IEmpty), new[] { typeof(IHaveMethodWithDefaultImplementation) }, interceptor);
337+
((IHaveMethodWithDefaultImplementation)proxy).MethodWithDefaultImplementation();
338+
Assert.AreSame(
339+
expected: typeof(IHaveMethodWithDefaultImplementation).GetMethod(nameof(IHaveMethodWithDefaultImplementation.MethodWithDefaultImplementation)),
340+
actual: interceptor.Invocation.MethodInvocationTarget);
341+
}
342+
299343
#endregion
300344

301345
#region Non-public methods with default implementation
@@ -457,6 +501,34 @@ public void Can_proceed_to_method_default_implementation_from_mixin_in_proxied_i
457501
var actual = ((IHaveMethodWithDefaultImplementation)proxy).MethodWithDefaultImplementation();
458502
Assert.AreEqual(expected, actual);
459503
}
504+
505+
[Test]
506+
public void Invocation_InvocationTarget_is_mixin_for_intercepted_method_with_default_implementation_from_mixin()
507+
{
508+
var options = new ProxyGenerationOptions();
509+
var mixin = new InheritsMethodWithDefaultImplementation();
510+
options.AddMixinInstance(mixin);
511+
var interceptor = new KeepDataInterceptor();
512+
var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IEmpty), options, interceptor);
513+
((IHaveMethodWithDefaultImplementation)proxy).MethodWithDefaultImplementation();
514+
Assert.AreSame(
515+
expected: mixin,
516+
actual: interceptor.Invocation.InvocationTarget);
517+
}
518+
519+
[Test]
520+
public void Invocation_MethodInvocationTarget_is_intercepted_method_with_default_implementation_from_mixin()
521+
{
522+
var options = new ProxyGenerationOptions();
523+
options.AddMixinInstance(new InheritsMethodWithDefaultImplementation());
524+
var interceptor = new KeepDataInterceptor();
525+
var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IEmpty), options, interceptor);
526+
((IHaveMethodWithDefaultImplementation)proxy).MethodWithDefaultImplementation();
527+
Assert.AreSame(
528+
expected: typeof(IHaveMethodWithDefaultImplementation).GetMethod(nameof(IHaveMethodWithDefaultImplementation.MethodWithDefaultImplementation)),
529+
actual: interceptor.Invocation.MethodInvocationTarget);
530+
}
531+
460532
#endregion
461533

462534
public interface IHaveGenericMethodWithDefaultImplementation

0 commit comments

Comments
 (0)