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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/Resolver/MemberLookupTests.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.IO;
 
21
using System.Linq;
 
22
using ICSharpCode.NRefactory.CSharp.TypeSystem;
 
23
using ICSharpCode.NRefactory.Semantics;
 
24
using ICSharpCode.NRefactory.TypeSystem;
 
25
using ICSharpCode.NRefactory.TypeSystem.Implementation;
 
26
using NUnit.Framework;
 
27
 
 
28
namespace ICSharpCode.NRefactory.CSharp.Resolver
 
29
{
 
30
        [TestFixture]
 
31
        public class MemberLookupTests : ResolverTestBase
 
32
        {
 
33
                MemberLookup lookup;
 
34
                
 
35
                public override void SetUp()
 
36
                {
 
37
                        base.SetUp();
 
38
                        lookup = new MemberLookup(null, compilation.MainAssembly);
 
39
                }
 
40
                
 
41
                CSharpUnresolvedFile Parse(string program)
 
42
                {
 
43
                        SyntaxTree syntaxTree = SyntaxTree.Parse(program, "test.cs");
 
44
                        CSharpUnresolvedFile unresolvedFile = syntaxTree.ToTypeSystem();
 
45
                        project = project.AddOrUpdateFiles(unresolvedFile);
 
46
                        compilation = project.CreateCompilation();
 
47
                        lookup = new MemberLookup(null, compilation.MainAssembly);
 
48
                        return unresolvedFile;
 
49
                }
 
50
                
 
51
                [Test]
 
52
                public void GroupMethodsByDeclaringType()
 
53
                {
 
54
                        string program = @"
 
55
class Base {
 
56
        public virtual void Method() {}
 
57
}
 
58
class Middle : Base {
 
59
        public void Method(int p) {}
 
60
}
 
61
class Derived : Middle {
 
62
        public override void Method() {}
 
63
}";
 
64
                        var unresolvedFile = Parse(program);
 
65
                        ITypeDefinition derived = compilation.MainAssembly.GetTypeDefinition(new TopLevelTypeName("Derived"));
 
66
                        var rr = lookup.Lookup(new ResolveResult(derived), "Method", EmptyList<IType>.Instance, true) as MethodGroupResolveResult;
 
67
                        Assert.AreEqual(2, rr.MethodsGroupedByDeclaringType.Count());
 
68
                        
 
69
                        var baseGroup = rr.MethodsGroupedByDeclaringType.ElementAt(0);
 
70
                        Assert.AreEqual("Base", baseGroup.DeclaringType.ReflectionName);
 
71
                        Assert.AreEqual(1, baseGroup.Count);
 
72
                        Assert.AreEqual("Derived.Method", baseGroup[0].FullName);
 
73
                        
 
74
                        var middleGroup = rr.MethodsGroupedByDeclaringType.ElementAt(1);
 
75
                        Assert.AreEqual("Middle", middleGroup.DeclaringType.ReflectionName);
 
76
                        Assert.AreEqual(1, middleGroup.Count);
 
77
                        Assert.AreEqual("Middle.Method", middleGroup[0].FullName);
 
78
                }
 
79
                
 
80
                [Test]
 
81
                public void MethodInGenericClassOverriddenByConcreteMethod()
 
82
                {
 
83
                        string program = @"
 
84
class Base<T> {
 
85
        public virtual void Method(T a) {}
 
86
}
 
87
class Derived : Base<int> {
 
88
        public override void Method(int a) {}
 
89
        public override void Method(string a) {}
 
90
}";
 
91
                        var unresolvedFile = Parse(program);
 
92
                        ITypeDefinition derived = compilation.MainAssembly.GetTypeDefinition(new TopLevelTypeName("Derived"));
 
93
                        var rr = lookup.Lookup(new ResolveResult(derived), "Method", EmptyList<IType>.Instance, true) as MethodGroupResolveResult;
 
94
                        Assert.AreEqual(2, rr.MethodsGroupedByDeclaringType.Count());
 
95
                        
 
96
                        var baseGroup = rr.MethodsGroupedByDeclaringType.ElementAt(0);
 
97
                        Assert.AreEqual("Base`1[[System.Int32]]", baseGroup.DeclaringType.ReflectionName);
 
98
                        Assert.AreEqual(1, baseGroup.Count);
 
99
                        Assert.AreEqual("Derived.Method", baseGroup[0].FullName);
 
100
                        Assert.AreEqual("System.Int32", baseGroup[0].Parameters[0].Type.ReflectionName);
 
101
                        
 
102
                        var derivedGroup = rr.MethodsGroupedByDeclaringType.ElementAt(1);
 
103
                        Assert.AreEqual("Derived", derivedGroup.DeclaringType.ReflectionName);
 
104
                        Assert.AreEqual(1, derivedGroup.Count);
 
105
                        Assert.AreEqual("Derived.Method", derivedGroup[0].FullName);
 
106
                        Assert.AreEqual("System.String", derivedGroup[0].Parameters[0].Type.ReflectionName);
 
107
                }
 
108
                
 
109
                [Test]
 
110
                public void GenericMethod()
 
111
                {
 
112
                        string program = @"
 
113
class Base {
 
114
        public virtual void Method<T>(T a) {}
 
115
}
 
116
class Derived : Base {
 
117
        public override void Method<S>(S a) {}
 
118
}";
 
119
                        var unresolvedFile = Parse(program);
 
120
                        ITypeDefinition derived = compilation.MainAssembly.GetTypeDefinition(new TopLevelTypeName("Derived"));
 
121
                        var rr = lookup.Lookup(new ResolveResult(derived), "Method", EmptyList<IType>.Instance, true) as MethodGroupResolveResult;
 
122
                        Assert.AreEqual(1, rr.MethodsGroupedByDeclaringType.Count());
 
123
                        
 
124
                        var baseGroup = rr.MethodsGroupedByDeclaringType.ElementAt(0);
 
125
                        Assert.AreEqual("Base", baseGroup.DeclaringType.ReflectionName);
 
126
                        Assert.AreEqual(1, baseGroup.Count);
 
127
                        Assert.AreEqual("Derived.Method", baseGroup[0].FullName);
 
128
                        Assert.AreEqual("``0", baseGroup[0].Parameters[0].Type.ReflectionName);
 
129
                }
 
130
                
 
131
                [Test]
 
132
                public void InstanceFieldImplicitThis()
 
133
                {
 
134
                        string program = @"class Test {
 
135
        public int Field;
 
136
        int M() { return $Field$; }
 
137
}";
 
138
                        var rr = Resolve<MemberResolveResult>(program);
 
139
                        Assert.AreEqual("Test.Field", rr.Member.FullName);
 
140
                        Assert.IsTrue(rr.TargetResult is ThisResolveResult);
 
141
                }
 
142
                
 
143
                [Test]
 
144
                public void InstanceFieldExplicitThis()
 
145
                {
 
146
                        string program = @"class Test {
 
147
        public int Field;
 
148
        int M() { return $this.Field$; }
 
149
}";
 
150
                        var rr = Resolve<MemberResolveResult>(program);
 
151
                        Assert.AreEqual("Test.Field", rr.Member.FullName);
 
152
                        Assert.IsTrue(rr.TargetResult is ThisResolveResult);
 
153
                }
 
154
                
 
155
                [Test]
 
156
                public void StaticField()
 
157
                {
 
158
                        string program = @"class Test {
 
159
        public static int Field;
 
160
        int M() { return $Field$; }
 
161
}";
 
162
                        var rr = Resolve<MemberResolveResult>(program);
 
163
                        Assert.AreEqual("Test.Field", rr.Member.FullName);
 
164
                        Assert.IsTrue(rr.TargetResult is TypeResolveResult);
 
165
                }
 
166
                
 
167
                [Test]
 
168
                public void InstanceMethodImplicitThis()
 
169
                {
 
170
                        string program = @"using System;
 
171
class Test {
 
172
        void F() {}
 
173
        public void M() {
 
174
                $F()$;
 
175
        }
 
176
}";
 
177
                        var rr = Resolve<CSharpInvocationResolveResult>(program);
 
178
                        Assert.AreEqual("Test.F", rr.Member.FullName);
 
179
                        Assert.IsInstanceOf<ThisResolveResult>(rr.TargetResult);
 
180
                }
 
181
                
 
182
                [Test]
 
183
                public void InstanceMethodExplicitThis()
 
184
                {
 
185
                        string program = @"using System;
 
186
class Test {
 
187
        void F() {}
 
188
        public void M() {
 
189
                $this.F()$;
 
190
        }
 
191
}";
 
192
                        var rr = Resolve<CSharpInvocationResolveResult>(program);
 
193
                        Assert.AreEqual("Test.F", rr.Member.FullName);
 
194
                        Assert.IsInstanceOf<ThisResolveResult>(rr.TargetResult);
 
195
                }
 
196
                
 
197
                [Test]
 
198
                public void StaticMethod()
 
199
                {
 
200
                        string program = @"using System;
 
201
class Test {
 
202
        static void F() {}
 
203
        public void M() {
 
204
                $F()$;
 
205
        }
 
206
}";
 
207
                        var rr = Resolve<CSharpInvocationResolveResult>(program);
 
208
                        Assert.AreEqual("Test.F", rr.Member.FullName);
 
209
                        Assert.IsInstanceOf<TypeResolveResult>(rr.TargetResult);
 
210
                }
 
211
                
 
212
                [Test]
 
213
                public void TestOuterTemplateParameter()
 
214
                {
 
215
                        string program = @"public class A<T>
 
216
{
 
217
        public class B
 
218
        {
 
219
                public T field;
 
220
        }
 
221
}
 
222
 
 
223
public class Foo
 
224
{
 
225
        public void Bar ()
 
226
        {
 
227
                A<int>.B baz = new A<int>.B ();
 
228
                $baz.field$.ToString ();
 
229
        }
 
230
}";
 
231
                        var lrr = Resolve<MemberResolveResult>(program);
 
232
                        Assert.AreEqual("System.Int32", lrr.Type.FullName);
 
233
                }
 
234
                
 
235
                [Test]
 
236
                public void TestOuterTemplateParameterInDerivedClass()
 
237
                {
 
238
                        string program = @"public class A<T>
 
239
{
 
240
        public class B
 
241
        {
 
242
                public T field;
 
243
        }
 
244
}
 
245
 
 
246
public class Foo : A<int>.B
 
247
{
 
248
        public void Bar ()
 
249
        {
 
250
                $field$.ToString ();
 
251
        }
 
252
}";
 
253
                        var lrr = Resolve<MemberResolveResult>(program);
 
254
                        Assert.AreEqual("System.Int32", lrr.Type.FullName);
 
255
                }
 
256
                
 
257
                [Test]
 
258
                public void TestOuterTemplateParameterInDerivedClass2()
 
259
                {
 
260
                        string program = @"public class A<T>
 
261
{
 
262
        public class B
 
263
        {
 
264
                public T field;
 
265
        }
 
266
}
 
267
 
 
268
public class Foo : A<int>
 
269
{
 
270
        public void Bar (B v)
 
271
        {
 
272
                $v.field$.ToString ();
 
273
        }
 
274
}";
 
275
                        var lrr = Resolve<MemberResolveResult>(program);
 
276
                        Assert.AreEqual("System.Int32", lrr.Type.FullName);
 
277
                }
 
278
                
 
279
                [Test]
 
280
                public void MemberInGenericClassReferringToInnerClass()
 
281
                {
 
282
                        string program = @"public class Foo<T> {
 
283
        public class TestFoo { }
 
284
        public TestFoo Bar = new TestFoo ();
 
285
}
 
286
public class Test {
 
287
        public void SomeMethod (Foo<Test> foo) {
 
288
                var f = $foo.Bar$;
 
289
        }
 
290
}";
 
291
                        var mrr = Resolve<MemberResolveResult>(program);
 
292
                        Assert.AreEqual("Foo`1+TestFoo[[Test]]", mrr.Type.ReflectionName);
 
293
                }
 
294
                
 
295
                [Test]
 
296
                public void ProtectedBaseMethodCall()
 
297
                {
 
298
                        string program = @"using System;
 
299
public class Base {
 
300
        protected virtual void M() {}
 
301
}
 
302
public class Test : Base {
 
303
        protected override void M() {
 
304
                $base.M()$;
 
305
        }
 
306
}";
 
307
                        var rr = Resolve<CSharpInvocationResolveResult>(program);
 
308
                        Assert.IsFalse(rr.IsError);
 
309
                        Assert.AreEqual("Base.M", rr.Member.FullName);
 
310
                }
 
311
                
 
312
                [Test]
 
313
                public void ProtectedBaseFieldAccess()
 
314
                {
 
315
                        string program = @"using System;
 
316
public class Base {
 
317
        protected int Field;
 
318
}
 
319
public class Test : Base {
 
320
        public new int Field;
 
321
        protected override void M() {
 
322
                $base.Field$ = 1;
 
323
        }
 
324
}";
 
325
                        var rr = Resolve<MemberResolveResult>(program);
 
326
                        Assert.IsFalse(rr.IsError);
 
327
                        Assert.AreEqual("Base.Field", rr.Member.FullName);
 
328
                }
 
329
                
 
330
                [Test]
 
331
                public void ThisHasSameTypeAsFieldInGenericClass()
 
332
                {
 
333
                        string program = @"using System;
 
334
public struct C<T> {
 
335
        public C(C<T> other) {
 
336
                $M(this, other)$;
 
337
        }
 
338
        static void M<T>(T a, T b) {}
 
339
}
 
340
";
 
341
                        var rr = Resolve<CSharpInvocationResolveResult>(program);
 
342
                        Assert.IsFalse(rr.IsError);
 
343
                        Assert.AreEqual("C`1[[`0]]", rr.Arguments[0].Type.ReflectionName);
 
344
                        Assert.AreEqual("C`1[[`0]]", rr.Arguments[1].Type.ReflectionName);
 
345
                }
 
346
                
 
347
                [Test]
 
348
                public void ProtectedFieldInOuterClass()
 
349
                {
 
350
                        string program = @"using System;
 
351
class Base {
 
352
  protected int X;
 
353
}
 
354
class Derived : Base {
 
355
  class Inner {
 
356
     public int M(Derived d) { return $d.X$; }
 
357
}}";
 
358
                        var rr = Resolve<MemberResolveResult>(program);
 
359
                        Assert.IsFalse(rr.IsError);
 
360
                        Assert.AreEqual("Base.X", rr.Member.FullName);
 
361
                }
 
362
                
 
363
                [Test]
 
364
                public void ProtectedMemberViaTypeParameter()
 
365
                {
 
366
                        string program = @"using System;
 
367
class Base
 
368
{
 
369
        protected void Test() {}
 
370
        public void Test(int a = 0) {}
 
371
}
 
372
class Derived<T> : Base where T : Derived<T>
 
373
{
 
374
        void M(Derived<T> a, Base b, T c) {
 
375
                a.Test(); // calls Test()
 
376
                b.Test(); // calls Test(int)
 
377
                c.Test(); // calls Test()
 
378
        }
 
379
}";
 
380
                        var rr = Resolve<CSharpInvocationResolveResult>(program.Replace("a.Test()", "$a.Test()$"));
 
381
                        Assert.IsFalse(rr.IsError);
 
382
                        Assert.AreEqual(0, rr.Member.Parameters.Count);
 
383
                        
 
384
                        rr = Resolve<CSharpInvocationResolveResult>(program.Replace("b.Test()", "$b.Test()$"));
 
385
                        Assert.IsFalse(rr.IsError);
 
386
                        Assert.AreEqual(1, rr.Member.Parameters.Count);
 
387
                        
 
388
                        rr = Resolve<CSharpInvocationResolveResult>(program.Replace("c.Test()", "$c.Test()$"));
 
389
                        Assert.IsFalse(rr.IsError);
 
390
                        Assert.AreEqual(0, rr.Member.Parameters.Count);
 
391
                }
 
392
                
 
393
                [Test]
 
394
                public void MethodGroupConversionForGenericMethodHasSpecializedMethod()
 
395
                {
 
396
                        string program = @"using System;
 
397
class TestClass {
 
398
        void F<T>(T x) {}
 
399
        public void M() {
 
400
                System.Action<int> f;
 
401
                f = $F$;
 
402
        }
 
403
}";
 
404
                        var conversion = GetConversion(program);
 
405
                        Assert.IsTrue(conversion.IsValid);
 
406
                        Assert.IsTrue(conversion.IsMethodGroupConversion);
 
407
                        Assert.IsTrue(conversion.Method.IsParameterized);
 
408
                        Assert.AreEqual(
 
409
                                new[] { "System.Int32" },
 
410
                                conversion.Method.TypeArguments.Select(t => t.ReflectionName).ToArray());
 
411
                }
 
412
                
 
413
                [Test]
 
414
                public void PartialMethod_WithoutImplementation()
 
415
                {
 
416
                        string program = @"using System;
 
417
class TestClass {
 
418
        $partial void M();$
 
419
}";
 
420
                        var mrr = Resolve<MemberResolveResult>(program);
 
421
                        Assert.AreEqual("TestClass.M", mrr.Member.FullName);
 
422
                }
 
423
                
 
424
                [Test]
 
425
                public void PartialMethod_Declaration()
 
426
                {
 
427
                        string program = @"using System;
 
428
class TestClass {
 
429
        $partial void M();$
 
430
        
 
431
        partial void M() {}
 
432
}";
 
433
                        var mrr = Resolve<MemberResolveResult>(program);
 
434
                        Assert.AreEqual("TestClass.M", mrr.Member.FullName);
 
435
                }
 
436
                
 
437
                [Test]
 
438
                public void PartialMethod_Implementation()
 
439
                {
 
440
                        string program = @"using System;
 
441
class TestClass {
 
442
        partial void M();
 
443
        
 
444
        $partial void M() {}$
 
445
}";
 
446
                        var mrr = Resolve<MemberResolveResult>(program);
 
447
                        Assert.AreEqual("TestClass.M", mrr.Member.FullName);
 
448
                }
 
449
                
 
450
                [Test]
 
451
                public void MembersWithoutWhitespace()
 
452
                {
 
453
                        string program = @"using System;
 
454
class TestClass {
 
455
        void A();$void B();$void C();
 
456
}";
 
457
                        var mrr = Resolve<MemberResolveResult>(program);
 
458
                        Assert.AreEqual("TestClass.B", mrr.Member.FullName);
 
459
                }
 
460
                
 
461
                [Test]
 
462
                public void GenericClassDoesNotHideField()
 
463
                {
 
464
                        string program = @"using System;
 
465
class A { public int F; }
 
466
class B : A { public class F<T> {} }
 
467
class C : B {
 
468
        public void M()
 
469
        {
 
470
                $F$ = 1;
 
471
        }
 
472
}";
 
473
                        var mrr = Resolve<MemberResolveResult>(program);
 
474
                        Assert.AreEqual("A.F", mrr.Member.FullName);
 
475
                }
 
476
                
 
477
                [Test]
 
478
                public void NonGenericClassHidesField_WithExplicitThisAccess()
 
479
                {
 
480
                        string program = @"using System;
 
481
class A { public int F; }
 
482
class B : A { public class F {} }
 
483
class C : B {
 
484
        public void M()
 
485
        {
 
486
                $this.F$ = 1;
 
487
        }
 
488
}";
 
489
                        var trr = Resolve<TypeResolveResult>(program);
 
490
                        Assert.AreEqual("B+F", trr.Type.ReflectionName);
 
491
                }
 
492
 
 
493
                /// <summary>
 
494
                /// Bug 9604 - Completion problem with indexers
 
495
                /// </summary>
 
496
                [Test]
 
497
                public void TestBug9604()
 
498
                {
 
499
                        string program = @"class Item
 
500
{
 
501
    public static int Foo = 42;
 
502
 
 
503
    public class Builder
 
504
    {
 
505
        public int Foo
 
506
        {
 
507
            get { return $Item.Foo$; }
 
508
        }
 
509
 
 
510
        public object this[int field, int i]
 
511
        {
 
512
            get { return null; }
 
513
        }
 
514
    }
 
515
}
 
516
";
 
517
                        var result = Resolve<MemberResolveResult>(program);
 
518
                        Assert.AreEqual("Item.Foo", result.Member.FullName);
 
519
                }
 
520
 
 
521
                [Test]
 
522
                public void Test9604OperatorCase()
 
523
                {
 
524
                        string program = @"class op_Addition
 
525
{
 
526
    public static int Foo = 42;
 
527
 
 
528
    public class Builder
 
529
    {
 
530
        public int Foo
 
531
        {
 
532
            get { return $op_Addition.Foo$; }
 
533
        }
 
534
 
 
535
        public static int operator + (Builder a, Builder b) 
 
536
                {
 
537
                        return 0;
 
538
                }
 
539
    }
 
540
}
 
541
";
 
542
                        var result = Resolve<MemberResolveResult>(program);
 
543
                        Assert.AreEqual("op_Addition.Foo", result.Member.FullName);
 
544
                }
 
545
 
 
546
                /// <summary>
 
547
                /// Bug 10201 - Wrong generics expansion for base recursive types
 
548
                /// </summary>
 
549
                [Test]
 
550
                public void TestBug10201()
 
551
                {
 
552
                        string program = @"public interface IA<T>
 
553
{
 
554
}
 
555
public class G<U, V> : IA<$G<V, string>$> 
 
556
{}
 
557
";
 
558
                        var rr = Resolve<TypeResolveResult>(program);
 
559
                        var baseType = rr.Type.DirectBaseTypes.First().TypeArguments.First () as ParameterizedType;
 
560
                        Assert.AreEqual("G", baseType.Name);
 
561
                        
 
562
                        Assert.AreEqual(2, baseType.TypeParameterCount);
 
563
                        Assert.AreEqual("System.String", baseType.TypeArguments [0].FullName);
 
564
                        Assert.AreEqual("System.String", baseType.TypeArguments [1].FullName);
 
565
                }
 
566
        }
 
567
}