From 98afac058ac45eacff2bca810a9c2388904e981d Mon Sep 17 00:00:00 2001 From: Martijn Hoogendoorn Date: Fri, 14 May 2021 22:50:57 +0200 Subject: [PATCH] Add test and fix signature off-by-one (#782) --- .../ParseAndCheckResults.fs | 2 +- .../ExtensionsTests.fs | 54 +++++++++++++++++++ test/FsAutoComplete.Tests.Lsp/Program.fs | 1 + .../TestCases/Signature/Script.fsx | 3 ++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test/FsAutoComplete.Tests.Lsp/TestCases/Signature/Script.fsx diff --git a/src/FsAutoComplete.Core/ParseAndCheckResults.fs b/src/FsAutoComplete.Core/ParseAndCheckResults.fs index cc97b2834..4eeb62469 100644 --- a/src/FsAutoComplete.Core/ParseAndCheckResults.fs +++ b/src/FsAutoComplete.Core/ParseAndCheckResults.fs @@ -213,7 +213,7 @@ type ParseAndCheckResults } member __.TryGetToolTip (pos: Pos) (lineStr: LineStr) = - match Lexer.findLongIdents(pos.Column - 1, lineStr) with + match Lexer.findLongIdents(pos.Column, lineStr) with | None -> ResultOrString.Error "Cannot find ident for tooltip" | Some(col,identIsland) -> let identIsland = Array.toList identIsland diff --git a/test/FsAutoComplete.Tests.Lsp/ExtensionsTests.fs b/test/FsAutoComplete.Tests.Lsp/ExtensionsTests.fs index 5a313b3fa..ba2295c2b 100644 --- a/test/FsAutoComplete.Tests.Lsp/ExtensionsTests.fs +++ b/test/FsAutoComplete.Tests.Lsp/ExtensionsTests.fs @@ -375,3 +375,57 @@ let analyzerTests state = do! server.Shutdown() }) ] + +let signatureTests state = + let server = + async { + let path = Path.Combine(__SOURCE_DIRECTORY__, "TestCases", "Signature") + let scriptPath = Path.Combine(path, "Script.fsx") + let! (server, events) = serverInitialize path defaultConfigDto state + do! waitForWorkspaceFinishedParsing events + do! server.TextDocumentDidOpen { TextDocument = loadDocument scriptPath } + match! waitForParseResultsForFile "Script.fsx" events with + | Ok () -> + () // all good, no parsing/checking errors + | Core.Result.Error errors -> + failtestf "Errors while parsing script %s: %A" scriptPath errors + + return server, scriptPath + } + |> Async.Cache + + let verifySignature line (characterStart, characterEnd) expectedSignature = + let verifyAt character = async { + let! server, scriptPath = server + let pos: TextDocumentPositionParams = { + TextDocument = { Uri = Path.FilePathToUri scriptPath } + Position = { Line = line; Character = character } + } + match! server.FSharpSignature pos with + | Ok { Content = content } -> + let r = JsonSerializer.readJson>(content) + Expect.equal r.Kind "typesig" "Should have a kind of 'typesig'" + Expect.equal r.Data expectedSignature (sprintf "Should have a signature of '%s' at character %d" expectedSignature character) + | Result.Error errors -> + failtestf "Error while getting signature: %A" errors + } + + testCaseAsync (sprintf "fsharp/signature for line %d characters [%d, %d] should be '%s'" line characterStart characterEnd expectedSignature) ( + [ for c in characterStart .. characterEnd -> verifyAt c ] + |> Async.Sequential + |> Async.map (fun _ -> ()) + ) + + testSequenced <| + testList "signature evaluation" [ + testList "tests" [ + verifySignature 0 (4, 16) "val arrayOfTuples : (int * int) []" + verifySignature 1 (4, 15) "val listOfTuples : (int * int) list" + verifySignature 2 (4, 15) "val someFunction : a:'a -> unit" + ] + testCaseAsync "cleanup" (async { + let! server, _ = server + do! server.Shutdown() + }) + ] + diff --git a/test/FsAutoComplete.Tests.Lsp/Program.fs b/test/FsAutoComplete.Tests.Lsp/Program.fs index e6a62800c..485685612 100644 --- a/test/FsAutoComplete.Tests.Lsp/Program.fs +++ b/test/FsAutoComplete.Tests.Lsp/Program.fs @@ -59,6 +59,7 @@ let tests = // fake isn't updated to FCS 39, disabling tests until that's resolved //fakeInteropTests toolsPath analyzerTests state + signatureTests state SignatureHelp.tests state CodeFixTests.tests state Completion.tests state diff --git a/test/FsAutoComplete.Tests.Lsp/TestCases/Signature/Script.fsx b/test/FsAutoComplete.Tests.Lsp/TestCases/Signature/Script.fsx new file mode 100644 index 000000000..6a5193492 --- /dev/null +++ b/test/FsAutoComplete.Tests.Lsp/TestCases/Signature/Script.fsx @@ -0,0 +1,3 @@ +let arrayOfTuples = [| 1, 2 |] +let listOfTuples = [ 1, 2 ] +let someFunction a = ()