Skip to content

Commit

Permalink
* feat(FSharp.Editor.Tests): add GetLastDocument method in RoslynHelp…
Browse files Browse the repository at this point in the history
…ers.fs

* refactor(RefactorTestFramework.fs): replace GetSingleDocument with GetLastDocument
* feat(AddExplicitReturnTypeTests.fs): add test for inferring custom type declared earlier in project
  • Loading branch information
SebastianAtWork committed Nov 14, 2023
1 parent 8a0338d commit 6e83b22
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ type RoslynTestHelpers private () =
let document = project.Documents |> Seq.exactlyOne
document

static member GetLastDocument(solution: Solution) =
let project = solution.Projects |> Seq.exactlyOne
let document = project.Documents |> Seq.last
document

static member CreateSolution(syntheticProject: SyntheticProject) =

let checker = syntheticProject.SaveAndCheck()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

open Microsoft.VisualStudio.FSharp.Editor
open Xunit
open NUnit.Framework
open System.Linq
open FSharp.Editor.Tests.Refactors.RefactorTestFramework

[<Theory>]
Expand Down Expand Up @@ -174,3 +174,36 @@ let sum a b :MyType= {Value=a+b}

let resultText = newDoc.GetTextAsync context.CT |> GetTaskResult
Assert.Equal(expectedCode, resultText.ToString())

[<Fact>]
let ``Correctly infer custom type that is declared earlier in project`` () =
let symbolName = "sum"

let myModule =
"""
module MyModule
type MyType = { Value: int }
"""

let code =
"""
open MyModule
let sum a b = {Value=a+b}
"""

use context = TestContext.CreateWithCodeAndDependency code myModule

let spanStart = code.IndexOf symbolName

let newDoc = tryRefactor code spanStart context (new AddExplicitReturnType())

let expectedCode =
"""
open MyModule
let sum a b :MyType= {Value=a+b}
"""

let resultText = newDoc.GetTextAsync context.CT |> GetTaskResult
Assert.Equal(expectedCode, resultText.ToString())
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module FSharp.Editor.Tests.Refactors.RefactorTestFramework

open System
open System.Linq
open System.Collections.Immutable
open System.Collections.Generic

Expand Down Expand Up @@ -35,16 +36,25 @@ type TestContext(Solution: Solution, CT) =
let ct = CancellationToken false
new TestContext(solution, ct)

static member CreateWithCodeAndDependency (code: string) (codeForPreviousFile: string) =
let mutable solution = RoslynTestHelpers.CreateSolution(codeForPreviousFile)

let firstProject = solution.Projects.First()
solution <- solution.AddDocument(DocumentId.CreateNewId(firstProject.Id), "test2.fs", code, filePath = "C:\\test2.fs")

let ct = CancellationToken false
new TestContext(solution, ct)

let mockAction =
Action<CodeActions.CodeAction, ImmutableArray<Diagnostic>>(fun _ _ -> ())

let tryRefactor (code: string) (cursorPosition) (context: TestContext) (refactorProvider: 'T :> CodeRefactoringProvider) =
let refactoringActions = new List<CodeAction>()
let existingDocument = RoslynTestHelpers.GetSingleDocument context.Solution
let existingDocument = RoslynTestHelpers.GetLastDocument context.Solution

context.Solution <- context.Solution.WithDocumentText(existingDocument.Id, SourceText.From(code))

let document = RoslynTestHelpers.GetSingleDocument context.Solution
let document = RoslynTestHelpers.GetLastDocument context.Solution

let mutable workspace = context.Solution.Workspace

Expand All @@ -70,11 +80,11 @@ let tryRefactor (code: string) (cursorPosition) (context: TestContext) (refactor
let tryGetRefactoringActions (code: string) (cursorPosition) (context: TestContext) (refactorProvider: 'T :> CodeRefactoringProvider) =
cancellableTask {
let refactoringActions = new List<CodeAction>()
let existingDocument = RoslynTestHelpers.GetSingleDocument context.Solution
let existingDocument = RoslynTestHelpers.GetLastDocument context.Solution

context.Solution <- context.Solution.WithDocumentText(existingDocument.Id, SourceText.From(code))

let document = RoslynTestHelpers.GetSingleDocument context.Solution
let document = RoslynTestHelpers.GetLastDocument context.Solution

let mutable workspace = context.Solution.Workspace

Expand Down

0 comments on commit 6e83b22

Please sign in to comment.