Skip to content

Commit

Permalink
Add test to verify linked file updating works properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Nov 17, 2017
1 parent 37f8ac2 commit 60f8761
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Diagnostics
Optional codeActionIndex As Integer = 0,
Optional verifyTokens As Boolean = True,
Optional fileNameToExpected As Dictionary(Of String, String) = Nothing,
Optional verifySolutions As Action(Of Solution, Solution) = Nothing,
Optional verifySolutions As Func(Of Solution, Solution, Task) = Nothing,
Optional onAfterWorkspaceCreated As Action(Of TestWorkspace) = Nothing) As Task
Using workspace = TestWorkspace.CreateWorkspace(definition)
onAfterWorkspaceCreated?.Invoke(workspace)
Expand All @@ -65,16 +65,20 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Diagnostics

Dim updatedSolution = workspace.CurrentSolution

verifySolutions?.Invoke(oldSolution, updatedSolution)
If verifySolutions IsNot Nothing Then
Await verifySolutions(oldSolution, updatedSolution)
End If

If expected Is Nothing AndAlso fileNameToExpected Is Nothing Then
If expected Is Nothing AndAlso
fileNameToExpected Is Nothing AndAlso
verifySolutions Is Nothing Then
Dim projectChanges = SolutionUtilities.GetSingleChangedProjectChanges(oldSolution, updatedSolution)
Assert.Empty(projectChanges.GetChangedDocuments())
ElseIf expected IsNot Nothing Then
Dim updatedDocument = SolutionUtilities.GetSingleChangedDocument(oldSolution, updatedSolution)

Await VerifyAsync(expected, verifyTokens, updatedDocument)
Else
ElseIf fileNameToExpected IsNot Nothing Then
For Each kvp In fileNameToExpected
Dim updatedDocument = updatedSolution.Projects.SelectMany(Function(p) p.Documents).Single(Function(d) d.Name = kvp.Key)
Await VerifyAsync(kvp.Value, verifyTokens, updatedDocument)
Expand Down Expand Up @@ -134,7 +138,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Diagnostics
Dim hostDocument = GetHostDocument(workspace)

Dim invocationBuffer = hostDocument.TextBuffer
Dim invocationPoint = workspace.Documents.Single(Function(d) d.CursorPosition.HasValue).CursorPosition.Value
Dim invocationPoint = workspace.Documents.Single(Function(d) d.CursorPosition.HasValue AndAlso Not d.IsLinkFile).CursorPosition.Value

Dim document = workspace.CurrentSolution.GetDocument(hostDocument.Id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Imports Microsoft.CodeAnalysis.IncrementalCaches
Imports Microsoft.CodeAnalysis.SolutionCrawler
Imports Microsoft.CodeAnalysis.UnitTests
Imports Microsoft.CodeAnalysis.VisualBasic.AddImport
Imports Roslyn.Utilities

Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Diagnostics.AddImport

Expand Down Expand Up @@ -509,12 +510,12 @@ namespace CSAssembly2
Optional codeActionIndex As Integer = 0,
Optional addedReference As String = Nothing,
Optional onAfterWorkspaceCreated As Action(Of TestWorkspace) = Nothing) As Task
Dim verifySolutions As Action(Of Solution, Solution) = Nothing
Dim verifySolutions As Func(Of Solution, Solution, Task) = Nothing
Dim workspace As TestWorkspace = Nothing

If addedReference IsNot Nothing Then
verifySolutions =
Sub(oldSolution As Solution, newSolution As Solution)
Function(oldSolution As Solution, newSolution As Solution)
Dim initialDocId = workspace.DocumentWithCursor.Id
Dim oldProject = oldSolution.GetDocument(initialDocId).Project
Dim newProject = newSolution.GetDocument(initialDocId).Project
Expand All @@ -529,7 +530,8 @@ namespace CSAssembly2
Select p.Name

Assert.True(newProjectReferences.Contains(addedReference))
End Sub
Return SpecializedTasks.EmptyTask
End Function
End If

Await TestAsync(definition, expected, codeActionIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,54 @@ end class
</text>.Value.Trim()}
})
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)>
Public Async Function TestLinkedFile() As System.Threading.Tasks.Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true" AssemblyName="LinkedProj" Name="CSProj.1">
<Document FilePath='C.cs'>
partial class C
{
$$int i;

public int P { get { return i; } }

public C()
{
this.i = 0;
}
}
</Document>
</Project>
<Project Language="C#" CommonReferences="true" AssemblyName="LinkedProj" Name="CSProj.2">
<Document IsLinkFile="true" LinkProjectName="CSProj.1" LinkFilePath="C.cs"/>
</Project>
</Workspace>

Dim expectedText = "
partial class C
{

public int P { get; private set; }

public C()
{
this.P = 0;
}
}".Trim()

Await TestAsync(input, verifySolutions:=
Async Function(oldSolution, newSolution)
Dim documents = newSolution.Projects.SelectMany(Function(p) p.Documents).
Where(Function(d) d.Name = "C.cs")
Assert.Equal(2, documents.Count())

For Each doc In documents
Dim text = (Await doc.GetTextAsync()).ToString().Trim()
Assert.Equal(expectedText, text)
Next
End Function)
End Function
End Class
End Namespace

0 comments on commit 60f8761

Please sign in to comment.