forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindReferencesTests.fs
More file actions
164 lines (120 loc) · 6.1 KB
/
Copy pathFindReferencesTests.fs
File metadata and controls
164 lines (120 loc) · 6.1 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
module FSharp.Editor.Tests.FindReferencesTests
open System.Threading.Tasks
open System.Threading
open System.IO
open System.Collections.Concurrent
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.Editor.FindUsages
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.FindUsages
open Microsoft.VisualStudio.FSharp.Editor
open Xunit
open FSharp.Test.ProjectGeneration
open FSharp.Editor.Tests.Helpers
let getPositionOf (subString: string) (filePath) =
filePath |> File.ReadAllText |> (fun source -> source.IndexOf subString)
module FindReferences =
let project =
SyntheticProject.Create(
{ sourceFile "First" [] with
SignatureFile = AutoGenerated
ExtraSource =
"let someFunc funcParam = funcParam * 1\n"
+ "let sharedFunc funcParam = funcParam * 2\n"
},
{ sourceFile "Second" [] with
ExtraSource = "let someFunc funcParam = funcParam * 1"
},
{ sourceFile "Third" [ "First" ] with
ExtraSource = "let someFunc x = ModuleFirst.sharedFunc x + 10"
}
)
let solution, checker = RoslynTestHelpers.CreateSolution project
let findUsagesService = FSharpFindUsagesService() :> IFSharpFindUsagesService
let getContext () =
let foundDefinitions = ConcurrentBag()
let foundReferences = ConcurrentBag()
let context =
{ new IFSharpFindUsagesContext with
member _.OnDefinitionFoundAsync(definition: FSharpDefinitionItem) =
foundDefinitions.Add definition
Task.CompletedTask
member _.OnReferenceFoundAsync(reference: FSharpSourceReferenceItem) =
foundReferences.Add reference
Task.CompletedTask
member _.ReportMessageAsync _ = Task.CompletedTask
member _.ReportProgressAsync(_, _) = Task.CompletedTask
member _.SetSearchTitleAsync _ = Task.CompletedTask
member _.CancellationToken = CancellationToken.None
}
context, foundDefinitions, foundReferences
[<Fact>]
let ``Find references to a document-local symbol`` () =
let context, foundDefinitions, foundReferences = getContext ()
let documentPath = project.GetFilePath "Second"
let document =
solution.TryGetDocumentFromPath documentPath
|> ValueOption.defaultWith (fun _ -> failwith "Document not found")
findUsagesService.FindReferencesAsync(document, getPositionOf "funcParam" documentPath, context).Wait()
// We cannot easily inspect what exactly was found here, but that should be verified
// in FSharp.Compiler.ComponentTests.FSharpChecker.FindReferences
if foundDefinitions.Count <> 1 then
failwith $"Expected 1 definition but found {foundDefinitions.Count}"
if foundReferences.Count <> 1 then
failwith $"Expected 1 reference but found {foundReferences.Count}"
[<Fact>]
let ``Find references to an implementation + signature symbol`` () =
let context, foundDefinitions, foundReferences = getContext ()
let documentPath = project.GetFilePath "First"
let document =
solution.TryGetDocumentFromPath documentPath
|> ValueOption.defaultWith (fun _ -> failwith "Document not found")
findUsagesService.FindReferencesAsync(document, getPositionOf "funcParam" documentPath, context).Wait()
if foundDefinitions.Count <> 1 then
failwith $"Expected 1 definition but found {foundDefinitions.Count}"
if
foundReferences.Count <> 2 // One in signature file, one in function body
then
failwith $"Expected 2 references but found {foundReferences.Count}"
[<Fact>]
let ``Find references to a symbol in project`` () =
let context, foundDefinitions, foundReferences = getContext ()
let documentPath = project.GetFilePath "First"
let document =
solution.TryGetDocumentFromPath documentPath
|> ValueOption.defaultWith (fun _ -> failwith "Document not found")
findUsagesService.FindReferencesAsync(document, getPositionOf "sharedFunc" documentPath, context).Wait()
if foundDefinitions.Count <> 1 then
failwith $"Expected 1 definition but found {foundDefinitions.Count}"
if
foundReferences.Count <> 2 // One in signature file, one in Third file
then
failwith $"Expected 2 references but found {foundReferences.Count}"
[<Theory>]
[<InlineData(">=>")>]
[<InlineData(">++")>]
let ``Find references to operators start with >`` operator =
let project2 =
SyntheticProject.Create(
{ sourceFile "First" [] with
SignatureFile = No
ExtraSource =
"let compose x = fun y -> x - y\n"
+ $"let ({operator}) = compose\n"
+ $"let test t = t {operator} 5\n"
},
{ sourceFile "Second" [ "First" ] with
ExtraSource = "open ModuleFirst\n" + $"let z = 4 {operator} 5\n"
}
)
let solution2, _ = RoslynTestHelpers.CreateSolution project2
let context, foundDefinitions, foundReferences = getContext ()
let documentPath = project2.GetFilePath "Second"
let document =
solution2.TryGetDocumentFromPath documentPath
|> ValueOption.defaultWith (fun _ -> failwith "Document not found")
findUsagesService.FindReferencesAsync(document, getPositionOf operator documentPath, context).Wait()
// We cannot easily inspect what exactly was found here, but that should be verified
// in FSharp.Compiler.ComponentTests.FSharpChecker.FindReferences
if foundDefinitions.Count <> 1 then
failwith $"Expected 1 definition but found {foundDefinitions.Count}"
if foundReferences.Count <> 2 then
failwith $"Expected 2 reference but found {foundReferences.Count}"