PageRenderTime 50ms CodeModel.GetById 26ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://github.com/icsharpcode/ILSpy
C# | 224 lines | 163 code | 61 blank | 0 comment | 0 complexity | 535833294906a711923b1d3149f69999 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 ParameterTests : BaseTestFixture {
 13
 14		[TestModule ("marshal.dll")]
 15		public void MarshalAsI4 (ModuleDefinition module)
 16		{
 17			var bar = module.GetType ("Bar");
 18			var pan = bar.GetMethod ("Pan");
 19
 20			Assert.AreEqual (1, pan.Parameters.Count);
 21
 22			var parameter = pan.Parameters [0];
 23
 24			Assert.IsTrue (parameter.HasMarshalInfo);
 25			var info = parameter.MarshalInfo;
 26
 27			Assert.AreEqual (typeof (MarshalInfo), info.GetType ());
 28			Assert.AreEqual (NativeType.I4, info.NativeType);
 29		}
 30
 31		[TestModule ("marshal.dll")]
 32		public void CustomMarshaler (ModuleDefinition module)
 33		{
 34			var bar = module.GetType ("Bar");
 35			var pan = bar.GetMethod ("PanPan");
 36
 37			var parameter = pan.Parameters [0];
 38
 39			Assert.IsTrue (parameter.HasMarshalInfo);
 40
 41			var info = (CustomMarshalInfo) parameter.MarshalInfo;
 42
 43			Assert.AreEqual (Guid.Empty, info.Guid);
 44			Assert.AreEqual (string.Empty, info.UnmanagedType);
 45			Assert.AreEqual (NativeType.CustomMarshaler, info.NativeType);
 46			Assert.AreEqual ("nomnom", info.Cookie);
 47
 48			Assert.AreEqual ("Boc", info.ManagedType.FullName);
 49			Assert.AreEqual (module, info.ManagedType.Scope);
 50		}
 51
 52		[TestModule ("marshal.dll")]
 53		public void SafeArrayMarshaler (ModuleDefinition module)
 54		{
 55			var bar = module.GetType ("Bar");
 56			var pan = bar.GetMethod ("PanPan");
 57
 58			Assert.IsTrue (pan.MethodReturnType.HasMarshalInfo);
 59
 60			var info = (SafeArrayMarshalInfo) pan.MethodReturnType.MarshalInfo;
 61
 62			Assert.AreEqual (VariantType.Dispatch, info.ElementType);
 63		}
 64
 65		[TestModule ("marshal.dll")]
 66		public void ArrayMarshaler (ModuleDefinition module)
 67		{
 68			var bar = module.GetType ("Bar");
 69			var pan = bar.GetMethod ("PanPan");
 70
 71			var parameter = pan.Parameters [1];
 72
 73			Assert.IsTrue (parameter.HasMarshalInfo);
 74
 75			var info = (ArrayMarshalInfo) parameter.MarshalInfo;
 76
 77			Assert.AreEqual (NativeType.I8, info.ElementType);
 78			Assert.AreEqual (66, info.Size);
 79			Assert.AreEqual (2, info.SizeParameterIndex);
 80
 81			parameter = pan.Parameters [3];
 82
 83			Assert.IsTrue (parameter.HasMarshalInfo);
 84
 85			info = (ArrayMarshalInfo) parameter.MarshalInfo;
 86
 87			Assert.AreEqual (NativeType.I2, info.ElementType);
 88			Assert.AreEqual (-1, info.Size);
 89			Assert.AreEqual (-1, info.SizeParameterIndex);
 90		}
 91
 92		[TestModule ("marshal.dll")]
 93		public void ArrayMarshalerSized (ModuleDefinition module)
 94		{
 95			var delegate_type = module.GetType ("SomeMethod");
 96			var parameter = delegate_type.GetMethod ("Invoke").Parameters [1];
 97
 98			Assert.IsTrue (parameter.HasMarshalInfo);
 99			var array_info = (ArrayMarshalInfo) parameter.MarshalInfo;
100
101			Assert.IsNotNull (array_info);
102
103			Assert.AreEqual (0, array_info.SizeParameterMultiplier);
104		}
105
106		[TestModule ("boxedoptarg.dll")]
107		public void BoxedDefaultArgumentValue (ModuleDefinition module)
108		{
109			var foo = module.GetType ("Foo");
110			var bar = foo.GetMethod ("Bar");
111			var baz = bar.Parameters [0];
112
113			Assert.IsTrue (baz.HasConstant);
114			Assert.AreEqual (-1, baz.Constant);
115		}
116
117		[Test]
118		public void AddParameterIndex ()
119		{
120			var object_ref = new TypeReference ("System", "Object", null, null, false);
121			var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
122
123			var x = new ParameterDefinition ("x", ParameterAttributes.None, object_ref);
124			var y = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
125
126			method.Parameters.Add (x);
127			method.Parameters.Add (y);
128
129			Assert.AreEqual (0, x.Index);
130			Assert.AreEqual (1, y.Index);
131
132			Assert.AreEqual (method, x.Method);
133			Assert.AreEqual (method, y.Method);
134		}
135
136		[Test]
137		public void RemoveAtParameterIndex ()
138		{
139			var object_ref = new TypeReference ("System", "Object", null, null, false);
140			var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
141
142			var x = new ParameterDefinition ("x", ParameterAttributes.None, object_ref);
143			var y = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
144			var z = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
145
146			method.Parameters.Add (x);
147			method.Parameters.Add (y);
148			method.Parameters.Add (z);
149
150			Assert.AreEqual (0, x.Index);
151			Assert.AreEqual (1, y.Index);
152			Assert.AreEqual (2, z.Index);
153
154			method.Parameters.RemoveAt (1);
155
156			Assert.AreEqual (0, x.Index);
157			Assert.AreEqual (-1, y.Index);
158			Assert.AreEqual (1, z.Index);
159		}
160
161		[Test]
162		public void RemoveParameterIndex ()
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.Remove (y);
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 InsertParameterIndex ()
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 (z);
198
199			Assert.AreEqual (0, x.Index);
200			Assert.AreEqual (-1, y.Index);
201			Assert.AreEqual (1, z.Index);
202
203			method.Parameters.Insert (1, y);
204
205			Assert.AreEqual (0, x.Index);
206			Assert.AreEqual (1, y.Index);
207			Assert.AreEqual (2, z.Index);
208		}
209
210		[TestIL ("hello.il")]
211		public void GenericParameterConstant (ModuleDefinition module)
212		{
213			var foo = module.GetType ("Foo");
214			var method = foo.GetMethod ("GetState");
215
216			Assert.IsNotNull (method);
217
218			var parameter = method.Parameters [1];
219
220			Assert.IsTrue (parameter.HasConstant);
221			Assert.IsNull (parameter.Constant);
222		}
223	}
224}