/src/Castle.Core.Tests/ClassEmitterTestCase.cs

https://github.com/mirajavora/Castle.Core-READONLY · C# · 173 lines · 145 code · 15 blank · 13 comment · 0 complexity · 3c2620b0220faa4a30bd4f207da7c7a7 MD5 · raw file

  1. // Copyright 2004-2011 Castle Project - http://www.castleproject.org/
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. namespace Castle.DynamicProxy.Tests
  15. {
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Reflection;
  19. using Castle.DynamicProxy.Generators.Emitters;
  20. using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
  21. using NUnit.Framework;
  22. [TestFixture]
  23. public class ClassEmitterTestCase : BasePEVerifyTestCase
  24. {
  25. [Test]
  26. public void AutomaticDefaultConstructorGeneration()
  27. {
  28. ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes);
  29. Type t = emitter.BuildType();
  30. Activator.CreateInstance(t);
  31. }
  32. [Test]
  33. public void AutomaticDefaultConstructorGenerationWithClosedGenericType()
  34. {
  35. ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (List<object>),
  36. Type.EmptyTypes);
  37. Type t = emitter.BuildType();
  38. Activator.CreateInstance(t);
  39. }
  40. [Test]
  41. public void StaticMethodArguments()
  42. {
  43. ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (List<object>),
  44. Type.EmptyTypes);
  45. MethodEmitter methodEmitter = emitter.CreateMethod("StaticMethod", MethodAttributes.Public | MethodAttributes.Static,
  46. typeof (string), typeof (string));
  47. methodEmitter.CodeBuilder.AddStatement(new ReturnStatement(methodEmitter.Arguments[0]));
  48. Type t = emitter.BuildType();
  49. Assert.AreEqual("five", t.GetMethod("StaticMethod").Invoke(null, new object[] {"five"}));
  50. }
  51. [Test]
  52. public void InstanceMethodArguments()
  53. {
  54. ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (List<object>),
  55. Type.EmptyTypes);
  56. MethodEmitter methodEmitter = emitter.CreateMethod("InstanceMethod", MethodAttributes.Public,
  57. typeof (string), typeof (string));
  58. methodEmitter.CodeBuilder.AddStatement(new ReturnStatement(methodEmitter.Arguments[0]));
  59. Type t = emitter.BuildType();
  60. object instance = Activator.CreateInstance(t);
  61. Assert.AreEqual("six", t.GetMethod("InstanceMethod").Invoke(instance, new object[] {"six"}));
  62. }
  63. [Test]
  64. public void ForceUnsignedFalseWithSignedTypes()
  65. {
  66. const bool shouldBeSigned = true;
  67. ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes,
  68. TypeAttributes.Public, false);
  69. Type t = emitter.BuildType();
  70. Assert.AreEqual(shouldBeSigned, StrongNameUtil.IsAssemblySigned(t.Assembly));
  71. }
  72. [Test]
  73. public void ForceUnsignedTrueWithSignedTypes()
  74. {
  75. ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes,
  76. TypeAttributes.Public, true);
  77. Type t = emitter.BuildType();
  78. Assert.IsFalse(StrongNameUtil.IsAssemblySigned(t.Assembly));
  79. }
  80. [Test]
  81. public void CreateFieldWithAttributes()
  82. {
  83. ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes);
  84. emitter.CreateField("myField", typeof (string), FieldAttributes.FamANDAssem | FieldAttributes.InitOnly);
  85. Type t = emitter.BuildType();
  86. FieldInfo field = t.GetField("myField", BindingFlags.NonPublic | BindingFlags.Instance);
  87. Assert.IsNotNull(field);
  88. Assert.AreEqual(FieldAttributes.FamANDAssem | FieldAttributes.InitOnly, field.Attributes);
  89. }
  90. [Test]
  91. public void CreateStaticFieldWithAttributes()
  92. {
  93. ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes);
  94. emitter.CreateStaticField("myField", typeof (string), FieldAttributes.FamANDAssem | FieldAttributes.InitOnly);
  95. Type t = emitter.BuildType();
  96. FieldInfo field = t.GetField("myField", BindingFlags.NonPublic | BindingFlags.Static);
  97. Assert.IsNotNull(field);
  98. Assert.AreEqual(FieldAttributes.Static | FieldAttributes.FamANDAssem | FieldAttributes.InitOnly, field.Attributes);
  99. }
  100. [Test]
  101. public void UsingClassEmitterForInterfaces()
  102. {
  103. ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes,
  104. TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);
  105. emitter.CreateMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
  106. typeof(void), Type.EmptyTypes);
  107. Type t = emitter.BuildType();
  108. Assert.IsTrue(t.IsInterface);
  109. MethodInfo method = t.GetMethod("MyMethod");
  110. Assert.IsNotNull(method);
  111. }
  112. [Test]
  113. [ExpectedException(typeof(InvalidOperationException))]
  114. public void NoBaseTypeForInterfaces()
  115. {
  116. DisableVerification();
  117. ClassEmitter emitter = new ClassEmitter (generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes,
  118. TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);
  119. #pragma warning disable 219
  120. Type t = emitter.BaseType;
  121. #pragma warning restore 219
  122. }
  123. [Test]
  124. [ExpectedException (typeof (InvalidOperationException))]
  125. public void NoDefaultCtorForInterfaces()
  126. {
  127. DisableVerification();
  128. ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes,
  129. TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);
  130. emitter.CreateDefaultConstructor();
  131. }
  132. [Test]
  133. [ExpectedException (typeof (InvalidOperationException))]
  134. public void NoCustomCtorForInterfaces()
  135. {
  136. DisableVerification();
  137. ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes,
  138. TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);
  139. emitter.CreateConstructor();
  140. }
  141. [Test]
  142. public void NestedInterface()
  143. {
  144. ClassEmitter outerEmitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IOuter", null, Type.EmptyTypes,
  145. TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);
  146. NestedClassEmitter innerEmitter = new NestedClassEmitter(outerEmitter, "IInner",
  147. TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.NestedPublic, null, Type.EmptyTypes);
  148. innerEmitter.CreateMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
  149. typeof(void), Type.EmptyTypes);
  150. Type inner = innerEmitter.BuildType();
  151. Type outer = outerEmitter.BuildType();
  152. Assert.IsTrue(inner.IsInterface);
  153. MethodInfo method = inner.GetMethod("MyMethod");
  154. Assert.IsNotNull(method);
  155. Assert.AreSame(inner, outer.GetNestedType("IInner", BindingFlags.Public));
  156. }
  157. }
  158. }