forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndentationServiceTests.fs
212 lines (169 loc) · 5.66 KB
/
IndentationServiceTests.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
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Editor.Tests
open Xunit
open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.Text
open Microsoft.VisualStudio.FSharp.Editor
open FSharp.Compiler.CodeAnalysis
open Microsoft.CodeAnalysis.Formatting
open FSharp.Editor.Tests.Helpers
open FSharp.Test
type IndentationServiceTests() =
let checker = FSharpChecker.Create()
let filePath = "C:\\test.fs"
let documentId = DocumentId.CreateNewId(ProjectId.CreateNewId())
let tabSize = 4
let indentStyle = FormattingOptions.IndentStyle.Smart
let indentComment =
System.Text.RegularExpressions.Regex(@"\$\s*Indent:\s*(\d+)\s*\$")
let consoleProjectTemplate =
"
// Learn more about F# at https://fsharp.org
// See the 'F# Tutorial' project for more help.
[<EntryPoint>]
let main argv =
printfn \"%A\" argv
0 // return an integer exit code"
let libraryProjectTemplate =
"
namespace ProjectNamespace
type Class1() =
member this.X = \"F#\""
let nestedTypesTemplate =
"
namespace testspace
type testtype
static member testmember = 1
"
let autoIndentTemplate =
"
let plus x y =
x + y // $Indent: 4$
let mutable x = 0
x <-
10 * 2 // $Indent: 4$
match some 10 with
| None -> 0
| Some x ->
x + 1 // $Indent: 4$
try
failwith \"fail\" // $Indent: 4$
with
| :? System.Exception -> \"error\"
if 10 > 0 then
true // $Indent: 4$
else
false // $Indent: 4$
(
1, // $Indent: 4$
2
)
[
1 // $Indent: 4$
2
]
[|
1 // $Indent: 4$
2
|]
[<
Literal // $Indent: 4$
>]
let constx = 10
let t = seq { // $Indent: 0$
yield 1 // $Indent: 4$
}
let g =
function
| None -> 1 // $Indent: 4$
| Some _ -> 0
module MyModule = begin
end // $Indent: 4$
type MyType() = class
end // $Indent: 4$
type MyStruct = struct
end // $Indent: 4$
while true do
printfn \"never end\" // $Indent: 4$
// After line has keyword in comment such as function
// should not be indented $Indent: 0$
// Even if the line before only had comment like this
// The follwing line should inherit that indentation too $Indent: 4$
"
let testCases =
[|
(None, 0, consoleProjectTemplate)
(None, 1, consoleProjectTemplate)
(Some(0), 2, consoleProjectTemplate)
(Some(0), 3, consoleProjectTemplate)
(Some(0), 4, consoleProjectTemplate)
(Some(0), 5, consoleProjectTemplate)
(Some(4), 6, consoleProjectTemplate)
(Some(4), 7, consoleProjectTemplate)
(Some(4), 8, consoleProjectTemplate)
(None, 0, libraryProjectTemplate)
(None, 1, libraryProjectTemplate)
(Some(0), 2, libraryProjectTemplate)
(Some(0), 3, libraryProjectTemplate)
(Some(4), 4, libraryProjectTemplate)
(Some(4), 5, libraryProjectTemplate)
(None, 0, nestedTypesTemplate)
(None, 1, nestedTypesTemplate)
(Some(0), 2, nestedTypesTemplate)
(Some(4), 3, nestedTypesTemplate)
(Some(8), 4, nestedTypesTemplate)
(Some(8), 5, nestedTypesTemplate)
|]
let autoIndentTestCases =
autoIndentTemplate.Split [| '\n' |]
|> Array.map (fun s -> s.Trim())
|> Array.indexed
|> Array.choose (fun (line, text) ->
let m = indentComment.Match text
if m.Success then
Some(line, System.Convert.ToInt32 m.Groups.[1].Value)
else
None)
|> Array.map (fun (lineNumber, expectedIndentation) -> (Some(expectedIndentation), lineNumber, autoIndentTemplate))
[<Fact>]
member this.TestIndentation() =
for (expectedIndentation, lineNumber, template) in testCases do
let sourceText = SourceText.From(template)
let parsingOptions, _ =
checker.GetParsingOptionsFromProjectOptions RoslynTestHelpers.DefaultProjectOptions
let actualIndentation =
FSharpIndentationService.GetDesiredIndentation(
documentId,
sourceText,
filePath,
lineNumber,
tabSize,
indentStyle,
parsingOptions
)
match expectedIndentation with
| None -> Assert.True(actualIndentation.IsNone, $"No indentation was expected at line {lineNumber}")
| Some indentation ->
actualIndentation.Value
|> Assert.shouldBeEqualWith indentation $"Indentation on line {lineNumber} doesn't match"
[<Fact>]
member this.TestAutoIndentation() =
for (expectedIndentation, lineNumber, template) in autoIndentTestCases do
let sourceText = SourceText.From(template)
let parsingOptions, _ =
checker.GetParsingOptionsFromProjectOptions RoslynTestHelpers.DefaultProjectOptions
let actualIndentation =
FSharpIndentationService.GetDesiredIndentation(
documentId,
sourceText,
filePath,
lineNumber,
tabSize,
indentStyle,
parsingOptions
)
match expectedIndentation with
| None -> Assert.True(actualIndentation.IsNone, $"No indentation was expected at line {lineNumber}")
| Some indentation ->
actualIndentation.Value
|> Assert.shouldBeEqualWith indentation $"Indentation on line {lineNumber} doesn't match"