1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
//
// ConstructorBuilderTest.cs - NUnit Test Cases for the ConstructorBuilder class
//
// Zoltan Varga (vargaz@freemail.hu)
//
// (C) Ximian, Inc. http://www.ximian.com
// TODO:
// - implement 'Signature' (what the hell it does???) and test it
// - implement Equals and test it
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security;
using System.Security.Permissions;
using NUnit.Framework;
namespace MonoTests.System.Reflection.Emit
{
[TestFixture]
public class ConstructorBuilderTest : Assertion
{
private TypeBuilder genClass;
private ModuleBuilder module;
private static int typeIndexer = 0;
[SetUp]
protected void SetUp () {
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MonoTests.System.Reflection.Emit.ConstructorBuilderTest";
AssemblyBuilder assembly
= Thread.GetDomain().DefineDynamicAssembly(
assemblyName, AssemblyBuilderAccess.Run);
module = assembly.DefineDynamicModule("module1");
genClass = module.DefineType(genTypeName (),
TypeAttributes.Public);
}
// Return a unique type name
private string genTypeName () {
return "class" + (typeIndexer ++);
}
public void TestAttributes () {
ConstructorBuilder cb = genClass.DefineConstructor (
MethodAttributes.Public, 0, new Type [0]);
Assert ("Attributes works",
(cb.Attributes & MethodAttributes.Public) != 0);
Assert ("Attributes works",
(cb.Attributes & MethodAttributes.SpecialName) != 0);
}
public void TestCallingConvention () {
/* This does not work under MS.NET
ConstructorBuilder cb3 = genClass.DefineConstructor (
0, CallingConventions.VarArgs, new Type [0]);
AssertEquals ("CallingConvetion works",
CallingConventions.VarArgs | CallingConventions.HasThis,
cb3.CallingConvention);
*/
ConstructorBuilder cb4 = genClass.DefineConstructor (
MethodAttributes.Static, CallingConventions.Standard, new Type [0]);
AssertEquals ("Static implies !HasThis",
cb4.CallingConvention,
CallingConventions.Standard);
}
public void TestDeclaringType () {
ConstructorBuilder cb = genClass.DefineConstructor (
0, 0, new Type[0]);
AssertEquals ("DeclaringType works",
cb.DeclaringType, genClass);
}
public void TestInitLocals () {
ConstructorBuilder cb = genClass.DefineConstructor (
0, 0, new Type[0]);
AssertEquals ("InitLocals defaults to true", cb.InitLocals, true);
cb.InitLocals = false;
AssertEquals ("InitLocals is settable", cb.InitLocals, false);
}
[Test]
[Category ("NotDotNet")]
public void TestMethodHandle ()
{
ConstructorBuilder cb = genClass.DefineConstructor (
0, 0, new Type [0]);
RuntimeMethodHandle handle = cb.MethodHandle;
// the previous line throws a NotSupportedException on MS 1.1 SP1
}
public void TestName () {
ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);
AssertEquals ("Name works", ".ctor", cb.Name);
ConstructorBuilder cb2 = genClass.DefineConstructor (MethodAttributes.Static, 0, new Type [0]);
AssertEquals ("Static constructors have the right name", ".cctor", cb2.Name);
}
public void TestReflectedType () {
ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);
AssertEquals ("ReflectedType works",
genClass, cb.ReflectedType);
}
public void TestReturnType () {
ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);
AssertEquals ("ReturnType works",
null, cb.ReturnType);
}
public void TestDefineParameter () {
TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
ConstructorBuilder cb = tb.DefineConstructor (
0, 0, new Type [2] { typeof(int), typeof(int) });
// index out of range
try {
cb.DefineParameter (0, 0, "param1");
Fail ();
} catch (ArgumentOutOfRangeException) {
}
try {
cb.DefineParameter (3, 0, "param1");
Fail ();
} catch (ArgumentOutOfRangeException) {
}
// Normal usage
cb.DefineParameter (1, 0, "param1");
cb.DefineParameter (1, 0, "param1");
cb.DefineParameter (2, 0, null);
// Can not be called on a created type
cb.GetILGenerator ().Emit (OpCodes.Ret);
tb.CreateType ();
try {
cb.DefineParameter (1, 0, "param1");
Fail ();
}
catch (InvalidOperationException) {
}
}
public void TestGetCustomAttributes () {
ConstructorBuilder cb = genClass.DefineConstructor (
0, 0, new Type [1] {typeof(int)});
try {
cb.GetCustomAttributes (true);
Fail ();
} catch (NotSupportedException) {
}
try {
cb.GetCustomAttributes (null, true);
Fail ();
} catch (NotSupportedException) {
}
}
public void TestMethodImplementationFlags () {
ConstructorBuilder cb = genClass.DefineConstructor (
0, 0, new Type [0]);
AssertEquals ("MethodImplementationFlags defaults to Managed+IL",
cb.GetMethodImplementationFlags (),
MethodImplAttributes.Managed | MethodImplAttributes.IL);
cb.SetImplementationFlags (MethodImplAttributes.OPTIL);
AssertEquals ("SetImplementationFlags works",
cb.GetMethodImplementationFlags (),
MethodImplAttributes.OPTIL);
// Can not be called on a created type
TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
ConstructorBuilder cb2 = tb.DefineConstructor (
0, 0, new Type [0]);
cb2.GetILGenerator ().Emit (OpCodes.Ret);
cb2.SetImplementationFlags (MethodImplAttributes.Managed);
tb.CreateType ();
try {
cb2.SetImplementationFlags (MethodImplAttributes.OPTIL);
Fail ();
}
catch (InvalidOperationException) {
}
}
public void TestGetModule () {
ConstructorBuilder cb = genClass.DefineConstructor (
0, 0, new Type [0]);
AssertEquals ("GetModule works",
module, cb.GetModule ());
}
public void TestGetParameters () {
TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
ConstructorBuilder cb = tb.DefineConstructor (
0, 0, new Type [1] {typeof(int)});
cb.GetILGenerator ().Emit (OpCodes.Ret);
// Can't be called before CreateType ()
/* This does not work under mono
try {
cb.GetParameters ();
Fail ();
} catch (InvalidOperationException) {
}
*/
tb.CreateType ();
/* This does not work under MS.NET !
cb.GetParameters ();
*/
}
public void TestGetToken () {
TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
ConstructorBuilder cb = tb.DefineConstructor (
0, 0, new Type [1] {typeof(void)});
cb.GetToken ();
}
public void TestInvoke () {
ConstructorBuilder cb = genClass.DefineConstructor (
0, 0,
new Type [1] {typeof(int)});
try {
cb.Invoke (null, new object [1] { 42 });
Fail ();
} catch (NotSupportedException) {
}
try {
cb.Invoke (null, 0, null, new object [1] { 42 }, null);
Fail ();
} catch (NotSupportedException) {
}
}
public void TestIsDefined () {
ConstructorBuilder cb = genClass.DefineConstructor (
0, 0,
new Type [1] {typeof(int)});
try {
cb.IsDefined (null, true);
Fail ();
} catch (NotSupportedException) {
}
}
public void TestSetCustomAttribute () {
TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
ConstructorBuilder cb = tb.DefineConstructor (
0, 0,
new Type [1] {typeof(int)});
cb.GetILGenerator ().Emit (OpCodes.Ret);
// Null argument
try {
cb.SetCustomAttribute (null);
Fail ();
} catch (ArgumentNullException) {
}
byte[] custAttrData = { 1, 0, 0, 0, 0};
Type attrType = Type.GetType
("System.Reflection.AssemblyKeyNameAttribute");
Type[] paramTypes = new Type[1];
paramTypes[0] = typeof(String);
ConstructorInfo ctorInfo =
attrType.GetConstructor(paramTypes);
cb.SetCustomAttribute (ctorInfo, custAttrData);
// Null arguments again
try {
cb.SetCustomAttribute (null, new byte[2]);
Fail ();
} catch (ArgumentNullException) {
}
try {
cb.SetCustomAttribute (ctorInfo, null);
Fail ();
} catch (ArgumentNullException) {
}
}
[Test]
public void GetCustomAttributes () {
TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
ConstructorBuilder cb = tb.DefineConstructor (
MethodAttributes.Public, 0,
new Type [1] {typeof(int)});
cb.GetILGenerator ().Emit (OpCodes.Ret);
Type attrType = typeof (ObsoleteAttribute);
ConstructorInfo ctorInfo =
attrType.GetConstructor (new Type [] { typeof (String) });
cb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
Type t = tb.CreateType ();
// Try the created type
{
ConstructorInfo ci = t.GetConstructors () [0];
object[] attrs = ci.GetCustomAttributes (true);
AssertEquals (1, attrs.Length);
Assert (attrs [0] is ObsoleteAttribute);
AssertEquals ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
}
// Try the type builder
{
ConstructorInfo ci = tb.GetConstructors () [0];
object[] attrs = ci.GetCustomAttributes (true);
AssertEquals (1, attrs.Length);
Assert (attrs [0] is ObsoleteAttribute);
AssertEquals ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
}
}
// Same as in MethodBuilderTest
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void TestAddDeclarativeSecurityAlreadyCreated () {
ConstructorBuilder cb = genClass.DefineConstructor (
MethodAttributes.Public, 0, new Type [0]);
ILGenerator ilgen = cb.GetILGenerator ();
ilgen.Emit (OpCodes.Ret);
genClass.CreateType ();
PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
cb.AddDeclarativeSecurity (SecurityAction.Demand, set);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void TestAddDeclarativeSecurityNullPermissionSet () {
ConstructorBuilder cb = genClass.DefineConstructor (
MethodAttributes.Public, 0, new Type [0]);
cb.AddDeclarativeSecurity (SecurityAction.Demand, null);
}
[Test]
public void TestAddDeclarativeSecurityInvalidAction () {
ConstructorBuilder cb = genClass.DefineConstructor (
MethodAttributes.Public, 0, new Type [0]);
SecurityAction[] actions = new SecurityAction [] {
SecurityAction.RequestMinimum,
SecurityAction.RequestOptional,
SecurityAction.RequestRefuse };
PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
foreach (SecurityAction action in actions) {
try {
cb.AddDeclarativeSecurity (action, set);
Fail ();
} catch (ArgumentOutOfRangeException) {
}
}
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void TestAddDeclarativeSecurityDuplicateAction () {
ConstructorBuilder cb = genClass.DefineConstructor (
MethodAttributes.Public, 0, new Type [0]);
PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
cb.AddDeclarativeSecurity (SecurityAction.Demand, set);
cb.AddDeclarativeSecurity (SecurityAction.Demand, set);
}
}
}
|