* AssemblyTest.cs: Added test for bug #334203. Use more meaningful name
[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                 [Test]
199                 public void AccessorsReflectedType ()
200                 {
201                         PropertyInfo pi = typeof (Derived).GetProperty ("T");
202                         Assert.AreEqual (typeof (Derived), pi.GetGetMethod ().ReflectedType);
203                         Assert.AreEqual (typeof (Derived), pi.GetSetMethod ().ReflectedType);
204                 }
205
206                 public class ThisAttribute : Attribute
207                 {
208                 }
209
210                 class Base
211                 {
212                         [ThisAttribute]
213                         public virtual string P {
214                                 get { return null; }
215                                 set { }
216                         }
217
218                         [ThisAttribute]
219                         [ComVisible (false)]
220                         public virtual string T {
221                                 get { return null; }
222                                 set { }
223                         }
224
225                         public virtual string Z {
226                                 get { return null; }
227                                 set { }
228                         }
229                 }
230
231                 class Derived : Base
232                 {
233                         public override string P {
234                                 get { return null; }
235                                 set { }
236                         }
237                 }
238
239 #if NET_2_0
240                 public class A<T>
241                 {
242                         public string Property {
243                                 get { return typeof (T).FullName; }
244                         }
245                 }
246
247                 public int? nullable_field;
248
249                 public int? NullableProperty {
250                         get { return nullable_field; }
251                         set { nullable_field = value; }
252                 }
253
254                 [Test]
255                 public void NullableTests ()
256                 {
257                         PropertyInfoTest t = new PropertyInfoTest ();
258
259                         PropertyInfo pi = typeof(PropertyInfoTest).GetProperty("NullableProperty");
260
261                         pi.SetValue (t, 100, null);
262                         Assert.AreEqual (100, pi.GetValue (t, null));
263                         pi.SetValue (t, null, null);
264                         Assert.AreEqual (null, pi.GetValue (t, null));
265                 }
266
267                 [Test]
268                 public void Bug77160 ()
269                 {
270                         object instance = new A<string> ();
271                         Type type = instance.GetType ();
272                         PropertyInfo property = type.GetProperty ("Property");
273                         Assert.AreEqual (typeof (string).FullName, property.GetValue (instance, null));
274                 }
275 #endif
276
277                 static bool HasAttribute (object [] attrs, Type attributeType)
278                 {
279                         foreach (object attr in attrs)
280                                 if (attr.GetType () == attributeType)
281                                         return true;
282                         return false;
283                 }
284
285                 static bool HasMethod (MethodInfo [] methods, string name)
286                 {
287                         foreach (MethodInfo method in methods)
288                                 if (method.Name == name)
289                                         return true;
290                         return false;
291                 }
292
293                 private class TestClass 
294                 {
295                         public string ReadOnlyProperty {
296                                 get { return string.Empty; }
297                         }
298
299                         private string Private {
300                                 get { return null; }
301                                 set { }
302                         }
303
304 #if NET_2_0
305                         public string PrivateSetter {
306                                 get { return null; }
307                                 private set { }
308                         }
309 #endif
310                 }
311         }
312 }