forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTooltipTests.fs
More file actions
570 lines (440 loc) · 16.8 KB
/
Copy pathTooltipTests.fs
File metadata and controls
570 lines (440 loc) · 16.8 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
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
563
564
565
566
567
568
569
570
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.EditorServices
open FSharp.Compiler.Symbols
open FSharp.Test
open Xunit
let testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource (expectedContent: string) =
let context = Checker.getResolveContext implSource
let files =
Map.ofArray
[| "A.fsi", SourceText.ofString sigSource
"A.fs", SourceText.ofString context.Source |]
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(context)
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
/// Comment
val bar: a: int -> b: int -> int
"""
let implSource =
"""
module Foo
let bar{caret} a b = a - b
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource "Comment"
[<Fact>]
let ``Display XML doc of signature file for partial AP if implementation doesn't have one`` () =
let sigSource =
"""
module Foo
/// Comment
val (|IsThree|_|): x: int -> int option
"""
let implSource =
"""
module Foo
let (|IsThr{caret}ee|_|) x = if x = 3 then Some x else None
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource "Comment"
[<Fact>]
let ``Display XML doc of signature file for DU if implementation doesn't have one`` () =
let sigSource =
"""
module Foo
/// Comment
type Bar =
| Case1 of int * string
| Case2 of string
"""
let implSource =
"""
module Foo
type Bar{caret} =
| Case1 of int * string
| Case2 of string
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource "Comment"
[<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
/// CommentSig
| BarCase2 of string
"""
let implSource =
"""
module Foo
type Bar =
| BarCase1 of int * string
// CommentImpl
| BarCase2{caret} of string
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource "CommentSig"
[<Fact>]
let ``Display XML doc of signature file for record type if implementation doesn't have one`` () =
let sigSource =
"""
module Foo
/// Comment
type Bar = {
SomeField: int
}
"""
let implSource =
"""
module Foo
type B{caret}ar = {
SomeField: int
}
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource "Comment"
[<Fact>]
let ``Display XML doc of signature file for record field if implementation doesn't have one`` () =
let sigSource =
"""
module Foo
type Bar = {
/// Comment
SomeField: int
}
"""
let implSource =
"""
module Foo
type Bar = {
SomeFiel{caret}d: int
}
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource "Comment"
[<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 B{caret}ar() =
member val Foo = "bla" with get, set
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource "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
/// Comment1
member Foo: string
/// Comment2
member Func: int -> int -> int
"""
let implSource =
"""
module Foo
type Bar() =
member val Foo = "bla" with get, set
member _.Func{caret} x y = x * y
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource "Comment2"
[<Fact>]
let ``Display XML doc of signature file for module if implementation doesn't have one`` () =
let sigSource =
"""
/// Comment
module Foo
val a: int
"""
let implSource =
"""
module Fo{caret}o
let a = 23
"""
testXmlDocFallbackToSigFileWhileInImplFile sigSource implSource "Comment"
let testToolTipSquashing source =
let context = Checker.getResolveContext source
let files = Map.ofArray [| "A.fs", SourceText.ofString context.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(context)
let (ToolTipText tooltipElementsSquashed) = checkResults.GetTooltip(context, 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`` () =
testToolTipSquashing """
module Foo
let bar{caret} (fileName: string) (fileVersion: int) (sourceText: string) (options: int) (userOpName: string) = 0
"""
[<Fact>]
let ``Squashed tooltip of record with long field signature should have newlines added`` () =
testToolTipSquashing """
module Foo
type Fo{caret}o =
{ Field1: string
Field2: (string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string * string) }
"""
[<Fact>]
let ``Squashed tooltip of DU with long case signature should have newlines added`` () =
testToolTipSquashing """
module Foo
type SomeDis{caret}cUnion =
| 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)
"""
[<Fact>]
let ``Squashed tooltip of constructor with long signature should have newlines added`` () =
testToolTipSquashing """
module Foo
type Some{caret}Class(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
"""
[<Fact>]
let ``Squashed tooltip of property with long signature should have newlines added`` () =
testToolTipSquashing """
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.Ab{caret}c
"""
let getCheckResults source options =
let fileName, options =
mkTestFileAndOptions
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`` () =
Checker.getTooltip """
namespace Foo
/// Some comment on class
type Bar() =
/// Some comment on class member
member val Fo{caret}o = "bla" with get, set
"""
|> assertAndGetSingleToolTipText
|> Assert.shouldBeEquivalentTo "property Bar.Foo: string with get, set"
[<Theory>]
[<InlineData("(string | null) list", "val x: (string | null) list")>]
[<InlineData("(int -> int) | null", "val x: (int -> int) | null")>]
[<InlineData("(string | null) * int", "val x: (string | null) * int")>]
let ``Should display correct nullable types`` declaredType tooltip =
Checker.getTooltip $"""
module Foo
let f (x{{caret}}: {declaredType}) = ()
"""
|> assertAndGetSingleToolTipText
|> Assert.shouldBeEquivalentTo tooltip
[<FactForNETCOREAPP>]
let ``Should display nullable Csharp code analysis annotations on method argument`` () =
Checker.getTooltipWithOptions [|"--checknulls+";"--langversion:preview"|] """
module Foo
let exists() = System.IO.Path.Exist{caret}s(null:string)
"""
|> assertAndGetSingleToolTipText
|> Assert.shouldBeEquivalentTo "System.IO.Path.Exists([<NotNullWhenAttribute (true)>] path: string | null) : bool"
[<FactForNETCOREAPP>]
let ``Should display xml doc on a nullable BLC method`` () =
Checker.getTooltipWithOptions [|"--checknulls+";"--langversion:preview"|] """
module Foo
let exists() = System.IO.Path.Exi{caret}sts(null:string)
"""
|> 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`` () =
Checker.getTooltipWithOptions [|"--checknulls+";"--langversion:preview"|] """
module Foo
/// This is a xml doc above myFunc
let myFunc(x:string|null) : string | null = x
let exists() = myFu{caret}nc(null)
"""
|> 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`` () =
Checker.getTooltipWithOptions [|"--checknulls+";"--langversion:preview"|] """
module Foo
let getPath() = System.IO.Path.GetFile{caret}Name(null:string)
"""
|> 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`` () =
Checker.getTooltipWithOptions [|"--checknulls+";"--langversion:preview"|] """
module Foo
let success,version = System.Version.TryPar{caret}se(null)
"""
|> 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`` () =
Checker.getTooltipWithOptions [|"--checknulls+";"--langversion:preview"|] """
module Foo
let success,version = System.Version.Try{caret}Parse(null)
"""
|> 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`` () =
Checker.getTooltipWithOptions [|"--checknulls+";"--langversion:preview"|] """
module Foo
open System
let myAction : Acti{caret}on<int> | null = null
"""
|> assertAndGetSingleToolTipText
|> Assert.shouldStartWith ("""type Action<'T (allows ref struct)>""" |> normalize)
[<FactForNETCOREAPP>]
let ``Allows ref struct is shown for each T on BCL interface declaration`` () =
Checker.getTooltipWithOptions [|"--checknulls+";"--langversion:preview"|] """
module Foo
open System
let myAction : Acti{caret}on<int,_,_,_> | null = null
"""
|> 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`` () =
Checker.getTooltip """
module Foo
open System
open System.Collections.Generic
let doIt (dict:Dictionary<'a,'b>) = dict.GetAltern{caret}ateLookup<'a,'b,ReadOnlySpan<char>>()
"""
|> assertAndGetSingleToolTipText
|> Assert.shouldContain ("""'TAlternateKey (allows ref struct)""" |> normalize)
[<FactForNETCOREAPP>]
let ``Allows ref struct is not shown on BCL interface usage`` () =
Checker.getTooltip """
module Foo
open System
let doIt(myAction : Action<int>) = myAc{caret}tion.Invoke(42)
"""
|> assertAndGetSingleToolTipText
|> Assert.shouldBeEquivalentTo ("""val myAction: Action<int>""" |> normalize)
[<Fact>]
let ``Super type should be formatted in the prefix style`` () =
Checker.getTooltip """
namespace Foo
type A{caret} =
inherit seq<int list>
"""
|> assertAndGetSingleToolTipText
|> Assert.shouldBeEquivalentTo "type A =\n inherit seq<int list>"
[<Fact>]
let ``Interface impl should be formatted in the prefix style`` () =
Checker.getTooltip """
namespace Foo
type A{caret} =
interface seq<int list> with
"""
|> assertAndGetSingleToolTipText
|> Assert.shouldBeEquivalentTo "type A =\n interface seq<int list>"
[<Fact>]
let ``Flexible generic type should be formatted in the prefix style`` () =
Checker.getTooltip """
module Foo
let f (x{caret}: #seq<int list>) = ()
"""
|> assertAndGetSingleToolTipText
|> Assert.shouldBeEquivalentTo "val x: #seq<int list>"