提交 cbe906ce 编写于 作者: N Neal Gafter

Merge pull request #7472 from mavasani/UnitTests_7446

Add a skipped unit tests for #7446
// 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.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Test.Utilities;
using Xunit;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.CSharp.UnitTests
{
......@@ -124,5 +126,141 @@ public void Foo()
Assert.Equal(DiagnosticSeverity.Warning, info.DefaultSeverity);
Assert.Equal(4, info.WarningLevel);
}
[Fact(Skip ="7446"), WorkItem(7446, "https://github.com/dotnet/roslyn/issues/7446")]
public void TestCompilationEventQueueWithSemanticModelGetDiagnostics()
{
var source1 = @"
namespace N1
{
partial class Class
{
private void NonPartialMethod1() { }
}
}
";
var source2 = @"
namespace N1
{
partial class Class
{
private void NonPartialMethod2() { }
}
}
";
var tree1 = CSharpSyntaxTree.ParseText(source1, path: "file1");
var tree2 = CSharpSyntaxTree.ParseText(source2, path: "file2");
var eventQueue = new AsyncQueue<CompilationEvent>();
var compilation = CreateCompilationWithMscorlib45(new[] { tree1, tree2 }).WithEventQueue(eventQueue);
// Invoke SemanticModel.GetDiagnostics to force populate the event queue for symbols in the first source file.
var tree = compilation.SyntaxTrees.Single(t => t == tree1);
var root = tree.GetRoot();
var model = compilation.GetSemanticModel(tree);
model.GetDiagnostics(root.FullSpan);
Assert.True(eventQueue.Count > 0);
bool compilationStartedFired;
HashSet<string> declaredSymbolNames, completedCompilationUnits;
Assert.True(DequeueCompilationEvents(eventQueue, out compilationStartedFired, out declaredSymbolNames, out completedCompilationUnits));
// Verify symbol declared events fired for all symbols declared in the first source file.
Assert.True(compilationStartedFired);
Assert.True(declaredSymbolNames.Contains(compilation.GlobalNamespace.Name));
Assert.True(declaredSymbolNames.Contains("N1"));
Assert.True(declaredSymbolNames.Contains("Class"));
Assert.True(declaredSymbolNames.Contains("NonPartialMethod1"));
Assert.True(completedCompilationUnits.Contains(tree.FilePath));
}
[Fact(Skip = "7446"), WorkItem(7446, "https://github.com/dotnet/roslyn/issues/7446")]
public void TestCompilationEventsForPartialMethod()
{
var source1 = @"
namespace N1
{
partial class Class
{
private void NonPartialMethod1() { }
partial void PartialMethod();
}
}
";
var source2 = @"
namespace N1
{
partial class Class
{
private void NonPartialMethod2() { }
partial void PartialMethod() { }
}
}
";
var tree1 = CSharpSyntaxTree.ParseText(source1, path: "file1");
var tree2 = CSharpSyntaxTree.ParseText(source2, path: "file2");
var eventQueue = new AsyncQueue<CompilationEvent>();
var compilation = CreateCompilationWithMscorlib45(new[] { tree1, tree2 }).WithEventQueue(eventQueue);
// Invoke SemanticModel.GetDiagnostics to force populate the event queue for symbols in the first source file.
var tree = compilation.SyntaxTrees.Single(t => t == tree1);
var root = tree.GetRoot();
var model = compilation.GetSemanticModel(tree);
model.GetDiagnostics(root.FullSpan);
Assert.True(eventQueue.Count > 0);
bool compilationStartedFired;
HashSet<string> declaredSymbolNames, completedCompilationUnits;
Assert.True(DequeueCompilationEvents(eventQueue, out compilationStartedFired, out declaredSymbolNames, out completedCompilationUnits));
// Verify symbol declared events fired for all symbols declared in the first source file.
Assert.True(compilationStartedFired);
Assert.True(declaredSymbolNames.Contains(compilation.GlobalNamespace.Name));
Assert.True(declaredSymbolNames.Contains("N1"));
Assert.True(declaredSymbolNames.Contains("Class"));
Assert.True(declaredSymbolNames.Contains("NonPartialMethod1"));
Assert.True(declaredSymbolNames.Contains("PartialMethod"));
Assert.True(completedCompilationUnits.Contains(tree.FilePath));
}
private static bool DequeueCompilationEvents(AsyncQueue<CompilationEvent> eventQueue, out bool compilationStartedFired, out HashSet<string> declaredSymbolNames, out HashSet<string> completedCompilationUnits)
{
compilationStartedFired = false;
declaredSymbolNames = new HashSet<string>();
completedCompilationUnits = new HashSet<string>();
if (eventQueue.Count == 0)
{
return false;
}
CompilationEvent compEvent;
while (eventQueue.TryDequeue(out compEvent))
{
if (compEvent is CompilationStartedEvent)
{
Assert.False(compilationStartedFired, "Unexpected multiple compilation stated events");
compilationStartedFired = true;
}
else
{
var symbolDeclaredEvent = compEvent as SymbolDeclaredCompilationEvent;
if (symbolDeclaredEvent != null)
{
Assert.True(declaredSymbolNames.Add(symbolDeclaredEvent.Symbol.Name), "Unexpected multiple symbol declared events for same symbol");
}
else
{
var compilationCompeletedEvent = compEvent as CompilationUnitCompletedEvent;
if (compilationCompeletedEvent != null)
{
Assert.True(completedCompilationUnits.Add(compilationCompeletedEvent.CompilationUnit.FilePath));
}
}
}
}
return true;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册