New test.
[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 using System.Reflection.Emit;
14 using System.Runtime.InteropServices;
15
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Reflection
19 {
20
21
22 public class Marshal1 : ICustomMarshaler
23 {
24         public static ICustomMarshaler GetInstance (string s) {
25                 return new Marshal1 ();
26         }
27
28         public void CleanUpManagedData (object managedObj)
29         {
30         }
31
32         public void CleanUpNativeData (IntPtr pNativeData)
33         {
34         }
35
36         public int GetNativeDataSize ()
37         {
38                 return 4;
39         }
40
41         public IntPtr MarshalManagedToNative (object managedObj)
42         {
43                 return IntPtr.Zero;
44         }
45
46         public object MarshalNativeToManaged (IntPtr pNativeData)
47         {
48                 return null;
49         }
50 }
51
52 [TestFixture]
53 public class ParameterInfoTest : Assertion
54 {
55 #if NET_2_0
56         public enum ParamEnum {
57                 None = 0,
58                 Foo = 1,
59                 Bar = 2
60         };
61
62         public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m, [DefaultParameterValue (ParamEnum.Foo)] ParamEnum n) {
63         }
64
65         [DllImport ("foo")]
66         public extern static void marshalAsMethod (
67                 [MarshalAs(UnmanagedType.Bool)]int p0, 
68                 [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string [] p1,
69                 [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object p2);
70
71         [Test]
72         public void DefaultValueEnum () {
73                 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
74
75                 AssertEquals (typeof (ParamEnum), info [5].DefaultValue.GetType ());
76                 AssertEquals (ParamEnum.Foo, info [5].DefaultValue);
77         }
78
79         [Test]
80         public void PseudoCustomAttributes () {
81                 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
82                 AssertEquals (0, info[0].GetCustomAttributes (true).Length);
83                 AssertEquals (1, info[1].GetCustomAttributes (typeof (InAttribute), true).Length);
84                 AssertEquals (1, info[2].GetCustomAttributes (typeof (OutAttribute), true).Length);
85                 AssertEquals (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length);
86                 AssertEquals (2, info[4].GetCustomAttributes (true).Length);
87
88                 ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
89                 MarshalAsAttribute attr;
90
91                 attr = (MarshalAsAttribute)(pi [0].GetCustomAttributes (true) [0]);
92                 AssertEquals (UnmanagedType.Bool, attr.Value);
93
94                 attr = (MarshalAsAttribute)(pi [1].GetCustomAttributes (true) [0]);
95                 AssertEquals (UnmanagedType.LPArray, attr.Value);
96                 AssertEquals (UnmanagedType.LPStr, attr.ArraySubType);
97
98                 attr = (MarshalAsAttribute)(pi [2].GetCustomAttributes (true) [0]);
99                 AssertEquals (UnmanagedType.CustomMarshaler, attr.Value);
100                 AssertEquals ("5", attr.MarshalCookie);
101                 AssertEquals (typeof (Marshal1), Type.GetType (attr.MarshalType));
102         }
103 #endif
104 }               
105 }