1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
20
using System.Collections.Generic;
22
using System.Reflection;
23
using ICSharpCode.NRefactory.TypeSystem.Implementation;
24
using NUnit.Framework;
26
namespace ICSharpCode.NRefactory.TypeSystem
29
public unsafe class ReflectionHelperTests
31
ICompilation compilation = new SimpleCompilation(CecilLoaderTests.Mscorlib);
33
void TestFindType(Type type)
35
IType t = compilation.FindType(type);
36
Assert.IsNotNull(t, type.FullName);
37
Assert.AreEqual(type.FullName, t.ReflectionName);
41
public void TestGetInnerClass()
43
TestFindType(typeof(Environment.SpecialFolder));
47
public void TestGetGenericClass1()
49
TestFindType(typeof(Action<>));
53
public void TestGetGenericClass2()
55
TestFindType(typeof(Action<,>));
59
public void TestGetInnerClassInGenericClass1()
61
TestFindType(typeof(Dictionary<,>.ValueCollection));
65
public void TestGetInnerClassInGenericClass2()
67
TestFindType(typeof(Dictionary<,>.ValueCollection.Enumerator));
71
public void TestToTypeReferenceInnerClass()
73
Assert.AreEqual("System.Environment+SpecialFolder",
74
compilation.FindType(typeof(Environment.SpecialFolder)).ReflectionName);
78
public void TestToTypeReferenceUnboundGenericClass()
80
Assert.AreEqual("System.Action`1",
81
compilation.FindType(typeof(Action<>)).ReflectionName);
82
Assert.AreEqual("System.Action`2",
83
compilation.FindType(typeof(Action<,>)).ReflectionName);
87
public void TestToTypeReferenceBoundGenericClass()
89
Assert.AreEqual("System.Action`1[[System.String]]",
90
compilation.FindType(typeof(Action<string>)).ReflectionName);
91
Assert.AreEqual("System.Action`2[[System.Int32],[System.Int16]]",
92
compilation.FindType(typeof(Action<int, short>)).ReflectionName);
97
public void TestToTypeReferenceNullableType()
99
Assert.AreEqual("System.Nullable`1[[System.Int32]]",
100
compilation.FindType(typeof(int?)).ReflectionName);
104
public void TestToTypeReferenceInnerClassInUnboundGenericType()
106
Assert.AreEqual("System.Collections.Generic.Dictionary`2+ValueCollection",
107
compilation.FindType(typeof(Dictionary<,>.ValueCollection)).ReflectionName);
111
public void TestToTypeReferenceInnerClassInBoundGenericType()
113
Assert.AreEqual("System.Collections.Generic.Dictionary`2+KeyCollection[[System.String],[System.Int32]]",
114
compilation.FindType(typeof(Dictionary<string, int>.KeyCollection)).ReflectionName);
118
public void TestToTypeReferenceArrayType()
120
Assert.AreEqual(typeof(int[]).FullName,
121
compilation.FindType(typeof(int[])).ReflectionName);
125
public void TestToTypeReferenceMultidimensionalArrayType()
127
Assert.AreEqual(typeof(int[,]).FullName,
128
compilation.FindType(typeof(int[,])).ReflectionName);
132
public void TestToTypeReferenceJaggedMultidimensionalArrayType()
134
Assert.AreEqual(typeof(int[,][,,]).FullName,
135
compilation.FindType(typeof(int[,][,,])).ReflectionName);
139
public void TestToTypeReferencePointerType()
141
Assert.AreEqual(typeof(int*).FullName,
142
compilation.FindType(typeof(int*)).ReflectionName);
146
public void TestToTypeReferenceByReferenceType()
148
Assert.AreEqual(typeof(int).MakeByRefType().FullName,
149
compilation.FindType(typeof(int).MakeByRefType()).ReflectionName);
153
public void TestToTypeReferenceGenericType()
155
MethodInfo convertAllInfo = typeof(List<>).GetMethod("ConvertAll");
156
ITypeReference parameterType = convertAllInfo.GetParameters()[0].ParameterType.ToTypeReference(); // Converter[[`0],[``0]]
157
// cannot resolve generic types without knowing the parent entity:
158
IType resolvedWithoutEntity = parameterType.Resolve(compilation.TypeResolveContext);
159
Assert.AreEqual("System.Converter`2[[`0],[``0]]", resolvedWithoutEntity.ReflectionName);
160
Assert.IsNull(((ITypeParameter)((ParameterizedType)resolvedWithoutEntity).GetTypeArgument(0)).Owner);
161
// now try with parent entity:
162
IMethod convertAll = compilation.FindType(typeof(List<>)).GetMethods(m => m.Name == "ConvertAll").Single();
163
IType resolvedWithEntity = parameterType.Resolve(new SimpleTypeResolveContext(convertAll));
164
Assert.AreEqual("System.Converter`2[[`0],[``0]]", resolvedWithEntity.ReflectionName);
165
Assert.AreSame(convertAll.DeclaringTypeDefinition, ((ITypeParameter)((ParameterizedType)resolvedWithEntity).GetTypeArgument(0)).Owner);
169
public void ParseReflectionName()
171
var context = new SimpleTypeResolveContext(compilation.MainAssembly);
172
Assert.AreEqual("System.Int32", ReflectionHelper.ParseReflectionName("System.Int32").Resolve(context).ReflectionName);
173
Assert.AreEqual("System.Int32&", ReflectionHelper.ParseReflectionName("System.Int32&").Resolve(context).ReflectionName);
174
Assert.AreEqual("System.Int32*&", ReflectionHelper.ParseReflectionName("System.Int32*&").Resolve(context).ReflectionName);
175
Assert.AreEqual("System.Int32", ReflectionHelper.ParseReflectionName(typeof(int).AssemblyQualifiedName).Resolve(context).ReflectionName);
176
Assert.AreEqual("System.Action`1[[System.String]]", ReflectionHelper.ParseReflectionName("System.Action`1[[System.String]]").Resolve(context).ReflectionName);
177
Assert.AreEqual("System.Action`1[[System.String]]", ReflectionHelper.ParseReflectionName("System.Action`1[[System.String, mscorlib]]").Resolve(context).ReflectionName);
178
Assert.AreEqual("System.Int32[,,][,]", ReflectionHelper.ParseReflectionName(typeof(int[,][,,]).AssemblyQualifiedName).Resolve(context).ReflectionName);
179
Assert.AreEqual("System.Environment+SpecialFolder", ReflectionHelper.ParseReflectionName("System.Environment+SpecialFolder").Resolve(context).ReflectionName);
183
public void ParseOpenGenericReflectionName()
185
ITypeReference typeRef = ReflectionHelper.ParseReflectionName("System.Converter`2[[`0],[``0]]");
186
Assert.AreEqual("System.Converter`2[[`0],[``0]]", typeRef.Resolve(new SimpleTypeResolveContext(compilation.MainAssembly)).ReflectionName);
187
IMethod convertAll = compilation.FindType(typeof(List<>)).GetMethods(m => m.Name == "ConvertAll").Single();
188
Assert.AreEqual("System.Converter`2[[`0],[``0]]", typeRef.Resolve(new SimpleTypeResolveContext(convertAll)).ReflectionName);
192
public void ArrayOfTypeParameter()
194
var context = new SimpleTypeResolveContext(compilation.MainAssembly);
195
Assert.AreEqual("`0[,]", ReflectionHelper.ParseReflectionName("`0[,]").Resolve(context).ReflectionName);
198
[Test, ExpectedException(typeof(ArgumentNullException))]
199
public void ParseNullReflectionName()
201
ReflectionHelper.ParseReflectionName(null);
204
[Test, ExpectedException(typeof(ReflectionNameParseException))]
205
public void ParseInvalidReflectionName1()
207
ReflectionHelper.ParseReflectionName(string.Empty);
210
[Test, ExpectedException(typeof(ReflectionNameParseException))]
211
public void ParseInvalidReflectionName2()
213
ReflectionHelper.ParseReflectionName("`");
216
[Test, ExpectedException(typeof(ReflectionNameParseException))]
217
public void ParseInvalidReflectionName3()
219
ReflectionHelper.ParseReflectionName("``");
222
[Test, ExpectedException(typeof(ReflectionNameParseException))]
223
public void ParseInvalidReflectionName4()
225
ReflectionHelper.ParseReflectionName("System.Action`A");
228
[Test, ExpectedException(typeof(ReflectionNameParseException))]
229
public void ParseInvalidReflectionName5()
231
ReflectionHelper.ParseReflectionName("System.Environment+");
234
[Test, ExpectedException(typeof(ReflectionNameParseException))]
235
public void ParseInvalidReflectionName5b()
237
ReflectionHelper.ParseReflectionName("System.Environment+`");
240
[Test, ExpectedException(typeof(ReflectionNameParseException))]
241
public void ParseInvalidReflectionName6()
243
ReflectionHelper.ParseReflectionName("System.Int32[");
246
[Test, ExpectedException(typeof(ReflectionNameParseException))]
247
public void ParseInvalidReflectionName7()
249
ReflectionHelper.ParseReflectionName("System.Int32[`]");
252
[Test, ExpectedException(typeof(ReflectionNameParseException))]
253
public void ParseInvalidReflectionName8()
255
ReflectionHelper.ParseReflectionName("System.Int32[,");
258
[Test, ExpectedException(typeof(ReflectionNameParseException))]
259
public void ParseInvalidReflectionName9()
261
ReflectionHelper.ParseReflectionName("System.Int32]");
264
[Test, ExpectedException(typeof(ReflectionNameParseException))]
265
public void ParseInvalidReflectionName10()
267
ReflectionHelper.ParseReflectionName("System.Int32*a");
270
[Test, ExpectedException(typeof(ReflectionNameParseException))]
271
public void ParseInvalidReflectionName11()
273
ReflectionHelper.ParseReflectionName("System.Action`1[[]]");
276
[Test, ExpectedException(typeof(ReflectionNameParseException))]
277
public void ParseInvalidReflectionName12()
279
ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32]a]");
282
[Test, ExpectedException(typeof(ReflectionNameParseException))]
283
public void ParseInvalidReflectionName13()
285
ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32],]");
288
[Test, ExpectedException(typeof(ReflectionNameParseException))]
289
public void ParseInvalidReflectionName14()
291
ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32]");
294
[Test, ExpectedException(typeof(ReflectionNameParseException))]
295
public void ParseInvalidReflectionName15()
297
ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32");
300
[Test, ExpectedException(typeof(ReflectionNameParseException))]
301
public void ParseInvalidReflectionName16()
303
ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32],[System.String");