New errors + tiny update ;-)
[mono.git] / mcs / class / corlib / Test / System / AttributeTest.cs
1 //
2 // AttributeTest.cs - NUnit Test Cases for the System.Attribute class
3 //
4 // Authors:
5 //      Duco Fijma (duco@lorentz.xs4all.nl)
6 //      Gonzalo Paniagua (gonzalo@ximian.com)
7 //      Gert Driesen (drieseng@users.sourceforge.net)
8 //
9 //      (C) 2002 Duco Fijma
10 //      (c) 2004 Novell, Inc. (http://www.novell.com)
11 //
12
13 using System;
14 using System.Reflection;
15 using System.Runtime.InteropServices;
16
17 using NUnit.Framework;
18
19 namespace MonoTests.System
20 {
21         using MonoTests.System.AttributeTestInternals;
22
23         namespace AttributeTestInternals
24         {
25                 [AttributeUsage (AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
26                 internal class MyCustomAttribute : Attribute
27                 {
28                         private string _info;
29
30                         public MyCustomAttribute (string info)
31                         {
32                                 _info = info;
33                         }
34
35                         public string Info {
36                                 get {
37                                         return _info;
38                                 }
39                         }
40                 }
41
42                 [AttributeUsage (AttributeTargets.Class)]
43                 internal class YourCustomAttribute : Attribute
44                 {
45                         private int _value;
46
47                         public YourCustomAttribute (int value)
48                         {
49                                 _value = value;
50                         }
51
52                         public int Value {
53                                 get {
54                                         return _value;
55                                 }
56                         }
57                 }
58
59                 [AttributeUsage (AttributeTargets.Class)]
60                 internal class UnusedAttribute : Attribute
61                 {
62                 }
63
64                 [MyCustomAttribute ("MyBaseClass"), YourCustomAttribute (37)]
65                 internal class MyClass
66                 {
67                         int Value {
68                                 get { return 42; }
69                         }
70
71                         public static void ParamsMethod(params object[] args)
72                         {
73                         }
74                 }
75
76                 [MyCustomAttribute ("MyDerivedClass")]
77                 internal class MyDerivedClass : MyClass
78                 {
79                         public void Do ()
80                         {
81                         }
82                 }
83         }
84
85         [TestFixture]
86         public class AttributeTest
87         {
88                 [Test]
89                 public void TestIsDefined ()
90                 {
91                         Assert.IsTrue (Attribute.IsDefined (typeof(MyDerivedClass), typeof(MyCustomAttribute)), "#1");
92                         Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute)), "#2");
93                         Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute)), "#3");
94                         Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (MyCustomAttribute), true), "#4");
95                         Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute), true), "#5");
96                         Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute), false), "#6");
97                         Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (MyCustomAttribute), false), "#7");
98                         Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute), false), "#8");
99                         Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute), false), "#9");
100                         Assert.IsTrue (Attribute.IsDefined (typeof (MyClass).GetMethod ("ParamsMethod").GetParameters () [0], typeof (ParamArrayAttribute), false), "#10");
101                 }
102
103                 [Test]
104                 public void IsDefined_PropertyInfo ()
105                 {
106                         PropertyInfo pi = typeof (TestBase).GetProperty ("PropBase3");
107                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#A1");
108                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#A2");
109                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#A3");
110                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#A4");
111                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#A5");
112                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#A6");
113
114                         pi = typeof (TestBase).GetProperty ("PropBase2");
115                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#C1");
116                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#C2");
117                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#C3");
118                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#C4");
119                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#C5");
120                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#C6");
121
122                         pi = typeof (TestSub).GetProperty ("PropBase2");
123                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#D1");
124                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#D2");
125                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#D3");
126                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#D4");
127                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#D5");
128                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#D6");
129                 }
130
131                 [Test]
132 #if NET_2_0
133                 [Category ("NotWorking")] // bug #81797
134 #endif
135                 public void IsDefined_PropertyInfo_Override ()
136                 {
137                         PropertyInfo pi = typeof (TestSub).GetProperty ("PropBase3");
138 #if NET_2_0
139                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#B1");
140 #else
141                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#B1");
142 #endif
143                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#B2");
144 #if NET_2_0
145                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#B3");
146 #else
147                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#B3");
148 #endif
149                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#B4");
150                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#B5");
151                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#B6");
152                 }
153
154                 [Test]
155                 public void TestGetCustomAttribute ()
156                 {
157                         int i = 1;
158                         Type t = typeof(MyDerivedClass);
159                         try {
160                                 Assert.AreEqual  ("MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute), false))).Info, "#1");
161                                 i++;
162                                 Assert.IsNull (((YourCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(YourCustomAttribute), false))), "#2");
163                                 i++;
164                                 Assert.AreEqual ("MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute)))).Info, "#3");
165                                 i++;
166                                 Assert.IsNotNull (Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)), "#4");
167                                 i++;
168                                 Assert.AreEqual (37, ((YourCustomAttribute) (Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)))).Value, "#5");
169                         } catch (Exception e) {
170                                 Assert.Fail ("Unexpected exception thrown at i=" + i + " with t=" + t + ". e=" + e);
171                         }
172                 }
173
174                 [Test]
175                 public void GetCustomAttribute_PropertyInfo ()
176                 {
177                         PropertyInfo pi = typeof (TestBase).GetProperty ("PropBase3");
178                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
179                                 typeof (PropTestAttribute)), "#A1");
180                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
181                                 typeof (PropTestAttribute), false), "#A2");
182                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
183                                 typeof (PropTestAttribute), true), "#A3");
184                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
185                                 typeof (ComVisibleAttribute)), "#A4");
186                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
187                                 typeof (ComVisibleAttribute), false), "#A5");
188                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
189                                 typeof (ComVisibleAttribute), true), "#A6");
190
191                         pi = typeof (TestBase).GetProperty ("PropBase2");
192                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
193                                 typeof (PropTestAttribute)), "#C1");
194                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
195                                 typeof (PropTestAttribute), false), "#C2");
196                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
197                                 typeof (PropTestAttribute), true), "#C3");
198                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
199                                 typeof (ComVisibleAttribute)), "#C4");
200                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
201                                 typeof (ComVisibleAttribute), false), "#C5");
202                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
203                                 typeof (ComVisibleAttribute), true), "#C6");
204
205                         pi = typeof (TestSub).GetProperty ("PropBase2");
206                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
207                                 typeof (PropTestAttribute)), "#D1");
208                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
209                                 typeof (PropTestAttribute), false), "#D2");
210                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
211                                 typeof (PropTestAttribute), true), "#D3");
212                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
213                                 typeof (ComVisibleAttribute)), "#D4");
214                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
215                                 typeof (ComVisibleAttribute), false), "#D5");
216                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
217                                 typeof (ComVisibleAttribute), true), "#D6");
218                 }
219
220                 [Test]
221                 [Category ("NotWorking")] // bug #81797
222                 public void GetCustomAttribute_PropertyInfo_Override ()
223                 {
224                         PropertyInfo pi = typeof (TestSub).GetProperty ("PropBase3");
225                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
226                                 typeof (PropTestAttribute)), "#B1");
227                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
228                                 typeof (PropTestAttribute), false), "#B2");
229                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
230                                 typeof (PropTestAttribute), true), "#B3");
231                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
232                                 typeof (ComVisibleAttribute)), "#B4");
233                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
234                                 typeof (ComVisibleAttribute), false), "#B5");
235                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
236                                 typeof (ComVisibleAttribute), true), "#B6");
237                 }
238
239                 /* Test for bug 54518 */
240                 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
241                 public class PropTestAttribute : Attribute
242                 {
243                         public PropTestAttribute ()
244                         {
245                         }
246                 }
247
248                 public class TestBase
249                 {
250                         [PropTest]
251                         public int PropBase1 {
252                                 get { return 0; }
253                                 set { }
254                         }
255
256                         [PropTest]
257                         [ComVisible (false)]
258                         public string PropBase2 {
259                                 get { return ""; }
260                                 set { }
261                         }
262
263                         [PropTest]
264                         public virtual string PropBase3 {
265                                 get { return ""; }
266                                 set { }
267                         }
268                 }
269
270                 public class TestSub : TestBase
271                 {
272                         [PropTest]
273                         public int PropSub1 {
274                                 get { return 0; }
275                                 set { }
276                         }
277
278                         [PropTest]
279                         public string PropSub2 {
280                                 get { return ""; }
281                                 set { }
282                         }
283
284                         public override string PropBase3 {
285                                 get { return ""; }
286                                 set { }
287                         }
288                 }
289
290                 [Test]
291                 public void GetCustomAttributes_Element_Null ()
292                 {
293                         // 
294                         // Assembly
295                         // 
296
297                         try {
298                                 Attribute.GetCustomAttributes ((Assembly) null);
299                                 Assert.Fail ("#A1");
300                         } catch (ArgumentNullException ex) {
301                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
302                                 Assert.IsNull (ex.InnerException, "#A3");
303                                 Assert.IsNotNull (ex.Message, "#A4");
304                                 Assert.IsNotNull (ex.ParamName, "#A5");
305                                 Assert.AreEqual ("element", ex.ParamName, "#A6");
306                         }
307
308                         try {
309                                 Attribute.GetCustomAttributes ((Assembly) null, false);
310                                 Assert.Fail ("#B1");
311                         } catch (ArgumentNullException ex) {
312                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
313                                 Assert.IsNull (ex.InnerException, "#B3");
314                                 Assert.IsNotNull (ex.Message, "#B4");
315                                 Assert.IsNotNull (ex.ParamName, "#B5");
316                                 Assert.AreEqual ("element", ex.ParamName, "#B6");
317                         }
318
319                         try {
320                                 Attribute.GetCustomAttributes ((Assembly) null, typeof (PropTestAttribute));
321                                 Assert.Fail ("#C1");
322                         } catch (ArgumentNullException ex) {
323                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
324                                 Assert.IsNull (ex.InnerException, "#C3");
325                                 Assert.IsNotNull (ex.Message, "#C4");
326                                 Assert.IsNotNull (ex.ParamName, "#C5");
327                                 Assert.AreEqual ("element", ex.ParamName, "#C6");
328                         }
329
330                         try {
331                                 Attribute.GetCustomAttributes ((Assembly) null, typeof (PropTestAttribute), false);
332                                 Assert.Fail ("#D1");
333                         } catch (ArgumentNullException ex) {
334                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
335                                 Assert.IsNull (ex.InnerException, "#D3");
336                                 Assert.IsNotNull (ex.Message, "#D4");
337                                 Assert.IsNotNull (ex.ParamName, "#D5");
338                                 Assert.AreEqual ("element", ex.ParamName, "#D6");
339                         }
340
341                         // 
342                         // MemberInfo
343                         // 
344
345                         try {
346                                 Attribute.GetCustomAttributes ((MemberInfo) null);
347                                 Assert.Fail ("#A1");
348                         } catch (ArgumentNullException ex) {
349                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
350                                 Assert.IsNull (ex.InnerException, "#A3");
351                                 Assert.IsNotNull (ex.Message, "#A4");
352                                 Assert.IsNotNull (ex.ParamName, "#A5");
353                                 Assert.AreEqual ("element", ex.ParamName, "#A6");
354                         }
355
356                         try {
357                                 Attribute.GetCustomAttributes ((MemberInfo) null, false);
358                                 Assert.Fail ("#B1");
359                         } catch (ArgumentNullException ex) {
360                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
361                                 Assert.IsNull (ex.InnerException, "#B3");
362                                 Assert.IsNotNull (ex.Message, "#B4");
363                                 Assert.IsNotNull (ex.ParamName, "#B5");
364                                 Assert.AreEqual ("element", ex.ParamName, "#B6");
365                         }
366
367                         try {
368                                 Attribute.GetCustomAttributes ((MemberInfo) null, typeof (PropTestAttribute));
369                                 Assert.Fail ("#C1");
370                         } catch (ArgumentNullException ex) {
371                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
372                                 Assert.IsNull (ex.InnerException, "#C3");
373                                 Assert.IsNotNull (ex.Message, "#C4");
374                                 Assert.IsNotNull (ex.ParamName, "#C5");
375                                 Assert.AreEqual ("element", ex.ParamName, "#C6");
376                         }
377
378                         try {
379                                 Attribute.GetCustomAttributes ((MemberInfo) null, typeof (PropTestAttribute), false);
380                                 Assert.Fail ("#D1");
381                         } catch (ArgumentNullException ex) {
382                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
383                                 Assert.IsNull (ex.InnerException, "#D3");
384                                 Assert.IsNotNull (ex.Message, "#D4");
385                                 Assert.IsNotNull (ex.ParamName, "#D5");
386                                 Assert.AreEqual ("element", ex.ParamName, "#D6");
387                         }
388
389                         // 
390                         // Module
391                         // 
392
393                         try {
394                                 Attribute.GetCustomAttributes ((Module) null);
395                                 Assert.Fail ("#A1");
396                         } catch (ArgumentNullException ex) {
397                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
398                                 Assert.IsNull (ex.InnerException, "#A3");
399                                 Assert.IsNotNull (ex.Message, "#A4");
400                                 Assert.IsNotNull (ex.ParamName, "#A5");
401                                 Assert.AreEqual ("element", ex.ParamName, "#A6");
402                         }
403
404                         try {
405                                 Attribute.GetCustomAttributes ((Module) null, false);
406                                 Assert.Fail ("#B1");
407                         } catch (ArgumentNullException ex) {
408                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
409                                 Assert.IsNull (ex.InnerException, "#B3");
410                                 Assert.IsNotNull (ex.Message, "#B4");
411                                 Assert.IsNotNull (ex.ParamName, "#B5");
412                                 Assert.AreEqual ("element", ex.ParamName, "#B6");
413                         }
414
415                         try {
416                                 Attribute.GetCustomAttributes ((Module) null, typeof (PropTestAttribute));
417                                 Assert.Fail ("#C1");
418                         } catch (ArgumentNullException ex) {
419                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
420                                 Assert.IsNull (ex.InnerException, "#C3");
421                                 Assert.IsNotNull (ex.Message, "#C4");
422                                 Assert.IsNotNull (ex.ParamName, "#C5");
423                                 Assert.AreEqual ("element", ex.ParamName, "#C6");
424                         }
425
426                         try {
427                                 Attribute.GetCustomAttributes ((Module) null, typeof (PropTestAttribute), false);
428                                 Assert.Fail ("#D1");
429                         } catch (ArgumentNullException ex) {
430                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
431                                 Assert.IsNull (ex.InnerException, "#D3");
432                                 Assert.IsNotNull (ex.Message, "#D4");
433                                 Assert.IsNotNull (ex.ParamName, "#D5");
434                                 Assert.AreEqual ("element", ex.ParamName, "#D6");
435                         }
436
437                         // 
438                         // ParameterInfo
439                         // 
440
441                         try {
442                                 Attribute.GetCustomAttributes ((ParameterInfo) null);
443                                 Assert.Fail ("#A1");
444                         } catch (ArgumentNullException ex) {
445                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
446                                 Assert.IsNull (ex.InnerException, "#A3");
447                                 Assert.IsNotNull (ex.Message, "#A4");
448                                 Assert.IsNotNull (ex.ParamName, "#A5");
449                                 Assert.AreEqual ("element", ex.ParamName, "#A6");
450                         }
451
452                         try {
453                                 Attribute.GetCustomAttributes ((ParameterInfo) null, false);
454                                 Assert.Fail ("#B1");
455                         } catch (ArgumentNullException ex) {
456                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
457                                 Assert.IsNull (ex.InnerException, "#B3");
458                                 Assert.IsNotNull (ex.Message, "#B4");
459                                 Assert.IsNotNull (ex.ParamName, "#B5");
460                                 Assert.AreEqual ("element", ex.ParamName, "#B6");
461                         }
462
463                         try {
464                                 Attribute.GetCustomAttributes ((ParameterInfo) null, typeof (PropTestAttribute));
465                                 Assert.Fail ("#C1");
466                         } catch (ArgumentNullException ex) {
467                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
468                                 Assert.IsNull (ex.InnerException, "#C3");
469                                 Assert.IsNotNull (ex.Message, "#C4");
470                                 Assert.IsNotNull (ex.ParamName, "#C5");
471                                 Assert.AreEqual ("element", ex.ParamName, "#C6");
472                         }
473
474                         try {
475                                 Attribute.GetCustomAttributes ((ParameterInfo) null, typeof (PropTestAttribute), false);
476                                 Assert.Fail ("#D1");
477                         } catch (ArgumentNullException ex) {
478                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
479                                 Assert.IsNull (ex.InnerException, "#D3");
480                                 Assert.IsNotNull (ex.Message, "#D4");
481                                 Assert.IsNotNull (ex.ParamName, "#D5");
482                                 Assert.AreEqual ("element", ex.ParamName, "#D6");
483                         }
484                 }
485
486                 [Test]
487                 public void GetCustomAttributes_PropertyInfo ()
488                 {
489                         object[] attrs;
490                         PropertyInfo pi;
491
492                         pi = typeof (TestBase).GetProperty ("PropBase3");
493                         attrs = Attribute.GetCustomAttributes (pi);
494                         Assert.AreEqual (1, attrs.Length, "#A1");
495                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A2");
496                         attrs = Attribute.GetCustomAttributes (pi, false);
497                         Assert.AreEqual (1, attrs.Length, "#A3");
498                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A4");
499                         attrs = Attribute.GetCustomAttributes (pi, true);
500                         Assert.AreEqual (1, attrs.Length, "#A5");
501                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A6");
502                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
503                         Assert.AreEqual (1, attrs.Length, "#A7");
504                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A8");
505                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
506                         Assert.AreEqual (1, attrs.Length, "#A9");
507                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A10");
508                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
509                         Assert.AreEqual (1, attrs.Length, "#A11");
510                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A12");
511                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
512                         Assert.AreEqual (0, attrs.Length, "#A13");
513                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
514                         Assert.AreEqual (0, attrs.Length, "#A14");
515                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
516                         Assert.AreEqual (0, attrs.Length, "#A15");
517
518                         pi = typeof (TestBase).GetProperty ("PropBase2");
519                         attrs = Attribute.GetCustomAttributes (pi);
520                         Assert.AreEqual (2, attrs.Length, "#C1");
521                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C2");
522                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C3");
523                         attrs = Attribute.GetCustomAttributes (pi, false);
524                         Assert.AreEqual (2, attrs.Length, "#C4");
525                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C5");
526                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C6");
527                         attrs = Attribute.GetCustomAttributes (pi, true);
528                         Assert.AreEqual (2, attrs.Length, "#C7");
529                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C8");
530                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C9");
531                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
532                         Assert.AreEqual (1, attrs.Length, "#C10");
533                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C11");
534                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
535                         Assert.AreEqual (1, attrs.Length, "#C12");
536                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C13");
537                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
538                         Assert.AreEqual (1, attrs.Length, "#C14");
539                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C15");
540                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
541                         Assert.AreEqual (1, attrs.Length, "#C16");
542                         Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C17");
543                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
544                         Assert.AreEqual (1, attrs.Length, "#C18");
545                         Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C19");
546                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
547                         Assert.AreEqual (1, attrs.Length, "#C20");
548                         Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C21");
549
550                         pi = typeof (TestSub).GetProperty ("PropBase2");
551                         attrs = Attribute.GetCustomAttributes (pi);
552                         Assert.AreEqual (2, attrs.Length, "#D1");
553                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D2");
554                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D3");
555                         attrs = Attribute.GetCustomAttributes (pi, false);
556                         Assert.AreEqual (2, attrs.Length, "#D4");
557                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D5");
558                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D6");
559                         attrs = Attribute.GetCustomAttributes (pi, true);
560                         Assert.AreEqual (2, attrs.Length, "#D7");
561                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D8");
562                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D9");
563                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
564                         Assert.AreEqual (1, attrs.Length, "#D10");
565                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D11");
566                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
567                         Assert.AreEqual (1, attrs.Length, "#D12");
568                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D13");
569                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
570                         Assert.AreEqual (1, attrs.Length, "#D14");
571                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D15");
572                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
573                         Assert.AreEqual (1, attrs.Length, "#D16");
574                         Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D17");
575                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
576                         Assert.AreEqual (1, attrs.Length, "#D18");
577                         Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D19");
578                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
579                         Assert.AreEqual (1, attrs.Length, "#D20");
580                         Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D21");
581
582                         pi = typeof (TestSub).GetProperty ("PropSub1");
583                         attrs = Attribute.GetCustomAttributes (pi);
584                         Assert.AreEqual (1, attrs.Length, "#E1");
585                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E2");
586                         attrs = Attribute.GetCustomAttributes (pi, false);
587                         Assert.AreEqual (1, attrs.Length, "#E3");
588                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E4");
589                         attrs = Attribute.GetCustomAttributes (pi, true);
590                         Assert.AreEqual (1, attrs.Length, "#E5");
591                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E6");
592                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
593                         Assert.AreEqual (1, attrs.Length, "#E7");
594                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E8");
595                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
596                         Assert.AreEqual (1, attrs.Length, "#E9");
597                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E10");
598                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
599                         Assert.AreEqual (1, attrs.Length, "#E11");
600                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E12");
601                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
602                         Assert.AreEqual (0, attrs.Length, "#E13");
603                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
604                         Assert.AreEqual (0, attrs.Length, "#E14");
605                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
606                         Assert.AreEqual (0, attrs.Length, "#D15");
607                 }
608
609                 [Test]
610                 [Category ("NotWorking")] // bug #81797
611                 public void GetCustomAttributes_PropertyInfo_Override ()
612                 {
613                         object [] attrs;
614                         PropertyInfo pi;
615
616                         pi = typeof (TestSub).GetProperty ("PropBase3");
617                         attrs = Attribute.GetCustomAttributes (pi);
618                         Assert.AreEqual (1, attrs.Length, "#B1");
619                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B2");
620                         attrs = Attribute.GetCustomAttributes (pi, false);
621                         Assert.AreEqual (0, attrs.Length, "#B3");
622                         attrs = Attribute.GetCustomAttributes (pi, true);
623                         Assert.AreEqual (1, attrs.Length, "#B4");
624                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B5");
625                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
626                         Assert.AreEqual (1, attrs.Length, "#B6");
627                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B7");
628                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
629                         Assert.AreEqual (0, attrs.Length, "#B8");
630                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
631                         Assert.AreEqual (1, attrs.Length, "#B9");
632                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B10");
633                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
634                         Assert.AreEqual (0, attrs.Length, "#B11");
635                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
636                         Assert.AreEqual (0, attrs.Length, "#B12");
637                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
638                         Assert.AreEqual (0, attrs.Length, "#B13");
639                 }
640
641                 [Test]
642                 public void GetCustomAttributeOK ()
643                 {
644                         Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
645                                 typeof(DerivedTestCustomAttributeInherit));
646                         Assert.IsNotNull (attribute);
647                 }
648
649                 [Test]
650                 [ExpectedException (typeof(AmbiguousMatchException))]
651                 public void GetCustomAttributeAmbiguous ()
652                 {
653                         Attribute.GetCustomAttribute (typeof(ClassA), typeof(TestCustomAttribute));
654                 }
655
656                 [Test]
657                 public void GetCustomAttributeNull ()
658                 {
659                         Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
660                                 typeof(DerivedTestCustomAttributeMultipleInherit));
661                         Assert.IsNull (attribute);
662                 }
663
664                 [Test]
665                 public void GetCustomAttributesTypeNoInherit ()
666                 {
667                         object[] attributes;
668
669                         attributes = Attribute.GetCustomAttributes (typeof(ClassA), false);
670                         Assert.AreEqual (3, attributes.Length, "#A1");
671                         Assert.AreEqual (1, GetAttributeCount (attributes,
672                                 typeof(TestCustomAttribute)), "#A2");
673                         Assert.AreEqual (1, GetAttributeCount (attributes,
674                                 typeof(DerivedTestCustomAttributeMultiple)), "#A3");
675                         Assert.AreEqual (1, GetAttributeCount (attributes,
676                                 typeof(DerivedTestCustomAttributeInherit)), "#A4");
677
678                         attributes = Attribute.GetCustomAttributes (typeof(ClassB), false);
679                         Assert.AreEqual (4, attributes.Length, "#B1");
680                         Assert.AreEqual (1, GetAttributeCount (attributes,
681                                 typeof(TestCustomAttribute)), "#B2");
682                         Assert.AreEqual (2, GetAttributeCount (attributes,
683                                 typeof(DerivedTestCustomAttributeMultiple)), "#B3");
684                         Assert.AreEqual (1, GetAttributeCount (attributes,
685                                 typeof(DerivedTestCustomAttributeMultipleInherit)), "#B4");
686                 }
687
688                 [Test]
689                 public void GetCustomAttributesTypeInherit ()
690                 {
691                         object[] attributes;
692
693                         attributes = Attribute.GetCustomAttributes (typeof(ClassA), true);
694                         Assert.AreEqual (3, attributes.Length, "#A1");
695                         Assert.AreEqual (1, GetAttributeCount (attributes,
696                                 typeof(TestCustomAttribute)), "#A2");
697                         Assert.AreEqual (1, GetAttributeCount (attributes,
698                                 typeof(DerivedTestCustomAttributeMultiple)), "#A3");
699                         Assert.AreEqual (1, GetAttributeCount (attributes,
700                                 typeof(DerivedTestCustomAttributeInherit)), "#A4");
701
702                         attributes = Attribute.GetCustomAttributes (typeof(ClassB), true);
703                         Assert.AreEqual (5, attributes.Length, "#B1");
704                         Assert.AreEqual (1, GetAttributeCount (attributes,
705                                 typeof(TestCustomAttribute)), "#B2");
706                         Assert.AreEqual (2, GetAttributeCount (attributes,
707                                 typeof(DerivedTestCustomAttributeMultiple)), "#B3");
708                         Assert.AreEqual (1, GetAttributeCount (attributes,
709                                 typeof(DerivedTestCustomAttributeInherit)), "#B4");
710                         Assert.AreEqual (1, GetAttributeCount (attributes,
711                                 typeof(DerivedTestCustomAttributeMultipleInherit)), "#B5");
712                 }
713
714                 [Test]
715                 public void TestEquality ()
716                 {
717                         MyCustomAttribute a = new MyCustomAttribute ("one");
718                         MyCustomAttribute b = new MyCustomAttribute ("two");
719                         MyCustomAttribute c = new MyCustomAttribute ("one");
720                         MyCustomAttribute d = a;
721                         
722                         Assert.IsTrue (a.Equals (c), "#1");
723                         Assert.IsTrue (c.Equals (a), "#2");
724                         Assert.IsFalse (c.Equals (b), "#3");
725                         Assert.IsFalse (b.Equals (a), "#4");
726                         Assert.IsFalse (b.Equals (c), "#5");
727                         Assert.IsTrue (a.Equals (a), "#6");
728                         Assert.IsTrue (a.Equals (d), "#7");
729                         Assert.IsFalse (a.Equals (null), "#8");
730                 }
731
732                 private int GetAttributeCount (object[] attributes, Type attributeType)
733                 {
734                         int counter = 0;
735
736                         foreach (Attribute attribute in attributes) {
737                                 if (attribute.GetType () == attributeType)
738                                         counter++;
739                         }
740
741                         return counter;
742                 }
743
744                 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
745                 private class TestCustomAttribute : Attribute
746                 {
747                 }
748
749                 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = false)]
750                 private class DerivedTestCustomAttributeMultiple : TestCustomAttribute
751                 {
752                 }
753
754                 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
755                 private class DerivedTestCustomAttributeInherit : TestCustomAttribute
756                 {
757                 }
758
759                 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = true)]
760                 private class DerivedTestCustomAttributeMultipleInherit : TestCustomAttribute
761                 {
762                 }
763
764                 [TestCustomAttribute]
765                 [DerivedTestCustomAttributeMultiple]
766                 [DerivedTestCustomAttributeInherit]
767                 private class ClassA
768                 {
769                 }
770
771                 [TestCustomAttribute ()]
772                 [DerivedTestCustomAttributeMultiple ()]
773                 [DerivedTestCustomAttributeMultiple ()]
774                 [DerivedTestCustomAttributeMultipleInherit ()]
775                 private class ClassB : ClassA
776                 {
777                 }
778
779                 [TestCustomAttribute ()]
780                 [DerivedTestCustomAttributeMultiple ()]
781                 [DerivedTestCustomAttributeMultipleInherit ()]
782                 private class ClassC : ClassB
783                 {
784                 }
785         }
786 }