forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickInfoTests.fs
653 lines (532 loc) · 16.6 KB
/
QuickInfoTests.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Editor.Tests
open Microsoft.VisualStudio.FSharp.Editor
open Microsoft.VisualStudio.FSharp.Editor.QuickInfo
open Xunit
open FSharp.Editor.Tests.Helpers
module QuickInfo =
open FSharp.Compiler.EditorServices
let private GetCaretPosition (codeWithCaret: string) =
let caretSentinel = "$$"
let mutable cursorInfo: (int * string) = (0, null)
// find the '$$' sentinel that represents the cursor location
let caretPosition = codeWithCaret.IndexOf(caretSentinel)
if caretPosition >= 0 then
let newContents =
codeWithCaret.Substring(0, caretPosition)
+ codeWithCaret.Substring(caretPosition + caretSentinel.Length)
cursorInfo <- caretPosition, newContents
cursorInfo
let internal GetQuickInfo (code: string) caretPosition =
asyncMaybe {
let document =
RoslynTestHelpers.CreateSolution(code) |> RoslynTestHelpers.GetSingleDocument
let! _, _, _, tooltip = FSharpAsyncQuickInfoSource.TryGetToolTip(document, caretPosition)
return tooltip
}
|> Async.RunSynchronously
let GetQuickInfoTextFromCode (codeWithCaret: string) =
let caretPosition, code = GetCaretPosition codeWithCaret
let sigHelp = GetQuickInfo code caretPosition
match sigHelp with
| Some (ToolTipText elements) when not elements.IsEmpty ->
let documentationBuilder =
{ new IDocumentationBuilder with
override _.AppendDocumentationFromProcessedXML(_, _, _, _, _, _) = ()
override _.AppendDocumentation(_, _, _, _, _, _, _) = ()
}
let _, mainDescription, docs =
XmlDocumentation.BuildSingleTipText(documentationBuilder, elements.Head, XmlDocumentation.DefaultLineLimits)
let mainTextItems = mainDescription |> Seq.map (fun x -> x.Text)
let docTextItems = docs |> Seq.map (fun x -> x.Text)
System.String.Join(System.String.Empty, (Seq.concat [ mainTextItems; docTextItems ]))
| _ -> ""
let expectedLines (lines: string list) = System.String.Join("\n", lines)
[<Fact>]
let ``Automation.EnumDUInterfacefromFSBrowse.InsideComputationExpression`` () =
let code =
"""
namespace FsTest
type MyColors =
| Red = 0
| Green = 1
| Blue = 2
module Test =
let test() =
let x =
seq {
for i in 1..10 do
let f = MyColors.Re$$d
yield f
}
()
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected = "MyColors.Red: MyColors = 0"
Assert.Equal(expected, quickInfo)
[<Fact>]
let ``Automation.EnumDUInterfacefromFSBrowse.InsideMatch`` () =
let code =
"""
namespace FsTest
type MyDistance =
| Kilometers of float
| Miles of float
| NauticalMiles of float
module Test =
let test() =
let myDuList = (fun x ->
match x with
| 0 -> MyDistanc$$e.Kilometers
| 1 -> MyDistance.Miles
| _ -> MyDistance.NauticalMiles
)
()
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected =
expectedLines
[
"type MyDistance ="
" | Kilometers of float"
" | Miles of float"
" | NauticalMiles of float"
"Full name: FsTest.MyDistance"
]
Assert.Equal(expected, quickInfo)
[<Fact>]
let ``Automation.EnumDUInterfacefromFSBrowse.InsideLambda`` () =
let code =
"""
namespace FsTest
type IMyInterface =
interface
abstract Represent : unit -> string
end
type MyTestType() =
[<DefaultValue>]
val mutable field : int
interface IMyInterface with
member this.Represent () = "Implement Interface"
module Test =
let test() =
let s = new MyTestType()
|> fun (x:MyTestType) -> x :> IMyInterface
|> fun (x:IMyInterface) -> x.Represen$$t()
()
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected = "abstract IMyInterface.Represent: unit -> string"
Assert.Equal(expected, quickInfo)
[<Fact>]
let ``Automation.RecordAndInterfaceFromFSProj.InsideComputationExpression`` () =
let code =
"""
namespace FsTest
type MyEmployee =
{ mutable Name : string;
mutable Age : int;
mutable IsFTE : bool }
module Test =
let test() =
let construct =
seq {
for i in 1..10 do
let a = MyEmploye$$e.MakeDummy()
()
}
()
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected =
expectedLines
[
"type MyEmployee ="
" {"
" mutable Name: string"
" mutable Age: int"
" mutable IsFTE: bool"
" }"
"Full name: FsTest.MyEmployee"
]
Assert.Equal(expected, quickInfo)
()
[<Fact>]
let ``Automation.RecordAndInterfaceFromFSProj.InsideQuotation`` () =
let code =
"""
namespace FsTest
type MyEmployee =
{ mutable Name : string;
mutable Age : int;
mutable IsFTE : bool }
module Test =
let test() =
let aa = { Name: "name";
Age: 1;
IsFTE: false; }
let b = <@ a$$a.Name @>
()
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected = "val aa: MyEmployee"
Assert.Equal(expected, quickInfo)
()
[<Fact>]
let ``Automation.RecordAndInterfaceFromFSProj.InsideLambda`` () =
let code =
"""
namespace FsTest
type MyEmployee =
{ mutable Name : string;
mutable Age : int;
mutable IsFTE : bool }
module Test =
let test() =
let aa = { Name: "name";
Age: 1;
IsFTE: false; }
let b =
[ aa ]
|> List.filter (fun e -> e.IsFT$$E)
()
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected = "MyEmployee.IsFTE: bool"
Assert.Equal(expected, quickInfo)
()
[<Fact>]
let ``Automation.TupleRecordFromFSBrowse.InsideComputationExpression`` () =
let code =
"""
namespace FsTest
module Test =
let GenerateTuple =
fun x ->
let tuple = (x, x.ToString(), (float)x, (fun y -> (y.ToString(), y + 1)))
tuple
let test() =
let mySeq =
seq {
for i in 1..9 do
let my$$Tuple = GenerateTuple i
yield myTuple
}
()
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected = "val myTuple: int * string * float * (int -> string * int)"
Assert.Equal(expected, quickInfo)
()
[<Fact>]
let ``Automation.TupleRecordFromFSBrowse.SequenceOfMethods`` () =
let code =
"""
namespace FsTest
module Test =
let GenerateTuple =
fun x ->
let tuple = (x, x.ToString(), (float)x, (fun y -> (y.ToString(), y + 1)))
tuple
let GetTupleMethod tuple =
let (_intInTuple, _stringInTuple, _floatInTuple, methodInTuple) = tuple
methodInTuple
let test() =
let mySeq =
seq {
for i in 1..9 do
let myTuple = GenerateTuple i
yield myTuple
}
let method$$Seq = Seq.map GetTupleMethod mySeq
()
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected = "val methodSeq: seq<(int -> string * int)>"
Assert.Equal(expected, quickInfo)
()
[<Fact>]
let ``Automation.UnionAndStructFromFSProj.MatchExpression`` () =
let code =
"""
namespace FsTest
[<Struct>]
type MyPoint(x:int, y:int) =
member this.X = x
member this.Y = y
module Test =
let test() =
let p1 = MyPoint(1, 2)
match p$$1 with
| p3 when p3.X = 1 -> 0
| _ -> 1
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected = "val p1: MyPoint"
Assert.Equal(expected, quickInfo)
()
[<Fact>]
let ``Automation.UnionAndStructFromFSProj.MatchPattern`` () =
let code =
"""
namespace FsTest
[<Struct>]
type MyPoint(x:int, y:int) =
member this.X = x
member this.Y = y
module Test =
let test() =
let p1 = MyPoint(1, 2)
match p1 with
| p$$3 when p3.X = 1 -> 0
| _ -> 1
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected = "val p3: MyPoint"
Assert.Equal(expected, quickInfo)
()
[<Fact>]
let ``Automation.UnionAndStructFromFSProj.UnionIfPredicate`` () =
let code =
"""
namespace FsTest
type MyDistance =
| Kilometers of float
| Miles of float
| NauticalMiles of float
module Test =
let test() =
let dd = MyDistance.Kilometers 1.0
if MyDistance.toMiles d$$d > 0 then ()
else ()
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected = "val dd: MyDistance"
Assert.Equal(expected, quickInfo)
()
[<Fact>]
let ``Automation.UnionAndStructFromFSProj.UnionForPattern`` () =
let code =
"""
namespace FsTest
type MyDistance =
| Kilometers of float
| Miles of float
| NauticalMiles of float
module Test =
let test() =
let distances = [ MyDistance.Kilometers 1.0; MyDistance.Miles 1.0 ]
for dist$$ance in distances do
()
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected = "val distance: MyDistance"
Assert.Equal(expected, quickInfo)
()
[<Fact>]
let ``Automation.UnionAndStructFromFSProj.UnionMethodPatternMatch`` () =
let code =
"""
namespace FsTest
type MyDistance =
| Kilometers of float
| Miles of float
| NauticalMiles of float
static member toMiles x =
Miles(
match x with
| Miles x -> x
| Kilometers x -> x / 1.6
| NauticalMiles x -> x * 1.15
)
module Test =
let test() =
let dd = MyDistance.Kilometers 1.0
match MyDistance.to$$Miles dd with
| Miles x -> 0
| _ -> 1
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected = "static member MyDistance.toMiles: x: MyDistance -> MyDistance"
Assert.Equal(expected, quickInfo)
()
[<Fact>]
let ``Automation.UnionAndStructFromFSProj.UnionMethodPatternMatchBody`` () =
let code =
"""
namespace FsTest
type MyDistance =
| Kilometers of float
| Miles of float
| NauticalMiles of float
member this.IncreaseBy dist =
match this with
| Kilometers x -> Kilometers (x + dist)
| Miles x -> Miles (x + dist)
| NauticalMiles x -> NauticalMiles (x + dist)
module Test =
let test() =
let dd = MyDistance.Kilometers 1.0
match dd.toMiles() with
| Miles x -> dd.Increase$$By 1.0
| _ -> dd
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected = "member MyDistance.IncreaseBy: dist: float -> MyDistance"
Assert.Equal(expected, quickInfo)
()
[<Fact>]
let ``Automation.UnionAndStructFromFSProj.UnionPropertyInComputationExpression`` () =
let code =
"""
namespace FsTest
type MyDistance =
| Kilometers of float
| Miles of float
| NauticalMiles of float
member this.asNautical =
NauticalMiles(
match this with
| Kilometers x -> x / 1.852
| Miles x -> x / 1.15
| NauticalMiles x -> x
)
module Test =
let test() =
let dd = MyDistance.Kilometers 1.0
async {
let r = dd.as$$Nautical
return r
}
"""
let quickInfo = GetQuickInfoTextFromCode code
let expected = "property MyDistance.asNautical: MyDistance with get"
Assert.Equal(expected, quickInfo)
()
[<Fact>]
let ``Automation.LetBindings.Module`` () =
let code =
"""
namespace FsTest
module Test =
let fu$$nc x = ()
"""
let expectedSignature = "val func: x: 'a -> unit"
let tooltip = GetQuickInfoTextFromCode code
Assert.StartsWith(expectedSignature, tooltip)
()
[<Fact>]
let ``Automation.LetBindings.InsideType.Instance`` () =
let code =
"""
namespace FsTest
module Test =
type T() =
let fu$$nc x = ()
"""
let expectedSignature = "val func: x: 'a -> unit"
let tooltip = GetQuickInfoTextFromCode code
Assert.StartsWith(expectedSignature, tooltip)
[<Fact>]
let ``Automation.LetBindings.InsideType.Static`` () =
let code =
"""
namespace FsTest
module Test =
type T() =
static let fu$$nc x = ()
"""
let expectedSignature = "val func: x: 'a -> unit"
let tooltip = GetQuickInfoTextFromCode code
Assert.StartsWith(expectedSignature, tooltip)
()
[<Fact>]
let ``Automation.LetBindings`` () =
let code =
"""
namespace FsTest
module Test =
do
let fu$$nc x = ()
()
"""
let expectedSignature = "val func: x: 'a -> unit"
let tooltip = GetQuickInfoTextFromCode code
Assert.StartsWith(expectedSignature, tooltip)
[<Fact>]
let ``quick info for IWSAM property get`` () =
let code =
"""
type IStaticProperty<'T when 'T :> IStaticProperty<'T>> =
static abstract StaticProperty: 'T
let f_IWSAM_flex_StaticProperty(x: #IStaticProperty<'T>) =
'T.StaticPr$$operty
"""
let expectedSignature = "property IStaticProperty.StaticProperty: 'T with get"
let tooltip = GetQuickInfoTextFromCode code
Assert.StartsWith(expectedSignature, tooltip)
[<Fact>]
let ``quick info for IWSAM method call`` () =
let code =
"""
type IStaticMethod<'T when 'T :> IStaticMethod<'T>> =
static abstract StaticMethod: unit -> 'T
let f (x: #IStaticMethod<'T>) =
'T.StaticMe$$thod()
"""
let expectedSignature = "static abstract IStaticMethod.StaticMethod: unit -> 'T"
let tooltip = GetQuickInfoTextFromCode code
Assert.StartsWith(expectedSignature, tooltip)
[<Fact>]
let ``quick info for SRTP property get`` () =
let code =
"""
let inline f_StaticProperty_SRTP<'T when 'T : (static member StaticProperty: 'T) >() =
'T.StaticPr$$operty
"""
let expectedSignature = "'T: (static member StaticProperty: 'T)"
let tooltip = GetQuickInfoTextFromCode code
Assert.StartsWith(expectedSignature, tooltip)
[<Fact>]
let ``quick info for SRTP method call`` () =
let code =
"""
let inline f_StaticProperty_SRTP<'T when 'T : (static member StaticMethod: unit -> 'T) >() =
'T.StaticMe$$thod()
"""
let expectedSignature = "'T: (static member StaticMethod: unit -> 'T)"
let tooltip = GetQuickInfoTextFromCode code
Assert.StartsWith(expectedSignature, tooltip)
[<Fact>]
let ``Display names for exceptions with backticks preserve backticks`` () =
let code =
"""
exception SomeError of ``thing wi$$th space``: string
"""
let expected = "``thing with space``"
let actual = GetQuickInfoTextFromCode code
Assert.Contains(expected, actual)
()
[<Fact>]
let ``Display names for anonymous record fields with backticks preserve backticks`` () =
let code =
"""
type R = {| ``thing wi$$th space``: string |}
"""
let expected = "``thing with space``"
let actual = GetQuickInfoTextFromCode code
Assert.Contains(expected, actual)
()
[<Fact>]
let ``Display names identifiers for active patterns with backticks preserve backticks`` () =
let code =
"""
let (|``Thing with space``|_|) x = if x % 2 = 0 then Some (x/2) else None
match 4 with
| ``Thing wi$$th space`` _ -> "yes"
| _ -> "no"
"""
let expected = "``Thing with space``"
let actual = GetQuickInfoTextFromCode code
Assert.Contains(expected, actual)
()