-
Notifications
You must be signed in to change notification settings - Fork 806
/
Copy pathTooltipTests.fs
562 lines (439 loc) · 19.3 KB
/
TooltipTests.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
module FSharp.Compiler.Service.Tests.TooltipTests
#nowarn "57"
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Service.Tests.Common
open FSharp.Compiler.Text
open FSharp.Compiler.Tokenization
open FSharp.Compiler.EditorServices
open FSharp.Compiler.Symbols
open FSharp.Compiler.Xml
open FSharp.Test
open Xunit
let testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource line colAtEndOfNames lineText names (expectedContent: string) =
let files =
Map.ofArray
[| "A.fsi",
SourceText.ofString sigSource
"A.fs",
SourceText.ofString implSource |]
let documentSource fileName = Map.tryFind fileName files |> async.Return
let projectOptions =
let _, projectOptions = mkTestFileAndOptions "" Array.empty
{ projectOptions with
SourceFiles = [| "A.fsi"; "A.fs" |] }
let checker =
FSharpChecker.Create(documentSource = DocumentSource.Custom documentSource,
useTransparentCompiler = CompilerAssertHelpers.UseTransparentCompiler)
let checkResult =
checker.ParseAndCheckFileInProject("A.fs", 0, Map.find "A.fs" files, projectOptions)
|> Async.RunImmediate
match checkResult with
| _, FSharpCheckFileAnswer.Succeeded(checkResults) ->
// Get the tooltip for (line, colAtEndOfNames) in the implementation file
let (ToolTipText tooltipElements) =
checkResults.GetToolTip(line, colAtEndOfNames, lineText, names, FSharpTokenTag.Identifier)
match tooltipElements with
| ToolTipElement.Group [ element ] :: _ ->
match element.XmlDoc with
| FSharpXmlDoc.FromXmlText xmlDoc ->
Assert.True xmlDoc.NonEmpty
Assert.True (xmlDoc.UnprocessedLines[0].Contains(expectedContent))
| xmlDoc -> failwith $"Expected FSharpXmlDoc.FromXmlText, got {xmlDoc}"
| elements -> failwith $"Expected at least one tooltip group element, got {elements}"
| _ -> failwith "Expected checking to succeed."
[<Fact>]
let ``Display XML doc of signature file for let if implementation doesn't have one`` () =
let sigSource =
"""
module Foo
/// Great XML doc comment
val bar: a: int -> b: int -> int
"""
let implSource =
"""
module Foo
// No XML doc here because the signature file has one right?
let bar a b = a - b
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource 4 4 "let bar a b = a - b" [ "bar" ] "Great XML doc comment"
[<Fact>]
let ``Display XML doc of signature file for partial AP if implementation doesn't have one`` () =
let sigSource =
"""
module Foo
/// Some Sig Doc on IsThree
val (|IsThree|_|): x: int -> int option
"""
let implSource =
"""
module Foo
// No XML doc here because the signature file has one right?
let (|IsThree|_|) x = if x = 3 then Some x else None
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource 4 4 "let (|IsThree|_|) x = if x = 3 then Some x else None" [ "IsThree" ] "Some Sig Doc on IsThree"
[<Fact>]
let ``Display XML doc of signature file for DU if implementation doesn't have one`` () =
let sigSource =
"""
module Foo
/// Some sig comment on the disc union type
type Bar =
| Case1 of int * string
| Case2 of string
"""
let implSource =
"""
module Foo
// No XML doc here because the signature file has one right?
type Bar =
| Case1 of int * string
| Case2 of string
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource 4 7 "type Bar =" [ "Bar" ] "Some sig comment on the disc union type"
[<Fact>]
let ``Display XML doc of signature file for DU case if implementation doesn't have one`` () =
let sigSource =
"""
module Foo
type Bar =
| BarCase1 of int * string
/// Some sig comment on the disc union case
| BarCase2 of string
"""
let implSource =
"""
module Foo
type Bar =
| BarCase1 of int * string
// No XML doc here because the signature file has one right?
| BarCase2 of string
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource 7 14 " | BarCase2 of string" [ "BarCase2" ] "Some sig comment on the disc union case"
[<Fact>]
let ``Display XML doc of signature file for record type if implementation doesn't have one`` () =
let sigSource =
"""
module Foo
/// Some sig comment on record type
type Bar = {
SomeField: int
}
"""
let implSource =
"""
module Foo
type Bar = {
SomeField: int
}
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource 3 9 "type Bar = {" [ "Bar" ] "Some sig comment on record type"
[<Fact>]
let ``Display XML doc of signature file for record field if implementation doesn't have one`` () =
let sigSource =
"""
module Foo
type Bar = {
/// Some sig comment on record field
SomeField: int
}
"""
let implSource =
"""
module Foo
type Bar = {
SomeField: int
}
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource 5 13 " SomeField: int" [ "SomeField" ] "Some sig comment on record field"
[<Fact>]
let ``Display XML doc of signature file for class type if implementation doesn't have one`` () =
let sigSource =
"""
module Foo
/// Some sig comment on class type
type Bar =
new: unit -> Bar
member Foo: string
"""
let implSource =
"""
module Foo
type Bar() =
member val Foo = "bla" with get, set
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource 3 9 "type Bar() =" [ "Bar" ] "Some sig comment on class type"
[<Fact>]
let ``Display XML doc of signature file for class member if implementation doesn't have one`` () =
let sigSource =
"""
module Foo
type Bar =
new: unit -> Bar
/// Some sig comment on auto property
member Foo: string
/// Some sig comment on class member
member Func: int -> int -> int
"""
let implSource =
"""
module Foo
type Bar() =
member val Foo = "bla" with get, set
member _.Func x y = x * y
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource 6 30 " member _.Func x y = x * y" [ "_"; "Func" ] "Some sig comment on class member"
[<Fact>]
let ``Display XML doc of signature file for module if implementation doesn't have one`` () =
let sigSource =
"""
/// Some sig comment on module
module Foo
val a: int
"""
let implSource =
"""
module Foo
let a = 23
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource 2 10 "module Foo" [ "Foo" ] "Some sig comment on module"
let testToolTipSquashing source line colAtEndOfNames lineText names tokenTag =
let files =
Map.ofArray
[| "A.fs",
SourceText.ofString source |]
let documentSource fileName = Map.tryFind fileName files |> async.Return
let projectOptions =
let _, projectOptions = mkTestFileAndOptions "" Array.empty
{ projectOptions with
SourceFiles = [| "A.fs" |] }
let checker =
FSharpChecker.Create(documentSource = DocumentSource.Custom documentSource,
useTransparentCompiler = CompilerAssertHelpers.UseTransparentCompiler)
let checkResult =
checker.ParseAndCheckFileInProject("A.fs", 0, Map.find "A.fs" files, projectOptions)
|> Async.RunImmediate
match checkResult with
| _, FSharpCheckFileAnswer.Succeeded(checkResults) ->
// Get the tooltip for `bar`
let (ToolTipText tooltipElements) =
checkResults.GetToolTip(line, colAtEndOfNames, lineText, names, tokenTag)
let (ToolTipText tooltipElementsSquashed) =
checkResults.GetToolTip(line, colAtEndOfNames, lineText, names, tokenTag, 10)
match tooltipElements, tooltipElementsSquashed with
| groups, groupsSquashed ->
let breaks =
groups
|> List.map
(fun g ->
match g with
| ToolTipElement.Group gr -> gr |> List.map (fun g -> g.MainDescription)
| _ -> failwith "expected TooltipElement.Group")
|> List.concat
|> Array.concat
|> Array.sumBy (fun t -> if t.Tag = TextTag.LineBreak then 1 else 0)
let squashedBreaks =
groupsSquashed
|> List.map
(fun g ->
match g with
| ToolTipElement.Group gr -> gr |> List.map (fun g -> g.MainDescription)
| _ -> failwith "expected TooltipElement.Group")
|> List.concat
|> Array.concat
|> Array.sumBy (fun t -> if t.Tag = TextTag.LineBreak then 1 else 0)
Assert.True(breaks < squashedBreaks)
| _ -> failwith "Expected checking to succeed."
[<Fact>]
let ``Squashed tooltip of long function signature should have newlines added`` () =
let source =
"""
module Foo
let bar (fileName: string) (fileVersion: int) (sourceText: string) (options: int) (userOpName: string) = 0
"""
testToolTipSquashing source 3 6 "let bar (fileName: string) (fileVersion: int) (sourceText: string) (options: int) (userOpName: string) = 0;" [ "bar" ] FSharpTokenTag.Identifier
[<Fact>]
let ``Squashed tooltip of record with long field signature should have newlines added`` () =
let source =
"""
module Foo
type Foo =
{ Field1: string
Field2: (string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string) }
"""
testToolTipSquashing source 3 7 "type Foo =" [ "Foo" ] FSharpTokenTag.Identifier
[<Fact>]
let ``Squashed tooltip of DU with long case signature should have newlines added`` () =
let source =
"""
module Foo
type SomeDiscUnion =
| Case1 of string
| Case2 of (string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string)
"""
testToolTipSquashing source 3 7 "type SomeDiscUnion =" [ "SomeDiscUnion" ] FSharpTokenTag.Identifier
[<Fact>]
let ``Squashed tooltip of constructor with long signature should have newlines added`` () =
let source =
"""
module Foo
type SomeClass(a1: int, a2: int, a3: int, a4: int, a5: int, a6: int, a7: int, a8: int, a9: int, a10: int, a11: int, a12: int, a13: int, a14: int, a15: int, a16: int, a17: int, a18: int, a19: int, a20: int) =
member _.A = a1
"""
testToolTipSquashing source 3 7 "type SomeClass(a1: int, a2: int, a3: int, a4: int, a5: int, a6: int, a7: int, a8: int, a9: int, a10: int, a11: int, a12: int, a13: int, a14: int, a15: int, a16: int, a17: int, a18: int, a19: int, a20: int) =" [ "SomeClass" ] FSharpTokenTag.Identifier
[<Fact>]
let ``Squashed tooltip of property with long signature should have newlines added`` () =
let source =
"""
module Foo
type SomeClass() =
member _.Abc: (int * int * int * int * int * int * int * int * int * int * int * int * int * int * int * int * int * int * int * int) = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
let c = SomeClass()
c.Abc
"""
testToolTipSquashing source 7 5 "c.Abc" [ "c"; "Abc" ] FSharpTokenTag.Identifier
let getCheckResults source options =
let fileName, options =
mkTestFileAndOptions
source
options
let _, checkResults = parseAndCheckFile fileName source options
checkResults
let taggedTextsToString (t: TaggedText array) =
t
|> Array.map (fun taggedText -> taggedText.Text)
|> String.concat ""
let assertAndExtractTooltip (ToolTipText(items)) =
Assert.Equal(1,items.Length)
match items.[0] with
| ToolTipElement.Group [ singleElement ] ->
let toolTipText =
singleElement.MainDescription
|> taggedTextsToString
toolTipText, singleElement.XmlDoc, singleElement.Remarks |> Option.map taggedTextsToString
| _ -> failwith $"Expected group, got {items.[0]}"
let assertAndGetSingleToolTipText items =
let text,_xml,_remarks = assertAndExtractTooltip items
text
let normalize (s:string) = s.Replace("\r\n", "\n").Replace("\n\n", "\n")
[<Fact>]
let ``Auto property should display a single tool tip`` () =
let source = """
namespace Foo
/// Some comment on class
type Bar() =
/// Some comment on class member
member val Foo = "bla" with get, set
"""
let checkResults = getCheckResults source Array.empty
checkResults.GetToolTip(7, 18, " member val Foo = \"bla\" with get, set", [ "Foo" ], FSharpTokenTag.Identifier)
|> assertAndGetSingleToolTipText
|> Assert.shouldBeEquivalentTo "property Bar.Foo: string with get, set"
[<FactForNETCOREAPP>]
let ``Should display nullable Csharp code analysis annotations on method argument`` () =
let source = """module Foo
let exists() = System.IO.Path.Exists(null:string)
"""
let checkResults = getCheckResults source [|"--checknulls+";"--langversion:preview"|]
checkResults.GetToolTip(2, 36, "let exists() = System.IO.Path.Exists(null:string)", [ "Exists" ], FSharpTokenTag.Identifier)
|> assertAndGetSingleToolTipText
|> Assert.shouldBeEquivalentTo "System.IO.Path.Exists([<NotNullWhenAttribute (true)>] path: string | null) : bool"
[<FactForNETCOREAPP>]
let ``Should display xml doc on a nullable BLC method`` () =
let source = """module Foo
let exists() = System.IO.Path.Exists(null:string)
"""
let checkResults = getCheckResults source [|"--checknulls+";"--langversion:preview"|]
checkResults.GetToolTip(2, 36, "let exists() = System.IO.Path.Exists(null:string)", [ "Exists" ], FSharpTokenTag.Identifier)
|> assertAndExtractTooltip
|> fun (text,xml,remarks) ->
text |> Assert.shouldBeEquivalentTo "System.IO.Path.Exists([<NotNullWhenAttribute (true)>] path: string | null) : bool"
match xml with
| FSharpXmlDoc.FromXmlFile (_dll,sigPath) -> sigPath |> Assert.shouldBeEquivalentTo "M:System.IO.Path.Exists(System.String)"
| _ -> failwith $"Xml wrong type %A{xml}"
[<FactForNETCOREAPP>]
let ``Should display xml doc on fsharp hosted nullable function`` () =
let source = """module Foo
/// This is a xml doc above myFunc
let myFunc(x:string|null) : string | null = x
let exists() = myFunc(null)
"""
let checkResults = getCheckResults source [|"--checknulls+";"--langversion:preview"|]
checkResults.GetToolTip(5, 21, "let exists() = myFunc(null)", [ "myFunc" ], FSharpTokenTag.Identifier)
|> assertAndExtractTooltip
|> fun (text,xml,remarks) ->
match xml with
| FSharpXmlDoc.FromXmlText t ->
t.UnprocessedLines |> Assert.shouldBeEquivalentTo [|" This is a xml doc above myFunc"|]
| _ -> failwith $"xml was %A{xml}"
text |> Assert.shouldBeEquivalentTo "val myFunc: x: string | null -> string | null"
remarks |> Assert.shouldBeEquivalentTo (Some "Full name: Foo.myFunc")
[<FactForNETCOREAPP>]
let ``Should display nullable Csharp code analysis annotations on method return type`` () =
let source = """module Foo
let getPath() = System.IO.Path.GetFileName(null:string)
"""
let checkResults = getCheckResults source [|"--checknulls+";"--langversion:preview"|]
checkResults.GetToolTip(2, 42, "let getPath() = System.IO.Path.GetFileName(null:string)", [ "GetFileName" ], FSharpTokenTag.Identifier)
|> assertAndGetSingleToolTipText
|> Assert.shouldBeEquivalentTo ("""[<return:NotNullIfNotNullAttribute ("path")>]
System.IO.Path.GetFileName(path: string | null) : string | null""" |> normalize)
[<FactForNETCOREAPP>]
let ``Should display nullable Csharp code analysis annotations on TryParse pattern`` () =
let source = """module Foo
let success,version = System.Version.TryParse(null)
"""
let checkResults = getCheckResults source [|"--checknulls+";"--langversion:preview"|]
checkResults.GetToolTip(2, 45, "let success,version = System.Version.TryParse(null)", [ "TryParse" ], FSharpTokenTag.Identifier)
|> assertAndGetSingleToolTipText
|> Assert.shouldBeEquivalentTo ("""System.Version.TryParse([<NotNullWhenAttribute (true)>] input: string | null, [<NotNullWhenAttribute (true)>] result: byref<System.Version | null>) : bool""")
[<FactForNETCOREAPP>]
let ``Display with nullable annotations can be squashed`` () =
let source = """module Foo
let success,version = System.Version.TryParse(null)
"""
let checkResults = getCheckResults source [|"--checknulls+";"--langversion:preview"|]
checkResults.GetToolTip(2, 45, "let success,version = System.Version.TryParse(null)", [ "TryParse" ], FSharpTokenTag.Identifier,width=100)
|> assertAndGetSingleToolTipText
|> Assert.shouldBeEquivalentTo ("""System.Version.TryParse([<NotNullWhenAttribute (true)>] input: string | null,
[<NotNullWhenAttribute (true)>] result: byref<System.Version | null>) : bool""" |> normalize)
[<FactForNETCOREAPP>]
let ``Allows ref struct is shown on BCL interface declaration`` () =
let source = """module Foo
open System
let myAction : Action<int> | null = null
"""
let checkResults = getCheckResults source [|"--checknulls+";"--langversion:preview"|]
checkResults.GetToolTip(3, 21, "let myAction : Action<int> | null = null", [ "Action" ], FSharpTokenTag.Identifier)
|> assertAndGetSingleToolTipText
|> Assert.shouldStartWith ("""type Action<'T (allows ref struct)>""" |> normalize)
[<FactForNETCOREAPP>]
let ``Allows ref struct is shown for each T on BCL interface declaration`` () =
let source = """module Foo
open System
let myAction : Action<int,_,_,_> | null = null
"""
let checkResults = getCheckResults source [|"--checknulls+";"--langversion:preview"|]
checkResults.GetToolTip(3, 21, "let myAction : Action<int,_,_,_> | null = null", [ "Action" ], FSharpTokenTag.Identifier)
|> assertAndGetSingleToolTipText
|> Assert.shouldStartWith ("""type Action<'T1,'T2,'T3,'T4 (allows ref struct and allows ref struct and allows ref struct and allows ref struct)>""" |> normalize)
[<FactForNETCOREAPP>]
let ``Allows ref struct is shown on BCL method usage`` () =
let source = """module Foo
open System
open System.Collections.Generic
let doIt (dict:Dictionary<'a,'b>) = dict.GetAlternateLookup<'a,'b,ReadOnlySpan<char>>()
"""
let checkResults = getCheckResults source [|"--langversion:preview"|]
checkResults.GetToolTip(4, 59, "let doIt (dict:Dictionary<'a,'b>) = dict.GetAlternateLookup<'a,'b,ReadOnlySpan<char>>()", [ "GetAlternateLookup" ], FSharpTokenTag.Identifier)
|> assertAndGetSingleToolTipText
|> Assert.shouldContain ("""'TAlternateKey (allows ref struct)""" |> normalize)
[<FactForNETCOREAPP>]
let ``Allows ref struct is not shown on BCL interface usage`` () =
let source = """module Foo
open System
let doIt(myAction : Action<int>) = myAction.Invoke(42)
"""
let checkResults = getCheckResults source [|"--langversion:preview"|]
checkResults.GetToolTip(3, 43, "let doIt(myAction : Action<int>) = myAction.Invoke(42)", [ "myAction" ], FSharpTokenTag.Identifier)
|> assertAndGetSingleToolTipText
|> Assert.shouldBeEquivalentTo ("""val myAction: Action<int>""" |> normalize)