1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 namespace Castle
.DynamicProxy
.Tests
18 using System
.Collections
.Generic
;
19 using System
.Reflection
;
20 using Castle
.DynamicProxy
.Generators
.Emitters
;
21 using Castle
.DynamicProxy
.Generators
.Emitters
.SimpleAST
;
22 using NUnit
.Framework
;
25 public class ClassEmitterTestCase
: BasePEVerifyTestCase
28 public void AutomaticDefaultConstructorGeneration ()
30 ClassEmitter emitter
= new ClassEmitter (generator
.ProxyBuilder
.ModuleScope
, "Foo", typeof (object), Type
.EmptyTypes
);
31 Type t
= emitter
.BuildType();
32 Activator
.CreateInstance (t
);
36 public void AutomaticDefaultConstructorGenerationWithClosedGenericType ()
38 ClassEmitter emitter
= new ClassEmitter (generator
.ProxyBuilder
.ModuleScope
, "Foo", typeof (List
<object>), Type
.EmptyTypes
);
39 Type t
= emitter
.BuildType ();
40 Activator
.CreateInstance (t
);
44 public void StaticMethodArguments ()
46 ClassEmitter emitter
= new ClassEmitter (generator
.ProxyBuilder
.ModuleScope
, "Foo", typeof (List
<object>), Type
.EmptyTypes
);
47 MethodEmitter methodEmitter
= emitter
.CreateMethod ("StaticMethod", MethodAttributes
.Public
| MethodAttributes
.Static
,
48 typeof (string), typeof (string));
49 methodEmitter
.CodeBuilder
.AddStatement (new ReturnStatement (methodEmitter
.Arguments
[0]));
50 Type t
= emitter
.BuildType ();
51 Assert
.AreEqual ("five", t
.GetMethod ("StaticMethod").Invoke (null, new object[] { "five" }
));
55 public void InstanceMethodArguments ()
57 ClassEmitter emitter
= new ClassEmitter (generator
.ProxyBuilder
.ModuleScope
, "Foo", typeof (List
<object>), Type
.EmptyTypes
);
58 MethodEmitter methodEmitter
= emitter
.CreateMethod ("InstanceMethod", MethodAttributes
.Public
,
59 typeof (string), typeof (string));
60 methodEmitter
.CodeBuilder
.AddStatement (new ReturnStatement (methodEmitter
.Arguments
[0]));
61 Type t
= emitter
.BuildType ();
62 object instance
= Activator
.CreateInstance (t
);
63 Assert
.AreEqual ("six", t
.GetMethod ("InstanceMethod").Invoke (instance
, new object[] { "six" }
));
67 public void ForceUnsignedFalseWithSignedTypes ()
69 ClassEmitter emitter
= new ClassEmitter (generator
.ProxyBuilder
.ModuleScope
, "Foo", typeof (object), Type
.EmptyTypes
, TypeAttributes
.Public
, false);
70 Type t
= emitter
.BuildType ();
71 Assert
.IsTrue (StrongNameUtil
.IsAssemblySigned (t
.Assembly
));
75 public void ForceUnsignedTrueWithSignedTypes ()
77 ClassEmitter emitter
= new ClassEmitter (generator
.ProxyBuilder
.ModuleScope
, "Foo", typeof (object), Type
.EmptyTypes
, TypeAttributes
.Public
, true);
78 Type t
= emitter
.BuildType ();
79 Assert
.IsFalse (StrongNameUtil
.IsAssemblySigned (t
.Assembly
));