forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoToDefinitionServiceTests.fs
146 lines (116 loc) · 4.62 KB
/
GoToDefinitionServiceTests.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Editor.Tests
open Xunit
open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.Text
open Microsoft.VisualStudio.FSharp.Editor
open FSharp.Compiler.EditorServices
open FSharp.Compiler.Text
open FSharp.Editor.Tests.Helpers
module GoToDefinitionServiceTests =
let userOpName = "GoToDefinitionServiceTests"
let private findDefinition (document: Document, sourceText: SourceText, position: int, defines: string list) : range option =
maybe {
let textLine = sourceText.Lines.GetLineFromPosition position
let textLinePos = sourceText.Lines.GetLinePosition position
let fcsTextLineNumber = Line.fromZ textLinePos.Line
let! lexerSymbol =
Tokenizer.getSymbolAtPosition (
document.Id,
sourceText,
position,
document.FilePath,
defines,
SymbolLookupKind.Greedy,
false,
false,
System.Threading.CancellationToken.None
)
let _, checkFileResults =
document.GetFSharpParseAndCheckResultsAsync(nameof (userOpName))
|> Async.RunSynchronously
let declarations =
checkFileResults.GetDeclarationLocation(
fcsTextLineNumber,
lexerSymbol.Ident.idRange.EndColumn,
textLine.ToString(),
lexerSymbol.FullIsland,
false
)
match declarations with
| FindDeclResult.DeclFound range -> return range
| _ -> return! None
}
let GoToDefinitionTest (fileContents: string, caretMarker: string, expected) =
let caretPosition = fileContents.IndexOf(caretMarker) + caretMarker.Length - 1 // inside the marker
let sourceText = SourceText.From(fileContents)
let document =
RoslynTestHelpers.CreateSolution(fileContents)
|> RoslynTestHelpers.GetSingleDocument
let actual =
findDefinition (document, sourceText, caretPosition, [])
|> Option.map (fun range -> (range.StartLine, range.EndLine, range.StartColumn, range.EndColumn))
if actual <> expected then
failwithf
"Incorrect information returned for fileContents=<<<%s>>>, caretMarker=<<<%s>>>, expected =<<<%A>>>, actual = <<<%A>>>"
fileContents
caretMarker
expected
actual
[<Fact>]
let ``goto definition smoke test`` () =
let manyTestCases =
[
// Test1
("""
type TestType() =
member this.Member1(par1: int) =
printf "%d" par1
member this.Member2(par2: string) =
printf "%s" par2
[<EntryPoint>]
let main argv =
let obj = TestType()
obj.Member1(5)
obj.Member2("test")""",
[
("printf \"%d\" par1", Some(3, 3, 24, 28))
("printf \"%s\" par2", Some(5, 5, 24, 28))
("let obj = TestType", Some(2, 2, 5, 13))
("let obj", Some(10, 10, 8, 11))
("obj.Member1", Some(3, 3, 16, 23))
("obj.Member2", Some(5, 5, 16, 23))
])
// Test2
("""
module Module1 =
let foo x = x
let _ = Module1.foo 1
""",
[ ("let _ = Module", Some(2, 2, 7, 14)) ])
]
for fileContents, testCases in manyTestCases do
for caretMarker, expected in testCases do
printfn "Test case: caretMarker=<<<%s>>>" caretMarker
GoToDefinitionTest(fileContents, caretMarker, expected)
[<Fact>]
let ``goto definition for string interpolation`` () =
let fileContents =
"""
let xxxxx = 1
let yyyy = $"{abc{xxxxx}def}" """
let caretMarker = "xxxxx"
let expected = Some(2, 2, 4, 9)
GoToDefinitionTest(fileContents, caretMarker, expected)
[<Fact>]
let ``goto definition for static abstract method invocation`` () =
let fileContents =
"""
type IStaticProperty<'T when 'T :> IStaticProperty<'T>> =
static abstract StaticProperty: 'T
let f_IWSAM_flex_StaticProperty(x: #IStaticProperty<'T>) =
'T.StaticProperty
"""
let caretMarker = "'T.StaticProperty"
let expected = Some(3, 3, 20, 34)
GoToDefinitionTest(fileContents, caretMarker, expected)