forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommentTests.fs
365 lines (333 loc) · 10.1 KB
/
CommentTests.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
module Fantomas.Tests.CommentTests
open NUnit.Framework
open FsUnit
open Fantomas.CodeFormatter
open Fantomas.Tests.TestHelper
[<Test>]
let ``should keep sticky-to-the-left comments after nowarn directives``() =
formatSourceString false """#nowarn "51" // address-of operator can occur in the code""" config
|> should equal """#nowarn "51" // address-of operator can occur in the code
"""
[<Test>]
let ``should keep sticky-to-the-right comments before module definition``() =
formatSourceString false """
// The original idea for this typeprovider is from Ivan Towlson
// some text
module FSharpx.TypeProviders.VectorTypeProvider
let x = 1""" config
|> should equal """// The original idea for this typeprovider is from Ivan Towlson
// some text
module FSharpx.TypeProviders.VectorTypeProvider
let x = 1
"""
[<Test>]
let ``comments on local let bindings``() =
formatSourceString false """
let print_30_permut() =
/// declare and initialize
let permutation : int array = Array.init n (fun i -> Console.Write(i+1); i)
permutation
""" config
|> prepend newline
|> should equal """
let print_30_permut() =
/// declare and initialize
let permutation : int array =
Array.init n (fun i ->
Console.Write(i + 1)
i)
permutation
"""
[<Test>]
let ``comments on local let bindings with desugared lambda``() =
formatSourceString false """
let print_30_permut() =
/// declare and initialize
let permutation : int array = Array.init n (fun (i,j) -> Console.Write(i+1); i)
permutation
""" config
|> prepend newline
|> should equal """
let print_30_permut() =
/// declare and initialize
let permutation : int array =
Array.init n (fun (i, j) ->
Console.Write(i + 1)
i)
permutation
"""
[<Test>]
let ``xml documentation``() =
formatSourceString false """
/// <summary>
/// Kill Weight Mud
/// </summary>
///<param name="sidpp">description</param>
///<param name="tvd">xdescription</param>
///<param name="omw">ydescription</param>
let kwm sidpp tvd omw =
(sidpp / 0.052 / tvd) + omw
/// Kill Weight Mud
let kwm sidpp tvd omw = 1.0""" config
|> prepend newline
|> should equal """
/// <summary>
/// Kill Weight Mud
/// </summary>
///<param name="sidpp">description</param>
///<param name="tvd">xdescription</param>
///<param name="omw">ydescription</param>
let kwm sidpp tvd omw = (sidpp / 0.052 / tvd) + omw
/// Kill Weight Mud
let kwm sidpp tvd omw = 1.0
"""
[<Test>]
let ``should preserve comment-only source code``() =
formatSourceString false """(*
line1
line2
*)
""" config
|> should equal """
(*
line1
line2
*)
"""
[<Test>]
let ``should keep sticky-to-the-right comments``() =
formatSourceString false """
let f() =
// COMMENT
x + x
""" config
|> prepend newline
|> should equal """
let f() =
// COMMENT
x + x
"""
[<Test>]
let ``should keep sticky-to-the-left comments``() =
formatSourceString false """
let f() =
let x = 1 // COMMENT
x + x
""" config
|> prepend newline
|> should equal """
let f() =
let x = 1 // COMMENT
x + x
"""
[<Test>]
let ``should keep well-aligned comments``() =
formatSourceString false """
/// XML COMMENT
// Other comment
let f() =
// COMMENT A
let y = 1
(* COMMENT B *)
(* COMMENT C *)
x + x + x
""" config
|> prepend newline
|> should equal """
/// XML COMMENT
// Other comment
let f() =
// COMMENT A
let y = 1
(* COMMENT B *)
(* COMMENT C *)
x + x + x
"""
[<Test; Ignore>]
let ``should align mis-aligned comments``() =
formatSourceString false """
/// XML COMMENT A
// Other comment
let f() =
// COMMENT A
let y = 1
/// XML COMMENT B
let z = 1
// COMMENT B
x + x + x
""" config
|> prepend newline
|> should equal """
/// XML COMMENT A
// Other comment
let f() =
// COMMENT A
let y = 1
/// XML COMMENT B
let z = 1
// COMMENT B
x + x + x
"""
[<Test>]
let ``should indent comments properly``() =
formatSourceString false """
/// Non-local information related to internals of code generation within an assembly
type IlxGenIntraAssemblyInfo =
{ /// A table recording the generated name of the static backing fields for each mutable top level value where
/// we may need to take the address of that value, e.g. static mutable module-bound values which are structs. These are
/// only accessible intra-assembly. Across assemblies, taking the address of static mutable module-bound values is not permitted.
/// The key to the table is the method ref for the property getter for the value, which is a stable name for the Val's
/// that come from both the signature and the implementation.
StaticFieldInfo : Dictionary<ILMethodRef, ILFieldSpec> }
""" config
|> prepend newline
|> should equal """
/// Non-local information related to internals of code generation within an assembly
type IlxGenIntraAssemblyInfo =
{ /// A table recording the generated name of the static backing fields for each mutable top level value where
/// we may need to take the address of that value, e.g. static mutable module-bound values which are structs. These are
/// only accessible intra-assembly. Across assemblies, taking the address of static mutable module-bound values is not permitted.
/// The key to the table is the method ref for the property getter for the value, which is a stable name for the Val's
/// that come from both the signature and the implementation.
StaticFieldInfo : Dictionary<ILMethodRef, ILFieldSpec> }
"""
[<Test>]
let ``shouldn't break on one-line comment``() =
formatSourceString false """
1 + (* Comment *) 1""" config
|> prepend newline
|> should equal """
1 + (* Comment *) 1
"""
[<Test>]
let ``should keep comments on DU cases``() =
formatSourceString false """
/// XML comment
type X =
/// Hello
A
/// Goodbye
| B
""" config
|> prepend newline
|> should equal """
/// XML comment
type X =
/// Hello
| A
/// Goodbye
| B
"""
[<Test>]
let ``should keep comments before attributes``() =
formatSourceString false """
[<NoEquality; NoComparison>]
type IlxGenOptions =
{ fragName: string
generateFilterBlocks: bool
workAroundReflectionEmitBugs: bool
emitConstantArraysUsingStaticDataBlobs: bool
// If this is set, then the last module becomes the "main" module and its toplevel bindings are executed at startup
mainMethodInfo: Tast.Attribs option
localOptimizationsAreOn: bool
generateDebugSymbols: bool
testFlagEmitFeeFeeAs100001: bool
ilxBackend: IlxGenBackend
/// Indicates the code is being generated in FSI.EXE and is executed immediately after code generation
/// This includes all interactively compiled code, including #load, definitions, and expressions
isInteractive: bool
// Indicates the code generated is an interactive 'it' expression. We generate a setter to allow clearing of the underlying
// storage, even though 'it' is not logically mutable
isInteractiveItExpr: bool
// Indicates System.SerializableAttribute is available in the target framework
netFxHasSerializableAttribute : bool
/// Whenever possible, use callvirt instead of call
alwaysCallVirt: bool}
""" { config with SemicolonAtEndOfLine = true }
|> prepend newline
|> should equal """
[<NoEquality; NoComparison>]
type IlxGenOptions =
{ fragName : string;
generateFilterBlocks : bool;
workAroundReflectionEmitBugs : bool;
emitConstantArraysUsingStaticDataBlobs : bool;
// If this is set, then the last module becomes the "main" module and its toplevel bindings are executed at startup
mainMethodInfo : Tast.Attribs option;
localOptimizationsAreOn : bool;
generateDebugSymbols : bool;
testFlagEmitFeeFeeAs100001 : bool;
ilxBackend : IlxGenBackend;
/// Indicates the code is being generated in FSI.EXE and is executed immediately after code generation
/// This includes all interactively compiled code, including #load, definitions, and expressions
isInteractive : bool;
// Indicates the code generated is an interactive 'it' expression. We generate a setter to allow clearing of the underlying
// storage, even though 'it' is not logically mutable
isInteractiveItExpr : bool;
// Indicates System.SerializableAttribute is available in the target framework
netFxHasSerializableAttribute : bool;
/// Whenever possible, use callvirt instead of call
alwaysCallVirt : bool }
"""
[<Test; Ignore>]
let ``should keep comments on else if``() =
formatSourceString false """
if true then ()
else
// Comment 1
if true then ()
// Comment 2
else ()
""" config
|> prepend newline
|> should equal """
if true then ()
else
// Comment 1
if true then ()
// Comment 2
else ()
"""
[<Test>]
let ``should keep comments on almost-equal identifiers``() =
formatSourceString false """
let zp = p1 lxor p2
// Comment 1
let b = zp land (zp)
(* Comment 2 *)
let p = p1 land (b - 1)
""" config
|> prepend newline
|> should equal """
let zp = p1 ``lxor`` p2
// Comment 1
let b = zp ``land`` (zp)
(* Comment 2 *)
let p = p1 ``land`` (b - 1)
"""
[<Test>]
let ``should not write sticky-to-the-left comments in a new line``() =
formatSourceString false """
let moveFrom source =
getAllFiles source
|> Seq.filter (fun f -> Path.GetExtension(f).ToLower() <> ".db") //exlcude the thumbs.db files
|> move @"C:\_EXTERNAL_DRIVE\_Camera"
""" config
|> prepend newline
|> should equal """
let moveFrom source =
getAllFiles source
|> Seq.filter (fun f -> Path.GetExtension(f).ToLower() <> ".db") //exlcude the thumbs.db files
|> move @"C:\_EXTERNAL_DRIVE\_Camera"
"""
[<Test>]
let ``should handle comments at the end of file``() =
formatSourceString false """
let hello() = "hello world"
(* This is a comment. *)
""" config
|> prepend newline
|> should equal """
let hello() = "hello world"
(* This is a comment. *)
"""