forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpContextServiceTests.fs
421 lines (357 loc) · 14.1 KB
/
HelpContextServiceTests.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
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Editor.Tests
open System
open System.Threading
open Xunit
open Microsoft.CodeAnalysis
open Microsoft.VisualStudio.FSharp.Editor
open Microsoft.IO
open FSharp.Editor.Tests.Helpers
open Microsoft.CodeAnalysis.Text
open Microsoft.VisualStudio.FSharp.Editor.CancellableTasks
type HelpContextServiceTests() =
let getMarkers (source: string) =
let mutable cnt = 0
[
for i in 0 .. (source.Length - 1) do
if source.[i] = '$' then
yield (i - cnt)
cnt <- cnt + 1
]
let TestF1KeywordsWithOptions (expectedKeywords: string option list, lines: string list) =
let fileContentsWithMarkers = String.Join("\r\n", lines)
let fileContents = fileContentsWithMarkers.Replace("$", "")
let sourceText = SourceText.From(fileContents)
let document =
RoslynTestHelpers.CreateSolution(fileContents)
|> RoslynTestHelpers.GetSingleDocument
let markers = getMarkers fileContentsWithMarkers
let res =
[
for marker in markers do
let span = Microsoft.CodeAnalysis.Text.TextSpan(marker, 0)
let textLine = sourceText.Lines.GetLineFromPosition(marker)
let documentId = DocumentId.CreateNewId(ProjectId.CreateNewId())
let classifiedSpans = ResizeArray<_>()
Tokenizer.classifySpans (
documentId,
sourceText,
textLine.Span,
Some "test.fs",
[],
None,
None,
classifiedSpans,
CancellationToken.None
)
let task =
FSharpHelpContextService.GetHelpTerm(document, span, classifiedSpans)
|> CancellableTask.start CancellationToken.None
task.Result
]
let equalLength = (expectedKeywords.Length = res.Length)
Assert.True(equalLength)
for (exp, res) in List.zip expectedKeywords res do
let exp = Option.defaultValue "" exp
Assert.Equal(exp, res)
let TestF1Keywords (expectedKeywords, lines) =
TestF1KeywordsWithOptions(expectedKeywords, lines)
#if RELEASE
[<Fact(Skip = "Fails in some CI, reproduces locally in Release mode, needs investigation")>]
#else
[<Fact>]
#endif
member _.``F1 help keyword NoKeyword.Negative``() =
let file =
[
"let s = \"System.Con$sole\""
"let n = 999$99"
"#if UNDEFINED"
" let w = List.re$v []"
"#endif"
]
let keywords = [ None; None; None ]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Preprocessor``() =
let file = [ "#i$f foobaz"; "#e$ndif" ]
let keywords = [ Some "#if_FS"; Some "#endif_FS" ]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Regression.DotNetMethod-854364``() =
let file = [ "let i : int = 42"; "i.ToStri$ng()"; "i.ToStri$ng(\"format\")" ]
let keywords = [ Some "System.Int32.ToString"; Some "System.Int32.ToString" ]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Namespaces``() =
let file =
[
"open Syst$em.N$et"
"open System.I$O"
"open Microsoft.FSharp.Core"
""
"System.Cons$ole.WriteLine()"
]
let keywords =
[ Some "System"; Some "System.Net"; Some "System.IO"; Some "System.Console" ]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Namespaces.BeforeDot``() =
let file =
[
"open System$.Net$"
"open System$.IO"
"open System$.Collections$.Generic$"
"open Microsoft.FSharp.Core"
""
"System$.Console$.WriteLine()"
]
let keywords =
[
Some "System"
Some "System.Net"
Some "System"
Some "System"
Some "System.Collections"
Some "System.Collections.Generic"
Some "System"
Some "System.Console"
]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Namespaces.AfterDot``() =
let file =
[
"open $System.$Net"
"open $System.IO"
"open $System.$Collections.$Generic"
"open Microsoft.FSharp.Core"
""
"$System.$Console.$WriteLine()"
]
let keywords =
[
Some "System"
Some "System.Net"
Some "System"
Some "System"
Some "System.Collections"
Some "System.Collections.Generic"
Some "System"
Some "System.Console"
Some "System.Console.WriteLine"
]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword QuotedIdentifiers``() =
let file =
[
"let `$`escaped func`` x y = x + y"
"let ``escaped value`$` = 1"
"let x = 1"
"``escaped func`` x$ ``escaped value``"
"``escaped func``$ x ``escaped value``"
"``escaped func`` x $``escaped value``"
"let ``z$`` = 1"
"``$z`` |> printfn \"%d\""
]
let keywords =
[
Some "Test.escaped func"
Some "Test.escaped value"
Some "Test.x"
Some "Test.escaped func"
Some "Test.escaped value"
Some "Test.z"
Some "Test.z"
]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Attributes``() =
let file =
[
"open System.Runtime.InteropServices"
"open System.Runtime.CompilerServices"
"[<St$ruct>]"
"type X = "
" [<Default$Value(false)>]"
" val mutable f : int"
" [<Me$thodImpl(1s)>]"
" member _.Run() = ()"
"[<StructLayout(LayoutKind.Auto, S$ize=1)>]"
"type Y = class end"
]
let keywords =
[
Some "Microsoft.FSharp.Core.StructAttribute.#ctor"
Some "Microsoft.FSharp.Core.DefaultValueAttribute.#ctor"
Some "System.Runtime.CompilerServices.MethodImplAttribute.#ctor"
Some "System.Runtime.InteropServices.StructLayoutAttribute.Size"
]
TestF1Keywords(keywords, file)
#if RELEASE
[<Fact(Skip = "Fails in some CI, reproduces locally in Release mode, needs investigation")>]
#else
[<Fact>]
#endif
//This test case Verify that when F1 is Hit on TypeProvider namespaces it contain the right keyword
member _.``F1 help keyword TypeProvider.Namespaces``() =
let file = [ "open N$1" ]
let keywords = [ Some "N1" ]
TestF1KeywordsWithOptions(keywords, file)
#if RELEASE
[<Fact(Skip = "Fails in some CI, reproduces locally in Release mode, needs investigation")>]
#else
[<Fact>]
#endif
//This test case Verify that when F1 is Hit on TypeProvider Type it contain the right keyword
member _.``F1 help keyword TypeProvider.type``() =
let file =
[
//Dummy Type Provider exposes a parametric type (N1.T) that takes 2 static params (string * int)
"""let foo = typeof<N1.$T< const "Hello World",2>>"""
]
let keywords = [ Some "N1.T" ]
TestF1KeywordsWithOptions(keywords, file)
[<Fact>]
member _.``F1 help keyword EndOfLine``() =
let file = [ "open System.Net$"; "open System.IO$" ]
let keywords = [ Some "System.Net"; Some "System.IO" ]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword EndOfLine2``() =
let file = [ "module M"; "open System.Net$"; "open System.IO$" ]
let keywords = [ Some "System.Net"; Some "System.IO" ]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Comments``() =
let file = [ "($* co$mment *$)"; "/$/ com$ment" ]
let keywords =
[
Some "comment_FS"
Some "comment_FS"
Some "comment_FS"
Some "comment_FS"
Some "comment_FS"
]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword FSharpEntities``() =
let file =
[
"let (KeyValu$e(k,v)) = null"
"let w : int lis$t = []"
"let b = w.IsEm$pty"
"let m : map<int,int> = Map.empty"
"m.A$dd(1,1)"
"let z = Li$st.r$ev w"
"let o = No$ne"
"let o1 = So$me 1"
"let c : System.IO.Str$eam = null"
"c.Async$Read(10)"
"let r = r$ef 0"
"r.conten$ts"
]
let keywords =
[
Some "Microsoft.FSharp.Core.Operators.KeyValuePattern``2"
Some "Microsoft.FSharp.Collections.FSharpList`1"
Some "Microsoft.FSharp.Collections.FSharpList`1.IsEmpty"
Some "Microsoft.FSharp.Collections.FSharpMap`2.Add"
Some "Microsoft.FSharp.Collections.ListModule"
Some "Microsoft.FSharp.Collections.ListModule.Reverse``1" // per F1 keyword spec - one tick for classes, two ticks for members
Some "Microsoft.FSharp.Core.FSharpOption`1.None"
Some "Microsoft.FSharp.Core.FSharpOption`1.Some"
Some "System.IO.Stream"
Some "Microsoft.FSharp.Control.CommonExtensions.AsyncReadBytes"
Some "Microsoft.FSharp.Core.Operators.Ref``1"
Some "Microsoft.FSharp.Core.FSharpRef`1.contents"
]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Keywords``() =
let file =
[
"l$et r = ref 0"
"r :$= 1"
"let mut$able foo = 1"
"foo <$- 2"
"let$ z = 1"
]
let keywords =
[ Some "let_FS"; Some ":=_FS"; Some "mutable_FS"; Some "<-_FS"; Some "let_FS" ]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Regression.NewInstance-854367``() =
let file = [ "let q : System.Runtime.Remoting.TypeE$ntry = null" ]
let keywords = [ Some "System.Runtime.Remoting.TypeEntry" ]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Regression.NewInstance-854367-2``() =
let file =
[
"let q1 = new System.Runtime.Remoting.Type$Entry()" // this constructor exists but is not accessible (it is protected), but the help entry still goes to the type
]
let keywords = [ Some "System.Runtime.Remoting.TypeEntry" ]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Classes.WebClient``() =
let file = [ "let w : System.Net.Web$Client = new System.Net.Web$Client()" ]
let keywords = [ Some "System.Net.WebClient"; Some "System.Net.WebClient.#ctor" ]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Classes.Object``() =
let file = [ "let w : System.Ob$ject = new System.Obj$ect()" ]
let keywords = [ Some "System.Object"; Some "System.Object.#ctor" ]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Classes.Generic``() =
let file = [ "let x : System.Collections.Generic.L$ist<int> = null" ]
let keywords = [ Some "System.Collections.Generic.List`1" ]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Classes.Abbrev``() =
let file = [ "let z : Resi$zeArray<int> = null" ]
let keywords = [ Some "System.Collections.Generic.List`1" ]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword Members``() =
let file =
[
"open System.Linq"
"open System"
"let l = new ResizeArray<int>()"
"let i = l.Cou$nt"
"l.Ad$d(1)"
"let m = new System.IO.MemoryStream()"
"m.BeginRe$ad()"
"l.Se$lect(fun i -> i + 1)"
"let d = new System.DateTi$me()"
"let s = String.Empty"
"s.Equ$als(null)"
"let i = 12"
"i.ToStr$ing()"
]
let keywords =
[
Some "System.Collections.Generic.List`1.Count"
Some "System.Collections.Generic.List`1.Add"
Some "System.IO.Stream.BeginRead"
Some "System.Linq.Enumerable.Select``2" // per F1 keyword spec - one tick for classes, two ticks for members
Some "System.DateTime.#ctor"
Some "System.String.Equals"
Some "System.Int32.ToString"
]
TestF1Keywords(keywords, file)
[<Fact>]
member _.``F1 help keyword static abstract interface method``() =
let file =
[
"type IStaticProperty<'T when 'T :> IStaticProperty<'T>> ="
" static abstract StaticProperty: 'T"
""
"let f_IWSAM_flex_StaticProperty(x: #IStaticProperty<'T>) ="
" 'T.StaticProp$erty"
]
let keywords = [ Some "Test.IStaticProperty`1.StaticProperty" ]
TestF1Keywords(keywords, file)