~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/TypeSystem/ReflectionHelperTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
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:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
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.
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Linq;
 
22
using System.Reflection;
 
23
using ICSharpCode.NRefactory.TypeSystem.Implementation;
 
24
using NUnit.Framework;
 
25
 
 
26
namespace ICSharpCode.NRefactory.TypeSystem
 
27
{
 
28
        [TestFixture]
 
29
        public unsafe class ReflectionHelperTests
 
30
        {
 
31
                ICompilation compilation = new SimpleCompilation(CecilLoaderTests.Mscorlib);
 
32
                
 
33
                void TestFindType(Type type)
 
34
                {
 
35
                        IType t = compilation.FindType(type);
 
36
                        Assert.IsNotNull(t, type.FullName);
 
37
                        Assert.AreEqual(type.FullName, t.ReflectionName);
 
38
                }
 
39
                
 
40
                [Test]
 
41
                public void TestGetInnerClass()
 
42
                {
 
43
                        TestFindType(typeof(Environment.SpecialFolder));
 
44
                }
 
45
                
 
46
                [Test]
 
47
                public void TestGetGenericClass1()
 
48
                {
 
49
                        TestFindType(typeof(Action<>));
 
50
                }
 
51
                
 
52
                [Test]
 
53
                public void TestGetGenericClass2()
 
54
                {
 
55
                        TestFindType(typeof(Action<,>));
 
56
                }
 
57
                
 
58
                [Test]
 
59
                public void TestGetInnerClassInGenericClass1()
 
60
                {
 
61
                        TestFindType(typeof(Dictionary<,>.ValueCollection));
 
62
                }
 
63
                
 
64
                [Test]
 
65
                public void TestGetInnerClassInGenericClass2()
 
66
                {
 
67
                        TestFindType(typeof(Dictionary<,>.ValueCollection.Enumerator));
 
68
                }
 
69
                
 
70
                [Test]
 
71
                public void TestToTypeReferenceInnerClass()
 
72
                {
 
73
                        Assert.AreEqual("System.Environment+SpecialFolder",
 
74
                                        compilation.FindType(typeof(Environment.SpecialFolder)).ReflectionName);
 
75
                }
 
76
                
 
77
                [Test]
 
78
                public void TestToTypeReferenceUnboundGenericClass()
 
79
                {
 
80
                        Assert.AreEqual("System.Action`1",
 
81
                                        compilation.FindType(typeof(Action<>)).ReflectionName);
 
82
                        Assert.AreEqual("System.Action`2",
 
83
                                        compilation.FindType(typeof(Action<,>)).ReflectionName);
 
84
                }
 
85
                
 
86
                [Test]
 
87
                public void TestToTypeReferenceBoundGenericClass()
 
88
                {
 
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);
 
93
                }
 
94
                
 
95
                
 
96
                [Test]
 
97
                public void TestToTypeReferenceNullableType()
 
98
                {
 
99
                        Assert.AreEqual("System.Nullable`1[[System.Int32]]",
 
100
                                        compilation.FindType(typeof(int?)).ReflectionName);
 
101
                }
 
102
                
 
103
                [Test]
 
104
                public void TestToTypeReferenceInnerClassInUnboundGenericType()
 
105
                {
 
106
                        Assert.AreEqual("System.Collections.Generic.Dictionary`2+ValueCollection",
 
107
                                        compilation.FindType(typeof(Dictionary<,>.ValueCollection)).ReflectionName);
 
108
                }
 
109
                
 
110
                [Test]
 
111
                public void TestToTypeReferenceInnerClassInBoundGenericType()
 
112
                {
 
113
                        Assert.AreEqual("System.Collections.Generic.Dictionary`2+KeyCollection[[System.String],[System.Int32]]",
 
114
                                        compilation.FindType(typeof(Dictionary<string, int>.KeyCollection)).ReflectionName);
 
115
                }
 
116
                
 
117
                [Test]
 
118
                public void TestToTypeReferenceArrayType()
 
119
                {
 
120
                        Assert.AreEqual(typeof(int[]).FullName,
 
121
                                        compilation.FindType(typeof(int[])).ReflectionName);
 
122
                }
 
123
                
 
124
                [Test]
 
125
                public void TestToTypeReferenceMultidimensionalArrayType()
 
126
                {
 
127
                        Assert.AreEqual(typeof(int[,]).FullName,
 
128
                                        compilation.FindType(typeof(int[,])).ReflectionName);
 
129
                }
 
130
                
 
131
                [Test]
 
132
                public void TestToTypeReferenceJaggedMultidimensionalArrayType()
 
133
                {
 
134
                        Assert.AreEqual(typeof(int[,][,,]).FullName,
 
135
                                        compilation.FindType(typeof(int[,][,,])).ReflectionName);
 
136
                }
 
137
                
 
138
                [Test]
 
139
                public void TestToTypeReferencePointerType()
 
140
                {
 
141
                        Assert.AreEqual(typeof(int*).FullName,
 
142
                                        compilation.FindType(typeof(int*)).ReflectionName);
 
143
                }
 
144
                
 
145
                [Test]
 
146
                public void TestToTypeReferenceByReferenceType()
 
147
                {
 
148
                        Assert.AreEqual(typeof(int).MakeByRefType().FullName,
 
149
                                        compilation.FindType(typeof(int).MakeByRefType()).ReflectionName);
 
150
                }
 
151
                
 
152
                [Test]
 
153
                public void TestToTypeReferenceGenericType()
 
154
                {
 
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);
 
166
                }
 
167
                
 
168
                [Test]
 
169
                public void ParseReflectionName()
 
170
                {
 
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);
 
180
                }
 
181
                
 
182
                [Test]
 
183
                public void ParseOpenGenericReflectionName()
 
184
                {
 
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);
 
189
                }
 
190
                
 
191
                [Test]
 
192
                public void ArrayOfTypeParameter()
 
193
                {
 
194
                        var context = new SimpleTypeResolveContext(compilation.MainAssembly);
 
195
                        Assert.AreEqual("`0[,]", ReflectionHelper.ParseReflectionName("`0[,]").Resolve(context).ReflectionName);
 
196
                }
 
197
                
 
198
                [Test, ExpectedException(typeof(ArgumentNullException))]
 
199
                public void ParseNullReflectionName()
 
200
                {
 
201
                        ReflectionHelper.ParseReflectionName(null);
 
202
                }
 
203
                
 
204
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
205
                public void ParseInvalidReflectionName1()
 
206
                {
 
207
                        ReflectionHelper.ParseReflectionName(string.Empty);
 
208
                }
 
209
                
 
210
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
211
                public void ParseInvalidReflectionName2()
 
212
                {
 
213
                        ReflectionHelper.ParseReflectionName("`");
 
214
                }
 
215
                
 
216
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
217
                public void ParseInvalidReflectionName3()
 
218
                {
 
219
                        ReflectionHelper.ParseReflectionName("``");
 
220
                }
 
221
                
 
222
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
223
                public void ParseInvalidReflectionName4()
 
224
                {
 
225
                        ReflectionHelper.ParseReflectionName("System.Action`A");
 
226
                }
 
227
                
 
228
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
229
                public void ParseInvalidReflectionName5()
 
230
                {
 
231
                        ReflectionHelper.ParseReflectionName("System.Environment+");
 
232
                }
 
233
                
 
234
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
235
                public void ParseInvalidReflectionName5b()
 
236
                {
 
237
                        ReflectionHelper.ParseReflectionName("System.Environment+`");
 
238
                }
 
239
                
 
240
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
241
                public void ParseInvalidReflectionName6()
 
242
                {
 
243
                        ReflectionHelper.ParseReflectionName("System.Int32[");
 
244
                }
 
245
                
 
246
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
247
                public void ParseInvalidReflectionName7()
 
248
                {
 
249
                        ReflectionHelper.ParseReflectionName("System.Int32[`]");
 
250
                }
 
251
                
 
252
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
253
                public void ParseInvalidReflectionName8()
 
254
                {
 
255
                        ReflectionHelper.ParseReflectionName("System.Int32[,");
 
256
                }
 
257
                
 
258
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
259
                public void ParseInvalidReflectionName9()
 
260
                {
 
261
                        ReflectionHelper.ParseReflectionName("System.Int32]");
 
262
                }
 
263
                
 
264
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
265
                public void ParseInvalidReflectionName10()
 
266
                {
 
267
                        ReflectionHelper.ParseReflectionName("System.Int32*a");
 
268
                }
 
269
                
 
270
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
271
                public void ParseInvalidReflectionName11()
 
272
                {
 
273
                        ReflectionHelper.ParseReflectionName("System.Action`1[[]]");
 
274
                }
 
275
                
 
276
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
277
                public void ParseInvalidReflectionName12()
 
278
                {
 
279
                        ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32]a]");
 
280
                }
 
281
                
 
282
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
283
                public void ParseInvalidReflectionName13()
 
284
                {
 
285
                        ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32],]");
 
286
                }
 
287
                
 
288
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
289
                public void ParseInvalidReflectionName14()
 
290
                {
 
291
                        ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32]");
 
292
                }
 
293
                
 
294
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
295
                public void ParseInvalidReflectionName15()
 
296
                {
 
297
                        ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32");
 
298
                }
 
299
                
 
300
                [Test, ExpectedException(typeof(ReflectionNameParseException))]
 
301
                public void ParseInvalidReflectionName16()
 
302
                {
 
303
                        ReflectionHelper.ParseReflectionName("System.Action`1[[System.Int32],[System.String");
 
304
                }
 
305
        }
 
306
}