2009-08-26 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Reflection / ParameterInfoTest.cs
1 //
2 // ParameterInfoTest - NUnit Test Cases for the ParameterInfo class
3 //
4 // Zoltan Varga (vargaz@freemail.hu)
5 //
6 // (C) Ximian, Inc.  http://www.ximian.com
7 //
8 //
9
10 using System;
11 using System.Threading;
12 using System.Reflection;
13 #if !TARGET_JVM
14 using System.Reflection.Emit;
15 #endif // TARGET_JVM
16 using System.Runtime.InteropServices;
17
18 using NUnit.Framework;
19
20 namespace MonoTests.System.Reflection
21 {
22         public class Marshal1 : ICustomMarshaler
23         {
24                 public static ICustomMarshaler GetInstance (string s)
25                 {
26                         return new Marshal1 ();
27                 }
28
29                 public void CleanUpManagedData (object managedObj)
30                 {
31                 }
32
33                 public void CleanUpNativeData (IntPtr pNativeData)
34                 {
35                 }
36
37                 public int GetNativeDataSize ()
38                 {
39                         return 4;
40                 }
41
42                 public IntPtr MarshalManagedToNative (object managedObj)
43                 {
44                         return IntPtr.Zero;
45                 }
46
47                 public object MarshalNativeToManaged (IntPtr pNativeData)
48                 {
49                         return null;
50                 }
51         }
52
53         [TestFixture]
54         public class ParameterInfoTest
55         {
56                 [Test]
57                 public void IsDefined_AttributeType_Null ()
58                 {
59                         MethodInfo mi = typeof (object).GetMethod ("Equals", 
60                                 new Type [1] { typeof (object) });
61                         ParameterInfo pi = mi.GetParameters () [0];
62
63                         try {
64                                 pi.IsDefined ((Type) null, false);
65                                 Assert.Fail ("#1");
66                         } catch (ArgumentNullException ex) {
67                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
68                                 Assert.IsNull (ex.InnerException, "#3");
69                                 Assert.IsNotNull (ex.Message, "#4");
70                                 Assert.IsNotNull (ex.ParamName, "#5");
71                                 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
72                         }
73                 }
74
75 #if NET_2_0
76                 public enum ParamEnum {
77                         None = 0,
78                         Foo = 1,
79                         Bar = 2
80                 };
81
82                 public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m, [DefaultParameterValue (ParamEnum.Foo)] ParamEnum n)
83                 {
84                 }
85
86 #if !TARGET_JVM // No support for extern methods in TARGET_JVM
87                 [DllImport ("foo")]
88                 public extern static void marshalAsMethod (
89                         [MarshalAs(UnmanagedType.Bool)]int p0, 
90                         [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string [] p1,
91                         [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object p2);
92 #endif
93                 [Test]
94                 public void DefaultValueEnum () {
95                         ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
96
97                         Assert.AreEqual (typeof (ParamEnum), info [5].DefaultValue.GetType (), "#1");
98                         Assert.AreEqual (ParamEnum.Foo, info [5].DefaultValue, "#2");
99                 }
100
101                 [Test] // bug #339013
102                 public void TestDefaultValues ()
103                 {
104                         ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
105
106                         Assert.AreEqual (pi [0].DefaultValue.GetType (), typeof (DBNull), "#1");
107                         Assert.AreEqual (pi [1].DefaultValue.GetType (), typeof (Missing), "#2");
108                 }
109
110                 public void Sample (int a, [Optional] int b)
111                 {
112                 }
113
114                 [Test]
115                 public void PseudoCustomAttributes () {
116                         ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
117                         Assert.AreEqual (0, info[0].GetCustomAttributes (true).Length, "#A1");
118                         Assert.AreEqual (1, info[1].GetCustomAttributes (typeof (InAttribute), true).Length, "#A2");
119                         Assert.AreEqual (1, info[2].GetCustomAttributes (typeof (OutAttribute), true).Length, "#A3");
120                         Assert.AreEqual (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length, "#A4");
121                         Assert.AreEqual (2, info[4].GetCustomAttributes (true).Length, "#A5");
122
123 #if !TARGET_JVM // No support for extern methods in TARGET_JVM
124                         ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
125                         MarshalAsAttribute attr;
126
127                         attr = (MarshalAsAttribute)(pi [0].GetCustomAttributes (true) [0]);
128                         Assert.AreEqual (UnmanagedType.Bool, attr.Value, "#B");
129
130                         attr = (MarshalAsAttribute)(pi [1].GetCustomAttributes (true) [0]);
131                         Assert.AreEqual (UnmanagedType.LPArray, attr.Value, "#C1");
132                         Assert.AreEqual (UnmanagedType.LPStr, attr.ArraySubType, "#C2");
133
134                         attr = (MarshalAsAttribute)(pi [2].GetCustomAttributes (true) [0]);
135                         Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#D1");
136                         Assert.AreEqual ("5", attr.MarshalCookie, "#D2");
137                         Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#D3");
138 #endif
139                 }
140
141                 [Test] // bug #342536
142                 public void Generics_Name ()
143                 {
144                         MethodInfo mi;
145                         Type type;
146                         ParameterInfo [] info;
147                 
148                         type = typeof (BaseType<string>);
149
150                         mi = type.GetMethod ("GetItems");
151                         Assert.IsNotNull (mi, "#A1");
152                         info = mi.GetParameters ();
153                         Assert.AreEqual (1, info.Length, "#A2");
154                         Assert.AreEqual ("count", info [0].Name, "#A3");
155
156                         mi = type.GetMethod ("Add");
157                         Assert.IsNotNull (mi, "#B1");
158                         info = mi.GetParameters ();
159                         Assert.AreEqual (2, info.Length, "#B2");
160                         Assert.AreEqual ("item", info [0].Name, "#B3");
161                         Assert.AreEqual ("index", info [1].Name, "#B4");
162
163                         mi = type.GetMethod ("Create");
164                         Assert.IsNotNull (mi, "#C1");
165                         info = mi.GetParameters ();
166                         Assert.AreEqual (2, info.Length, "#C2");
167                         Assert.AreEqual ("x", info [0].Name, "#C3");
168                         Assert.AreEqual ("item", info [1].Name, "#C4");
169                 }
170
171                 public class BaseType <T>
172                 {
173                         public void GetItems (int count)
174                         {
175                         }
176
177                         public void Add (T item, int index)
178                         {
179                         }
180
181                         public V Create <V> (int x, T item)
182                         {
183                                 return default (V);
184                         }
185                 }
186 #endif
187
188                 [Test]
189                 public void Member () {
190                         ParameterInfo parm = typeof (Derived).GetMethod ("SomeMethod").GetParameters()[0];
191                         Assert.AreEqual (typeof (Derived), parm.Member.ReflectedType);
192                         Assert.AreEqual (typeof (Base), parm.Member.DeclaringType);
193                 }
194
195                 class Base
196                 {
197                         public void SomeMethod( int x )
198                         {
199                         }
200                 }
201
202                 class Derived : Base
203                 {
204                 }
205
206 #if NET_4_0
207                 public static void TestC (decimal u = decimal.MaxValue) {
208                 }
209
210                 [Test]
211                 public void DefaultValueDecimal () {
212                         var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
213                         Assert.AreEqual (decimal.MaxValue, info [0].DefaultValue);
214                 }
215 #endif
216         }
217 }