2 // AttributeTest.cs - NUnit Test Cases for the System.Attribute class
5 // Duco Fijma (duco@lorentz.xs4all.nl)
6 // Gonzalo Paniagua (gonzalo@ximian.com)
7 // Gert Driesen (drieseng@users.sourceforge.net)
10 // (c) 2004 Novell, Inc. (http://www.novell.com)
13 using NUnit.Framework;
15 using System.Reflection;
17 namespace MonoTests.System
19 using MonoTests.System.AttributeTestInternals;
21 namespace AttributeTestInternals
23 [AttributeUsage (AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
24 internal class MyCustomAttribute : Attribute
28 public MyCustomAttribute (string info)
42 [AttributeUsage (AttributeTargets.Class)]
43 internal class YourCustomAttribute : Attribute
47 public YourCustomAttribute (int value)
61 [AttributeUsage (AttributeTargets.Class)]
62 internal class UnusedAttribute : Attribute
66 [MyCustomAttribute ("MyBaseClass"), YourCustomAttribute (37)]
67 internal class MyClass
69 int Value { get { return 42; } }
71 public static void ParamsMethod(params object[] args)
76 [MyCustomAttribute ("MyDerivedClass")]
77 internal class MyDerivedClass : MyClass
84 public class AttributeTest : Assertion
86 public AttributeTest () { }
88 public void TestIsDefined ()
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));
102 public void TestGetCustomAttribute ()
105 Type t = typeof(MyDerivedClass);
108 AssertEquals ("A1", "MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute), false))).Info);
110 AssertEquals ("A2", null, ((YourCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(YourCustomAttribute), false))));
112 AssertEquals ("A3", "MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute)))).Info);
114 Console.WriteLine ("A4a");
115 AssertNotNull ("A4a", Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)));
117 AssertEquals ("A4", 37, ((YourCustomAttribute) (Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)))).Value);
121 Fail ("Unexpected exception thrown at i=" + i + " with t=" + t + ". e=" + e);
125 /* Test for bug 54518 */
126 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
127 public class PropTestAttribute : Attribute
129 public PropTestAttribute () { }
132 public class TestBase
134 public TestBase () { }
144 public string PropBase2
151 public class TestSub : TestBase
153 public TestSub () { }
163 public string PropSub2
171 public void BaseAttributes ()
174 PropertyInfo[] props = typeof (TestSub).GetProperties (BindingFlags.Public | BindingFlags.Instance);
176 foreach (PropertyInfo prop in props)
178 attrs = prop.GetCustomAttributes (typeof(PropTestAttribute), true);
179 AssertEquals (prop.Name, true, attrs.Length > 0);
184 public void GetCustomAttributeOK ()
186 Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
187 typeof(DerivedTestCustomAttributeInherit));
188 AssertNotNull ("GetCustomAttributeNull", attribute);
191 [NUnit.Framework.Test]
192 [ExpectedException (typeof(AmbiguousMatchException))]
193 public void GetCustomAttributeAmbiguous ()
195 Attribute.GetCustomAttribute (typeof(ClassA), typeof(TestCustomAttribute));
199 public void GetCustomAttributeNull ()
201 Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
202 typeof(DerivedTestCustomAttributeMultipleInherit));
203 AssertNull ("GetCustomAttributeNull", attribute);
207 public void GetCustomAttributesTypeNoInherit ()
211 attributes = Attribute.GetCustomAttributes (typeof(ClassA), false);
212 AssertEquals ("GetCustomAttributesTypeNoInherit#1", 3, attributes.Length);
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)));
221 attributes = Attribute.GetCustomAttributes (typeof(ClassB), false);
222 AssertEquals ("GetCustomAttributesTypeNoInherit#5", 4, attributes.Length);
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)));
233 public void GetCustomAttributesTypeInherit ()
237 attributes = Attribute.GetCustomAttributes (typeof(ClassA), true);
239 AssertEquals ("GetCustomAttributesTypeInherit#1", 3, attributes.Length);
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)));
248 attributes = Attribute.GetCustomAttributes (typeof(ClassB), true);
249 AssertEquals ("GetCustomAttributesTypeInherit#5", 5, attributes.Length);
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)));
262 public void TestEquality ()
264 MyCustomAttribute a = new MyCustomAttribute ("one");
265 MyCustomAttribute b = new MyCustomAttribute ("two");
266 MyCustomAttribute c = new MyCustomAttribute ("one");
267 MyCustomAttribute d = a;
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));
277 AssertEquals ("Attribute, TestEquality #7", false, a.Equals (null));
280 private int GetAttributeCount (object[] attributes, Type attributeType)
284 foreach (Attribute attribute in attributes)
286 if (attribute.GetType () == attributeType)
295 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
296 private class TestCustomAttribute : Attribute
300 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = false)]
301 private class DerivedTestCustomAttributeMultiple : TestCustomAttribute
305 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
306 private class DerivedTestCustomAttributeInherit : TestCustomAttribute
310 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = true)]
311 private class DerivedTestCustomAttributeMultipleInherit : TestCustomAttribute
315 [TestCustomAttribute]
316 [DerivedTestCustomAttributeMultiple]
317 [DerivedTestCustomAttributeInherit]
322 [TestCustomAttribute ()]
323 [DerivedTestCustomAttributeMultiple ()]
324 [DerivedTestCustomAttributeMultiple ()]
325 [DerivedTestCustomAttributeMultipleInherit ()]
326 private class ClassB : ClassA
330 [TestCustomAttribute ()]
331 [DerivedTestCustomAttributeMultiple ()]
332 [DerivedTestCustomAttributeMultipleInherit ()]
333 private class ClassC : ClassB