SymbolKeyMetadataVsSourceTests.cs 14.0 KB
Newer Older
J
Jonathon Marolf 已提交
1 2 3
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.SymbolId
{
    public partial class SymbolKeyTest : SymbolKeyTestBase
    {
        #region "Metadata vs. Source"

20
        [Fact]
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
        public void M2SNamedTypeSymbols01()
        {
            var src1 = @"using System;

public delegate void D(int p1, string p2);

namespace N1.N2
{
    public interface I { }
    namespace N3
    {
        public class C 
        {
            public struct S 
            {
                public enum E { Zero, One, Two }
                public void M(int n) { Console.WriteLine(n); }
            }
        }
    }
}
";

            var src2 = @"using System;
using N1.N2.N3;

public class App : C
{
    private event D myEvent;
    internal N1.N2.I Prop { get; set; }
    protected C.S.E this[int x] { set { } }
    public void M(C.S s) { s.M(123); }
}
";

J
Jared Parsons 已提交
56
            var comp1 = CreateCompilation(src1);
57 58

            // Compilation to Compilation
59
            var comp2 = (Compilation)CreateCompilation(src2, new MetadataReference[] { new CSharpCompilationReference(comp1) });
60 61 62 63 64 65

            var originalSymbols = GetSourceSymbols(comp1, SymbolCategory.DeclaredType).OrderBy(s => s.Name).ToList();
            Assert.Equal(5, originalSymbols.Count);

            // ---------------------------
            // Metadata symbols
66
            var typesym = comp2.SourceModule.GlobalNamespace.GetTypeMembers("App").FirstOrDefault() as INamedTypeSymbol;
67 68

            // 'D'
69
            var member01 = (typesym.GetMembers("myEvent").Single() as IEventSymbol).Type;
70 71

            // 'I'
72
            var member02 = (typesym.GetMembers("Prop").Single() as IPropertySymbol).Type;
73 74

            // 'C'
75
            var member03 = typesym.BaseType;
76 77

            // 'S'
78
            var member04 = (typesym.GetMembers("M").Single() as IMethodSymbol).Parameters[0].Type;
79 80

            // 'E'
81
            var member05 = (typesym.GetMembers(WellKnownMemberNames.Indexer).Single() as IPropertySymbol).Type;
82

83 84 85 86 87
            ResolveAndVerifySymbol(member03, originalSymbols[0], comp1, SymbolKeyComparison.None);
            ResolveAndVerifySymbol(member01, originalSymbols[1], comp1, SymbolKeyComparison.None);
            ResolveAndVerifySymbol(member05, originalSymbols[2], comp1, SymbolKeyComparison.None);
            ResolveAndVerifySymbol(member02, originalSymbols[3], comp1, SymbolKeyComparison.None);
            ResolveAndVerifySymbol(member04, originalSymbols[4], comp1, SymbolKeyComparison.None);
88 89
        }

J
Jared Parsons 已提交
90
        [WorkItem(542700, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/542700")]
91
        [Fact]
92 93 94 95 96 97
        public void M2SNonTypeMemberSymbols01()
        {
            var src1 = @"using System;

namespace N1
{
98
    public interface IGoo
99 100 101 102 103 104 105 106 107 108 109
    {
        void M(int p1, int p2);
        void M(params short[] ary);

        void M(string p1);
        void M(ref string p1);
    }

    public struct S
    {
        public event Action<S> PublicEvent { add { } remove { } }
110
        public IGoo PublicField;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        public string PublicProp { get; set; }
        public short this[sbyte p] { get { return p; } }
    }
}
";

            var src2 = @"using System;
using AN = N1;

public class App
{
    static void Main()
    {
        var obj = new AN.S();

        /*<bind0>*/obj.PublicEvent/*</bind0>*/ += EH;

128
        var igoo = /*<bind1>*/obj.PublicField/*</bind1>*/;
129

130
        /*<bind3>*/igoo.M(/*<bind2>*/obj.PublicProp/*</bind2>*/)/*</bind3>*/;
131

132
        /*<bind5>*/igoo.M(obj[12], /*<bind4>*/obj[123]/*</bind4>*/)/*</bind5>*/;
133 134 135 136 137 138
    }

    static void EH(AN.S s) { }
}
";

J
Jared Parsons 已提交
139
            var comp1 = CreateCompilation(src1);
140 141

            // Compilation to Assembly
J
Jared Parsons 已提交
142
            var comp2 = CreateCompilation(src2, new MetadataReference[] { comp1.EmitToImageReference() });
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

            // ---------------------------
            // Source symbols
            var originalSymbols = GetSourceSymbols(comp1, SymbolCategory.NonTypeMember | SymbolCategory.Parameter).ToList();
            originalSymbols = originalSymbols.Where(s => !s.IsAccessor() && s.Kind != SymbolKind.Parameter).OrderBy(s => s.Name).Select(s => s).ToList();
            Assert.Equal(8, originalSymbols.Count);

            // ---------------------------
            // Metadata symbols
            var bindingtuples = GetBindingNodesAndModel<ExpressionSyntax>(comp2);
            var model = bindingtuples.Item2;
            var list = bindingtuples.Item1;
            Assert.Equal(6, list.Count);

            // event
158
            ResolveAndVerifySymbol(list[0], originalSymbols[4], model, comp1, SymbolKeyComparison.None);
159 160

            // field
161
            ResolveAndVerifySymbol(list[1], originalSymbols[5], model, comp1, SymbolKeyComparison.None);
162 163

            // prop
164
            ResolveAndVerifySymbol(list[2], originalSymbols[6], model, comp1, SymbolKeyComparison.None);
165 166

            // index:
167
            ResolveAndVerifySymbol(list[4], originalSymbols[7], model, comp1, SymbolKeyComparison.None);
168 169

            // M(string p1)
170
            ResolveAndVerifySymbol(list[3], originalSymbols[2], model, comp1, SymbolKeyComparison.None);
171 172

            // M(params short[] ary)
173
            ResolveAndVerifySymbol(list[5], originalSymbols[1], model, comp1, SymbolKeyComparison.None);
174 175 176 177 178 179
        }

        #endregion

        #region "Metadata vs. Metadata"

180
        [Fact]
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
        public void M2MMultiTargetingMsCorLib01()
        {
            var src1 = @"using System;
using System.IO;

public class A
{
    public FileInfo GetFileInfo(string path)
    {
        if (File.Exists(path))
        {
            return new FileInfo(path);
        }

        return null;
    }

    public void PrintInfo(Array ary, ref DateTime time)
    {
        if (ary != null)
            Console.WriteLine(ary);
        else
            Console.WriteLine(""null"");

        time = DateTime.Now;
    }
}
";

            var src2 = @"using System;

class Test
{
    static void Main()
    {
        var a = new A();
        var fi = a.GetFileInfo(null);
        Console.WriteLine(fi);

        var dt = DateTime.Now;
        var ary = Array.CreateInstance(typeof(string), 2);
        a.PrintInfo(ary, ref dt);
    }
}
";
226
            var comp20 = (Compilation)CreateEmptyCompilation(src1, new[] { TestReferences.NetFx.v4_0_21006.mscorlib });
227 228

            // "Compilation 2 Assembly"
229
            var comp40 = (Compilation)CreateCompilation(src2, new MetadataReference[] { comp20.EmitToImageReference() });
230 231

            var typeA = comp20.SourceModule.GlobalNamespace.GetTypeMembers("A").Single();
232 233
            var mem20_1 = typeA.GetMembers("GetFileInfo").Single() as IMethodSymbol;
            var mem20_2 = typeA.GetMembers("PrintInfo").Single() as IMethodSymbol;
234 235

            // FileInfo
236
            var mtsym20_1 = mem20_1.ReturnType;
237 238 239
            Assert.Equal(2, mem20_2.Parameters.Length);

            // Array
240
            var mtsym20_2 = mem20_2.Parameters[0].Type;
241 242

            // ref DateTime
243
            var mtsym20_3 = mem20_2.Parameters[1].Type;
244 245 246

            // ====================
            var typeTest = comp40.SourceModule.GlobalNamespace.GetTypeMembers("Test").FirstOrDefault();
247
            var mem40 = typeTest.GetMembers("Main").Single() as IMethodSymbol;
248
            var list = GetBlockSyntaxList(mem40);
249
            var model = comp40.GetSemanticModel(comp40.SyntaxTrees.First());
250 251 252 253

            foreach (var body in list)
            {
                var df = model.AnalyzeDataFlow(body.Statements.First(), body.Statements.Last());
254
                foreach (var local in df.VariablesDeclared)
255
                {
256
                    var localType = ((ILocalSymbol)local).Type;
257 258 259

                    if (local.Name == "fi")
                    {
260
                        ResolveAndVerifySymbol(localType, mtsym20_1, comp20, SymbolKeyComparison.None);
261 262 263
                    }
                    else if (local.Name == "ary")
                    {
264
                        ResolveAndVerifySymbol(localType, mtsym20_2, comp20, SymbolKeyComparison.None);
265 266
                    }
                    else if (local.Name == "dt")
267
                    {
268
                        ResolveAndVerifySymbol(localType, mtsym20_3, comp20, SymbolKeyComparison.None);
269 270 271 272 273
                    }
                }
            }
        }

J
Jared Parsons 已提交
274
        [Fact, WorkItem(546255, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/546255")]
275 276 277 278 279
        public void M2MMultiTargetingMsCorLib02()
        {
            var src1 = @"using System;
namespace Mscorlib20
{
280
    public interface IGoo
281 282 283 284 285 286 287
    {
        // interface
        IDisposable Prop { get; set; }
        // class
        Exception this[ArgumentException t] { get; }
    }

288
    public class CGoo : IGoo
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    {
        // enum
        public DayOfWeek PublicField;
        // delegate
        public event System.Threading.ParameterizedThreadStart PublicEventField;

        public IDisposable Prop { get; set; }
        public Exception this[ArgumentException t] { get { return t; } }
    }
}
";

            var src2 = @"using System;
using N20 = Mscorlib20;

class Test
{
    public IDisposable M()
    {
308 309
        var obj = new N20::CGoo();
        N20.IGoo igoo = obj;
310 311

        /*<bind0>*/obj.PublicEventField/*</bind0>*/ += /*<bind1>*/MyEveHandler/*</bind1>*/;
312
        var local = /*<bind2>*/igoo[null]/*</bind2>*/;
313 314 315

        if (/*<bind3>*/obj.PublicField /*</bind3>*/== DayOfWeek.Friday)
        {
316
            return /*<bind4>*/(obj as N20.IGoo).Prop/*</bind4>*/;
317 318 319 320 321 322 323
        }
        return null;
    }

    public void MyEveHandler(object o) { }
}
";
J
Jared Parsons 已提交
324
            var comp20 = CreateEmptyCompilation(src1, new[] { TestReferences.NetFx.v4_0_21006.mscorlib });
325 326

            // "Compilation ref Compilation"
J
Jared Parsons 已提交
327
            var comp40 = CreateCompilation(src2, new[] { new CSharpCompilationReference(comp20) });
328 329 330 331

            var originals = GetSourceSymbols(comp20, SymbolCategory.NonTypeMember | SymbolCategory.Parameter);
            var originalSymbols = originals.Where(s => !s.IsAccessor() && s.Kind != SymbolKind.Parameter).OrderBy(s => s.Name).ToList();

332
            // IGoo.Prop, CGoo.Prop, Event, Field, IGoo.This, CGoo.This
333 334 335 336 337 338 339 340 341 342 343 344
            Assert.Equal(6, originalSymbols.Count);

            // ====================
            var bindingtuples = GetBindingNodesAndModel<ExpressionSyntax>(comp40);
            var model = bindingtuples.Item2;
            var list = bindingtuples.Item1;
            Assert.Equal(5, list.Count);

            // PublicEventField
            ResolveAndVerifySymbol(list[0], originalSymbols[2], model, comp20);

            // delegate ParameterizedThreadStart
345
            ResolveAndVerifyTypeSymbol(list[0], (originalSymbols[2] as IEventSymbol).Type, model, comp20);
346 347

            // MethodGroup
348
            ResolveAndVerifyTypeSymbol(list[1], (originalSymbols[2] as IEventSymbol).Type, model, comp20);
349 350 351 352 353

            // Indexer
            ResolveAndVerifySymbol(list[2], originalSymbols[4], model, comp20);

            // class Exception
354
            ResolveAndVerifyTypeSymbol(list[2], (originalSymbols[4] as IPropertySymbol).Type, model, comp20);
355 356 357 358 359

            // PublicField
            ResolveAndVerifySymbol(list[3], originalSymbols[3], model, comp20);

            // enum DayOfWeek
360
            ResolveAndVerifyTypeSymbol(list[3], (originalSymbols[3] as IFieldSymbol).Type, model, comp20);
361 362 363 364 365

            // Prop
            ResolveAndVerifySymbol(list[4], originalSymbols[0], model, comp20);

            // interface IDisposable
366
            ResolveAndVerifyTypeSymbol(list[4], (originalSymbols[0] as IPropertySymbol).Type, model, comp20);
367 368
        }

J
Jared Parsons 已提交
369
        [Fact, WorkItem(546255, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/546255")]
370 371 372 373 374
        public void M2MMultiTargetingMsCorLib03()
        {
            var src1 = @"using System;
namespace Mscorlib20
{
375
    public interface IGoo
376 377 378 379 380 381 382
    {
        // interface
        IDisposable Prop { get; set; }
        // class
        Exception this[ArgumentException t] { get; }
    }

383
    public class CGoo : IGoo
384 385
    {
        // explicit
386 387
        IDisposable IGoo.Prop { get; set; }
        Exception IGoo.this[ArgumentException t] { get { return t; } }
388 389 390 391 392 393 394 395 396 397 398
    }
}
";

            var src2 = @"using System;
using N20 = Mscorlib20;

class Test
{
    public IDisposable M()
    {
399
        N20.IGoo igoo = new N20::CGoo();
400

401 402
        var local = /*<bind0>*/igoo[new ArgumentException()]/*</bind0>*/;
        return /*<bind1>*/igoo.Prop/*</bind1>*/;
403 404 405
    }
}
";
J
Jared Parsons 已提交
406
            var comp20 = CreateEmptyCompilation(src1, new[] { TestReferences.NetFx.v4_0_21006.mscorlib });
407 408

            // "Compilation ref Compilation"
J
Jared Parsons 已提交
409
            var comp40 = CreateCompilation(src2, new[] { new CSharpCompilationReference(comp20) });
410 411 412 413

            var originals = GetSourceSymbols(comp20, SymbolCategory.NonTypeMember | SymbolCategory.Parameter);
            var originalSymbols = originals.Where(s => !s.IsAccessor() && s.Kind != SymbolKind.Parameter).OrderBy(s => s.Name).ToList();

414
            // CGoo.Prop, CGoo.This, IGoo.Prop, IGoo.This
415 416 417 418 419 420 421 422 423 424 425 426
            Assert.Equal(4, originalSymbols.Count);

            // ====================
            var bindingtuples = GetBindingNodesAndModel<ExpressionSyntax>(comp40);
            var model = bindingtuples.Item2;
            var list = bindingtuples.Item1;
            Assert.Equal(2, list.Count);

            // Indexer
            ResolveAndVerifySymbol(list[0], originalSymbols[3], model, comp20);

            // class Exception
427
            ResolveAndVerifyTypeSymbol(list[0], (originalSymbols[3] as IPropertySymbol).Type, model, comp20);
428 429 430 431 432

            // Prop
            ResolveAndVerifySymbol(list[1], originalSymbols[2], model, comp20);

            // interface IDisposable
433
            ResolveAndVerifyTypeSymbol(list[1], (originalSymbols[2] as IPropertySymbol).Type, model, comp20);
434 435 436 437 438
        }

        #endregion
    }
}