-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathILAmbienceTests.cs
More file actions
403 lines (345 loc) · 17.4 KB
/
ILAmbienceTests.cs
File metadata and controls
403 lines (345 loc) · 17.4 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.Decompiler.IL;
using ICSharpCode.Decompiler.Output;
using ICSharpCode.Decompiler.Tests.TypeSystem;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.TypeSystem.Implementation;
using NUnit.Framework;
using static ICSharpCode.Decompiler.Output.ConversionFlags;
namespace ICSharpCode.Decompiler.Tests.Output
{
[TestFixture]
public class ILAmbienceTests
{
ICompilation compilation;
ILAmbience ambience;
[OneTimeSetUp]
public void FixtureSetUp()
{
ambience = new ILAmbience();
compilation = new SimpleCompilation(TypeSystemLoaderTests.TestAssembly,
TypeSystemLoaderTests.Mscorlib.WithOptions(TypeSystemOptions.Default));
}
ITypeDefinition GetDefinition(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
var foundType = compilation.FindType(type).GetDefinition();
Assert.That(foundType, Is.Not.Null);
return foundType;
}
const ConversionFlags ILSpyMainTreeViewTypeFlags = ShowTypeParameterList | PlaceReturnTypeAfterParameterList;
const ConversionFlags ILSpyMainTreeViewMemberFlags = ILSpyMainTreeViewTypeFlags | ShowParameterList | ShowReturnType | ShowParameterModifiers;
#region ITypeDefinition tests
[TestCase(None, "Dictionary`2")]
[TestCase(ShowDefinitionKeyword, ".class Dictionary`2")]
[TestCase(ShowAccessibility, "public Dictionary`2")]
[TestCase(ShowDefinitionKeyword | ShowAccessibility, ".class public Dictionary`2")]
[TestCase(ShowTypeParameterList, "Dictionary`2<TKey,TValue>")]
[TestCase(ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, ".class public Dictionary`2<TKey,TValue>")]
[TestCase(UseFullyQualifiedEntityNames | ShowTypeParameterList, "System.Collections.Generic.Dictionary`2<TKey,TValue>")]
[TestCase(UseFullyQualifiedEntityNames | ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, ".class public System.Collections.Generic.Dictionary`2<TKey,TValue>")]
[TestCase(ILSpyMainTreeViewTypeFlags, "Dictionary`2<TKey,TValue>")]
public void GenericType(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(Dictionary<,>));
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(typeDef), Is.EqualTo(expectedOutput));
}
[TestCase(None, "Object")]
[TestCase(ShowDefinitionKeyword, ".class Object")]
[TestCase(ShowAccessibility, "public Object")]
[TestCase(ShowDefinitionKeyword | ShowAccessibility, ".class public Object")]
[TestCase(ShowTypeParameterList, "Object")]
[TestCase(ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, ".class public Object")]
[TestCase(UseFullyQualifiedEntityNames | ShowTypeParameterList, "System.Object")]
[TestCase(All, ".class public serializable beforefieldinit System.Object")]
[TestCase(ILSpyMainTreeViewTypeFlags, "Object")]
public void SimpleType(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(object));
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(typeDef), Is.EqualTo(expectedOutput));
}
[TestCase(None, "IEnumerable`1")]
[TestCase(ShowTypeParameterList, "IEnumerable`1<T>")]
[TestCase(ShowTypeParameterList | ShowTypeParameterVarianceModifier, "IEnumerable`1<+T>")]
[TestCase(All, ".class interface public abstract System.Collections.Generic.IEnumerable`1<+T>")]
[TestCase(ILSpyMainTreeViewTypeFlags, "IEnumerable`1<T>")]
public void GenericInterface(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(IEnumerable<>));
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(typeDef), Is.EqualTo(expectedOutput));
}
[TestCase(None, "Enumerator")]
[TestCase(ShowDefinitionKeyword, ".class Enumerator")]
[TestCase(ShowAccessibility, "nested public Enumerator")]
[TestCase(ShowDefinitionKeyword | ShowAccessibility, ".class nested public Enumerator")]
[TestCase(ShowTypeParameterList, "Enumerator<T>")]
[TestCase(ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, ".class nested public Enumerator<T>")]
[TestCase(UseFullyQualifiedEntityNames | ShowTypeParameterList, "System.Collections.Generic.List`1<T>.Enumerator<T>")]
[TestCase(ShowDeclaringType | ShowTypeParameterList, "List`1<T>.Enumerator<T>")]
[TestCase(All, ".class nested public sealed serializable beforefieldinit System.Collections.Generic.List`1<T>.Enumerator<T>")]
[TestCase(ILSpyMainTreeViewTypeFlags, "Enumerator<T>")]
public void GenericTypeWithNested(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(List<>.Enumerator));
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(typeDef), Is.EqualTo(expectedOutput));
}
[TestCase(None, "StaticClass")]
[TestCase(ShowDefinitionKeyword, ".class StaticClass")]
[TestCase(ShowModifiers | ShowDefinitionKeyword, ".class abstract sealed beforefieldinit StaticClass")]
[TestCase(ShowModifiers | ShowAccessibility, "private abstract sealed beforefieldinit StaticClass")]
[TestCase(ShowModifiers | ShowDefinitionKeyword | ShowAccessibility, ".class private abstract sealed beforefieldinit StaticClass")]
[TestCase(ShowModifiers | ShowTypeParameterList, "abstract sealed beforefieldinit StaticClass")]
[TestCase(ShowModifiers | ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, ".class private abstract sealed beforefieldinit StaticClass")]
[TestCase(All, ".class private abstract sealed beforefieldinit ICSharpCode.Decompiler.Tests.Output.StaticClass")]
[TestCase(ILSpyMainTreeViewTypeFlags, "StaticClass")]
public void StaticClassTest(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(StaticClass));
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(typeDef), Is.EqualTo(expectedOutput));
}
[TestCase(None, "SealedClass")]
[TestCase(ShowDefinitionKeyword, ".class SealedClass")]
[TestCase(ShowModifiers | ShowDefinitionKeyword, ".class sealed beforefieldinit SealedClass")]
[TestCase(ShowModifiers | ShowAccessibility, "private sealed beforefieldinit SealedClass")]
[TestCase(ShowModifiers | ShowDefinitionKeyword | ShowAccessibility, ".class private sealed beforefieldinit SealedClass")]
[TestCase(ShowModifiers | ShowTypeParameterList, "sealed beforefieldinit SealedClass")]
[TestCase(ShowModifiers | ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, ".class private sealed beforefieldinit SealedClass")]
[TestCase(All, ".class private sealed beforefieldinit ICSharpCode.Decompiler.Tests.Output.SealedClass")]
[TestCase(ILSpyMainTreeViewTypeFlags, "SealedClass")]
public void SealedClassTest(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(SealedClass));
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(typeDef), Is.EqualTo(expectedOutput));
}
[TestCase(None, "RefStruct")]
[TestCase(ShowDefinitionKeyword, ".class RefStruct")]
[TestCase(ShowModifiers | ShowDefinitionKeyword, ".class sealed beforefieldinit RefStruct")]
[TestCase(ShowModifiers | ShowAccessibility, "private sealed beforefieldinit RefStruct")]
[TestCase(ShowModifiers | ShowDefinitionKeyword | ShowAccessibility, ".class private sealed beforefieldinit RefStruct")]
[TestCase(ShowModifiers | ShowTypeParameterList, "sealed beforefieldinit RefStruct")]
[TestCase(ShowModifiers | ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, ".class private sealed beforefieldinit RefStruct")]
[TestCase(All, ".class private sealed beforefieldinit ICSharpCode.Decompiler.Tests.Output.RefStruct")]
[TestCase(ILSpyMainTreeViewTypeFlags, "RefStruct")]
public void RefStructTest(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(RefStruct));
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(typeDef), Is.EqualTo(expectedOutput));
}
[TestCase(None, "ReadonlyStruct")]
[TestCase(ShowDefinitionKeyword, ".class ReadonlyStruct")]
[TestCase(ShowModifiers | ShowDefinitionKeyword, ".class sealed beforefieldinit ReadonlyStruct")]
[TestCase(ShowModifiers | ShowAccessibility, "private sealed beforefieldinit ReadonlyStruct")]
[TestCase(ShowModifiers | ShowDefinitionKeyword | ShowAccessibility, ".class private sealed beforefieldinit ReadonlyStruct")]
[TestCase(ShowModifiers | ShowTypeParameterList, "sealed beforefieldinit ReadonlyStruct")]
[TestCase(ShowModifiers | ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, ".class private sealed beforefieldinit ReadonlyStruct")]
[TestCase(All, ".class private sealed beforefieldinit ICSharpCode.Decompiler.Tests.Output.ReadonlyStruct")]
[TestCase(ILSpyMainTreeViewTypeFlags, "ReadonlyStruct")]
public void ReadonlyStructTest(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(ReadonlyStruct));
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(typeDef), Is.EqualTo(expectedOutput));
}
[TestCase(None, "ReadonlyRefStruct")]
[TestCase(ShowDefinitionKeyword, ".class ReadonlyRefStruct")]
[TestCase(ShowModifiers | ShowDefinitionKeyword, ".class sealed beforefieldinit ReadonlyRefStruct")]
[TestCase(ShowModifiers | ShowAccessibility, "private sealed beforefieldinit ReadonlyRefStruct")]
[TestCase(ShowModifiers | ShowDefinitionKeyword | ShowAccessibility, ".class private sealed beforefieldinit ReadonlyRefStruct")]
[TestCase(ShowModifiers | ShowTypeParameterList, "sealed beforefieldinit ReadonlyRefStruct")]
[TestCase(ShowModifiers | ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, ".class private sealed beforefieldinit ReadonlyRefStruct")]
[TestCase(All, ".class private sealed beforefieldinit ICSharpCode.Decompiler.Tests.Output.ReadonlyRefStruct")]
[TestCase(ILSpyMainTreeViewTypeFlags, "ReadonlyRefStruct")]
public void ReadonlyRefStructTest(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(ReadonlyRefStruct));
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(typeDef), Is.EqualTo(expectedOutput));
}
#endregion
#region Delegate tests
[TestCase(None, "Func`2")]
[TestCase(ShowTypeParameterList, "Func`2<T,TResult>")]
[TestCase(ShowTypeParameterList | ShowTypeParameterVarianceModifier, "Func`2<-T,+TResult>")]
[TestCase(All, ".class public sealed System.Func`2<-T,+TResult>")]
[TestCase(ILSpyMainTreeViewTypeFlags, "Func`2<T,TResult>")]
public void FuncDelegate(ConversionFlags flags, string expectedOutput)
{
var func = GetDefinition(typeof(Func<,>));
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(func), Is.EqualTo(expectedOutput));
}
#endregion
#region IField tests
[TestCase(All & ~PlaceReturnTypeAfterParameterList, ".field private instance int32 ICSharpCode.Decompiler.Tests.Output.Program::test")]
[TestCase(ILSpyMainTreeViewMemberFlags, "test : int32")]
[TestCase(All & ~(ShowDeclaringType | ShowModifiers | ShowAccessibility | PlaceReturnTypeAfterParameterList), ".field int32 ICSharpCode.Decompiler.Tests.Output.Program::test")]
public void SimpleField(ConversionFlags flags, string expectedOutput)
{
var field = GetDefinition(typeof(Program)).GetFields(f => f.Name == "test").Single();
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(field), Is.EqualTo(expectedOutput));
}
[TestCase(All & ~PlaceReturnTypeAfterParameterList, ".field private static literal int32 ICSharpCode.Decompiler.Tests.Output.Program::TEST2")]
[TestCase(ILSpyMainTreeViewMemberFlags, "TEST2 : int32")]
public void SimpleConstField(ConversionFlags flags, string expectedOutput)
{
var field = compilation.FindType(typeof(Program)).GetFields(f => f.Name == "TEST2").Single();
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(field), Is.EqualTo(expectedOutput));
}
#endregion
#region IEvent tests
[Test]
public void EventWithDeclaringType()
{
var ev = compilation.FindType(typeof(Program)).GetEvents(f => f.Name == "ProgramChanged").Single();
ambience.ConversionFlags = ConversionFlags.StandardConversionFlags | ConversionFlags.ShowDeclaringType;
string result = ambience.ConvertSymbol(ev);
Assert.That(result, Is.EqualTo(".event instance EventHandler Program::ProgramChanged"));
}
[Test]
public void CustomEvent()
{
var ev = compilation.FindType(typeof(Program)).GetEvents(f => f.Name == "SomeEvent").Single();
ambience.ConversionFlags = ConversionFlags.StandardConversionFlags;
string result = ambience.ConvertSymbol(ev);
Assert.That(result, Is.EqualTo(".event instance EventHandler SomeEvent"));
}
#endregion
#region Property tests
[TestCase(StandardConversionFlags, ".property instance int32 Test")]
[TestCase(ILSpyMainTreeViewMemberFlags, "Test : int32")]
public void AutomaticProperty(ConversionFlags flags, string expectedOutput)
{
var prop = compilation.FindType(typeof(Program)).GetProperties(p => p.Name == "Test").Single();
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(prop), Is.EqualTo(expectedOutput));
}
[TestCase(StandardConversionFlags, ".property instance int32 Item(int32 index)")]
[TestCase(ILSpyMainTreeViewMemberFlags, "Item(int32) : int32")]
public void Indexer(ConversionFlags flags, string expectedOutput)
{
var prop = compilation.FindType(typeof(Program)).GetProperties(p => p.IsIndexer && !p.IsExplicitInterfaceImplementation).Single();
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(prop), Is.EqualTo(expectedOutput));
}
[TestCase(StandardConversionFlags, ".property instance int32 ICSharpCode.Decompiler.Tests.Output.Interface.Item(int32 index)")]
[TestCase(ILSpyMainTreeViewMemberFlags, "ICSharpCode.Decompiler.Tests.Output.Interface.Item(int32) : int32")]
public void ExplicitIndexer(ConversionFlags flags, string expectedOutput)
{
var prop = compilation.FindType(typeof(Program)).GetProperties(p => p.IsIndexer && p.IsExplicitInterfaceImplementation).Single();
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(prop), Is.EqualTo(expectedOutput));
}
#endregion
#region IMethod tests
[TestCase(StandardConversionFlags, ".method public hidebysig specialname rtspecialname instance .ctor(int32 x)")]
[TestCase(ILSpyMainTreeViewMemberFlags, ".ctor(int32)")]
public void ConstructorTests(ConversionFlags flags, string expectedOutput)
{
var prop = compilation.FindType(typeof(Program)).GetConstructors().Single();
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(prop), Is.EqualTo(expectedOutput));
}
[TestCase(StandardConversionFlags, ".method family hidebysig virtual instance void Finalize()")]
[TestCase(ILSpyMainTreeViewMemberFlags, "Finalize() : void")]
public void DestructorTests(ConversionFlags flags, string expectedOutput)
{
var dtor = compilation.FindType(typeof(Program))
.GetMembers(m => m.SymbolKind == SymbolKind.Destructor, GetMemberOptions.IgnoreInheritedMembers).Single();
ambience.ConversionFlags = flags;
Assert.That(ambience.ConvertSymbol(dtor), Is.EqualTo(expectedOutput));
}
#endregion
}
#region Test types
#pragma warning disable 169, 67
class Test { }
static class StaticClass { }
sealed class SealedClass { }
ref struct RefStruct { }
readonly struct ReadonlyStruct { }
readonly ref struct ReadonlyRefStruct { }
interface Interface
{
int this[int x] { get; }
}
class Program : Interface
{
int test;
const int TEST2 = 2;
public int Test { get; set; }
public int this[int index] {
get {
return index;
}
}
int Interface.this[int index] {
get {
return index;
}
}
public event EventHandler ProgramChanged;
public event EventHandler SomeEvent {
add { }
remove { }
}
public static bool operator +(Program lhs, Program rhs)
{
throw new NotImplementedException();
}
public static implicit operator Test(Program lhs)
{
throw new NotImplementedException();
}
public static explicit operator int(Program lhs)
{
throw new NotImplementedException();
}
public Program(int x)
{
}
~Program()
{
}
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
public static void InParameter(in int a)
{
}
}
#endregion
}