| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636 |
- // CustomAttributeBuilderTest.cs
- //
- // Author: Vineeth N <[email protected]>
- //
- // (C) 2004 Ximian, Inc. http://www.ximian.com
- //
- using System;
- using System.Reflection;
- using System.Reflection.Emit;
- using System.Threading;
- using NUnit.Framework;
- namespace MonoTests.System.Reflection.Emit
- {
- /// <summary>
- /// TestFixture for CustomAttributeBuilderTest.
- /// The members to be tested are as follows:
- /// 4 constructors:
- /// 1) public CustomAttributeBuilder(ConstructorInfo, object[]);
- /// 2) public CustomAttributeBuilder(ConstructorInfo, object[], FieldInfo[], object[]);
- /// 3) public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[]);
- /// 4) public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[], FieldInfo[], object[]);
- /// and the exceptions that are thrown.
- /// In the implementation , it can be seen that the first
- /// three type of constructors call the 4th type of ctor, which takes 6 args
- /// by filling args and substituting null as required.
- /// For testing constructors we have use 4 different test functions,
- /// Various exceptions have been checked for 4th type of consturctor.
- /// </summary>
- [TestFixture]
- public class CustomAttributeBuilderTest
- {
-
- // the CustomAttribute class is used for testing and it has to be public
- //since it will be associated with a class that belongs to another assembly
- [AttributeUsage(AttributeTargets.Method | AttributeTargets.Field | AttributeTargets.Class)]
- public class CustomAttribute: Attribute
- {
- private string attr1;
- private string attr2;
- public string Feild; //used for testing the second type of constructor
- public CustomAttribute () {}
- public CustomAttribute (String s1 , String s2)
- {
- attr1 = s1;
- attr2=s2;
- }
- private CustomAttribute (String s1) {}
- static CustomAttribute () {}
- public string AttributeOne
- {
- get { return attr1; }
- set { attr1 = value; }
- }
-
- public string AttributeTwo
- {
- get { return attr2; }
- //the set is skipped and is used later in testing
- }
- }
-
- private class TempClass
- {
- //used for testing the ArgumentException
- public string Field;
- public string FieldProperty
- {
- get { return Field; }
- set { Field = value; }
- }
- }
-
-
- [Test]
- public void CtorOneTest ()
- {
- //test for the constructor with signature--
- // public CustomAttributeBuilder(ConstructorInfo, object[]);
- /*
- * WE build a imaginary type as follows
- * class TestType
- * {
- * [CustomAttribute("one","two")]
- * public string Str;
- *
- * [CustomAttribute("hello","world")]
- * public void Print()
- * {Console.WriteLine("Hello World"); }
- *
- * }
- * And then check for the validity of attributes in the test functions
- */
- AssemblyName asmName = new AssemblyName ();
- asmName.Name = "TestAssembly.dll";
- AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (
- asmName , AssemblyBuilderAccess.Run);
-
- ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");
-
- TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",
- TypeAttributes.Public);
- Type[] ctorParams = new Type[] { typeof (string),typeof (string) };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- CustomAttributeBuilder feildCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { "one","two" }
- ),
- methodCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { "hello","world" }
- );
- //now let's build a feild of type string and associate a attribute with it
- FieldBuilder fieldBuilder= typeBuilder.DefineField ("Str",
- typeof (string), FieldAttributes.Public);
- fieldBuilder.SetCustomAttribute (feildCABuilder);
- //now build a method
- MethodBuilder methodBuilder= typeBuilder.DefineMethod ("Print",
- MethodAttributes.Public, null, null);
- methodBuilder.SetCustomAttribute (methodCABuilder);
- ILGenerator methodIL = methodBuilder.GetILGenerator ();
- methodIL.EmitWriteLine ("Hello, world!");
- methodIL.Emit (OpCodes.Ret);
-
- // create the type
- Type myType = typeBuilder.CreateType ();
- //Now check for the validity of the attributes.
- object testInstance = Activator.CreateInstance (myType);
- //check the validity of the attribute associated with Print method
-
- object [] methodAttrs = myType.GetMember ("Print") [0].GetCustomAttributes (true);
- Assert.AreEqual (methodAttrs.Length, 1, "#1");
- CustomAttribute methodAttr = methodAttrs [0] as CustomAttribute;
- Assert.AreEqual (methodAttr.AttributeOne, "hello", "#2");
- Assert.AreEqual (methodAttr.AttributeTwo, "world", "#3");
-
- //check the validity of the attribute associated with Str feild
- object [] fieldAttrs = myType.GetField ("Str").GetCustomAttributes (true);
- Assert.AreEqual(fieldAttrs.Length, 1, "#4");
- CustomAttribute fieldAttr = fieldAttrs [0] as CustomAttribute;
- Assert.AreEqual(fieldAttr.AttributeOne, "one", "#5");
- Assert.AreEqual(fieldAttr.AttributeTwo, "two", "#6");
- }
- [Test]
- public void CtorTwoTest ()
- {
- //test for the constructor with signature--
- // CustomAttributeBuilder Constructor (ConstructorInfo, Object[], FieldInfo[], Object[]) ;
- /*
- * WE build a imaginary type as follows
- * [CustomAttribute("Test","Type")]
- * public class TestType
- * {
- *
- * }
- * We also set the "Feild" of class CustomAttribute and the value;
- * And then check for the validity of attributes in the test functions
- */
-
- AssemblyName asmName = new AssemblyName ();
- asmName.Name = "TestAssembly.dll";
- AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (
- asmName, AssemblyBuilderAccess.Run);
-
- ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");
-
- TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",
- TypeAttributes.Public);
- Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { "Test","Type" },
- typeof(CustomAttribute).GetFields(),
- new object [] { "TestCase" }
- );
-
- typeBuilder.SetCustomAttribute (typeCABuilder);
-
- // create the type
- Type myType = typeBuilder.CreateType ();
- //Now check for the validity of the attributes.
- object testInstance = Activator.CreateInstance (myType);
- //check the validity of the attribute associated with Print method
- object [] customAttrs = myType.GetCustomAttributes (false);
- Assert.AreEqual (customAttrs.Length, 1, "1");
- //Custom Attributes of TestType
- CustomAttribute attr = customAttrs [0] as CustomAttribute;
- Assert.AreEqual (attr.AttributeOne, "Test", "#2");
- Assert.AreEqual (attr.AttributeTwo, "Type", "#3");
- Assert.AreEqual (attr.Feild, "TestCase", "#4");
- }
- [Test]
- public void CtorThreeTest ()
- {
- //test for the constructor with signature--
- // CustomAttributeBuilder Constructor (ConstructorInfo, Object[], PropertyInfo[], Object[]) ;
- /*
- * WE build a imaginary type as follows
- * [CustomAttribute()]
- * public class TestType
- * {
- *
- * }
- * We also set the "AttributeOne" of class CustomAttribute by means of the constuctor
- * And then check for the validity of attribute state
- */
-
- AssemblyName asmName = new AssemblyName ();
- asmName.Name = "TestAssembly.dll";
- AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (
- asmName, AssemblyBuilderAccess.Run);
-
- ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");
-
- TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",
- TypeAttributes.Public);
-
- Type [] ctorParams = new Type [] { };
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { },
- new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
- new object [] { "TestCase" }
- );
-
- typeBuilder.SetCustomAttribute (typeCABuilder);
-
- // create the type
- Type myType = typeBuilder.CreateType ();
- //Now check for the validity of the attributes.
- object testInstance = Activator.CreateInstance (myType);
- //check the validity of the attribute associated with Print method
- object [] customAttrs = myType.GetCustomAttributes (false);
- Assert.AreEqual (customAttrs.Length , 1, "#1");
- //Custom Attributes of TestType
- CustomAttribute attr = customAttrs [0] as CustomAttribute;
- Assert.AreEqual(attr.AttributeOne, "TestCase", "#2");
- }
- [Test]
- public void CtorFourTest ()
- {
- //test for the constructor with signature--
- //public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[], FieldInfo[], object[]);
- /*
- * WE build a imaginary type as follows
- * [CustomAttribute()]
- * public class TestType
- * {
- *
- * }
- * We also set the "AttributeOne" property ,
- * and "Feild" of class CustomAttribute
- * by means of the constuctor of CustomAttributeBuilder
- * And then check for the validity
- */
-
- AssemblyName asmName = new AssemblyName ();
- asmName.Name = "TestAssembly.dll";
- AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (
- asmName , AssemblyBuilderAccess.Run);
-
- ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");
-
- TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",
- TypeAttributes.Public);
-
- Type [] ctorParams = new Type [] { };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder(
- classCtorInfo,
- new object [] { },
- new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
- new object [] { "TestCase" },
- typeof(CustomAttribute).GetFields (),
- new object [] { "FieldValue" }
- );
-
- typeBuilder.SetCustomAttribute (typeCABuilder);
-
- // create the type
- Type myType = typeBuilder.CreateType ();
- //Now check for the validity of the attributes.
- object testInstance = Activator.CreateInstance (myType);
- //check the validity of the attribute associated with Print method
- object [] customAttrs = myType.GetCustomAttributes (false);
- Assert.AreEqual(customAttrs.Length , 1, "#1");
- //Custom Attributes of TestType
- CustomAttribute attr = customAttrs [0] as CustomAttribute;
- Assert.AreEqual (attr.AttributeOne, "TestCase", "#2");
- Assert.AreEqual (attr.Feild, "FieldValue", "#3");
- }
-
-
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ArgumentExceptionTest_1 ()
- {
- //here the constructor is static
-
- Type [] ctorParams = new Type [] { };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (BindingFlags.Static | BindingFlags.NonPublic,
- null, ctorParams, null);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { },
- new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
- new object [] { "TestCase" },
- typeof (CustomAttribute).GetFields (),
- new object [] { "FieldValue" }
- );
- }
-
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ArgumentExceptionTest_2 ()
- {
- //here the consturctor is private
-
- Type [] ctorParams = new Type[] {typeof(string) };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (BindingFlags.Instance |
- BindingFlags.NonPublic, null, ctorParams, null);
- Assert.IsNotNull (classCtorInfo);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { "hello" },
- new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
- new object [] { "TestCase" },
- typeof (CustomAttribute).GetFields (),
- new object [] { "FieldValue" }
- );
- }
-
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ArgumentExceptionTest_3 ()
- {
- // The lengths of the namedProperties and
- //propertyValues arrays are different.
-
- Type [] ctorParams = new Type [] { };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { },
- new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
- new object [] { "TestCase","extra arg" },//<--here is the error
- typeof (CustomAttribute).GetFields (),
- new object [] { "FieldValue" }
- );
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ArgumentExceptionTest_4()
- {
- //The length of the namedFields and
- //namedValues are different
-
- Type [] ctorParams = new Type [] { };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { },
- new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
- new object [] { "TestCase" },
- typeof (CustomAttribute).GetFields (),
- new object [] { }//<--here is the error
- );
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ArgumentExceptionTest_6 ()
- {
- //The type of supplied argument does not
- //match the type of the parameter declared
- //in the constructor.
-
- Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { "1", 123 },//<--here is the error,(int instead of string)
- new PropertyInfo[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
- new object [] { "TestCase" },
- typeof (CustomAttribute).GetFields (),
- new object [] { "FeildValue" }
- );
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ArgumentExceptionTest_7 ()
- {
- //A property has no setter.(CustomAttribute.AttributeTwo)
-
- Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { "1","2" },
- new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeTwo") },
- new object [] { "TestCase" },
- typeof (CustomAttribute).GetFields (),
- new object [] { "FeildValue" }
- );
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ArgumentExceptionTest_8 ()
- {
- //A property doesnot belong to same class
-
- Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { "1","2" },
- new PropertyInfo [] { typeof (TempClass).GetProperty ("FieldProperty")}, //here is the error
- new object [] { "TestCase" },
- typeof (CustomAttribute).GetFields (),
- new object [] { "FeildValue" }
- );
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ArgumentExceptionTest_9 ()
- {
- //A field doesnot belong to same class
-
- Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { "1","2" },
- new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
- new object [] {"TestCase"},
- typeof (TempClass).GetFields (), //<-- fields of TempClass are passed
- new object [] { "FeildValue" }
- );
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ArgumentExceptionTest_10 ()
- {
- //The types of the property values do
- //not match the types of the named properties.
-
-
- Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { "1","2" },
- new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
- new object [] { (long)1212121212 }, //<---type mismatch error(long for string)
- typeof (CustomAttribute).GetFields (),
- new object [] { "FeildValue" }
- );
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ArgumentExceptionTest_11 ()
- {
- //The types of the field values do
- //not match the types of the named properties.
-
- Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- Assert.IsNotNull (classCtorInfo);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { "1","2" },
- new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
- new object [] { "One" },
- typeof (CustomAttribute).GetFields (),
- new object []{ 12.1212 } //<---type mismatch error(double for string)
- );
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ArgumentNullException_1 ()
- {
- //the ctor value array (2nd argument) is null
- Type [] ctorParams = new Type [] { typeof (string),typeof (string) };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- Assert.IsNotNull (classCtorInfo);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- null, //<-- here is the error
- new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
- new object [] { "One" },
- typeof (CustomAttribute).GetFields (),
- new object [] { "feild" }
- );
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ArgumentNullException_2 ()
- {
- //the property value array (4th argument) is null
- Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- Assert.IsNotNull (classCtorInfo);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { "one","two" },
- new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
- null, // <-- here is the error
- typeof (CustomAttribute).GetFields (),
- new object [] { "feild" }
- );
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ArgumentNullException_3 ()
- {
- //the field value array (6th argument) is null
- Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
-
- ConstructorInfo classCtorInfo =
- typeof (CustomAttribute).GetConstructor (ctorParams);
- Assert.IsNotNull (classCtorInfo);
- CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
- classCtorInfo,
- new object [] { "one","two" },
- new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
- new object [] {"property"},
- typeof (CustomAttribute).GetFields (),
- null // <-- here is the error
- );
- }
- }
- }
|