Applied patch from Fabian Schmied:
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy.Tests / OutRefParams.cs
blob27f44557d2e57dd6a852ec47faa33f1149438c59
1 // Copyright 2004-2007 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.
16 namespace Castle.DynamicProxy.Tests
18 using System;
19 using Castle.Core.Interceptor;
20 using Castle.DynamicProxy.Tests.Interceptors;
21 using NUnit.Framework;
23 [TestFixture]
24 public class OutRefParams
26 private ProxyGenerator generator;
28 [SetUp]
29 public void Init()
31 generator = new ProxyGenerator();
34 [Test]
35 public void CanCreateProxyOfInterfaceWithOutParameter()
37 KeepDataInterceptor interceptor = new KeepDataInterceptor();
38 object proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(WithOut), interceptor);
39 Assert.IsNotNull(proxy);
42 [Test]
43 public void CanCallMethodWithOutParameter()
45 int i;
46 InvocatingInterceptor interceptor = new InvocatingInterceptor(delegate { });
47 WithOut proxy = (WithOut) generator.CreateInterfaceProxyWithoutTarget(typeof(WithOut), interceptor);
48 proxy.Do(out i);
51 [Test]
52 public void CanAffectValueOfOutParameter()
54 int i;
55 InvocatingInterceptor interceptor =
56 new InvocatingInterceptor(delegate(IInvocation invocation) { invocation.Arguments[0] = 5; });
57 WithOut proxy = (WithOut) generator.CreateInterfaceProxyWithoutTarget(typeof(WithOut), interceptor);
58 proxy.Do(out i);
59 Assert.AreEqual(5, i);
62 [Test]
63 public void CanCreateProxyWithRefParam()
65 int i = 3;
66 InvocatingInterceptor interceptor =
67 new InvocatingInterceptor(delegate(IInvocation invocation) { invocation.Arguments[0] = 5; });
68 WithOut proxy = (WithOut) generator.CreateInterfaceProxyWithoutTarget(typeof(WithOut), interceptor);
69 proxy.Did(ref i);
70 Assert.AreEqual(5, i);
74 [Test]
75 public void CanCreateComplexOutRefProxyOnClass()
77 int i = 3;
78 string s1 = "2";
79 string s2;
80 InvocatingInterceptor interceptor = new InvocatingInterceptor(delegate(IInvocation invocation)
82 invocation.Arguments[0] = 5;
83 invocation.Arguments[1] = "aaa";
84 invocation.Arguments[3] = "bbb";
85 });
86 MyClass proxy = (MyClass) generator.CreateClassProxy(typeof(MyClass), interceptor);
87 proxy.MyMethod(out i, ref s1, 1, out s2);
88 Assert.AreEqual(5, i);
89 Assert.AreEqual(s1, "aaa");
90 Assert.AreEqual(s2, "bbb");
93 [Test, Explicit]
94 public void CanCreateProxyWithStructRefParam()
96 MyStruct s = new MyStruct(10);
97 MyClass proxy = (MyClass)generator.CreateClassProxy(typeof(MyClass), new StandardInterceptor());
98 proxy.MyMethodWithStruct(ref s);
99 Assert.AreEqual(20, s.Value);
102 public struct MyStruct
104 public int Value;
105 public MyStruct(int value)
107 Value = value;
111 public interface WithOut
113 void Do(out int i);
114 void Did(ref int i);
118 public class MyClass
120 public virtual void MyMethod(out int i, ref string s, int i1, out string s2)
122 throw new NotImplementedException();
125 public virtual void MyMethodWithStruct(ref MyStruct s)
127 s.Value = 2 * s.Value;
131 public class InvocatingInterceptor : IInterceptor
133 public delegate void Invoked(IInvocation invocation);
135 private Invoked invoked;
137 public InvocatingInterceptor(Invoked invoked)
139 this.invoked = invoked;
143 public void Intercept(IInvocation invocation)
145 invoked(invocation);