/
CSharpAmbienceTests.cs
383 lines (331 loc) · 16.6 KB
/
CSharpAmbienceTests.cs
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
// 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.CSharp.OutputVisitor;
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 CSharpAmbienceTests
{
ICompilation compilation;
CSharpAmbience ambience;
[OneTimeSetUp]
public void FixtureSetUp()
{
ambience = new CSharpAmbience();
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.IsNotNull(foundType);
return foundType;
}
const ConversionFlags ILSpyMainTreeViewTypeFlags = ShowTypeParameterList | PlaceReturnTypeAfterParameterList;
const ConversionFlags ILSpyMainTreeViewMemberFlags = ILSpyMainTreeViewTypeFlags | ShowParameterList | ShowReturnType | ShowParameterModifiers;
#region ITypeDefinition tests
[TestCase(None, "Dictionary")]
[TestCase(ShowDefinitionKeyword, "class Dictionary")]
[TestCase(ShowAccessibility, "public Dictionary")]
[TestCase(ShowDefinitionKeyword | ShowAccessibility, "public class Dictionary")]
[TestCase(ShowTypeParameterList, "Dictionary<TKey,TValue>")]
[TestCase(ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, "public class Dictionary<TKey,TValue>")]
[TestCase(UseFullyQualifiedEntityNames | ShowTypeParameterList, "System.Collections.Generic.Dictionary<TKey,TValue>")]
[TestCase(UseFullyQualifiedEntityNames | ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, "public class System.Collections.Generic.Dictionary<TKey,TValue>")]
[TestCase(ILSpyMainTreeViewTypeFlags, "Dictionary<TKey,TValue>")]
public void GenericType(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(Dictionary<,>));
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef));
}
[TestCase(None, "Object")]
[TestCase(ShowDefinitionKeyword, "class Object")]
[TestCase(ShowAccessibility, "public Object")]
[TestCase(ShowDefinitionKeyword | ShowAccessibility, "public class Object")]
[TestCase(ShowTypeParameterList, "Object")]
[TestCase(ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, "public class Object")]
[TestCase(UseFullyQualifiedEntityNames | ShowTypeParameterList, "System.Object")]
[TestCase(All, "public class System.Object")]
[TestCase(ILSpyMainTreeViewTypeFlags, "Object")]
public void SimpleType(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(object));
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef));
}
[TestCase(None, "IEnumerable")]
[TestCase(ShowTypeParameterList, "IEnumerable<T>")]
[TestCase(ShowTypeParameterList | ShowTypeParameterVarianceModifier, "IEnumerable<out T>")]
[TestCase(All, "public interface System.Collections.Generic.IEnumerable<out T>")]
[TestCase(ILSpyMainTreeViewTypeFlags, "IEnumerable<T>")]
public void GenericInterface(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(IEnumerable<>));
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef));
}
[TestCase(None, "Enumerator")]
[TestCase(ShowDefinitionKeyword, "struct Enumerator")]
[TestCase(ShowAccessibility, "public Enumerator")]
[TestCase(ShowDefinitionKeyword | ShowAccessibility, "public struct Enumerator")]
[TestCase(ShowTypeParameterList, "Enumerator")]
[TestCase(ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, "public struct Enumerator")]
[TestCase(UseFullyQualifiedEntityNames | ShowTypeParameterList, "System.Collections.Generic.List<T>.Enumerator")]
[TestCase(ShowDeclaringType | ShowTypeParameterList, "List<T>.Enumerator")]
[TestCase(All, "public struct System.Collections.Generic.List<T>.Enumerator")]
[TestCase(ILSpyMainTreeViewTypeFlags, "Enumerator")]
public void GenericTypeWithNested(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(List<>.Enumerator));
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef));
}
[TestCase(None, "StaticClass")]
[TestCase(ShowDefinitionKeyword, "class StaticClass")]
[TestCase(ShowModifiers | ShowDefinitionKeyword, "static class StaticClass")]
[TestCase(ShowModifiers | ShowAccessibility, "private static StaticClass")]
[TestCase(ShowModifiers | ShowDefinitionKeyword | ShowAccessibility, "private static class StaticClass")]
[TestCase(ShowModifiers | ShowTypeParameterList, "static StaticClass")]
[TestCase(ShowModifiers | ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, "private static class StaticClass")]
[TestCase(All, "private static class ICSharpCode.Decompiler.Tests.Output.CSharpAmbienceTests.StaticClass")]
[TestCase(ILSpyMainTreeViewTypeFlags, "StaticClass")]
public void StaticClassTest(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(StaticClass));
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef));
}
[TestCase(None, "SealedClass")]
[TestCase(ShowDefinitionKeyword, "class SealedClass")]
[TestCase(ShowModifiers | ShowDefinitionKeyword, "sealed class SealedClass")]
[TestCase(ShowModifiers | ShowAccessibility, "private sealed SealedClass")]
[TestCase(ShowModifiers | ShowDefinitionKeyword | ShowAccessibility, "private sealed class SealedClass")]
[TestCase(ShowModifiers | ShowTypeParameterList, "sealed SealedClass")]
[TestCase(ShowModifiers | ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, "private sealed class SealedClass")]
[TestCase(All, "private sealed class ICSharpCode.Decompiler.Tests.Output.CSharpAmbienceTests.SealedClass")]
[TestCase(ILSpyMainTreeViewTypeFlags, "SealedClass")]
public void SealedClassTest(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(SealedClass));
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef));
}
[TestCase(None, "RefStruct")]
[TestCase(ShowDefinitionKeyword, "struct RefStruct")]
[TestCase(ShowModifiers | ShowDefinitionKeyword, "ref struct RefStruct")]
[TestCase(ShowModifiers | ShowAccessibility, "private ref RefStruct")]
[TestCase(ShowModifiers | ShowDefinitionKeyword | ShowAccessibility, "private ref struct RefStruct")]
[TestCase(ShowModifiers | ShowTypeParameterList, "ref RefStruct")]
[TestCase(ShowModifiers | ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, "private ref struct RefStruct")]
[TestCase(All, "private ref struct ICSharpCode.Decompiler.Tests.Output.CSharpAmbienceTests.RefStruct")]
[TestCase(ILSpyMainTreeViewTypeFlags, "RefStruct")]
public void RefStructTest(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(RefStruct));
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef));
}
[TestCase(None, "ReadonlyStruct")]
[TestCase(ShowDefinitionKeyword, "struct ReadonlyStruct")]
[TestCase(ShowModifiers | ShowDefinitionKeyword, "readonly struct ReadonlyStruct")]
[TestCase(ShowModifiers | ShowAccessibility, "private readonly ReadonlyStruct")]
[TestCase(ShowModifiers | ShowDefinitionKeyword | ShowAccessibility, "private readonly struct ReadonlyStruct")]
[TestCase(ShowModifiers | ShowTypeParameterList, "readonly ReadonlyStruct")]
[TestCase(ShowModifiers | ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, "private readonly struct ReadonlyStruct")]
[TestCase(All, "private readonly struct ICSharpCode.Decompiler.Tests.Output.CSharpAmbienceTests.ReadonlyStruct")]
[TestCase(ILSpyMainTreeViewTypeFlags, "ReadonlyStruct")]
public void ReadonlyStructTest(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(ReadonlyStruct));
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef));
}
[TestCase(None, "ReadonlyRefStruct")]
[TestCase(ShowDefinitionKeyword, "struct ReadonlyRefStruct")]
[TestCase(ShowModifiers | ShowDefinitionKeyword, "readonly ref struct ReadonlyRefStruct")]
[TestCase(ShowModifiers | ShowAccessibility, "private readonly ref ReadonlyRefStruct")]
[TestCase(ShowModifiers | ShowDefinitionKeyword | ShowAccessibility, "private readonly ref struct ReadonlyRefStruct")]
[TestCase(ShowModifiers | ShowTypeParameterList, "readonly ref ReadonlyRefStruct")]
[TestCase(ShowModifiers | ShowTypeParameterList | ShowDefinitionKeyword | ShowAccessibility, "private readonly ref struct ReadonlyRefStruct")]
[TestCase(All, "private readonly ref struct ICSharpCode.Decompiler.Tests.Output.CSharpAmbienceTests.ReadonlyRefStruct")]
[TestCase(ILSpyMainTreeViewTypeFlags, "ReadonlyRefStruct")]
public void ReadonlyRefStructTest(ConversionFlags flags, string expectedOutput)
{
var typeDef = GetDefinition(typeof(ReadonlyRefStruct));
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef));
}
#endregion
#region Delegate tests
[TestCase(None, "Func")]
[TestCase(ShowTypeParameterList, "Func<T,TResult>")]
[TestCase(ShowTypeParameterList | ShowTypeParameterVarianceModifier, "Func<in T,out TResult>")]
[TestCase(ShowTypeParameterList | ShowReturnType | ShowTypeParameterVarianceModifier, "TResult Func<in T,out TResult>")]
[TestCase(ShowTypeParameterList | ShowParameterList | ShowTypeParameterVarianceModifier, "Func<in T,out TResult>(T)")]
[TestCase(ShowTypeParameterList | ShowParameterList | ShowReturnType | ShowTypeParameterVarianceModifier, "TResult Func<in T,out TResult>(T)")]
[TestCase(All & ~PlaceReturnTypeAfterParameterList, "public delegate TResult System.Func<in T,out TResult>(T arg);")]
[TestCase(All, "public delegate System.Func<in T,out TResult>(T arg) : TResult;")]
[TestCase(ILSpyMainTreeViewTypeFlags, "Func<T,TResult>")]
public void FuncDelegate(ConversionFlags flags, string expectedOutput)
{
var func = GetDefinition(typeof(Func<,>));
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(func));
}
#endregion
#region IField tests
[TestCase(All & ~PlaceReturnTypeAfterParameterList, "private int ICSharpCode.Decompiler.Tests.Output.CSharpAmbienceTests.Program.test;")]
[TestCase(ILSpyMainTreeViewMemberFlags, "test : int")]
[TestCase(ConversionFlags.All & ~(ConversionFlags.ShowDeclaringType | ConversionFlags.ShowModifiers | ConversionFlags.ShowAccessibility | ConversionFlags.PlaceReturnTypeAfterParameterList), "int test;")]
public void SimpleField(ConversionFlags flags, string expectedOutput)
{
var field = GetDefinition(typeof(CSharpAmbienceTests.Program)).GetFields(f => f.Name == "test").Single();
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(field));
}
[TestCase(All & ~PlaceReturnTypeAfterParameterList, "private const int ICSharpCode.Decompiler.Tests.Output.CSharpAmbienceTests.Program.TEST2;")]
[TestCase(ILSpyMainTreeViewMemberFlags, "TEST2 : int")]
public void SimpleConstField(ConversionFlags flags, string expectedOutput)
{
var field = compilation.FindType(typeof(CSharpAmbienceTests.Program)).GetFields(f => f.Name == "TEST2").Single();
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(field));
}
#endregion
#region IEvent tests
[Test]
public void EventWithDeclaringType()
{
var ev = compilation.FindType(typeof(CSharpAmbienceTests.Program)).GetEvents(f => f.Name == "ProgramChanged").Single();
ambience.ConversionFlags = ConversionFlags.StandardConversionFlags | ConversionFlags.ShowDeclaringType;
string result = ambience.ConvertSymbol(ev);
Assert.AreEqual("public event EventHandler Program.ProgramChanged;", result);
}
[Test]
public void CustomEvent()
{
var ev = compilation.FindType(typeof(CSharpAmbienceTests.Program)).GetEvents(f => f.Name == "SomeEvent").Single();
ambience.ConversionFlags = ConversionFlags.StandardConversionFlags;
string result = ambience.ConvertSymbol(ev);
Assert.AreEqual("public event EventHandler SomeEvent;", result);
}
#endregion
#region Property tests
[TestCase(StandardConversionFlags, "public int Test { get; set; }")]
[TestCase(ILSpyMainTreeViewMemberFlags, "Test : int")]
public void AutomaticProperty(ConversionFlags flags, string expectedOutput)
{
var prop = compilation.FindType(typeof(CSharpAmbienceTests.Program)).GetProperties(p => p.Name == "Test").Single();
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(prop));
}
[TestCase(StandardConversionFlags, "public int this[int index] { get; }")]
[TestCase(ILSpyMainTreeViewMemberFlags, "this[int] : int")]
public void Indexer(ConversionFlags flags, string expectedOutput)
{
var prop = compilation.FindType(typeof(CSharpAmbienceTests.Program)).GetProperties(p => p.IsIndexer).Single();
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(prop));
}
#endregion
#region IMethod tests
[TestCase(StandardConversionFlags, "public Program(int x);")]
[TestCase(ILSpyMainTreeViewMemberFlags, "Program(int)")]
public void ConstructorTests(ConversionFlags flags, string expectedOutput)
{
var prop = compilation.FindType(typeof(CSharpAmbienceTests.Program)).GetConstructors().Single();
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(prop));
}
[TestCase(StandardConversionFlags, "~Program();")]
[TestCase(ILSpyMainTreeViewMemberFlags, "~Program()")]
public void DestructorTests(ConversionFlags flags, string expectedOutput)
{
var dtor = compilation.FindType(typeof(CSharpAmbienceTests.Program))
.GetMembers(m => m.SymbolKind == SymbolKind.Destructor, GetMemberOptions.IgnoreInheritedMembers).Single();
ambience.ConversionFlags = flags;
Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(dtor));
}
#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 { }
class Program
{
int test;
const int TEST2 = 2;
public int Test { get; set; }
public int 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
}
}