forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BraceMatchingServiceTests.fs
222 lines (187 loc) · 7.79 KB
/
BraceMatchingServiceTests.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
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Editor.Tests
open System
open Xunit
open Microsoft.CodeAnalysis.Text
open FSharp.Compiler.CodeAnalysis
open Microsoft.VisualStudio.FSharp.Editor
open FSharp.Editor.Tests.Helpers
type BraceMatchingServiceTests() =
let checker = FSharpChecker.Create()
let fileName = "C:\\test.fs"
member private this.VerifyNoBraceMatch(fileContents: string, marker: string) =
let sourceText = SourceText.From(fileContents)
let position = fileContents.IndexOf(marker)
Assert.True(position >= 0, $"Cannot find marker '{marker}' in file contents")
let parsingOptions, _ =
checker.GetParsingOptionsFromProjectOptions RoslynTestHelpers.DefaultProjectOptions
match
FSharpBraceMatchingService.GetBraceMatchingResult(checker, sourceText, fileName, parsingOptions, position, "UnitTest")
|> Async.RunImmediateExceptOnUI
with
| None -> ()
| Some (left, right) -> failwith $"Found match for brace '{marker}'"
member private this.VerifyBraceMatch(fileContents: string, startMarker: string, endMarker: string) =
let sourceText = SourceText.From(fileContents)
let startMarkerPosition = fileContents.IndexOf(startMarker)
let endMarkerPosition = fileContents.IndexOf(endMarker)
Assert.True(startMarkerPosition >= 0, $"Cannot find start marker '{startMarkerPosition}' in file contents")
Assert.True(endMarkerPosition >= 0, $"Cannot find end marker '{endMarkerPosition}' in file contents")
let parsingOptions, _ =
checker.GetParsingOptionsFromProjectOptions RoslynTestHelpers.DefaultProjectOptions
match
FSharpBraceMatchingService.GetBraceMatchingResult(
checker,
sourceText,
fileName,
parsingOptions,
startMarkerPosition,
"UnitTest"
)
|> Async.RunImmediateExceptOnUI
with
| None -> failwith $"Didn't find a match for start brace at position '{startMarkerPosition}"
| Some (left, right) ->
let endPositionInRange (range) =
let span = RoslynHelpers.FSharpRangeToTextSpan(sourceText, range)
span.Start <= endMarkerPosition && endMarkerPosition <= span.End
Assert.True(endPositionInRange (left) || endPositionInRange (right), "Found end match at incorrect position")
[<Theory>]
// Starting Brace
[<InlineData("(marker1", ")marker1")>]
[<InlineData("{marker2", "}marker2")>]
[<InlineData("(marker3", ")marker3")>]
[<InlineData("[marker4", "]marker4")>]
// Ending Brace
[<InlineData(")marker1", "(marker1")>]
[<InlineData("}marker2", "{marker2")>]
[<InlineData(")marker3", "(marker3")>]
[<InlineData("]marker4", "[marker4")>]
member this.NestedBrackets(startMarker: string, endMarker: string) =
let code =
"
(marker1
{marker2
(marker3
[marker4
]marker4
)marker3
}marker2
)marker1"
this.VerifyBraceMatch(code, startMarker, endMarker)
[<Fact>]
member this.BracketInExpression() =
this.VerifyBraceMatch("let x = (3*5)-1", "(3*", ")-1")
[<Fact>]
member this.BraceInInterpolatedStringSimple() =
this.VerifyBraceMatch("let x = $\"abc{1}def\"", "{1", "}def")
[<Fact>]
member this.BraceInInterpolatedStringTwoHoles() =
this.VerifyBraceMatch("let x = $\"abc{1}def{2+3}hij\"", "{2", "}hij")
[<Fact>]
member this.BraceInInterpolatedStringNestedRecord() =
this.VerifyBraceMatch("let x = $\"abc{ id{contents=3}.contents }\"", "{contents", "}.contents")
this.VerifyBraceMatch("let x = $\"abc{ id{contents=3}.contents }\"", "{ id", "}\"")
[<Theory>]
[<InlineData("[start")>]
[<InlineData("]end")>]
member this.BraceInMultiLineCommentShouldNotBeMatched(startMarker: string) =
let code =
"
let x = 3
(* This [start
is a multiline
comment ]end
*)
printf \"%d\" x"
this.VerifyNoBraceMatch(code, startMarker)
[<Fact>]
member this.BraceInAttributesMatch() =
let code =
"
[<Attribute>]
module internal name"
this.VerifyBraceMatch(code, "[<", ">]")
[<Fact>]
member this.BraceEncapsulatingACommentShouldBeMatched() =
let code =
"
let x = 3 + (start
(* this is a comment *)
)end"
this.VerifyBraceMatch(code, "(start", ")end")
[<Theory>]
[<InlineData("(endsInComment")>]
[<InlineData(")endsInComment")>]
[<InlineData("<startsInComment")>]
[<InlineData(">startsInComment")>]
member this.BraceStartingOrEndingInCommentShouldNotBeMatched(startMarker: string) =
let code =
"
let x = 123 + (endsInComment
(* )endsInComment <startsInComment *)
>startsInComment"
this.VerifyNoBraceMatch(code, startMarker)
[<Theory>]
[<InlineData("(endsInDisabledCode")>]
[<InlineData(")endsInDisabledCode")>]
[<InlineData("<startsInDisabledCode")>]
[<InlineData(">startsInDisabledCode")>]
member this.BraceStartingOrEndingInDisabledCodeShouldNotBeMatched(startMarker: string) =
let code =
"
let x = 123 + (endsInDisabledCode
#if UNDEFINED
)endsInDisabledCode <startsInDisabledCode
#endif
>startsInDisabledCode"
this.VerifyNoBraceMatch(code, startMarker)
[<Theory>]
[<InlineData("(endsInString")>]
[<InlineData(")endsInString")>]
[<InlineData("<startsInString")>]
[<InlineData(">startsInString")>]
member this.BraceStartingOrEndingInStringShouldNotBeMatched(startMarker: string) =
let code =
"
let x = \"stringValue\" + (endsInString +
\" )endsInString <startsInString \" +
+ >startsInString"
this.VerifyNoBraceMatch(code, startMarker)
[<Fact>]
member this.BraceMatchingAtEndOfLine_Bug1597() =
// https://github.com/dotnet/fsharp/issues/1597
let code =
"""
[<EntryPoint>]
let main argv =
let arg1 = ""
let arg2 = ""
let arg3 = ""
(printfn "%A '%A' '%A'" (arg1) (arg2) (arg3))endBrace
0 // return an integer exit code"""
this.VerifyBraceMatch(code, "(printfn", ")endBrace")
[<Theory>]
[<InlineData("let a1 = [ 0 .. 100 ]", 9, 20)>]
[<InlineData("let a2 = [| 0 .. 100 |]", 9, 10, 22)>]
[<InlineData("let a3 = <@ 0 @>", 9, 10, 15)>]
[<InlineData("let a4 = <@@ 0 @@>", 9, 10, 11, 15, 16)>]
[<InlineData("let a6 = ( () )", 9, 16)>]
[<InlineData("[<ReflectedDefinition>]\nlet a7 = 70", 0, 1, 22)>]
[<InlineData("let a8 = seq { yield() }", 13, 23)>]
member this.DoNotMatchOnInnerSide(fileContents: string, [<ParamArray>] matchingPositions: int[]) =
let sourceText = SourceText.From(fileContents)
let parsingOptions, _ =
checker.GetParsingOptionsFromProjectOptions RoslynTestHelpers.DefaultProjectOptions
for position in matchingPositions do
match
FSharpBraceMatchingService.GetBraceMatchingResult(checker, sourceText, fileName, parsingOptions, position, "UnitTest")
|> Async.RunSynchronously
with
| Some _ -> ()
| None ->
match position with
| 0 -> ""
| _ -> fileContents.[position - 1] |> sprintf " (previous character '%c')"
|> sprintf "Didn't find a matching brace at position '%d' %s" position
|> raise (exn ())