|
| 1 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 2 | +namespace Microsoft.VisualStudio.FSharp.Editor.Tests.Roslyn |
| 3 | + |
| 4 | +open System |
| 5 | +open System.IO |
| 6 | +open System.Threading |
| 7 | +open System.Linq |
| 8 | + |
| 9 | +open NUnit.Framework |
| 10 | + |
| 11 | +open Microsoft.CodeAnalysis.Completion |
| 12 | +open Microsoft.CodeAnalysis.Classification |
| 13 | +open Microsoft.CodeAnalysis.Text |
| 14 | +open Microsoft.VisualStudio.FSharp.Editor |
| 15 | + |
| 16 | +open Microsoft.VisualStudio.FSharp.Editor |
| 17 | +open Microsoft.VisualStudio.FSharp.LanguageService |
| 18 | + |
| 19 | +open Microsoft.FSharp.Compiler.SourceCodeServices |
| 20 | +open Microsoft.FSharp.Compiler.Range |
| 21 | + |
| 22 | +[<TestFixture>] |
| 23 | +type GoToDefinitionServiceTests() = |
| 24 | + |
| 25 | + [<TestCase("printf \"%d\" par1", 3, 24, 28)>] |
| 26 | + [<TestCase("printf \"%s\" par2", 5, 24, 28)>] |
| 27 | + [<TestCase("let obj = TestType", 2, 5, 13)>] |
| 28 | + [<TestCase("let obj", 10, 8, 11)>] |
| 29 | + [<TestCase("obj.Member1", 3, 16, 23)>] |
| 30 | + [<TestCase("obj.Member2", 5, 16, 23)>] |
| 31 | + member this.VerifyDefinition(caretMarker: string, definitionLine: int, definitionStartColumn: int, definitionEndColumn: int) = |
| 32 | + let fileContents = """ |
| 33 | +type TestType() = |
| 34 | + member this.Member1(par1: int) = |
| 35 | + printf "%d" par1 |
| 36 | + member this.Member2(par2: string) = |
| 37 | + printf "%s" par2 |
| 38 | +
|
| 39 | +[<EntryPoint>] |
| 40 | +let main argv = |
| 41 | + let obj = TestType() |
| 42 | + obj.Member1(5) |
| 43 | + obj.Member2("test")""" |
| 44 | + |
| 45 | + let filePath = Path.GetTempFileName() + ".fs" |
| 46 | + let options: FSharpProjectOptions = { |
| 47 | + ProjectFileName = "C:\\test.fsproj" |
| 48 | + ProjectFileNames = [| filePath |] |
| 49 | + ReferencedProjects = [| |] |
| 50 | + OtherOptions = [| |] |
| 51 | + IsIncompleteTypeCheckEnvironment = true |
| 52 | + UseScriptResolutionRules = false |
| 53 | + LoadTime = DateTime.MaxValue |
| 54 | + UnresolvedReferences = None |
| 55 | + } |
| 56 | + |
| 57 | + File.WriteAllText(filePath, fileContents) |
| 58 | + |
| 59 | + let caretPosition = fileContents.IndexOf(caretMarker) + caretMarker.Length - 1 // inside the marker |
| 60 | + let definitionOption = FSharpGoToDefinitionService.FindDefinition(SourceText.From(fileContents), filePath, caretPosition, [], options, 0, CancellationToken.None) |> Async.RunSynchronously |
| 61 | + |
| 62 | + match definitionOption with |
| 63 | + | None -> Assert.Fail("No definition found") |
| 64 | + | Some(range) -> |
| 65 | + Assert.AreEqual(range.StartLine, range.EndLine, "Range must be on the same line") |
| 66 | + Assert.AreEqual(definitionLine, range.StartLine, "Range line should match") |
| 67 | + Assert.AreEqual(definitionStartColumn, range.StartColumn, "Range start column should match") |
| 68 | + Assert.AreEqual(definitionEndColumn, range.EndColumn, "Range end column should match") |
0 commit comments