提交 b9f84c15 编写于 作者: C CyrusNajmabadi

Add tests.

上级 92fba2bd
......@@ -537,9 +537,7 @@ public Location ReadLocation()
}
public ImmutableArray<Location> ReadLocationArray()
{
return ReadArray(_readLocation);
}
=> ReadArray(_readLocation);
#endregion
}
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
......@@ -14,7 +16,6 @@ public static void Create(INamedTypeSymbol symbol, SymbolKeyWriter visitor)
{
Debug.Assert(symbol.IsTupleType);
var friendlyNames = ArrayBuilder<string>.GetInstance();
var locations = ArrayBuilder<Location>.GetInstance();
......@@ -54,7 +55,7 @@ public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
{
var elementTypes = reader.ReadSymbolKeyArray().SelectAsArray(r => r.GetAnySymbol() as ITypeSymbol);
var elementNames = reader.ReadStringArray();
var elementLocations = reader.ReadLocationArray();
var elementLocations = ReadElementLocations(reader);
if (!elementTypes.Any(t => t == null))
{
......@@ -73,7 +74,8 @@ public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
{
var underlyingTypeResolution = reader.ReadSymbolKey();
var elementNames = reader.ReadStringArray();
var elementLocations = reader.ReadLocationArray();
var elementLocations = ReadElementLocations(reader);
try
{
var result = GetAllSymbols<INamedTypeSymbol>(underlyingTypeResolution).Select(
......@@ -87,6 +89,19 @@ public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
return new SymbolKeyResolution(reader.Compilation.ObjectType);
}
private static ImmutableArray<Location> ReadElementLocations(SymbolKeyReader reader)
{
// Compiler API requires that all the locations are non-null, or that there is a default
// immutable array passed in.
var elementLocations = reader.ReadLocationArray();
if (elementLocations.All(loc => loc == null))
{
elementLocations = default(ImmutableArray<Location>);
}
return elementLocations;
}
}
}
}
\ No newline at end of file
......@@ -647,6 +647,74 @@ object LocalFunction<T>()
Assert.True(tested);
}
[Fact, WorkItem(17702, "https://github.com/dotnet/roslyn/issues/17702")]
public void TestTupleWithLocalTypeReferences1()
{
var source = @"
using System.Linq;
class C
{
void Method((C, int) t)
{
}
}";
// Tuples store locations along with them. But we can only recover those locations
// if we're re-resolving into a compilation with the same files.
var compilation1 = GetCompilation(source, LanguageNames.CSharp, "File1.cs");
var compilation2 = GetCompilation(source, LanguageNames.CSharp, "File2.cs");
var symbol = GetAllSymbols(
compilation1.GetSemanticModel(compilation1.SyntaxTrees.Single()),
n => n is CSharp.Syntax.MethodDeclarationSyntax).Single();
// Ensure we don't crash getting these symbol keys.
var id = SymbolKey.ToString(symbol);
Assert.NotNull(id);
var found = SymbolKey.Resolve(id, compilation2).GetAnySymbol();
Assert.NotNull(found);
Assert.Equal(symbol.Name, found.Name);
Assert.Equal(symbol.Kind, found.Kind);
var method = found as IMethodSymbol;
Assert.True(method.Parameters[0].Type.IsTupleType);
}
[Fact, WorkItem(17702, "https://github.com/dotnet/roslyn/issues/17702")]
public void TestTupleWithLocalTypeReferences2()
{
var source = @"
using System.Linq;
class C
{
void Method((C a, int b) t)
{
}
}";
// Tuples store locations along with them. But we can only recover those locations
// if we're re-resolving into a compilation with the same files.
var compilation1 = GetCompilation(source, LanguageNames.CSharp, "File1.cs");
var compilation2 = GetCompilation(source, LanguageNames.CSharp, "File2.cs");
var symbol = GetAllSymbols(
compilation1.GetSemanticModel(compilation1.SyntaxTrees.Single()),
n => n is CSharp.Syntax.MethodDeclarationSyntax).Single();
// Ensure we don't crash getting these symbol keys.
var id = SymbolKey.ToString(symbol);
Assert.NotNull(id);
var found = SymbolKey.Resolve(id, compilation2).GetAnySymbol();
Assert.NotNull(found);
Assert.Equal(symbol.Name, found.Name);
Assert.Equal(symbol.Kind, found.Kind);
var method = found as IMethodSymbol;
Assert.True(method.Parameters[0].Type.IsTupleType);
}
private void TestRoundTrip(IEnumerable<ISymbol> symbols, Compilation compilation, Func<ISymbol, object> fnId = null)
{
foreach (var symbol in symbols)
......@@ -674,7 +742,7 @@ private void TestRoundTrip(ISymbol symbol, Compilation compilation, Func<ISymbol
}
}
private Compilation GetCompilation(string source, string language)
private Compilation GetCompilation(string source, string language, string path = "")
{
var references = new[]
{
......@@ -684,12 +752,12 @@ private Compilation GetCompilation(string source, string language)
if (language == LanguageNames.CSharp)
{
var tree = CSharp.SyntaxFactory.ParseSyntaxTree(source);
var tree = CSharp.SyntaxFactory.ParseSyntaxTree(source, path: path);
return CSharp.CSharpCompilation.Create("Test", syntaxTrees: new[] { tree }, references: references);
}
else if (language == LanguageNames.VisualBasic)
{
var tree = VisualBasic.SyntaxFactory.ParseSyntaxTree(source);
var tree = VisualBasic.SyntaxFactory.ParseSyntaxTree(source, path: path);
return VisualBasic.VisualBasicCompilation.Create("Test", syntaxTrees: new[] { tree }, references: references);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册