Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy.Tests / OutRefParams.cs
blobcf77a204f0b002d3d2c409b86bb8bc62cc88b45b
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.DynamicProxy.Tests
17 using System;
18 using Castle.Core.Interceptor;
19 using Castle.DynamicProxy.Tests.Interceptors;
20 using NUnit.Framework;
22 [TestFixture]
23 public class OutRefParams : BasePEVerifyTestCase
25 [Test]
26 public void CanCreateProxyOfInterfaceWithOutParameter()
28 KeepDataInterceptor interceptor = new KeepDataInterceptor();
29 object proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(WithOut), interceptor);
30 Assert.IsNotNull(proxy);
33 [Test]
34 public void CanCallMethodWithOutParameter()
36 int i;
37 InvocatingInterceptor interceptor = new InvocatingInterceptor(delegate { });
38 WithOut proxy = (WithOut) generator.CreateInterfaceProxyWithoutTarget(typeof(WithOut), interceptor);
39 proxy.Do(out i);
42 [Test]
43 public void CanAffectValueOfOutParameter()
45 int i;
46 InvocatingInterceptor interceptor =
47 new InvocatingInterceptor(delegate(IInvocation invocation) { invocation.Arguments[0] = 5; });
48 WithOut proxy = (WithOut) generator.CreateInterfaceProxyWithoutTarget(typeof(WithOut), interceptor);
49 proxy.Do(out i);
50 Assert.AreEqual(5, i);
53 [Test]
54 public void CanCreateProxyWithRefParam()
56 int i = 3;
57 InvocatingInterceptor interceptor =
58 new InvocatingInterceptor(delegate(IInvocation invocation) { invocation.Arguments[0] = 5; });
59 WithOut proxy = (WithOut) generator.CreateInterfaceProxyWithoutTarget(typeof(WithOut), interceptor);
60 proxy.Did(ref i);
61 Assert.AreEqual(5, i);
65 [Test]
66 public void CanCreateComplexOutRefProxyOnClass()
68 int i = 3;
69 string s1 = "2";
70 string s2;
71 InvocatingInterceptor interceptor = new InvocatingInterceptor(delegate(IInvocation invocation)
73 invocation.Arguments[0] = 5;
74 invocation.Arguments[1] = "aaa";
75 invocation.Arguments[3] = "bbb";
76 });
77 MyClass proxy = (MyClass) generator.CreateClassProxy(typeof(MyClass), interceptor);
78 proxy.MyMethod(out i, ref s1, 1, out s2);
79 Assert.AreEqual(5, i);
80 Assert.AreEqual(s1, "aaa");
81 Assert.AreEqual(s2, "bbb");
84 [Test, Explicit]
85 public void CanCreateProxyWithStructRefParam()
87 MyStruct s = new MyStruct(10);
88 MyClass proxy = (MyClass) generator.CreateClassProxy(typeof(MyClass), new StandardInterceptor());
89 proxy.MyMethodWithStruct(ref s);
90 Assert.AreEqual(20, s.Value);
93 public struct MyStruct
95 public int Value;
97 public MyStruct(int value)
99 Value = value;
103 public interface WithOut
105 void Do(out int i);
106 void Did(ref int i);
110 public class MyClass
112 public virtual void MyMethod(out int i, ref string s, int i1, out string s2)
114 throw new NotImplementedException();
117 public virtual void MyMethodWithStruct(ref MyStruct s)
119 s.Value = 2 * s.Value;
123 public class InvocatingInterceptor : IInterceptor
125 public delegate void Invoked(IInvocation invocation);
127 private Invoked invoked;
129 public InvocatingInterceptor(Invoked invoked)
131 this.invoked = invoked;
135 public void Intercept(IInvocation invocation)
137 invoked(invocation);