New test.
[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 NUnit.Framework;
14 using System;
15 using System.Reflection;
16
17 namespace MonoTests.System
18 {
19         using MonoTests.System.AttributeTestInternals;
20
21         namespace AttributeTestInternals
22         {
23                 [AttributeUsage (AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
24                 internal class MyCustomAttribute : Attribute
25                 {
26                         private string _info;
27
28                         public MyCustomAttribute (string info)
29                         {
30                                 _info = info;
31                         }
32
33                         public string Info
34                         {
35                                 get
36                                 {
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                         {
54                                 get
55                                 {
56                                         return _value;
57                                 }
58                         }
59                 }
60
61                 [AttributeUsage (AttributeTargets.Class)]
62                 internal class UnusedAttribute : Attribute
63                 {
64                 }
65
66                 [MyCustomAttribute ("MyBaseClass"), YourCustomAttribute (37)]
67                 internal class MyClass
68                 {
69                         int Value { get { return 42; } }
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         [TestFixture]
84         public class AttributeTest : Assertion
85         {
86                 public AttributeTest () { }
87
88                 public void TestIsDefined ()
89                 {
90                         AssertEquals ("A1", true, Attribute.IsDefined (typeof(MyDerivedClass), typeof(MyCustomAttribute)));
91                         AssertEquals ("A2", true, Attribute.IsDefined (typeof(MyDerivedClass), typeof(YourCustomAttribute)));
92                         AssertEquals ("A3", false, Attribute.IsDefined (typeof(MyDerivedClass), typeof(UnusedAttribute)));
93                         AssertEquals ("A4", true, Attribute.IsDefined (typeof(MyDerivedClass), typeof(MyCustomAttribute), true));
94                         AssertEquals ("A5", true, Attribute.IsDefined (typeof(MyDerivedClass), typeof(YourCustomAttribute), true));
95                         AssertEquals ("A6", false, Attribute.IsDefined (typeof(MyDerivedClass), typeof(UnusedAttribute), false));
96                         AssertEquals ("A7", true, Attribute.IsDefined (typeof(MyDerivedClass), typeof(MyCustomAttribute), false));
97                         AssertEquals ("A8", false, Attribute.IsDefined (typeof(MyDerivedClass), typeof(YourCustomAttribute), false));
98                         AssertEquals ("A9", false, Attribute.IsDefined (typeof(MyDerivedClass), typeof(UnusedAttribute), false));
99                         AssertEquals ("A10", true, Attribute.IsDefined (typeof (MyClass).GetMethod ("ParamsMethod").GetParameters ()[0], typeof (ParamArrayAttribute), false));
100                 }
101
102                 public void TestGetCustomAttribute ()
103                 {
104                         int i = 1;
105                         Type t = typeof(MyDerivedClass);
106                         try
107                         {
108                                 AssertEquals ("A1", "MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute), false))).Info);
109                                 i++;
110                                 AssertEquals ("A2", null, ((YourCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(YourCustomAttribute), false))));
111                                 i++;
112                                 AssertEquals ("A3", "MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute)))).Info);
113                                 i++;
114                                 Console.WriteLine ("A4a");
115                                 AssertNotNull ("A4a", Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)));
116                                 i++;
117                                 AssertEquals ("A4", 37, ((YourCustomAttribute) (Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)))).Value);
118                         }
119                         catch (Exception e)
120                         {
121                                 Fail ("Unexpected exception thrown at i=" + i + " with t=" + t + ". e=" + e);
122                         }
123                 }
124
125                 /* Test for bug 54518 */
126                 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
127                 public class PropTestAttribute : Attribute
128                 {
129                         public PropTestAttribute () { }
130                 }
131
132                 public class TestBase
133                 {
134                         public TestBase () { }
135
136                         [PropTest]
137                         public int PropBase1
138                         {
139                                 get { return 0; }
140                                 set { }
141                         }
142
143                         [PropTest]
144                         public string PropBase2
145                         {
146                                 get { return ""; }
147                                 set { }
148                         }
149                 }
150
151                 public class TestSub : TestBase
152                 {
153                         public TestSub () { }
154
155                         [PropTest]
156                         public int PropSub1
157                         {
158                                 get { return 0; }
159                                 set { }
160                         }
161
162                         [PropTest]
163                         public string PropSub2
164                         {
165                                 get { return ""; }
166                                 set { }
167                         }
168                 }
169
170                 [Test]
171                 public void BaseAttributes ()
172                 {
173                         object[] attrs;
174                         PropertyInfo[] props = typeof (TestSub).GetProperties (BindingFlags.Public | BindingFlags.Instance);
175
176                         foreach (PropertyInfo prop in props)
177                         {
178                                 attrs = prop.GetCustomAttributes (typeof(PropTestAttribute), true);
179                                 AssertEquals (prop.Name, true, attrs.Length > 0);
180                         }
181                 }
182
183                 [Test]
184                 public void GetCustomAttributeOK ()
185                 {
186                         Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
187                                 typeof(DerivedTestCustomAttributeInherit));
188                         AssertNotNull ("GetCustomAttributeNull", attribute);
189                 }
190
191                 [NUnit.Framework.Test]
192                 [ExpectedException (typeof(AmbiguousMatchException))]
193                 public void GetCustomAttributeAmbiguous ()
194                 {
195                         Attribute.GetCustomAttribute (typeof(ClassA), typeof(TestCustomAttribute));
196                 }
197
198                 [Test]
199                 public void GetCustomAttributeNull ()
200                 {
201                         Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
202                                 typeof(DerivedTestCustomAttributeMultipleInherit));
203                         AssertNull ("GetCustomAttributeNull", attribute);
204                 }
205
206                 [Test]
207                 public void GetCustomAttributesTypeNoInherit ()
208                 {
209                         object[] attributes;
210
211                         attributes = Attribute.GetCustomAttributes (typeof(ClassA), false);
212                         AssertEquals ("GetCustomAttributesTypeNoInherit#1", 3, attributes.Length);
213
214                         AssertEquals ("GetCustomAttributesTypeNoInherit#2", 1, GetAttributeCount (
215                                 attributes, typeof(TestCustomAttribute)));
216                         AssertEquals ("GetCustomAttributesTypeNoInherit#3", 1, GetAttributeCount (
217                                 attributes, typeof(DerivedTestCustomAttributeMultiple)));
218                         AssertEquals ("GetCustomAttributesTypeNoInherit#4", 1, GetAttributeCount (
219                                 attributes, typeof(DerivedTestCustomAttributeInherit)));
220
221                         attributes = Attribute.GetCustomAttributes (typeof(ClassB), false);
222                         AssertEquals ("GetCustomAttributesTypeNoInherit#5", 4, attributes.Length);
223
224                         AssertEquals ("GetCustomAttributesTypeNoInherit#2", 1, GetAttributeCount (
225                                 attributes, typeof(TestCustomAttribute)));
226                         AssertEquals ("GetCustomAttributesTypeNoInherit#3", 2, GetAttributeCount (
227                                 attributes, typeof(DerivedTestCustomAttributeMultiple)));
228                         AssertEquals ("GetCustomAttributesTypeNoInherit#4", 1, GetAttributeCount (
229                                 attributes, typeof(DerivedTestCustomAttributeMultipleInherit)));
230                 }
231
232                 [Test]
233                 public void GetCustomAttributesTypeInherit ()
234                 {
235                         object[] attributes;
236
237                         attributes = Attribute.GetCustomAttributes (typeof(ClassA), true);
238
239                         AssertEquals ("GetCustomAttributesTypeInherit#1", 3, attributes.Length);
240
241                         AssertEquals ("GetCustomAttributesTypeInherit#2", 1, GetAttributeCount (
242                                 attributes, typeof(TestCustomAttribute)));
243                         AssertEquals ("GetCustomAttributesTypeInherit#3", 1, GetAttributeCount (
244                                 attributes, typeof(DerivedTestCustomAttributeMultiple)));
245                         AssertEquals ("GetCustomAttributesTypeInherit#4", 1, GetAttributeCount (
246                                 attributes, typeof(DerivedTestCustomAttributeInherit)));
247
248                         attributes = Attribute.GetCustomAttributes (typeof(ClassB), true);
249                         AssertEquals ("GetCustomAttributesTypeInherit#5", 5, attributes.Length);
250
251                         AssertEquals ("GetCustomAttributesTypeInherit#6", 1, GetAttributeCount (
252                                 attributes, typeof(TestCustomAttribute)));
253                         AssertEquals ("GetCustomAttributesTypeInherit#7", 2, GetAttributeCount (
254                                 attributes, typeof(DerivedTestCustomAttributeMultiple)));
255                         AssertEquals ("GetCustomAttributesTypeInherit#8", 1, GetAttributeCount (
256                                 attributes, typeof(DerivedTestCustomAttributeInherit)));
257                         AssertEquals ("GetCustomAttributesTypeInherit#9", 1, GetAttributeCount (
258                                 attributes, typeof(DerivedTestCustomAttributeMultipleInherit)));
259                 }
260
261                 [Test]
262                 public void TestEquality ()
263                 {
264                         MyCustomAttribute a = new MyCustomAttribute ("one");
265                         MyCustomAttribute b = new MyCustomAttribute ("two");
266                         MyCustomAttribute c = new MyCustomAttribute ("one");
267                         MyCustomAttribute d = a;
268                         
269                         AssertEquals ("Attribute, TestEquality #1", true,  a.Equals (c));
270                         AssertEquals ("Attribute, TestEquality #2", true,  c.Equals (a));
271                         AssertEquals ("Attribute, TestEquality #3", false, c.Equals (b));
272                         AssertEquals ("Attribute, TestEquality #4", false, b.Equals (a));
273                         AssertEquals ("Attribute, TestEquality #5", false, b.Equals (c));
274                         AssertEquals ("Attribute, TestEquality #6", true,  a.Equals (a));
275                         AssertEquals ("Attribute, TestEquality #7", true,  a.Equals (d));
276
277                         AssertEquals ("Attribute, TestEquality #7", false, a.Equals (null));
278                 }
279
280                 private int GetAttributeCount (object[] attributes, Type attributeType)
281                 {
282                         int counter = 0;
283
284                         foreach (Attribute attribute in attributes)
285                         {
286                                 if (attribute.GetType () == attributeType)
287                                 {
288                                         counter++;
289                                 }
290                         }
291
292                         return counter;
293                 }
294
295                 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
296                 private class TestCustomAttribute : Attribute
297                 {
298                 }
299
300                 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = false)]
301                 private class DerivedTestCustomAttributeMultiple : TestCustomAttribute
302                 {
303                 }
304
305                 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
306                 private class DerivedTestCustomAttributeInherit : TestCustomAttribute
307                 {
308                 }
309
310                 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = true)]
311                 private class DerivedTestCustomAttributeMultipleInherit : TestCustomAttribute
312                 {
313                 }
314
315                 [TestCustomAttribute]
316                 [DerivedTestCustomAttributeMultiple]
317                 [DerivedTestCustomAttributeInherit]
318                 private class ClassA
319                 {
320                 }
321
322                 [TestCustomAttribute ()]
323                 [DerivedTestCustomAttributeMultiple ()]
324                 [DerivedTestCustomAttributeMultiple ()]
325                 [DerivedTestCustomAttributeMultipleInherit ()]
326                 private class ClassB : ClassA
327                 {
328                 }
329
330                 [TestCustomAttribute ()]
331                 [DerivedTestCustomAttributeMultiple ()]
332                 [DerivedTestCustomAttributeMultipleInherit ()]
333                 private class ClassC : ClassB
334                 {
335                 }
336         }
337 }