-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix global keyword null exception (#9)
* Add global keyword test * Bypass global alias emit
- Loading branch information
Showing
4 changed files
with
194 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using LsifDotnet.Lsif; | ||
using LsifDotnet.Roslyn; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace LsifDotnet.Tests | ||
{ | ||
public class KeywordTest | ||
{ | ||
/// <summary> | ||
/// The adhoc workspace. | ||
/// </summary> | ||
private readonly AdhocWorkspace _adhocWorkspace; | ||
|
||
private readonly Project _project; | ||
|
||
private readonly ServiceProvider _serviceProvider; | ||
|
||
public KeywordTest() | ||
{ | ||
this._adhocWorkspace = new AdhocWorkspace(); | ||
this._project = AddProject(nameof(KeywordTest)); | ||
this._serviceProvider = new ServiceCollection() | ||
.AddLogging() | ||
.AddTransient<IdentifierCollectorFactory>() | ||
.AddTransient<LsifIndexer>() | ||
.AddSingleton(this._adhocWorkspace as Workspace) | ||
.BuildServiceProvider(); | ||
} | ||
|
||
private Project AddProject(string projectName) | ||
{ | ||
return this._adhocWorkspace.AddProject(ProjectInfo.Create(ProjectId.CreateNewId(), | ||
VersionStamp.Create(), | ||
projectName, projectName, "C#", metadataReferences: new[] | ||
{ | ||
MetadataReference.CreateFromFile(typeof(object).Assembly.Location) | ||
})); | ||
} | ||
|
||
[Fact] | ||
public async Task GlobalKeywordTest() | ||
{ | ||
var code1 = SourceText.From( | ||
@"// <autogenerated /> | ||
using System; | ||
using System.Reflection; | ||
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute("".NETCoreApp,Version=v6.0"", FrameworkDisplayName = """")]"); | ||
|
||
var code2 = SourceText.From(@"class Test : global::System.object {} // Adding extra global symbol"); | ||
|
||
this._adhocWorkspace.AddDocument(this._project.Id, $"{nameof(this.GlobalKeywordTest)}.1.cs", code1); | ||
this._adhocWorkspace.AddDocument(AddProject("SecondProject").Id, $"{nameof(this.GlobalKeywordTest)}.2.cs", code2); | ||
|
||
var indexer = this._serviceProvider.GetRequiredService<LsifIndexer>(); | ||
|
||
await foreach (var item in indexer.EmitLsif()) | ||
{ | ||
Assert.NotNull(item); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// See https://github.com/dotnet/roslyn/issues/58999 | ||
/// </summary> | ||
/// <returns></returns> | ||
[Fact] | ||
public async Task RoslynAliasEqualBugTest() | ||
{ | ||
{ | ||
var code1 = SourceText.From( | ||
@"// <autogenerated /> | ||
using System; | ||
using System.Reflection; | ||
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute("".NETCoreApp,Version=v6.0"", FrameworkDisplayName = """")]"); | ||
|
||
var code2 = SourceText.From(@"using global::System.IO; // Adding extra global symbol"); | ||
|
||
var doc1 = this._adhocWorkspace.AddDocument(this._project.Id, $"{nameof(this.GlobalKeywordTest)}.1.cs", | ||
code1); | ||
var doc2 = this._adhocWorkspace.AddDocument(this._project.Id, $"{nameof(this.GlobalKeywordTest)}.2.cs", | ||
code2); | ||
|
||
var globalSymbol1 = (await doc1.GetSemanticModelAsync())! | ||
.GetAliasInfo((await doc1.GetSyntaxRootAsync())! | ||
.DescendantNodes() | ||
.OfType<IdentifierNameSyntax>() | ||
.First(ident => ident.Identifier.Text == "global")); | ||
var globalSymbol2 = (await doc2.GetSemanticModelAsync())! | ||
.GetAliasInfo((await doc2.GetSyntaxRootAsync())! | ||
.DescendantNodes() | ||
.OfType<IdentifierNameSyntax>() | ||
.First(ident => ident.Identifier.Text == "global")); | ||
|
||
Assert.NotNull(globalSymbol1); | ||
Assert.NotNull(globalSymbol2); | ||
|
||
Assert.Throws<NullReferenceException>(() => globalSymbol1!.Equals(globalSymbol2)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\LsifDotnet\LsifDotnet.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters