Fixed test issue - PEVerify is now called with a quoted assembly path (to allow for...
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy.Tests / OutRefParams.cs
blobfeb83ebbad5f03f5a31233e594edd5958e7e6c2f
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 : BasePEVerifyTestCase
26 [Test]
27 public void CanCreateProxyOfInterfaceWithOutParameter()
29 KeepDataInterceptor interceptor = new KeepDataInterceptor();
30 object proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(WithOut), interceptor);
31 Assert.IsNotNull(proxy);
34 [Test]
35 public void CanCallMethodWithOutParameter()
37 int i;
38 InvocatingInterceptor interceptor = new InvocatingInterceptor(delegate { });
39 WithOut proxy = (WithOut) generator.CreateInterfaceProxyWithoutTarget(typeof(WithOut), interceptor);
40 proxy.Do(out i);
43 [Test]
44 public void CanAffectValueOfOutParameter()
46 int i;
47 InvocatingInterceptor interceptor =
48 new InvocatingInterceptor(delegate(IInvocation invocation) { invocation.Arguments[0] = 5; });
49 WithOut proxy = (WithOut) generator.CreateInterfaceProxyWithoutTarget(typeof(WithOut), interceptor);
50 proxy.Do(out i);
51 Assert.AreEqual(5, i);
54 [Test]
55 public void CanCreateProxyWithRefParam()
57 int i = 3;
58 InvocatingInterceptor interceptor =
59 new InvocatingInterceptor(delegate(IInvocation invocation) { invocation.Arguments[0] = 5; });
60 WithOut proxy = (WithOut) generator.CreateInterfaceProxyWithoutTarget(typeof(WithOut), interceptor);
61 proxy.Did(ref i);
62 Assert.AreEqual(5, i);
66 [Test]
67 public void CanCreateComplexOutRefProxyOnClass()
69 int i = 3;
70 string s1 = "2";
71 string s2;
72 InvocatingInterceptor interceptor = new InvocatingInterceptor(delegate(IInvocation invocation)
74 invocation.Arguments[0] = 5;
75 invocation.Arguments[1] = "aaa";
76 invocation.Arguments[3] = "bbb";
77 });
78 MyClass proxy = (MyClass) generator.CreateClassProxy(typeof(MyClass), interceptor);
79 proxy.MyMethod(out i, ref s1, 1, out s2);
80 Assert.AreEqual(5, i);
81 Assert.AreEqual(s1, "aaa");
82 Assert.AreEqual(s2, "bbb");
85 [Test, Explicit]
86 public void CanCreateProxyWithStructRefParam()
88 MyStruct s = new MyStruct(10);
89 MyClass proxy = (MyClass) generator.CreateClassProxy(typeof(MyClass), new StandardInterceptor());
90 proxy.MyMethodWithStruct(ref s);
91 Assert.AreEqual(20, s.Value);
94 public struct MyStruct
96 public int Value;
98 public MyStruct(int value)
100 Value = value;
104 public interface WithOut
106 void Do(out int i);
107 void Did(ref int i);
111 public class MyClass
113 public virtual void MyMethod(out int i, ref string s, int i1, out string s2)
115 throw new NotImplementedException();
118 public virtual void MyMethodWithStruct(ref MyStruct s)
120 s.Value = 2 * s.Value;
124 public class InvocatingInterceptor : IInterceptor
126 public delegate void Invoked(IInvocation invocation);
128 private Invoked invoked;
130 public InvocatingInterceptor(Invoked invoked)
132 this.invoked = invoked;
136 public void Intercept(IInvocation invocation)
138 invoked(invocation);