New errors + tiny update ;-)
[mono.git] / mcs / class / corlib / Test / System.Reflection / PropertyInfoTest.cs
1 //
2 // PropertyInfoTest.cs - NUnit Test Cases for PropertyInfo
3 //
4 // Author:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (C) 2004-2007 Gert Driesen
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29
30 using System;
31 using System.Reflection;
32 using System.Runtime.InteropServices;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Reflection
37 {
38         [TestFixture]
39         public class PropertyInfoTest
40         {
41                 [Test]
42                 public void GetAccessorsTest ()
43                 {
44                         Type type = typeof (TestClass);
45                         PropertyInfo property = type.GetProperty ("ReadOnlyProperty");
46
47                         MethodInfo [] methods = property.GetAccessors (true);
48                         Assert.AreEqual (1, methods.Length, "#A1");
49                         Assert.IsNotNull (methods [0], "#A2");
50                         Assert.AreEqual ("get_ReadOnlyProperty", methods [0].Name, "#A3");
51
52                         methods = property.GetAccessors (false);
53                         Assert.AreEqual (1, methods.Length, "#B1");
54                         Assert.IsNotNull (methods [0], "#B2");
55                         Assert.AreEqual ("get_ReadOnlyProperty", methods [0].Name, "#B3");
56
57                         property = typeof (Base).GetProperty ("P");
58
59                         methods = property.GetAccessors (true);
60                         Assert.AreEqual (2, methods.Length, "#C1");
61                         Assert.IsNotNull (methods [0], "#C2");
62                         Assert.IsNotNull (methods [1], "#C3");
63                         Assert.IsTrue (HasMethod (methods, "get_P"), "#C4");
64                         Assert.IsTrue (HasMethod (methods, "set_P"), "#C5");
65
66                         methods = property.GetAccessors (false);
67                         Assert.AreEqual (2, methods.Length, "#D1");
68                         Assert.IsNotNull (methods [0], "#D2");
69                         Assert.IsNotNull (methods [1], "#D3");
70                         Assert.IsTrue (HasMethod (methods, "get_P"), "#D4");
71                         Assert.IsTrue (HasMethod (methods, "set_P"), "#D5");
72
73                         methods = property.GetAccessors ();
74                         Assert.AreEqual (2, methods.Length, "#E1");
75                         Assert.IsNotNull (methods [0], "#E2");
76                         Assert.IsNotNull (methods [1], "#E3");
77                         Assert.IsTrue (HasMethod (methods, "get_P"), "#E4");
78                         Assert.IsTrue (HasMethod (methods, "set_P"), "#E5");
79
80                         property = typeof (TestClass).GetProperty ("Private",
81                                 BindingFlags.NonPublic | BindingFlags.Instance);
82
83                         methods = property.GetAccessors (true);
84                         Assert.AreEqual (2, methods.Length, "#F1");
85                         Assert.IsNotNull (methods [0], "#F2");
86                         Assert.IsNotNull (methods [1], "#F3");
87                         Assert.IsTrue (HasMethod (methods, "get_Private"), "#F4");
88                         Assert.IsTrue (HasMethod (methods, "set_Private"), "#F5");
89
90                         methods = property.GetAccessors (false);
91                         Assert.AreEqual (0, methods.Length, "#G");
92
93                         methods = property.GetAccessors ();
94                         Assert.AreEqual (0, methods.Length, "#H");
95
96 #if NET_2_0
97                         property = typeof (TestClass).GetProperty ("PrivateSetter");
98
99                         methods = property.GetAccessors (true);
100                         Assert.AreEqual (2, methods.Length, "#H1");
101                         Assert.IsNotNull (methods [0], "#H2");
102                         Assert.IsNotNull (methods [1], "#H3");
103                         Assert.IsTrue (HasMethod (methods, "get_PrivateSetter"), "#H4");
104                         Assert.IsTrue (HasMethod (methods, "set_PrivateSetter"), "#H5");
105
106                         methods = property.GetAccessors (false);
107                         Assert.AreEqual (1, methods.Length, "#I1");
108                         Assert.IsNotNull (methods [0], "#I2");
109                         Assert.AreEqual ("get_PrivateSetter", methods [0].Name, "#I3");
110
111                         methods = property.GetAccessors ();
112                         Assert.AreEqual (1, methods.Length, "#J1");
113                         Assert.IsNotNull (methods [0], "#J2");
114                         Assert.AreEqual ("get_PrivateSetter", methods [0].Name, "#J3");
115 #endif
116                 }
117
118                 [Test]
119                 public void GetCustomAttributes ()
120                 {
121                         object [] attrs;
122                         PropertyInfo p = typeof (Base).GetProperty ("P");
123
124                         attrs = p.GetCustomAttributes (false);
125                         Assert.AreEqual (1, attrs.Length, "#A1");
126                         Assert.AreEqual (typeof (ThisAttribute), attrs [0].GetType (), "#A2");
127                         attrs = p.GetCustomAttributes (true);
128                         Assert.AreEqual (1, attrs.Length, "#A3");
129                         Assert.AreEqual (typeof (ThisAttribute), attrs [0].GetType (), "#A4");
130
131                         p = typeof (Base).GetProperty ("T");
132
133                         attrs = p.GetCustomAttributes (false);
134                         Assert.AreEqual (2, attrs.Length, "#B1");
135                         Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B2");
136                         Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B3");
137                         attrs = p.GetCustomAttributes (true);
138                         Assert.AreEqual (2, attrs.Length, "#B41");
139                         Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B5");
140                         Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B6");
141
142                         p = typeof (Base).GetProperty ("Z");
143
144                         attrs = p.GetCustomAttributes (false);
145                         Assert.AreEqual (0, attrs.Length, "#C1");
146                         attrs = p.GetCustomAttributes (true);
147                         Assert.AreEqual (0, attrs.Length, "#C2");
148                 }
149
150                 [Test]
151                 public void GetCustomAttributes_Inherited ()
152                 {
153                         object [] attrs;
154                         PropertyInfo p = typeof (Derived).GetProperty ("P");
155
156                         attrs = p.GetCustomAttributes (false);
157                         Assert.AreEqual (0, attrs.Length, "#A1");
158                         attrs = p.GetCustomAttributes (true);
159                         Assert.AreEqual (0, attrs.Length, "#A3");
160
161                         p = typeof (Derived).GetProperty ("T");
162
163                         attrs = p.GetCustomAttributes (false);
164                         Assert.AreEqual (2, attrs.Length, "#B1");
165                         Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B2");
166                         Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B3");
167                         attrs = p.GetCustomAttributes (true);
168                         Assert.AreEqual (2, attrs.Length, "#B41");
169                         Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B5");
170                         Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B6");
171
172                         p = typeof (Derived).GetProperty ("Z");
173
174                         attrs = p.GetCustomAttributes (false);
175                         Assert.AreEqual (0, attrs.Length, "#C1");
176                         attrs = p.GetCustomAttributes (true);
177                         Assert.AreEqual (0, attrs.Length, "#C2");
178                 }
179
180                 [Test]
181                 public void IsDefined_AttributeType_Null ()
182                 {
183                         Type derived = typeof (Derived);
184                         PropertyInfo pi = derived.GetProperty ("P");
185
186                         try {
187                                 pi.IsDefined ((Type) null, false);
188                                 Assert.Fail ("#1");
189                         } catch (ArgumentNullException ex) {
190                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
191                                 Assert.IsNull (ex.InnerException, "#3");
192                                 Assert.IsNotNull (ex.Message, "#4");
193                                 Assert.IsNotNull (ex.ParamName, "#5");
194                                 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
195                         }
196                 }
197
198                 public class ThisAttribute : Attribute
199                 {
200                 }
201
202                 class Base
203                 {
204                         [ThisAttribute]
205                         public virtual string P {
206                                 get { return null; }
207                                 set { }
208                         }
209
210                         [ThisAttribute]
211                         [ComVisible (false)]
212                         public virtual string T {
213                                 get { return null; }
214                                 set { }
215                         }
216
217                         public virtual string Z {
218                                 get { return null; }
219                                 set { }
220                         }
221                 }
222
223                 class Derived : Base
224                 {
225                         public override string P {
226                                 get { return null; }
227                                 set { }
228                         }
229                 }
230
231 #if NET_2_0
232                 public class A<T>
233                 {
234                         public string Property {
235                                 get { return typeof (T).FullName; }
236                         }
237                 }
238
239                 public int? nullable_field;
240
241                 public int? NullableProperty {
242                         get { return nullable_field; }
243                         set { nullable_field = value; }
244                 }
245
246                 [Test]
247                 public void NullableTests ()
248                 {
249                         PropertyInfoTest t = new PropertyInfoTest ();
250
251                         PropertyInfo pi = typeof(PropertyInfoTest).GetProperty("NullableProperty");
252
253                         pi.SetValue (t, 100, null);
254                         Assert.AreEqual (100, pi.GetValue (t, null));
255                         pi.SetValue (t, null, null);
256                         Assert.AreEqual (null, pi.GetValue (t, null));
257                 }
258
259                 [Test]
260                 public void Bug77160 ()
261                 {
262                         object instance = new A<string> ();
263                         Type type = instance.GetType ();
264                         PropertyInfo property = type.GetProperty ("Property");
265                         Assert.AreEqual (typeof (string).FullName, property.GetValue (instance, null));
266                 }
267 #endif
268
269                 static bool HasAttribute (object [] attrs, Type attributeType)
270                 {
271                         foreach (object attr in attrs)
272                                 if (attr.GetType () == attributeType)
273                                         return true;
274                         return false;
275                 }
276
277                 static bool HasMethod (MethodInfo [] methods, string name)
278                 {
279                         foreach (MethodInfo method in methods)
280                                 if (method.Name == name)
281                                         return true;
282                         return false;
283                 }
284
285                 private class TestClass 
286                 {
287                         public string ReadOnlyProperty {
288                                 get { return string.Empty; }
289                         }
290
291                         private string Private {
292                                 get { return null; }
293                                 set { }
294                         }
295
296 #if NET_2_0
297                         public string PrivateSetter {
298                                 get { return null; }
299                                 private set { }
300                         }
301 #endif
302                 }
303         }
304 }