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 )
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 TestParamAttribute : Attribute
267 public static int TestCustomAttribute_Method ([TestParamAttribute] string arg)
273 public void TestCustomAttribute ()
275 var metInfo = GetType ().GetMethod ("TestCustomAttribute_Method", new Type[] { typeof(string) });
276 var paramInfos = metInfo.GetParameters ();
277 var argParamInfo = paramInfos[0];
279 var custAttrs = argParamInfo.GetCustomAttributes ();
280 Assert.AreEqual (1, custAttrs.Count ());
283 class MyParameterInfo2 : ParameterInfo
285 public ParameterAttributes MyAttrsImpl;
287 public override ParameterAttributes Attributes {
288 get {return MyAttrsImpl;}
291 public IList<CustomAttributeData> myList = new List<CustomAttributeData> ();
293 public override IList<CustomAttributeData> GetCustomAttributesData () {
298 class MyParameterInfo : ParameterInfo
300 public void SetClassImpl (Type t)
305 public void SetDefaultValueImpl (object o)
307 DefaultValueImpl = o;
310 public void SetMemberImpl (MemberInfo o)
315 public void SetNameImpl (string s)
320 public void SetPositionImpl (int i)
325 public void SetAttrsImpl (ParameterAttributes a)
330 public void TestMethod (int a) {}
331 public int this[int x, int y] {
339 public void SubClassWithNoOverrides ()
341 var p = new MyParameterInfo ();
342 Assert.IsFalse (p.IsDefined (typeof (FlagsAttribute), false), "#1");
343 Assert.AreEqual (0, p.GetCustomAttributes (false).Length, "#2");
344 Assert.AreEqual (0, p.GetCustomAttributes (typeof (FlagsAttribute), false).Length, "#3");
345 Assert.AreEqual (0, p.GetOptionalCustomModifiers ().Length, "#4");
346 Assert.AreEqual (0, p.GetRequiredCustomModifiers ().Length, "#5");
349 var ign = p.HasDefaultValue;
351 } catch (NotImplementedException) {
354 Assert.IsFalse (p.IsIn, "#7");
356 Assert.IsFalse (p.IsLcid, "#8");
358 Assert.IsFalse (p.IsOptional, "#9");
359 Assert.IsFalse (p.IsOut, "#10");
360 Assert.IsFalse (p.IsRetval, "#10");
363 var ign = p.CustomAttributes;
365 } catch (NotImplementedException) {
369 p.GetCustomAttributesData ();
371 } catch (NotImplementedException) {
374 Assert.AreEqual (0x8000000, p.MetadataToken, "#13");
375 Assert.AreEqual (0, p.Position, "#14");
377 var ign = p.DefaultValue;
379 } catch (NotImplementedException) {
382 var ign = p.RawDefaultValue;
384 } catch (NotImplementedException) {
386 Assert.IsNull (p.Member, "#17");
387 Assert.AreEqual (ParameterAttributes.None, p.Attributes, "#18");
388 Assert.IsNull (p.Name, "#19");
389 Assert.IsNull (p.ParameterType, "#20");
393 public void SubClassWithValuesSet ()
395 var p = new MyParameterInfo ();
396 p.SetClassImpl (typeof (Decimal));
397 Assert.AreEqual (typeof (Decimal), p.ParameterType, "#1");
398 p.SetClassImpl (null);
400 p.SetDefaultValueImpl ("foo");
402 var ign = p.DefaultValue;
404 } catch (NotImplementedException) {
406 p.SetDefaultValueImpl (null);
408 var obj = typeof (object);
409 p.SetMemberImpl (obj);
410 Assert.AreEqual (obj, p.Member, "#3");
411 Assert.AreEqual (0x8000000, p.MetadataToken, "#4");
412 p.SetMemberImpl (null);
414 var method = typeof (MyParameterInfo).GetMethod ("TestMethod");
415 p.SetMemberImpl (method);
416 Assert.IsNotNull (method, "#5");
417 Assert.AreEqual (method, p.Member, "#6");
418 Assert.AreEqual (0x8000000, p.MetadataToken, "#7");
419 p.SetMemberImpl (null);
421 var property = typeof (MyParameterInfo).GetProperty ("Item");
422 p.SetMemberImpl (property);
423 Assert.IsNotNull (property, "#8");
424 Assert.AreEqual (property, p.Member, "#9");
425 Assert.AreEqual (0x8000000, p.MetadataToken, "#10");
426 p.SetMemberImpl (null);
428 p.SetNameImpl ("foo");
429 Assert.AreEqual ("foo", p.Name, "#11");
430 p.SetNameImpl (null);
432 p.SetPositionImpl (99);
433 Assert.AreEqual (p.Position, 99, "#12");
434 Assert.AreEqual (p.MetadataToken, 0x8000000, "#13");
435 p.SetPositionImpl (0);
437 Assert.IsFalse (p.IsIn, "#14");
438 p.SetAttrsImpl (ParameterAttributes.In);
439 Assert.IsTrue (p.IsIn, "#15");
443 public void SubClassWithOverrides()
445 var p2 = new MyParameterInfo2 ();
446 Assert.IsFalse (p2.IsIn, "#1");
447 p2.MyAttrsImpl = ParameterAttributes.In;
448 Assert.IsTrue (p2.IsIn, "#2");
450 Assert.AreEqual (p2.myList, p2.CustomAttributes, "#3");