2 // ParameterInfoTest - NUnit Test Cases for the ParameterInfo class
4 // Zoltan Varga (vargaz@freemail.hu)
6 // (C) Ximian, Inc. http://www.ximian.com
7 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
12 using System.Threading;
13 using System.Reflection;
14 using System.Runtime.InteropServices;
15 using System.Runtime.CompilerServices;
16 using System.Collections.Generic;
18 using NUnit.Framework;
20 namespace MonoTests.System.Reflection
22 public class Marshal1 : ICustomMarshaler
24 public static ICustomMarshaler GetInstance (string s)
26 return new Marshal1 ();
29 public void CleanUpManagedData (object managedObj)
33 public void CleanUpNativeData (IntPtr pNativeData)
37 public int GetNativeDataSize ()
42 public IntPtr MarshalManagedToNative (object managedObj)
47 public object MarshalNativeToManaged (IntPtr pNativeData)
54 public class ParameterInfoTest
57 public void IsDefined_AttributeType_Null ()
59 MethodInfo mi = typeof (object).GetMethod ("Equals",
60 new Type [1] { typeof (object) });
61 ParameterInfo pi = mi.GetParameters () [0];
64 pi.IsDefined ((Type) null, false);
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");
75 #if NET_2_0 && !NET_2_1
76 public enum ParamEnum {
82 public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m, ParamEnum n = ParamEnum.Foo)
87 public extern static void marshalAsMethod (
88 [MarshalAs(UnmanagedType.Bool)]int p0,
89 [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string [] p1,
90 [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object p2);
92 public void DefaultValueEnum () {
93 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
95 Assert.AreEqual (typeof (ParamEnum), info [5].DefaultValue.GetType (), "#1");
96 Assert.AreEqual (ParamEnum.Foo, info [5].DefaultValue, "#2");
101 public void HasDefaultValueEnum () {
102 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
104 Assert.IsTrue (info [5].HasDefaultValue);
108 public static void Sample2 ([DecimalConstantAttribute(2,2,2,2,2)] decimal a, [DateTimeConstantAttribute(123456)] DateTime b) {}
111 public void DefaultValuesFromCustomAttr () {
112 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("Sample2").GetParameters ();
114 Assert.AreEqual (typeof (Decimal), info [0].DefaultValue.GetType (), "#1");
115 Assert.AreEqual (typeof (DateTime), info [1].DefaultValue.GetType (), "#2");
118 [Test] // bug #339013
119 public void TestDefaultValues ()
121 ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
123 Assert.AreEqual (pi [0].DefaultValue.GetType (), typeof (DBNull), "#1");
124 Assert.AreEqual (pi [1].DefaultValue.GetType (), typeof (Missing), "#2");
129 public void TestHasDefaultValues ()
131 ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
133 Assert.IsFalse (pi [0].HasDefaultValue, "#1");
134 Assert.IsFalse (pi [1].HasDefaultValue, "#2");
135 Assert.IsTrue (pi [2].HasDefaultValue, "#3");
139 public void Sample (int a, [Optional] int b, object c = null)
144 public void PseudoCustomAttributes () {
145 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
146 Assert.AreEqual (0, info[0].GetCustomAttributes (true).Length, "#A1");
147 Assert.AreEqual (1, info[1].GetCustomAttributes (typeof (InAttribute), true).Length, "#A2");
148 Assert.AreEqual (1, info[2].GetCustomAttributes (typeof (OutAttribute), true).Length, "#A3");
149 Assert.AreEqual (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length, "#A4");
150 Assert.AreEqual (2, info[4].GetCustomAttributes (true).Length, "#A5");
152 ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
153 MarshalAsAttribute attr;
155 attr = (MarshalAsAttribute)(pi [0].GetCustomAttributes (true) [0]);
156 Assert.AreEqual (UnmanagedType.Bool, attr.Value, "#B");
158 attr = (MarshalAsAttribute)(pi [1].GetCustomAttributes (true) [0]);
159 Assert.AreEqual (UnmanagedType.LPArray, attr.Value, "#C1");
160 Assert.AreEqual (UnmanagedType.LPStr, attr.ArraySubType, "#C2");
162 attr = (MarshalAsAttribute)(pi [2].GetCustomAttributes (true) [0]);
163 Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#D1");
164 Assert.AreEqual ("5", attr.MarshalCookie, "#D2");
165 Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#D3");
168 [Test] // bug #342536
169 public void Generics_Name ()
173 ParameterInfo [] info;
175 type = typeof (BaseType<string>);
177 mi = type.GetMethod ("GetItems");
178 Assert.IsNotNull (mi, "#A1");
179 info = mi.GetParameters ();
180 Assert.AreEqual (1, info.Length, "#A2");
181 Assert.AreEqual ("count", info [0].Name, "#A3");
183 mi = type.GetMethod ("Add");
184 Assert.IsNotNull (mi, "#B1");
185 info = mi.GetParameters ();
186 Assert.AreEqual (2, info.Length, "#B2");
187 Assert.AreEqual ("item", info [0].Name, "#B3");
188 Assert.AreEqual ("index", info [1].Name, "#B4");
190 mi = type.GetMethod ("Create");
191 Assert.IsNotNull (mi, "#C1");
192 info = mi.GetParameters ();
193 Assert.AreEqual (2, info.Length, "#C2");
194 Assert.AreEqual ("x", info [0].Name, "#C3");
195 Assert.AreEqual ("item", info [1].Name, "#C4");
198 public class BaseType <T>
200 public void GetItems (int count)
204 public void Add (T item, int index)
208 public V Create <V> (int x, T item)
216 public void Member () {
217 ParameterInfo parm = typeof (Derived).GetMethod ("SomeMethod").GetParameters()[0];
218 Assert.AreEqual (typeof (Derived), parm.Member.ReflectedType);
219 Assert.AreEqual (typeof (Base), parm.Member.DeclaringType);
223 public void ArrayMethodParameters ()
225 var matrix_int_get = typeof (int[,,]).GetMethod ("Get");
226 var parameters = matrix_int_get.GetParameters ();
228 Assert.AreEqual (3, parameters.Length);
229 Assert.AreEqual (0, parameters [0].GetCustomAttributes (false).Length);
230 Assert.AreEqual (0, parameters [1].GetCustomAttributes (false).Length);
231 Assert.AreEqual (0, parameters [2].GetCustomAttributes (false).Length);
236 public void SomeMethod( int x )
246 public static void TestC (decimal u = decimal.MaxValue) {
250 public void DefaultValueDecimal () {
251 var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
252 Assert.AreEqual (decimal.MaxValue, info [0].DefaultValue);
257 public void HasDefaultValueDecimal () {
258 var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
259 Assert.IsTrue (info [0].HasDefaultValue);
263 class MyParameterInfo2 : ParameterInfo
265 public ParameterAttributes MyAttrsImpl;
267 public override ParameterAttributes Attributes {
268 get {return MyAttrsImpl;}
271 public IList<CustomAttributeData> myList = new List<CustomAttributeData> ();
273 public override IList<CustomAttributeData> GetCustomAttributesData () {
278 class MyParameterInfo : ParameterInfo
280 public void SetClassImpl (Type t)
285 public void SetDefaultValueImpl (object o)
287 DefaultValueImpl = o;
290 public void SetMemberImpl (MemberInfo o)
295 public void SetNameImpl (string s)
300 public void SetPositionImpl (int i)
305 public void SetAttrsImpl (ParameterAttributes a)
310 public void TestMethod (int a) {}
311 public int this[int x, int y] {
319 public void SubClassWithNoOverrides ()
321 var p = new MyParameterInfo ();
322 Assert.IsFalse (p.IsDefined (typeof (FlagsAttribute), false), "#1");
323 Assert.AreEqual (0, p.GetCustomAttributes (false).Length, "#2");
324 Assert.AreEqual (0, p.GetCustomAttributes (typeof (FlagsAttribute), false).Length, "#3");
325 Assert.AreEqual (0, p.GetOptionalCustomModifiers ().Length, "#4");
326 Assert.AreEqual (0, p.GetRequiredCustomModifiers ().Length, "#5");
329 var ign = p.HasDefaultValue;
331 } catch (NotImplementedException) {
334 Assert.IsFalse (p.IsIn, "#7");
335 Assert.IsFalse (p.IsLcid, "#8");
336 Assert.IsFalse (p.IsOptional, "#9");
337 Assert.IsFalse (p.IsOut, "#10");
338 Assert.IsFalse (p.IsRetval, "#10");
341 var ign = p.CustomAttributes;
343 } catch (NotImplementedException) {
347 p.GetCustomAttributesData ();
349 } catch (NotImplementedException) {
352 Assert.AreEqual (0x8000000, p.MetadataToken, "#13");
353 Assert.AreEqual (0, p.Position, "#14");
355 var ign = p.DefaultValue;
357 } catch (NotImplementedException) {
360 var ign = p.RawDefaultValue;
362 } catch (NotImplementedException) {
364 Assert.IsNull (p.Member, "#17");
365 Assert.AreEqual (ParameterAttributes.None, p.Attributes, "#18");
366 Assert.IsNull (p.Name, "#19");
367 Assert.IsNull (p.ParameterType, "#20");
371 public void SubClassWithValuesSet ()
373 var p = new MyParameterInfo ();
374 p.SetClassImpl (typeof (Decimal));
375 Assert.AreEqual (typeof (Decimal), p.ParameterType, "#1");
376 p.SetClassImpl (null);
378 p.SetDefaultValueImpl ("foo");
380 var ign = p.DefaultValue;
382 } catch (NotImplementedException) {
384 p.SetDefaultValueImpl (null);
386 var obj = typeof (object);
387 p.SetMemberImpl (obj);
388 Assert.AreEqual (obj, p.Member, "#3");
389 Assert.AreEqual (0x8000000, p.MetadataToken, "#4");
390 p.SetMemberImpl (null);
392 var method = typeof (MyParameterInfo).GetMethod ("TestMethod");
393 p.SetMemberImpl (method);
394 Assert.IsNotNull (method, "#5");
395 Assert.AreEqual (method, p.Member, "#6");
396 Assert.AreEqual (0x8000000, p.MetadataToken, "#7");
397 p.SetMemberImpl (null);
399 var property = typeof (MyParameterInfo).GetProperty ("Item");
400 p.SetMemberImpl (property);
401 Assert.IsNotNull (property, "#8");
402 Assert.AreEqual (property, p.Member, "#9");
403 Assert.AreEqual (0x8000000, p.MetadataToken, "#10");
404 p.SetMemberImpl (null);
406 p.SetNameImpl ("foo");
407 Assert.AreEqual ("foo", p.Name, "#11");
408 p.SetNameImpl (null);
410 p.SetPositionImpl (99);
411 Assert.AreEqual (p.Position, 99, "#12");
412 Assert.AreEqual (p.MetadataToken, 0x8000000, "#13");
413 p.SetPositionImpl (0);
415 Assert.IsFalse (p.IsIn, "#14");
416 p.SetAttrsImpl (ParameterAttributes.In);
417 Assert.IsTrue (p.IsIn, "#15");
421 public void SubClassWithOverrides()
423 var p2 = new MyParameterInfo2 ();
424 Assert.IsFalse (p2.IsIn, "#1");
425 p2.MyAttrsImpl = ParameterAttributes.In;
426 Assert.IsTrue (p2.IsIn, "#2");
428 Assert.AreEqual (p2.myList, p2.CustomAttributes, "#3");