PageRenderTime 93ms CodeModel.GetById 57ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/Test/Mono.Cecil.Tests/ParameterTests.cs

http://github.com/jbevain/cecil
C# | 274 lines | 206 code | 68 blank | 0 comment | 0 complexity | d9ac529508cb7ae6888e949b880282fc MD5 | raw file
  1using System;
  2
  3using NUnit.Framework;
  4
  5namespace Mono.Cecil.Tests {
  6
  7	[TestFixture]
  8	public class ParameterTests : BaseTestFixture {
  9
 10		[Test]
 11		public void MarshalAsI4 ()
 12		{
 13			TestModule ("marshal.dll", module => {
 14				var bar = module.GetType ("Bar");
 15				var pan = bar.GetMethod ("Pan");
 16
 17				Assert.AreEqual (1, pan.Parameters.Count);
 18
 19				var parameter = pan.Parameters [0];
 20
 21				Assert.IsTrue (parameter.HasMarshalInfo);
 22				var info = parameter.MarshalInfo;
 23
 24				Assert.AreEqual (typeof (MarshalInfo), info.GetType ());
 25				Assert.AreEqual (NativeType.I4, info.NativeType);
 26			});
 27		}
 28
 29		[Test]
 30		public void CustomMarshaler ()
 31		{
 32			TestModule ("marshal.dll", module => {
 33				var bar = module.GetType ("Bar");
 34				var pan = bar.GetMethod ("PanPan");
 35
 36				var parameter = pan.Parameters [0];
 37
 38				Assert.IsTrue (parameter.HasMarshalInfo);
 39
 40				var info = (CustomMarshalInfo) parameter.MarshalInfo;
 41
 42				Assert.AreEqual (Guid.Empty, info.Guid);
 43				Assert.AreEqual (string.Empty, info.UnmanagedType);
 44				Assert.AreEqual (NativeType.CustomMarshaler, info.NativeType);
 45				Assert.AreEqual ("nomnom", info.Cookie);
 46
 47				Assert.AreEqual ("Boc", info.ManagedType.FullName);
 48				Assert.AreEqual (module, info.ManagedType.Scope);
 49			});
 50		}
 51
 52		[Test]
 53		public void SafeArrayMarshaler ()
 54		{
 55			TestModule ("marshal.dll", module => {
 56				var bar = module.GetType ("Bar");
 57				var pan = bar.GetMethod ("PanPan");
 58
 59				Assert.IsTrue (pan.MethodReturnType.HasMarshalInfo);
 60
 61				var info = (SafeArrayMarshalInfo) pan.MethodReturnType.MarshalInfo;
 62
 63				Assert.AreEqual (VariantType.Dispatch, info.ElementType);
 64			});
 65		}
 66
 67		[Test]
 68		public void ArrayMarshaler ()
 69		{
 70			TestModule ("marshal.dll", module => {
 71				var bar = module.GetType ("Bar");
 72				var pan = bar.GetMethod ("PanPan");
 73
 74				var parameter = pan.Parameters [1];
 75
 76				Assert.IsTrue (parameter.HasMarshalInfo);
 77
 78				var info = (ArrayMarshalInfo) parameter.MarshalInfo;
 79
 80				Assert.AreEqual (NativeType.I8, info.ElementType);
 81				Assert.AreEqual (66, info.Size);
 82				Assert.AreEqual (2, info.SizeParameterIndex);
 83
 84				parameter = pan.Parameters [3];
 85
 86				Assert.IsTrue (parameter.HasMarshalInfo);
 87
 88				info = (ArrayMarshalInfo) parameter.MarshalInfo;
 89
 90				Assert.AreEqual (NativeType.I2, info.ElementType);
 91				Assert.AreEqual (-1, info.Size);
 92				Assert.AreEqual (-1, info.SizeParameterIndex);
 93			});
 94		}
 95
 96		[Test]
 97		public void ArrayMarshalerSized ()
 98		{
 99			TestModule ("marshal.dll", module => {
100				var delegate_type = module.GetType ("SomeMethod");
101				var parameter = delegate_type.GetMethod ("Invoke").Parameters [1];
102
103				Assert.IsTrue (parameter.HasMarshalInfo);
104				var array_info = (ArrayMarshalInfo) parameter.MarshalInfo;
105
106				Assert.IsNotNull (array_info);
107
108				Assert.AreEqual (0, array_info.SizeParameterMultiplier);
109			});
110		}
111
112		[Test]
113		public void NullableConstant ()
114		{
115			TestModule ("nullable-constant.exe", module => {
116				var type = module.GetType ("Program");
117				var method = type.GetMethod ("Foo");
118
119				Assert.IsTrue (method.Parameters [0].HasConstant);
120				Assert.IsTrue (method.Parameters [1].HasConstant);
121				Assert.IsTrue (method.Parameters [2].HasConstant);
122
123				Assert.AreEqual (1234, method.Parameters [0].Constant);
124				Assert.AreEqual (null, method.Parameters [1].Constant);
125				Assert.AreEqual (12, method.Parameters [2].Constant);
126			});
127		}
128
129		[Test]
130		public void BoxedDefaultArgumentValue ()
131		{
132			TestModule ("boxedoptarg.dll", module => {
133				var foo = module.GetType ("Foo");
134				var bar = foo.GetMethod ("Bar");
135				var baz = bar.Parameters [0];
136
137				Assert.IsTrue (baz.HasConstant);
138				Assert.AreEqual (-1, baz.Constant);
139			});
140		}
141
142		[Test]
143		public void AddParameterIndex ()
144		{
145			var object_ref = new TypeReference ("System", "Object", null, null, false);
146			var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
147
148			var x = new ParameterDefinition ("x", ParameterAttributes.None, object_ref);
149			var y = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
150
151			method.Parameters.Add (x);
152			method.Parameters.Add (y);
153
154			Assert.AreEqual (0, x.Index);
155			Assert.AreEqual (1, y.Index);
156
157			Assert.AreEqual (method, x.Method);
158			Assert.AreEqual (method, y.Method);
159		}
160
161		[Test]
162		public void RemoveAtParameterIndex ()
163		{
164			var object_ref = new TypeReference ("System", "Object", null, null, false);
165			var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
166
167			var x = new ParameterDefinition ("x", ParameterAttributes.None, object_ref);
168			var y = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
169			var z = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
170
171			method.Parameters.Add (x);
172			method.Parameters.Add (y);
173			method.Parameters.Add (z);
174
175			Assert.AreEqual (0, x.Index);
176			Assert.AreEqual (1, y.Index);
177			Assert.AreEqual (2, z.Index);
178
179			method.Parameters.RemoveAt (1);
180
181			Assert.AreEqual (0, x.Index);
182			Assert.AreEqual (-1, y.Index);
183			Assert.AreEqual (1, z.Index);
184		}
185
186		[Test]
187		public void RemoveParameterIndex ()
188		{
189			var object_ref = new TypeReference ("System", "Object", null, null, false);
190			var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
191
192			var x = new ParameterDefinition ("x", ParameterAttributes.None, object_ref);
193			var y = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
194			var z = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
195
196			method.Parameters.Add (x);
197			method.Parameters.Add (y);
198			method.Parameters.Add (z);
199
200			Assert.AreEqual (0, x.Index);
201			Assert.AreEqual (1, y.Index);
202			Assert.AreEqual (2, z.Index);
203
204			method.Parameters.Remove (y);
205
206			Assert.AreEqual (0, x.Index);
207			Assert.AreEqual (-1, y.Index);
208			Assert.AreEqual (1, z.Index);
209		}
210
211		[Test]
212		public void InsertParameterIndex ()
213		{
214			var object_ref = new TypeReference ("System", "Object", null, null, false);
215			var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
216
217			var x = new ParameterDefinition ("x", ParameterAttributes.None, object_ref);
218			var y = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
219			var z = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
220
221			method.Parameters.Add (x);
222			method.Parameters.Add (z);
223
224			Assert.AreEqual (0, x.Index);
225			Assert.AreEqual (-1, y.Index);
226			Assert.AreEqual (1, z.Index);
227
228			method.Parameters.Insert (1, y);
229
230			Assert.AreEqual (0, x.Index);
231			Assert.AreEqual (1, y.Index);
232			Assert.AreEqual (2, z.Index);
233		}
234
235		[Test]
236		public void GenericParameterConstant ()
237		{
238			TestIL ("hello.il", module => {
239				var foo = module.GetType ("Foo");
240				var method = foo.GetMethod ("GetState");
241
242				Assert.IsNotNull (method);
243
244				var parameter = method.Parameters [1];
245
246				Assert.IsTrue (parameter.HasConstant);
247				Assert.IsNull (parameter.Constant);
248			});
249		}
250
251		[Test]
252		public void NullablePrimitiveParameterConstant ()
253		{
254			TestModule ("nullable-parameter.dll", module => {
255				var test = module.GetType ("Test");
256				var method = test.GetMethod ("Foo");
257
258				Assert.IsNotNull (method);
259
260				var param = method.Parameters [0];
261				Assert.IsTrue (param.HasConstant);
262				Assert.AreEqual (1234, param.Constant);
263
264				param = method.Parameters [1];
265				Assert.IsTrue (param.HasConstant);
266				Assert.AreEqual (null, param.Constant);
267
268				param = method.Parameters [2];
269				Assert.IsTrue (param.HasConstant);
270				Assert.AreEqual (12, param.Constant);
271			});
272		}
273	}
274}