Welcome to mirror list, hosted at ThFree Co, Russian Federation.

VariableTests.cs « Mono.Cecil.Tests « Test - github.com/mono/cecil.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5454b937f201e7bd51f2c1d95ae4a79f28fa3334 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Linq;

using Mono.Cecil;
using Mono.Cecil.Cil;

using NUnit.Framework;

namespace Mono.Cecil.Tests {

	[TestFixture]
	public class VariableTests : BaseTestFixture {

		[Test]
		public void AddVariableIndex ()
		{
			var object_ref = new TypeReference ("System", "Object", null, null, false);
			var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
			var body = new MethodBody (method);

			var x = new VariableDefinition (object_ref);
			var y = new VariableDefinition (object_ref);

			body.Variables.Add (x);
			body.Variables.Add (y);

			Assert.AreEqual (0, x.Index);
			Assert.AreEqual (1, y.Index);
		}

		[Test]
		public void RemoveAtVariableIndex ()
		{
			var object_ref = new TypeReference ("System", "Object", null, null, false);
			var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
			var body = new MethodBody (method);

			var x = new VariableDefinition (object_ref);
			var y = new VariableDefinition (object_ref);
			var z = new VariableDefinition (object_ref);

			body.Variables.Add (x);
			body.Variables.Add (y);
			body.Variables.Add (z);

			Assert.AreEqual (0, x.Index);
			Assert.AreEqual (1, y.Index);
			Assert.AreEqual (2, z.Index);

			body.Variables.RemoveAt (1);

			Assert.AreEqual (0, x.Index);
			Assert.AreEqual (-1, y.Index);
			Assert.AreEqual (1, z.Index);
		}

		[Test]
		public void RemoveVariableIndex ()
		{
			var object_ref = new TypeReference ("System", "Object", null, null, false);
			var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
			var body = new MethodBody (method);

			var x = new VariableDefinition (object_ref);
			var y = new VariableDefinition (object_ref);
			var z = new VariableDefinition (object_ref);

			body.Variables.Add (x);
			body.Variables.Add (y);
			body.Variables.Add (z);

			Assert.AreEqual (0, x.Index);
			Assert.AreEqual (1, y.Index);
			Assert.AreEqual (2, z.Index);

			body.Variables.Remove (y);

			Assert.AreEqual (0, x.Index);
			Assert.AreEqual (-1, y.Index);
			Assert.AreEqual (1, z.Index);
		}

		[Test]
		public void RemoveVariableWithDebugInfo ()
		{
			var object_ref = new TypeReference ("System", "Object", null, null, false);
			var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
			var body = new MethodBody (method);
			var il = body.GetILProcessor ();
			il.Emit (OpCodes.Ret);

			var x = new VariableDefinition (object_ref);
			var y = new VariableDefinition (object_ref);
			var z = new VariableDefinition (object_ref);
			var z2 = new VariableDefinition (object_ref);

			body.Variables.Add (x);
			body.Variables.Add (y);
			body.Variables.Add (z);
			body.Variables.Add (z2);

			var scope = new ScopeDebugInformation (body.Instructions [0], body.Instructions [0]);
			method.DebugInformation = new MethodDebugInformation (method) {
				Scope = scope
			};
			scope.Variables.Add (new VariableDebugInformation (x.index, nameof (x)));
			scope.Variables.Add (new VariableDebugInformation (y.index, nameof (y)));
			scope.Variables.Add (new VariableDebugInformation (z.index, nameof (z)));
			scope.Variables.Add (new VariableDebugInformation (z2, nameof (z2)));

			body.Variables.Remove (y);

			Assert.AreEqual (3, scope.Variables.Count);
			Assert.AreEqual (x.Index, scope.Variables [0].Index);
			Assert.AreEqual (nameof (x), scope.Variables [0].Name);
			Assert.AreEqual (z.Index, scope.Variables [1].Index);
			Assert.AreEqual (nameof (z), scope.Variables [1].Name);
			Assert.AreEqual (z2.Index, scope.Variables [2].Index);
			Assert.AreEqual (nameof (z2), scope.Variables [2].Name);
		}

		[Test]
		public void InsertVariableIndex ()
		{
			var object_ref = new TypeReference ("System", "Object", null, null, false);
			var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
			var body = new MethodBody (method);

			var x = new VariableDefinition (object_ref);
			var y = new VariableDefinition (object_ref);
			var z = new VariableDefinition (object_ref);

			body.Variables.Add (x);
			body.Variables.Add (z);

			Assert.AreEqual (0, x.Index);
			Assert.AreEqual (-1, y.Index);
			Assert.AreEqual (1, z.Index);

			body.Variables.Insert (1, y);

			Assert.AreEqual (0, x.Index);
			Assert.AreEqual (1, y.Index);
			Assert.AreEqual (2, z.Index);
		}

		[Test]
		public void InsertVariableWithDebugInfo ()
		{
			var object_ref = new TypeReference ("System", "Object", null, null, false);
			var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
			var body = new MethodBody (method);
			var il = body.GetILProcessor ();
			il.Emit (OpCodes.Ret);

			var x = new VariableDefinition (object_ref);
			var y = new VariableDefinition (object_ref);
			var z = new VariableDefinition (object_ref);
			var z2 = new VariableDefinition (object_ref);

			body.Variables.Add (x);
			body.Variables.Add (z);
			body.Variables.Add (z2);

			var scope = new ScopeDebugInformation (body.Instructions [0], body.Instructions [0]);
			method.DebugInformation = new MethodDebugInformation (method) {
				Scope = scope
			};
			scope.Variables.Add (new VariableDebugInformation (x.index, nameof (x)));
			scope.Variables.Add (new VariableDebugInformation (z.index, nameof (z)));
			scope.Variables.Add (new VariableDebugInformation (z2, nameof (z2)));

			body.Variables.Insert (1, y);

			// Adding local variable doesn't add debug info for it (since there's no way to deduce the name of the variable)
			Assert.AreEqual (3, scope.Variables.Count);
			Assert.AreEqual (x.Index, scope.Variables [0].Index);
			Assert.AreEqual (nameof (x), scope.Variables [0].Name);
			Assert.AreEqual (z.Index, scope.Variables [1].Index);
			Assert.AreEqual (nameof (z), scope.Variables [1].Name);
			Assert.AreEqual (z2.Index, scope.Variables [2].Index);
			Assert.AreEqual (nameof (z2), scope.Variables [2].Name);
		}
	}
}