PageRenderTime 39ms CodeModel.GetById 18ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

/Mono.Cecil/Test/Mono.Cecil.Tests/MethodTests.cs

http://github.com/icsharpcode/ILSpy
C# | 201 lines | 143 code | 58 blank | 0 comment | 4 complexity | 7d428f83341902f9ecbe87e164dca6f3 MD5 | raw file
  1using System;
  2using System.Linq;
  3
  4using Mono.Cecil;
  5using Mono.Cecil.Metadata;
  6
  7using NUnit.Framework;
  8
  9namespace Mono.Cecil.Tests {
 10
 11	[TestFixture]
 12	public class MethodTests : BaseTestFixture {
 13
 14		[TestCSharp ("Methods.cs")]
 15		public void AbstractMethod (ModuleDefinition module)
 16		{
 17			var type = module.Types [1];
 18			Assert.AreEqual ("Foo", type.Name);
 19			Assert.AreEqual (2, type.Methods.Count);
 20
 21			var method = type.GetMethod ("Bar");
 22			Assert.AreEqual ("Bar", method.Name);
 23			Assert.IsTrue (method.IsAbstract);
 24			Assert.IsNotNull (method.ReturnType);
 25
 26			Assert.AreEqual (1, method.Parameters.Count);
 27
 28			var parameter = method.Parameters [0];
 29
 30			Assert.AreEqual ("a", parameter.Name);
 31			Assert.AreEqual ("System.Int32", parameter.ParameterType.FullName);
 32		}
 33
 34		[TestCSharp ("Methods.cs")]
 35		public void SimplePInvoke (ModuleDefinition module)
 36		{
 37			var bar = module.GetType ("Bar");
 38			var pan = bar.GetMethod ("Pan");
 39
 40			Assert.IsTrue (pan.IsPInvokeImpl);
 41			Assert.IsNotNull (pan.PInvokeInfo);
 42
 43			Assert.AreEqual ("Pan", pan.PInvokeInfo.EntryPoint);
 44			Assert.IsNotNull (pan.PInvokeInfo.Module);
 45			Assert.AreEqual ("foo.dll", pan.PInvokeInfo.Module.Name);
 46		}
 47
 48		[TestCSharp ("Generics.cs")]
 49		public void GenericMethodDefinition (ModuleDefinition module)
 50		{
 51			var baz = module.GetType ("Baz");
 52
 53			var gazonk = baz.GetMethod ("Gazonk");
 54
 55			Assert.IsNotNull (gazonk);
 56
 57			Assert.IsTrue (gazonk.HasGenericParameters);
 58			Assert.AreEqual (1, gazonk.GenericParameters.Count);
 59			Assert.AreEqual ("TBang", gazonk.GenericParameters [0].Name);
 60		}
 61
 62		[TestCSharp ("Generics.cs")]
 63		public void ReturnGenericInstance (ModuleDefinition module)
 64		{
 65			var bar = module.GetType ("Bar`1");
 66
 67			var self = bar.GetMethod ("Self");
 68			Assert.IsNotNull (self);
 69
 70			var bar_t = self.ReturnType;
 71
 72			Assert.IsTrue (bar_t.IsGenericInstance);
 73
 74			var bar_t_instance = (GenericInstanceType) bar_t;
 75
 76			Assert.AreEqual (bar.GenericParameters [0], bar_t_instance.GenericArguments [0]);
 77
 78			var self_str = bar.GetMethod ("SelfString");
 79			Assert.IsNotNull (self_str);
 80
 81			var bar_str = self_str.ReturnType;
 82			Assert.IsTrue (bar_str.IsGenericInstance);
 83
 84			var bar_str_instance = (GenericInstanceType) bar_str;
 85
 86			Assert.AreEqual ("System.String", bar_str_instance.GenericArguments [0].FullName);
 87		}
 88
 89		[TestCSharp ("Generics.cs")]
 90		public void ReturnGenericInstanceWithMethodParameter (ModuleDefinition module)
 91		{
 92			var baz = module.GetType ("Baz");
 93
 94			var gazoo = baz.GetMethod ("Gazoo");
 95			Assert.IsNotNull (gazoo);
 96
 97			var bar_bingo = gazoo.ReturnType;
 98
 99			Assert.IsTrue (bar_bingo.IsGenericInstance);
100
101			var bar_bingo_instance = (GenericInstanceType) bar_bingo;
102
103			Assert.AreEqual (gazoo.GenericParameters [0], bar_bingo_instance.GenericArguments [0]);
104		}
105
106		[TestCSharp ("Interfaces.cs")]
107		public void SimpleOverrides (ModuleDefinition module)
108		{
109			var ibingo = module.GetType ("IBingo");
110			var ibingo_foo = ibingo.GetMethod ("Foo");
111			Assert.IsNotNull (ibingo_foo);
112
113			var ibingo_bar = ibingo.GetMethod ("Bar");
114			Assert.IsNotNull (ibingo_bar);
115
116			var bingo = module.GetType ("Bingo");
117
118			var foo = bingo.GetMethod ("IBingo.Foo");
119			Assert.IsNotNull (foo);
120
121			Assert.IsTrue (foo.HasOverrides);
122			Assert.AreEqual (ibingo_foo, foo.Overrides [0]);
123
124			var bar = bingo.GetMethod ("IBingo.Bar");
125			Assert.IsNotNull (bar);
126
127			Assert.IsTrue (bar.HasOverrides);
128			Assert.AreEqual (ibingo_bar, bar.Overrides [0]);
129		}
130
131		[TestModule ("varargs.exe")]
132		public void VarArgs (ModuleDefinition module)
133		{
134			var module_type = module.Types [0];
135
136			Assert.AreEqual (3, module_type.Methods.Count);
137
138			var bar = module_type.GetMethod ("Bar");
139			var baz = module_type.GetMethod ("Baz");
140			var foo = module_type.GetMethod ("Foo");
141
142			Assert.IsTrue (bar.IsVarArg ());
143			Assert.IsFalse (baz.IsVarArg ());
144
145			Assert.IsTrue(foo.IsVarArg ());
146
147			var bar_reference = (MethodReference) baz.Body.Instructions.Where (i => i.Offset == 0x000a).First ().Operand;
148
149			Assert.IsTrue (bar_reference.IsVarArg ());
150			Assert.AreEqual (0, bar_reference.GetSentinelPosition ());
151
152			var foo_reference = (MethodReference) baz.Body.Instructions.Where (i => i.Offset == 0x0023).First ().Operand;
153
154			Assert.IsTrue (foo_reference.IsVarArg ());
155
156			Assert.AreEqual (1, foo_reference.GetSentinelPosition ());
157		}
158
159		[TestCSharp ("Generics.cs")]
160		public void GenericInstanceMethod (ModuleDefinition module)
161		{
162			var type = module.GetType ("It");
163			var method = type.GetMethod ("ReadPwow");
164
165			GenericInstanceMethod instance = null;
166
167			foreach (var instruction in method.Body.Instructions) {
168				instance = instruction.Operand as GenericInstanceMethod;
169				if (instance != null)
170					break;
171			}
172
173			Assert.IsNotNull (instance);
174
175			Assert.AreEqual (TokenType.MethodSpec, instance.MetadataToken.TokenType);
176			Assert.AreNotEqual (0, instance.MetadataToken.RID);
177		}
178
179		[TestCSharp ("Generics.cs")]
180		public void MethodRefDeclaredOnGenerics (ModuleDefinition module)
181		{
182			var type = module.GetType ("Tamtam");
183			var beta = type.GetMethod ("Beta");
184			var charlie = type.GetMethod ("Charlie");
185
186			var new_list_beta = (MethodReference) beta.Body.Instructions [0].Operand;
187			var new_list_charlie = (MethodReference) charlie.Body.Instructions [0].Operand;
188
189			Assert.AreEqual ("System.Collections.Generic.List`1<TBeta>", new_list_beta.DeclaringType.FullName);
190			Assert.AreEqual ("System.Collections.Generic.List`1<TCharlie>", new_list_charlie.DeclaringType.FullName);
191		}
192
193		[Test]
194		public void ReturnParameterMethod ()
195		{
196			var method = typeof (MethodTests).ToDefinition ().GetMethod ("ReturnParameterMethod");
197			Assert.IsNotNull (method);
198			Assert.AreEqual (method, method.MethodReturnType.Parameter.Method);
199		}
200	}
201}