diff options
Diffstat (limited to 'mcs/class/corlib/Test/System/AttributeTest.cs')
-rwxr-xr-x | mcs/class/corlib/Test/System/AttributeTest.cs | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/mcs/class/corlib/Test/System/AttributeTest.cs b/mcs/class/corlib/Test/System/AttributeTest.cs new file mode 100755 index 00000000000..25d4329623b --- /dev/null +++ b/mcs/class/corlib/Test/System/AttributeTest.cs @@ -0,0 +1,155 @@ +// +// AttributeTest.cs - NUnit Test Cases for the System.Attribute class +// +// author: +// Duco Fijma (duco@lorentz.xs4all.nl) +// +// (C) 2002 Duco Fijma +// + +using NUnit.Framework; +using System; + +namespace MonoTests.System +{ + +// Inner namespace for some test helper classes +using MonoTests.System.AttributeTestInternals; + +namespace AttributeTestInternals +{ + +[AttributeUsage(AttributeTargets.Class, AllowMultiple=true, Inherited=false)] +internal class MyCustomAttribute : Attribute { + + private string _info; + + public MyCustomAttribute (string info) + { + _info = info; + } + + public string Info + { + get { + return _info; + } + } + +} + +[AttributeUsage(AttributeTargets.Class)] +internal class YourCustomAttribute : Attribute { + + private int _value; + + public YourCustomAttribute (int value) + { + _value = value; + } + + public int Value + { + get { + return _value; + } + } +} + +[AttributeUsage(AttributeTargets.Class)] +internal class UnusedAttribute : Attribute { +} + +[MyCustomAttribute("MyBaseClass"), YourCustomAttribute(37)] +internal class MyClass { + int Value { get { return 42; }} +} + +[MyCustomAttribute("MyDerivedClass")] +internal class MyDerivedClass : MyClass { + public void Do () {} +} + +} // Namespace MonoTests.System.AttributeTestInternals + +public class AttributeTest : TestCase { + + public AttributeTest () : base("MonoTests.System.AttributeTest testcase") {} + public AttributeTest (string name) : base(name) {} + + public static ITest Suite { + get { + return new TestSuite(typeof(AttributeTest)); + } + } + + public void TestIsDefined () + { + AssertEquals ("A1", true, Attribute.IsDefined(typeof(MyDerivedClass), typeof(MyCustomAttribute))); + AssertEquals ("A2", true, Attribute.IsDefined(typeof(MyDerivedClass), typeof(YourCustomAttribute))); + AssertEquals ("A3", false, Attribute.IsDefined(typeof(MyDerivedClass), typeof(UnusedAttribute))); + AssertEquals ("A4", true, Attribute.IsDefined(typeof(MyDerivedClass), typeof(MyCustomAttribute), true)); + AssertEquals ("A5", true, Attribute.IsDefined(typeof(MyDerivedClass), typeof(YourCustomAttribute), true)); + AssertEquals ("A6", false, Attribute.IsDefined(typeof(MyDerivedClass), typeof(UnusedAttribute), false)); + AssertEquals ("A7", true, Attribute.IsDefined(typeof(MyDerivedClass), typeof(MyCustomAttribute), false)); + AssertEquals ("A8", false, Attribute.IsDefined(typeof(MyDerivedClass), typeof(YourCustomAttribute), false)); + AssertEquals ("A9", false, Attribute.IsDefined(typeof(MyDerivedClass), typeof(UnusedAttribute), false)); + } + +/* + public static void TestIsDefaultAttribute () + { + Console.WriteLine(">>>IsDefaultAttribute"); + + Attribute a = Attribute.GetCustomAttribute(typeof(MyClass), typeof(MyCustomAttribute)); + Console.WriteLine (a.IsDefaultAttribute() ); + } + + private static void WriteAttribute (Attribute a) + { + if (a == null) { + Console.WriteLine ("NULL"); + } + else { + Console.WriteLine (a); + } + } +*/ + + public void TestGetCustomAttribute () + { + AssertEquals ("A3", "MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute(typeof(MyDerivedClass), typeof(MyCustomAttribute), false))).Info); + AssertEquals("A4", null, ((YourCustomAttribute) (Attribute.GetCustomAttribute(typeof(MyDerivedClass), typeof(YourCustomAttribute), false)))); + AssertEquals ("A1", "MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute(typeof(MyDerivedClass), typeof(MyCustomAttribute)))).Info); + AssertEquals("A2", 37, ((YourCustomAttribute) (Attribute.GetCustomAttribute(typeof(MyDerivedClass), typeof(YourCustomAttribute)))).Value); + } +/* + + public static void WriteAttributes (Attribute[] attrs) + { + Console.WriteLine("length = {0}", attrs.Length); + foreach (Attribute a in attrs) { + WriteAttribute (a); + } + } + + + private static void TestGetCustomAttributes () + { + Console.WriteLine(">>>GetCustomAttributes"); + + + WriteAttributes (Attribute.GetCustomAttributes(typeof(MyDerivedClass), typeof(MyCustomAttribute))); + WriteAttributes (Attribute.GetCustomAttributes(typeof(MyDerivedClass), typeof(MyCustomAttribute), true)); + WriteAttributes (Attribute.GetCustomAttributes(typeof(MyDerivedClass), typeof(MyCustomAttribute), false)); + + WriteAttributes (Attribute.GetCustomAttributes(typeof(MyDerivedClass))); + WriteAttributes (Attribute.GetCustomAttributes(typeof(MyDerivedClass), true)); + WriteAttributes (Attribute.GetCustomAttributes(typeof(MyDerivedClass), false)); + } + +*/ + +} + +} |