1 // CustomAttributeBuilderTest.cs
3 // Author: Vineeth N <nvineeth@yahoo.com>
5 // (C) 2004 Ximian, Inc. http://www.ximian.com
8 using System
.Reflection
;
9 using System
.Reflection
.Emit
;
10 using System
.Threading
;
11 using NUnit
.Framework
;
13 namespace MonoTests
.System
.Reflection
.Emit
16 /// TestFixture for CustomAttributeBuilderTest.
17 /// The members to be tested are as follows:
19 /// 1) public CustomAttributeBuilder(ConstructorInfo, object[]);
20 /// 2) public CustomAttributeBuilder(ConstructorInfo, object[], FieldInfo[], object[]);
21 /// 3) public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[]);
22 /// 4) public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[], FieldInfo[], object[]);
23 /// and the exceptions that are thrown.
24 /// In the implementation , it can be seen that the first
25 /// three type of constructors call the 4th type of ctor, which takes 6 args
26 /// by filling args and substituting null as required.
27 /// For testing constructors we have use 4 different test functions,
28 /// Various exceptions have been checked for 4th type of consturctor.
32 public class CustomAttributeBuilderTest
: Assertion
35 // the CustomAttribute class is used for testing and it has to be public
36 //since it will be associated with a class that belongs to another assembly
38 [AttributeUsage(AttributeTargets
.Method
| AttributeTargets
.Field
| AttributeTargets
.Class
)]
39 public class CustomAttribute
: Attribute
43 public string Feild
; //used for testing the second type of constructor
45 public CustomAttribute () {}
46 public CustomAttribute (String s1
, String s2
)
52 private CustomAttribute (String s1
) {}
53 static CustomAttribute () {}
55 public string AttributeOne
58 set { attr1 = value; }
61 public string AttributeTwo
64 //the set is skipped and is used later in testing
69 private class TempClass
71 //used for testing the ArgumentException
73 public string FieldProperty
76 set { Field = value; }
84 public void CtorOneTest ()
86 //test for the constructor with signature--
87 // public CustomAttributeBuilder(ConstructorInfo, object[]);
89 * WE build a imaginary type as follows
92 * [CustomAttribute("one","two")]
95 * [CustomAttribute("hello","world")]
97 * {Console.WriteLine("Hello World"); }
100 * And then check for the validity of attributes in the test functions
102 AssemblyName asmName
= new AssemblyName ();
103 asmName
.Name
= "TestAssembly.dll";
105 AssemblyBuilder asmBuilder
= Thread
.GetDomain ().DefineDynamicAssembly (
106 asmName
, AssemblyBuilderAccess
.Run
);
108 ModuleBuilder modBuilder
= asmBuilder
.DefineDynamicModule ("TestModule");
110 TypeBuilder typeBuilder
= modBuilder
.DefineType ("TestType",
111 TypeAttributes
.Public
);
113 Type
[] ctorParams
= new Type
[] { typeof (string),typeof (string) }
;
115 ConstructorInfo classCtorInfo
=
116 typeof (CustomAttribute
).GetConstructor (ctorParams
);
118 CustomAttributeBuilder feildCABuilder
= new CustomAttributeBuilder (
120 new object [] { "one","two" }
122 methodCABuilder
= new CustomAttributeBuilder (
124 new object [] { "hello","world" }
126 //now let's build a feild of type string and associate a attribute with it
127 FieldBuilder fieldBuilder
= typeBuilder
.DefineField ("Str",
128 typeof (string), FieldAttributes
.Public
);
129 fieldBuilder
.SetCustomAttribute (feildCABuilder
);
131 MethodBuilder methodBuilder
= typeBuilder
.DefineMethod ("Print",
132 MethodAttributes
.Public
, null, null);
133 methodBuilder
.SetCustomAttribute (methodCABuilder
);
134 ILGenerator methodIL
= methodBuilder
.GetILGenerator ();
135 methodIL
.EmitWriteLine ("Hello, world!");
136 methodIL
.Emit (OpCodes
.Ret
);
139 Type myType
= typeBuilder
.CreateType ();
141 //Now check for the validity of the attributes.
142 object testInstance
= Activator
.CreateInstance (myType
);
144 //check the validity of the attribute associated with Print method
146 object [] methodAttrs
= myType
.GetMember ("Print") [0].GetCustomAttributes (true);
147 AssertEquals ("#method Print has exactly one attribute", methodAttrs
.Length
, 1);
148 CustomAttribute methodAttr
= methodAttrs
[0] as CustomAttribute
;
149 AssertEquals ("#AttributeOne", methodAttr
.AttributeOne
, "hello");
150 AssertEquals ("#AttributeTwo", methodAttr
.AttributeTwo
, "world");
152 //check the validity of the attribute associated with Str feild
154 object [] fieldAttrs
= myType
.GetField ("Str").GetCustomAttributes (true);
155 AssertEquals("#feild Str has exactly one attribute", fieldAttrs
.Length
, 1);
156 CustomAttribute fieldAttr
= fieldAttrs
[0] as CustomAttribute
;
157 AssertEquals("#AttributeOne", fieldAttr
.AttributeOne
, "one");
158 AssertEquals("#AttributeTwo", fieldAttr
.AttributeTwo
, "two");
162 public void CtorTwoTest ()
164 //test for the constructor with signature--
165 // CustomAttributeBuilder Constructor (ConstructorInfo, Object[], FieldInfo[], Object[]) ;
167 * WE build a imaginary type as follows
168 * [CustomAttribute("Test","Type")]
169 * public class TestType
173 * We also set the "Feild" of class CustomAttribute and the value;
174 * And then check for the validity of attributes in the test functions
177 AssemblyName asmName
= new AssemblyName ();
178 asmName
.Name
= "TestAssembly.dll";
180 AssemblyBuilder asmBuilder
= Thread
.GetDomain ().DefineDynamicAssembly (
181 asmName
, AssemblyBuilderAccess
.Run
);
183 ModuleBuilder modBuilder
= asmBuilder
.DefineDynamicModule ("TestModule");
185 TypeBuilder typeBuilder
= modBuilder
.DefineType ("TestType",
186 TypeAttributes
.Public
);
188 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
190 ConstructorInfo classCtorInfo
=
191 typeof (CustomAttribute
).GetConstructor (ctorParams
);
193 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
195 new object [] { "Test","Type" }
,
196 typeof(CustomAttribute
).GetFields(),
197 new object [] { "TestCase" }
200 typeBuilder
.SetCustomAttribute (typeCABuilder
);
203 Type myType
= typeBuilder
.CreateType ();
205 //Now check for the validity of the attributes.
206 object testInstance
= Activator
.CreateInstance (myType
);
208 //check the validity of the attribute associated with Print method
209 object [] customAttrs
= myType
.GetCustomAttributes (false);
210 AssertEquals ("#TestType has exactly one attribute", customAttrs
.Length
, 1);
212 //Custom Attributes of TestType
213 CustomAttribute attr
= customAttrs
[0] as CustomAttribute
;
214 AssertEquals ("#AttributeOne", attr
.AttributeOne
, "Test");
215 AssertEquals ("#AttributeTwo", attr
.AttributeTwo
, "Type");
216 AssertEquals ("#CustomAttribute.Feild",attr
.Feild
, "TestCase");
221 public void CtorThreeTest ()
223 //test for the constructor with signature--
224 // CustomAttributeBuilder Constructor (ConstructorInfo, Object[], PropertyInfo[], Object[]) ;
226 * WE build a imaginary type as follows
227 * [CustomAttribute()]
228 * public class TestType
232 * We also set the "AttributeOne" of class CustomAttribute by means of the constuctor
233 * And then check for the validity of attribute state
236 AssemblyName asmName
= new AssemblyName ();
237 asmName
.Name
= "TestAssembly.dll";
239 AssemblyBuilder asmBuilder
= Thread
.GetDomain ().DefineDynamicAssembly (
240 asmName
, AssemblyBuilderAccess
.Run
);
242 ModuleBuilder modBuilder
= asmBuilder
.DefineDynamicModule ("TestModule");
244 TypeBuilder typeBuilder
= modBuilder
.DefineType ("TestType",
245 TypeAttributes
.Public
);
247 Type
[] ctorParams
= new Type
[] { }
;
249 ConstructorInfo classCtorInfo
=
250 typeof (CustomAttribute
).GetConstructor (ctorParams
);
252 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
255 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
256 new object [] { "TestCase" }
259 typeBuilder
.SetCustomAttribute (typeCABuilder
);
262 Type myType
= typeBuilder
.CreateType ();
264 //Now check for the validity of the attributes.
265 object testInstance
= Activator
.CreateInstance (myType
);
267 //check the validity of the attribute associated with Print method
268 object [] customAttrs
= myType
.GetCustomAttributes (false);
269 AssertEquals ("#TestType has exactly one attribute", customAttrs
.Length
, 1);
271 //Custom Attributes of TestType
272 CustomAttribute attr
= customAttrs
[0] as CustomAttribute
;
273 AssertEquals("#AttributeOne", attr
.AttributeOne
, "TestCase");
277 public void CtorFourTest ()
279 //test for the constructor with signature--
280 //public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[], FieldInfo[], object[]);
282 * WE build a imaginary type as follows
283 * [CustomAttribute()]
284 * public class TestType
288 * We also set the "AttributeOne" property ,
289 * and "Feild" of class CustomAttribute
290 * by means of the constuctor of CustomAttributeBuilder
291 * And then check for the validity
294 AssemblyName asmName
= new AssemblyName ();
295 asmName
.Name
= "TestAssembly.dll";
297 AssemblyBuilder asmBuilder
= Thread
.GetDomain ().DefineDynamicAssembly (
298 asmName
, AssemblyBuilderAccess
.Run
);
300 ModuleBuilder modBuilder
= asmBuilder
.DefineDynamicModule ("TestModule");
302 TypeBuilder typeBuilder
= modBuilder
.DefineType ("TestType",
303 TypeAttributes
.Public
);
305 Type
[] ctorParams
= new Type
[] { }
;
307 ConstructorInfo classCtorInfo
=
308 typeof (CustomAttribute
).GetConstructor (ctorParams
);
310 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder(
313 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
314 new object [] { "TestCase" }
,
315 typeof(CustomAttribute
).GetFields (),
316 new object [] { "FieldValue" }
319 typeBuilder
.SetCustomAttribute (typeCABuilder
);
322 Type myType
= typeBuilder
.CreateType ();
324 //Now check for the validity of the attributes.
325 object testInstance
= Activator
.CreateInstance (myType
);
327 //check the validity of the attribute associated with Print method
328 object [] customAttrs
= myType
.GetCustomAttributes (false);
329 AssertEquals("#TestType has exactly one attribute",customAttrs
.Length
, 1);
331 //Custom Attributes of TestType
332 CustomAttribute attr
= customAttrs
[0] as CustomAttribute
;
333 AssertEquals ("#AttributeOne", attr
.AttributeOne
, "TestCase");
334 AssertEquals ("#Field ", attr
.Feild
, "FieldValue");
339 [ExpectedException (typeof (ArgumentException
))]
340 public void ArgumentExceptionTest_1 ()
342 //here the constructor is static
344 Type
[] ctorParams
= new Type
[] { }
;
346 ConstructorInfo classCtorInfo
=
347 typeof (CustomAttribute
).GetConstructor (BindingFlags
.Static
| BindingFlags
.NonPublic
,
348 null, ctorParams
, null);
350 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
353 new PropertyInfo
[] { typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
354 new object [] { "TestCase" }
,
355 typeof (CustomAttribute
).GetFields (),
356 new object [] { "FieldValue" }
362 [ExpectedException (typeof (ArgumentException
))]
363 public void ArgumentExceptionTest_2 ()
365 //here the consturctor is private
367 Type
[] ctorParams
= new Type
[] {typeof(string) }
;
369 ConstructorInfo classCtorInfo
=
370 typeof (CustomAttribute
).GetConstructor (BindingFlags
.Instance
|
371 BindingFlags
.NonPublic
, null, ctorParams
, null);
373 Assert ("#Custom Attribute has private constuctor ", classCtorInfo
!= null);
375 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
377 new object [] { "hello" }
,
378 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
379 new object [] { "TestCase" }
,
380 typeof (CustomAttribute
).GetFields (),
381 new object [] { "FieldValue" }
387 [ExpectedException (typeof (ArgumentException
))]
388 public void ArgumentExceptionTest_3 ()
390 // The lengths of the namedProperties and
391 //propertyValues arrays are different.
393 Type
[] ctorParams
= new Type
[] { }
;
395 ConstructorInfo classCtorInfo
=
396 typeof (CustomAttribute
).GetConstructor (ctorParams
);
398 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
401 new PropertyInfo
[] { typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
402 new object [] { "TestCase","extra arg" }
,//<--here is the error
403 typeof (CustomAttribute
).GetFields (),
404 new object [] { "FieldValue" }
410 [ExpectedException (typeof (ArgumentException
))]
411 public void ArgumentExceptionTest_4()
413 //The length of the namedFields and
414 //namedValues are different
416 Type
[] ctorParams
= new Type
[] { }
;
418 ConstructorInfo classCtorInfo
=
419 typeof (CustomAttribute
).GetConstructor (ctorParams
);
421 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
424 new PropertyInfo
[] { typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
425 new object [] { "TestCase" }
,
426 typeof (CustomAttribute
).GetFields (),
427 new object [] { }
//<--here is the error
433 [ExpectedException (typeof (ArgumentException
))]
434 public void ArgumentExceptionTest_5 ()
436 //This does not throw on .NET 1.1, so manually throw
437 if (Environment
.Version
.Major
== 1 && Environment
.Version
.Major
== 1)
438 throw new ArgumentException();
440 //The number of supplied arguments does not match
441 //the number of parameters of the constructor as
442 //required by the calling convention of the constructor
444 Type
[] ctorParams
= new Type
[] { }
;
446 ConstructorInfo classCtorInfo
=
447 typeof (CustomAttribute
).GetConstructor (ctorParams
);
449 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
451 new object [] { "extra1", "extra2" }, //<--here is the error
452 new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
453 new object [] { "TestCase" },
454 typeof (CustomAttribute).GetFields (),
455 new object [] { "FeildValue" }
462 [ExpectedException (typeof (ArgumentException
))]
463 public void ArgumentExceptionTest_6 ()
465 //The type of supplied argument does not
466 //match the type of the parameter declared
467 //in the constructor.
469 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
471 ConstructorInfo classCtorInfo
=
472 typeof (CustomAttribute
).GetConstructor (ctorParams
);
474 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
476 new object [] { "1", 123 }
,//<--here is the error,(int instead of string)
477 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
478 new object [] { "TestCase" }
,
479 typeof (CustomAttribute
).GetFields (),
480 new object [] { "FeildValue" }
486 [ExpectedException (typeof (ArgumentException
))]
487 public void ArgumentExceptionTest_7 ()
489 //A property has no setter.(CustomAttribute.AttributeTwo)
491 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
493 ConstructorInfo classCtorInfo
=
494 typeof (CustomAttribute
).GetConstructor (ctorParams
);
496 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
498 new object [] { "1","2" }
,
499 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeTwo") }
,
500 new object [] { "TestCase" }
,
501 typeof (CustomAttribute
).GetFields (),
502 new object [] { "FeildValue" }
508 [ExpectedException (typeof (ArgumentException
))]
509 public void ArgumentExceptionTest_8 ()
511 //A property doesnot belong to same class
513 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
515 ConstructorInfo classCtorInfo
=
516 typeof (CustomAttribute
).GetConstructor (ctorParams
);
518 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
520 new object [] { "1","2" }
,
521 new PropertyInfo
[] { typeof (TempClass).GetProperty ("FieldProperty")}
, //here is the error
522 new object [] { "TestCase" }
,
523 typeof (CustomAttribute
).GetFields (),
524 new object [] { "FeildValue" }
530 [ExpectedException (typeof (ArgumentException
))]
531 public void ArgumentExceptionTest_9 ()
533 //A field doesnot belong to same class
535 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
537 ConstructorInfo classCtorInfo
=
538 typeof (CustomAttribute
).GetConstructor (ctorParams
);
540 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
542 new object [] { "1","2" }
,
543 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
544 new object [] {"TestCase"}
,
545 typeof (TempClass
).GetFields (), //<-- fields of TempClass are passed
546 new object [] { "FeildValue" }
552 [ExpectedException (typeof (ArgumentException
))]
553 public void ArgumentExceptionTest_10 ()
555 //The types of the property values do
556 //not match the types of the named properties.
559 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
561 ConstructorInfo classCtorInfo
=
562 typeof (CustomAttribute
).GetConstructor (ctorParams
);
564 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
566 new object [] { "1","2" }
,
567 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
568 new object [] { (long)1212121212 }
, //<---type mismatch error(long for string)
569 typeof (CustomAttribute
).GetFields (),
570 new object [] { "FeildValue" }
576 [ExpectedException (typeof (ArgumentException
))]
577 public void ArgumentExceptionTest_11 ()
579 //The types of the field values do
580 //not match the types of the named properties.
582 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
584 ConstructorInfo classCtorInfo
=
585 typeof (CustomAttribute
).GetConstructor (ctorParams
);
587 Assert ("#ctor not null", classCtorInfo
!= null);
588 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
590 new object [] { "1","2" }
,
591 new PropertyInfo
[] { typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
592 new object [] { "One" }
,
593 typeof (CustomAttribute
).GetFields (),
594 new object []{ 12.1212 }
//<---type mismatch error(double for string)
600 [ExpectedException (typeof (ArgumentNullException
))]
601 public void ArgumentNullException_1 ()
603 //the ctor value array (2nd argument) is null
604 Type
[] ctorParams
= new Type
[] { typeof (string),typeof (string) }
;
606 ConstructorInfo classCtorInfo
=
607 typeof (CustomAttribute
).GetConstructor (ctorParams
);
609 Assert ("#ctor not null", classCtorInfo
!= null);
610 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
612 null, //<-- here is the error
613 new PropertyInfo
[] { typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
614 new object [] { "One" }
,
615 typeof (CustomAttribute
).GetFields (),
616 new object [] { "feild" }
622 [ExpectedException (typeof (ArgumentNullException
))]
623 public void ArgumentNullException_2 ()
625 //the property value array (4th argument) is null
626 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
628 ConstructorInfo classCtorInfo
=
629 typeof (CustomAttribute
).GetConstructor (ctorParams
);
631 Assert ("#ctor not null", classCtorInfo
!= null);
632 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
634 new object [] { "one","two" }
,
635 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
636 null, // <-- here is the error
637 typeof (CustomAttribute
).GetFields (),
638 new object [] { "feild" }
644 [ExpectedException (typeof (ArgumentNullException
))]
645 public void ArgumentNullException_3 ()
647 //the field value array (6th argument) is null
648 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
650 ConstructorInfo classCtorInfo
=
651 typeof (CustomAttribute
).GetConstructor (ctorParams
);
653 Assert ("#ctor not null", classCtorInfo
!= null);
654 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
656 new object [] { "one","two" }
,
657 new PropertyInfo
[] { typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
658 new object [] {"property"}
,
659 typeof (CustomAttribute
).GetFields (),
660 null // <-- here is the error