提交 2b14447d 编写于 作者: C Cyrus Najmabadi

Unify tests

上级 184b238e
......@@ -2,25 +2,20 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Implementation.TodoComments;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.SolutionCrawler;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities.RemoteHost;
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities.TodoComments;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.TodoComment
{
[UseExportProvider]
public class TodoCommentTests
public class TodoCommentTests : AbstractTodoCommentTests
{
protected override TestWorkspace CreateWorkspace(string codeWithMarker)
=> TestWorkspace.CreateCSharp(codeWithMarker);
[Fact]
public async Task SingleLineTodoComment_Colon()
{
......@@ -170,46 +165,5 @@ public async Task SinglelineDocumentComment_Multiline()
await TestAsync(code);
}
private static async Task TestAsync(string codeWithMarker)
{
await TestAsync(codeWithMarker, remote: false);
await TestAsync(codeWithMarker, remote: true);
}
private static async Task TestAsync(string codeWithMarker, bool remote)
{
using var workspace = TestWorkspace.CreateCSharp(codeWithMarker, openDocuments: false);
workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options
.WithChangedOption(RemoteHostOptions.RemoteHostTest, remote)));
var commentTokens = new TodoCommentTokens();
var provider = new TodoCommentIncrementalAnalyzerProvider(commentTokens, Array.Empty<Lazy<IEventListener, EventListenerMetadata>>());
var worker = (TodoCommentIncrementalAnalyzer)provider.CreateIncrementalAnalyzer(workspace);
var document = workspace.Documents.First();
var initialTextSnapshot = document.GetTextBuffer().CurrentSnapshot;
var documentId = document.Id;
var reasons = new InvocationReasons(PredefinedInvocationReasons.DocumentAdded);
await worker.AnalyzeSyntaxAsync(workspace.CurrentSolution.GetDocument(documentId), InvocationReasons.Empty, CancellationToken.None);
var todoLists = worker.GetItems_TestingOnly(documentId);
var expectedLists = document.SelectedSpans;
Assert.Equal(todoLists.Length, expectedLists.Count);
for (var i = 0; i < todoLists.Length; i++)
{
var todo = todoLists[i];
var span = expectedLists[i];
var line = initialTextSnapshot.GetLineFromPosition(span.Start);
var text = initialTextSnapshot.GetText(span.ToSpan());
Assert.Equal(todo.MappedLine, line.LineNumber);
Assert.Equal(todo.MappedColumn, span.Start - line.Start);
Assert.Equal(todo.Message, text);
}
}
}
}
// 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.
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Implementation.TodoComments;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
using Microsoft.CodeAnalysis.TodoComments;
using Xunit;
namespace Microsoft.CodeAnalysis.Test.Utilities.TodoComments
{
public abstract class AbstractTodoCommentTests
{
protected abstract TestWorkspace CreateWorkspace(string codeWithMarker);
protected async Task TestAsync(string codeWithMarker)
{
using var workspace = CreateWorkspace(codeWithMarker);
var hostDocument = workspace.Documents.First();
var initialTextSnapshot = hostDocument.GetTextBuffer().CurrentSnapshot;
var documentId = hostDocument.Id;
var document = workspace.CurrentSolution.GetDocument(documentId);
var service = document.GetLanguageService<ITodoCommentService>();
var todoLists = await service.GetTodoCommentsAsync(document,
TodoCommentDescriptor.Parse(TodoCommentOptions.TokenList.DefaultValue),
CancellationToken.None);
var expectedLists = hostDocument.SelectedSpans;
Assert.Equal(todoLists.Length, expectedLists.Count);
var sourceText = await document.GetTextAsync();
var tree = await document.GetSyntaxTreeAsync();
for (var i = 0; i < todoLists.Length; i++)
{
var todo = todoLists[i];
var span = expectedLists[i];
var line = initialTextSnapshot.GetLineFromPosition(span.Start);
var text = initialTextSnapshot.GetText(span.ToSpan());
var converted = todo.Convert(document, sourceText, tree);
Assert.Equal(converted.MappedLine, line.LineNumber);
Assert.Equal(converted.MappedColumn, span.Start - line.Start);
Assert.Equal(converted.Message, text);
}
}
}
}
......@@ -2,20 +2,19 @@
' The .NET Foundation licenses this file to you under the MIT license.
' See the LICENSE file in the project root for more information.
Imports System.Collections.Immutable
Imports System.Threading
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Editor.Implementation.TodoComments
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Extensions
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Host
Imports Microsoft.CodeAnalysis.SolutionCrawler
Imports Microsoft.CodeAnalysis.Test.Utilities.RemoteHost
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.Test.Utilities.TodoComments
Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.TodoComment
<[UseExportProvider]>
Public Class TodoCommentTests
Inherits AbstractTodoCommentTests
Protected Overrides Function CreateWorkspace(codeWithMarker As String) As TestWorkspace
Return TestWorkspace.CreateVisualBasic(codeWithMarker)
End Function
<Fact>
Public Async Function TestSingleLineTodoComment_Colon() As Task
Dim code = <code>' [|TODO:test|]</code>
......@@ -172,44 +171,8 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.TodoComment
Await TestAsync(code)
End Function
Private Shared Async Function TestAsync(codeWithMarker As XElement) As Tasks.Task
Await TestAsync(codeWithMarker, remote:=False)
Await TestAsync(codeWithMarker, remote:=True)
End Function
Private Shared Async Function TestAsync(codeWithMarker As XElement, remote As Boolean) As Task
Dim code As String = Nothing
Dim list As ImmutableArray(Of TextSpan) = Nothing
MarkupTestFile.GetSpans(codeWithMarker.NormalizedValue, code, list)
Using workspace = TestWorkspace.CreateVisualBasic(code, openDocuments:=False)
workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options _
.WithChangedOption(RemoteHostOptions.RemoteHostTest, remote)))
Dim commentTokens = New TodoCommentTokens()
Dim provider = New TodoCommentIncrementalAnalyzerProvider(commentTokens, Array.Empty(Of Lazy(Of IEventListener, EventListenerMetadata))())
Dim worker = DirectCast(provider.CreateIncrementalAnalyzer(workspace), TodoCommentIncrementalAnalyzer)
Dim document = workspace.Documents.First()
Dim initialTextSnapshot = document.GetTextBuffer().CurrentSnapshot
Dim documentId = document.Id
Await worker.AnalyzeSyntaxAsync(workspace.CurrentSolution.GetDocument(documentId), InvocationReasons.Empty, CancellationToken.None)
Dim todoLists = worker.GetItems_TestingOnly(documentId)
Assert.Equal(todoLists.Count, list.Count)
For i = 0 To todoLists.Count - 1 Step 1
Dim todo = todoLists(i)
Dim span = list(i)
Dim line = initialTextSnapshot.GetLineFromPosition(span.Start)
Assert.Equal(todo.MappedLine, line.LineNumber)
Assert.Equal(todo.MappedColumn, span.Start - line.Start.Position)
Assert.Equal(todo.Message, code.Substring(span.Start, span.Length))
Next
End Using
Private Overloads Function TestAsync(codeWithMarker As XElement) As Task
Return TestAsync(codeWithMarker.NormalizedValue())
End Function
End Class
End Namespace
......@@ -2,10 +2,13 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.TodoComments
{
......@@ -24,6 +27,31 @@ public TodoComment(TodoCommentDescriptor descriptor, string message, int positio
Message = message;
Position = position;
}
internal TodoCommentInfo Convert(
Document document, SourceText text, SyntaxTree tree)
{
// make sure given position is within valid text range.
var textSpan = new TextSpan(Math.Min(text.Length, Math.Max(0, this.Position)), 0);
var location = tree.GetLocation(textSpan);
// var location = tree == null ? Location.Create(document.FilePath, textSpan, text.Lines.GetLinePositionSpan(textSpan)) : tree.GetLocation(textSpan);
var originalLineInfo = location.GetLineSpan();
var mappedLineInfo = location.GetMappedLineSpan();
return new TodoCommentInfo
{
Priority = this.Descriptor.Priority,
Message = this.Message,
DocumentId = document.Id,
OriginalLine = originalLineInfo.StartLinePosition.Line,
OriginalColumn = originalLineInfo.StartLinePosition.Character,
OriginalFilePath = document.FilePath,
MappedLine = mappedLineInfo.StartLinePosition.Line,
MappedColumn = mappedLineInfo.StartLinePosition.Character,
MappedFilePath = mappedLineInfo.GetMappedFilePathIfExist(),
};
}
}
internal interface ITodoCommentService : ILanguageService
......
......@@ -146,7 +146,7 @@ public async Task TestTodoComments()
var comments = await client.TryRunRemoteAsync<IList<TodoComment>>(
WellKnownServiceHubServices.CodeAnalysisService,
nameof(IRemoteTodoCommentsService.GetTodoCommentsAsync),
nameof(IRemoteTodoCommentsService.ComputeTodoCommentsAsync),
solution,
new object[] { solution.Projects.First().DocumentIds.First(), ImmutableArray.Create(new TodoCommentDescriptor("TODO", 0)) },
callbackTarget: null,
......
......@@ -9,6 +9,7 @@ Imports Microsoft.CodeAnalysis.Common
Imports Microsoft.CodeAnalysis.Editor
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Test.Utilities
Imports Microsoft.CodeAnalysis.TodoComments
Imports Microsoft.VisualStudio.LanguageServices.Implementation.TableDataSource
Imports Microsoft.VisualStudio.Shell.TableManager
Imports Roslyn.Test.Utilities
......@@ -83,11 +84,11 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.Diagnostics
Dim sink = DirectCast(sinkAndSubscription.Key, TestTableManagerProvider.TestTableManager.TestSink)
provider.Items = New TodoItem() {CreateItem(documentId)}
provider.Items = New TodoCommentInfo() {CreateItem(documentId)}
provider.RaiseTodoListUpdated(workspace)
Assert.Equal(1, sink.Entries.Count)
provider.Items = Array.Empty(Of TodoItem)()
provider.Items = Array.Empty(Of TodoCommentInfo)()
provider.RaiseClearTodoListUpdated(workspace, documentId)
Assert.Equal(0, sink.Entries.Count)
End Using
......@@ -267,9 +268,9 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.Diagnostics
Dim factory = TryCast(sink.Entries.First(), TableEntriesFactory(Of TodoTableItem))
Dim snapshot1 = factory.GetCurrentSnapshot()
provider.Items = New TodoItem() {
New TodoItem(1, "test2", documentId, 11, 11, 21, 21, Nothing, "test2"),
New TodoItem(0, "test3", documentId, 11, 11, 21, 21, Nothing, "test3")}
provider.Items = New TodoCommentInfo() {
New TodoCommentInfo(1, "test2", documentId, 11, 11, 21, 21, Nothing, "test2"),
New TodoCommentInfo(0, "test3", documentId, 11, 11, 21, 21, Nothing, "test3")}
provider.RaiseTodoListUpdated(workspace)
......@@ -356,8 +357,18 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.Diagnostics
End Using
End Sub
Private Function CreateItem(documentId As DocumentId) As TodoItem
Return New TodoItem(0, "test", documentId, 10, 10, 20, 20, Nothing, "test1")
Private Function CreateItem(documentId As DocumentId) As TodoCommentInfo
Return New TodoCommentInfo With {
.Priority = 0,
.Message = "test",
.DocumentId = documentId,
.MappedLine = 10,
.OriginalLine = 10,
.MappedColumn = 20,
.OriginalColumn = 20,
.MappedFilePath = Nothing,
.OriginalFilePath = "test1"
}
End Function
Private Class TestTodoListProvider
......
......@@ -5,7 +5,6 @@
#nullable enable
using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;
......
......@@ -88,32 +88,7 @@ public override async Task AnalyzeSyntaxAsync(Document document, InvocationReaso
var syntaxTree = await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
foreach (var comment in todoComments)
converted.Add(Convert(document, sourceText, syntaxTree, comment));
}
private TodoCommentInfo Convert(
Document document, SourceText text, SyntaxTree tree, TodoComment comment)
{
// make sure given position is within valid text range.
var textSpan = new TextSpan(Math.Min(text.Length, Math.Max(0, comment.Position)), 0);
var location = tree.GetLocation(textSpan);
// var location = tree == null ? Location.Create(document.FilePath, textSpan, text.Lines.GetLinePositionSpan(textSpan)) : tree.GetLocation(textSpan);
var originalLineInfo = location.GetLineSpan();
var mappedLineInfo = location.GetMappedLineSpan();
return new TodoCommentInfo
{
Priority = comment.Descriptor.Priority,
Message = comment.Message,
DocumentId = document.Id,
OriginalLine = originalLineInfo.StartLinePosition.Line,
OriginalColumn = originalLineInfo.StartLinePosition.Character,
OriginalFilePath = document.FilePath,
MappedLine = mappedLineInfo.StartLinePosition.Line,
MappedColumn = mappedLineInfo.StartLinePosition.Character,
MappedFilePath = mappedLineInfo.GetMappedFilePathIfExist(),
};
converted.Add(comment.Convert(document, sourceText, syntaxTree));
}
}
}
......@@ -23,6 +23,6 @@ public RemoteTodoCommentsIncrementalAnalyzerProvider(RemoteEndPoint endPoint)
}
public IIncrementalAnalyzer CreateIncrementalAnalyzer(Workspace workspace)
=> new RemoteTodoCommentsIncrementalAnalyzer(workspace, _endPoint);
=> new RemoteTodoCommentsIncrementalAnalyzer(_endPoint);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册