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;
19 using NUnit.Framework;
21 namespace MonoTests.System.Reflection
23 public class Marshal1 : ICustomMarshaler
25 public static ICustomMarshaler GetInstance (string s)
27 return new Marshal1 ();
30 public void CleanUpManagedData (object managedObj)
34 public void CleanUpNativeData (IntPtr pNativeData)
38 public int GetNativeDataSize ()
43 public IntPtr MarshalManagedToNative (object managedObj)
48 public object MarshalNativeToManaged (IntPtr pNativeData)
55 public class ParameterInfoTest
58 public void IsDefined_AttributeType_Null ()
60 MethodInfo mi = typeof (object).GetMethod ("Equals",
61 new Type [1] { typeof (object) });
62 ParameterInfo pi = mi.GetParameters () [0];
65 pi.IsDefined ((Type) null, false);
67 } catch (ArgumentNullException ex) {
68 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
69 Assert.IsNull (ex.InnerException, "#3");
70 Assert.IsNotNull (ex.Message, "#4");
71 Assert.IsNotNull (ex.ParamName, "#5");
72 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
77 public enum ParamEnum {
83 public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m, ParamEnum n = ParamEnum.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);
93 public void DefaultValueEnum () {
94 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
96 Assert.AreEqual (typeof (ParamEnum), info [5].DefaultValue.GetType (), "#1");
97 Assert.AreEqual (ParamEnum.Foo, info [5].DefaultValue, "#2");
102 public void HasDefaultValueEnum () {
103 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
105 Assert.IsTrue (info [5].HasDefaultValue);
109 public static void Sample2 ([DecimalConstantAttribute(2,2,2,2,2)] decimal a, [DateTimeConstantAttribute(123456)] DateTime b) {}
112 public void DefaultValuesFromCustomAttr () {
113 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("Sample2").GetParameters ();
115 Assert.AreEqual (typeof (Decimal), info [0].DefaultValue.GetType (), "#1");
116 Assert.AreEqual (typeof (DateTime), info [1].DefaultValue.GetType (), "#2");
119 [Test] // bug #339013
120 public void TestDefaultValues ()
122 ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
124 Assert.AreEqual (pi [0].DefaultValue.GetType (), typeof (DBNull), "#1");
125 Assert.AreEqual (pi [1].DefaultValue.GetType (), typeof (Missing), "#2");
130 public void TestHasDefaultValues ()
132 ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
134 Assert.IsFalse (pi [0].HasDefaultValue, "#1");
135 Assert.IsFalse (pi [1].HasDefaultValue, "#2");
136 Assert.IsTrue (pi [2].HasDefaultValue, "#3");
140 public void Sample (int a, [Optional] int b, object c = null)
145 public void PseudoCustomAttributes () {
146 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
147 Assert.AreEqual (0, info[0].GetCustomAttributes (true).Length, "#A1");
148 Assert.AreEqual (1, info[1].GetCustomAttributes (typeof (InAttribute), true).Length, "#A2");
149 Assert.AreEqual (1, info[2].GetCustomAttributes (typeof (OutAttribute), true).Length, "#A3");
150 Assert.AreEqual (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length, "#A4");
151 Assert.AreEqual (2, info[4].GetCustomAttributes (true).Length, "#A5");
153 ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
154 MarshalAsAttribute attr;
156 attr = (MarshalAsAttribute)(pi [0].GetCustomAttributes (true) [0]);
157 Assert.AreEqual (UnmanagedType.Bool, attr.Value, "#B");
159 attr = (MarshalAsAttribute)(pi [1].GetCustomAttributes (true) [0]);
160 Assert.AreEqual (UnmanagedType.LPArray, attr.Value, "#C1");
161 Assert.AreEqual (UnmanagedType.LPStr, attr.ArraySubType, "#C2");
163 attr = (MarshalAsAttribute)(pi [2].GetCustomAttributes (true) [0]);
164 Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#D1");
165 Assert.AreEqual ("5", attr.MarshalCookie, "#D2");
166 Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#D3");
169 [Test] // bug #342536
170 public void Generics_Name ()
174 ParameterInfo [] info;
176 type = typeof (BaseType<string>);
178 mi = type.GetMethod ("GetItems");
179 Assert.IsNotNull (mi, "#A1");
180 info = mi.GetParameters ();
181 Assert.AreEqual (1, info.Length, "#A2");
182 Assert.AreEqual ("count", info [0].Name, "#A3");
184 mi = type.GetMethod ("Add");
185 Assert.IsNotNull (mi, "#B1");
186 info = mi.GetParameters ();
187 Assert.AreEqual (2, info.Length, "#B2");
188 Assert.AreEqual ("item", info [0].Name, "#B3");
189 Assert.AreEqual ("index", info [1].Name, "#B4");
191 mi = type.GetMethod ("Create");
192 Assert.IsNotNull (mi, "#C1");
193 info = mi.GetParameters ();
194 Assert.AreEqual (2, info.Length, "#C2");
195 Assert.AreEqual ("x", info [0].Name, "#C3");
196 Assert.AreEqual ("item", info [1].Name, "#C4");
199 public class BaseType <T>
201 public void GetItems (int count)
205 public void Add (T item, int index)
209 public V Create <V> (int x, T item)
217 public void Member () {
218 ParameterInfo parm = typeof (Derived).GetMethod ("SomeMethod").GetParameters()[0];
219 Assert.AreEqual (typeof (Derived), parm.Member.ReflectedType);
220 Assert.AreEqual (typeof (Base), parm.Member.DeclaringType);
224 public void ArrayMethodParameters ()
226 var matrix_int_get = typeof (int[,,]).GetMethod ("Get");
227 var parameters = matrix_int_get.GetParameters ();
229 Assert.AreEqual (3, parameters.Length);
230 Assert.AreEqual (0, parameters [0].GetCustomAttributes (false).Length);
231 Assert.AreEqual (0, parameters [1].GetCustomAttributes (false).Length);
232 Assert.AreEqual (0, parameters [2].GetCustomAttributes (false).Length);
237 public void SomeMethod( int x )
247 public static void TestC (decimal u = decimal.MaxValue) {
251 public void DefaultValueDecimal () {
252 var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
253 Assert.AreEqual (decimal.MaxValue, info [0].DefaultValue);
258 public void HasDefaultValueDecimal () {
259 var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
260 Assert.IsTrue (info [0].HasDefaultValue);
264 class TestAttribute : Attribute
268 public static int TestCustomAttribute_Method ([Test] string arg)
274 public void TestCustomAttribute ()
276 var metInfo = GetType ().GetMethod ("TestCustomAttribute_Method", new Type[] { typeof(string) });
277 var paramInfos = metInfo.GetParameters ();
278 var argParamInfo = paramInfos[0];
280 var custAttrs = argParamInfo.GetCustomAttributes ();
281 Assert.AreEqual (1, custAttrs.Count ());
284 class MyParameterInfo2 : ParameterInfo
286 public ParameterAttributes MyAttrsImpl;
288 public override ParameterAttributes Attributes {
289 get {return MyAttrsImpl;}
292 public IList<CustomAttributeData> myList = new List<CustomAttributeData> ();
294 public override IList<CustomAttributeData> GetCustomAttributesData () {
299 class MyParameterInfo : ParameterInfo
301 public void SetClassImpl (Type t)
306 public void SetDefaultValueImpl (object o)
308 DefaultValueImpl = o;
311 public void SetMemberImpl (MemberInfo o)
316 public void SetNameImpl (string s)
321 public void SetPositionImpl (int i)
326 public void SetAttrsImpl (ParameterAttributes a)
331 public void TestMethod (int a) {}
332 public int this[int x, int y] {
340 public void SubClassWithNoOverrides ()
342 var p = new MyParameterInfo ();
343 Assert.IsFalse (p.IsDefined (typeof (FlagsAttribute), false), "#1");
344 Assert.AreEqual (0, p.GetCustomAttributes (false).Length, "#2");
345 Assert.AreEqual (0, p.GetCustomAttributes (typeof (FlagsAttribute), false).Length, "#3");
346 Assert.AreEqual (0, p.GetOptionalCustomModifiers ().Length, "#4");
347 Assert.AreEqual (0, p.GetRequiredCustomModifiers ().Length, "#5");
350 var ign = p.HasDefaultValue;
352 } catch (NotImplementedException) {
355 Assert.IsFalse (p.IsIn, "#7");
357 Assert.IsFalse (p.IsLcid, "#8");
359 Assert.IsFalse (p.IsOptional, "#9");
360 Assert.IsFalse (p.IsOut, "#10");
361 Assert.IsFalse (p.IsRetval, "#10");
364 var ign = p.CustomAttributes;
366 } catch (NotImplementedException) {
370 p.GetCustomAttributesData ();
372 } catch (NotImplementedException) {
375 Assert.AreEqual (0x8000000, p.MetadataToken, "#13");
376 Assert.AreEqual (0, p.Position, "#14");
378 var ign = p.DefaultValue;
380 } catch (NotImplementedException) {
383 var ign = p.RawDefaultValue;
385 } catch (NotImplementedException) {
387 Assert.IsNull (p.Member, "#17");
388 Assert.AreEqual (ParameterAttributes.None, p.Attributes, "#18");
389 Assert.IsNull (p.Name, "#19");
390 Assert.IsNull (p.ParameterType, "#20");
394 public void SubClassWithValuesSet ()
396 var p = new MyParameterInfo ();
397 p.SetClassImpl (typeof (Decimal));
398 Assert.AreEqual (typeof (Decimal), p.ParameterType, "#1");
399 p.SetClassImpl (null);
401 p.SetDefaultValueImpl ("foo");
403 var ign = p.DefaultValue;
405 } catch (NotImplementedException) {
407 p.SetDefaultValueImpl (null);
409 var obj = typeof (object);
410 p.SetMemberImpl (obj);
411 Assert.AreEqual (obj, p.Member, "#3");
412 Assert.AreEqual (0x8000000, p.MetadataToken, "#4");
413 p.SetMemberImpl (null);
415 var method = typeof (MyParameterInfo).GetMethod ("TestMethod");
416 p.SetMemberImpl (method);
417 Assert.IsNotNull (method, "#5");
418 Assert.AreEqual (method, p.Member, "#6");
419 Assert.AreEqual (0x8000000, p.MetadataToken, "#7");
420 p.SetMemberImpl (null);
422 var property = typeof (MyParameterInfo).GetProperty ("Item");
423 p.SetMemberImpl (property);
424 Assert.IsNotNull (property, "#8");
425 Assert.AreEqual (property, p.Member, "#9");
426 Assert.AreEqual (0x8000000, p.MetadataToken, "#10");
427 p.SetMemberImpl (null);
429 p.SetNameImpl ("foo");
430 Assert.AreEqual ("foo", p.Name, "#11");
431 p.SetNameImpl (null);
433 p.SetPositionImpl (99);
434 Assert.AreEqual (p.Position, 99, "#12");
435 Assert.AreEqual (p.MetadataToken, 0x8000000, "#13");
436 p.SetPositionImpl (0);
438 Assert.IsFalse (p.IsIn, "#14");
439 p.SetAttrsImpl (ParameterAttributes.In);
440 Assert.IsTrue (p.IsIn, "#15");
444 public void SubClassWithOverrides()
446 var p2 = new MyParameterInfo2 ();
447 Assert.IsFalse (p2.IsIn, "#1");
448 p2.MyAttrsImpl = ParameterAttributes.In;
449 Assert.IsTrue (p2.IsIn, "#2");
451 Assert.AreEqual (p2.myList, p2.CustomAttributes, "#3");