PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Test/Mono.Cecil.Tests/ILProcessorTests.cs

http://github.com/jbevain/cecil
C# | 103 lines | 76 code | 27 blank | 0 comment | 2 complexity | 4a22cca0703e1e2310f1e647a5c219b8 MD5 | raw file
  1using System;
  2using System.Linq;
  3
  4using Mono.Cecil;
  5using Mono.Cecil.Cil;
  6
  7using NUnit.Framework;
  8
  9namespace Mono.Cecil.Tests {
 10
 11	[TestFixture]
 12	public class ILProcessorTests : BaseTestFixture {
 13
 14		[Test]
 15		public void Append ()
 16		{
 17			var method = CreateTestMethod ();
 18			var il = method.GetILProcessor ();
 19
 20			var ret = il.Create (OpCodes.Ret);
 21			il.Append (ret);
 22
 23			AssertOpCodeSequence (new [] { OpCodes.Ret }, method);
 24		}
 25
 26		[Test]
 27		public void InsertBefore ()
 28		{
 29			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
 30			var il = method.GetILProcessor ();
 31
 32			var ldloc_2 = method.Instructions.Where (i => i.OpCode == OpCodes.Ldloc_2).First ();
 33
 34			il.InsertBefore (
 35				ldloc_2,
 36				il.Create (OpCodes.Ldloc_1));
 37
 38			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }, method);
 39		}
 40
 41		[Test]
 42		public void InsertAfter ()
 43		{
 44			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
 45			var il = method.GetILProcessor ();
 46
 47			var ldloc_0 = method.Instructions.First ();
 48
 49			il.InsertAfter (
 50				ldloc_0,
 51				il.Create (OpCodes.Ldloc_1));
 52
 53			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }, method);
 54		}
 55
 56		[Test]
 57		public void InsertAfterUsingIndex ()
 58		{
 59			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
 60			var il = method.GetILProcessor ();
 61
 62			il.InsertAfter (
 63				0,
 64				il.Create (OpCodes.Ldloc_1));
 65
 66			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }, method);
 67		}
 68
 69		[Test]
 70		public void ReplaceUsingIndex ()
 71		{
 72			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
 73			var il = method.GetILProcessor ();
 74
 75			il.Replace (1, il.Create (OpCodes.Nop));
 76
 77			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Nop, OpCodes.Ldloc_3 }, method);
 78		}
 79
 80		static void AssertOpCodeSequence (OpCode [] expected, MethodBody body)
 81		{
 82			var opcodes = body.Instructions.Select (i => i.OpCode).ToArray ();
 83			Assert.AreEqual (expected.Length, opcodes.Length);
 84
 85			for (int i = 0; i < opcodes.Length; i++)
 86				Assert.AreEqual (expected [i], opcodes [i]);
 87		}
 88
 89		static MethodBody CreateTestMethod (params OpCode [] opcodes)
 90		{
 91			var method = new MethodDefinition {
 92				Name = "function",
 93			};
 94
 95			var il = method.Body.GetILProcessor ();
 96
 97			foreach (var opcode in opcodes)
 98				il.Emit (opcode);
 99
100			return method.Body;
101		}
102	}
103}