-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
TestHelpers.fs
326 lines (263 loc) · 10 KB
/
TestHelpers.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
module Fantomas.Tests.TestHelper
open FsUnit
open System
open Fantomas.FormatConfig
open Fantomas
open Fantomas.Extras
open FSharp.Compiler.Text
open FSharp.Compiler.SourceCodeServices
open FSharp.Compiler.SyntaxTree
open FSharp.Compiler.XmlDoc
open NUnit.Framework
open FsCheck
open System.IO
[<assembly: Parallelizable(ParallelScope.All)>]
do ()
let config = FormatConfig.Default
let newline = "\n"
let sharedChecker = lazy (FSharpChecker.Create())
let private safeToIgnoreWarnings =
[ "This construct is deprecated: it is only for use in the F# library"
"Identifiers containing '@' are reserved for use in F# code generation" ]
let private isValidAndHasNoWarnings fileName source parsingOptions =
let allDefineOptions, _ = TokenParser.getDefines source
allDefineOptions
|> List.map
(fun conditionalCompilationDefines ->
async {
let parsingOptionsWithDefines =
{ parsingOptions with
ConditionalCompilationDefines = conditionalCompilationDefines }
// Run the first phase (untyped parsing) of the compiler
let sourceText = SourceText.ofString source
let! untypedRes = sharedChecker.Value.ParseFile(fileName, sourceText, parsingOptionsWithDefines)
let errors =
untypedRes.Errors
|> Array.filter (fun e -> not (List.contains e.Message safeToIgnoreWarnings))
// FSharpErrorInfo contains both Errors and Warnings
// https://fsharp.github.io/FSharp.Compiler.Service/reference/fsharp-compiler-sourcecodeservices-fsharperrorinfo.html
return Array.isEmpty errors
})
|> Async.Parallel
|> Async.map (Seq.fold (&&) true)
let formatSourceString isFsiFile (s: string) config =
// On Linux/Mac this will exercise different line endings
let s = s.Replace("\r\n", Environment.NewLine)
let fileName =
if isFsiFile then
"/src.fsi"
else
"/src.fsx"
let parsingOptions =
FakeHelpers.createParsingOptionsFromFile fileName
async {
let! formatted =
CodeFormatter.FormatDocumentAsync(
fileName,
SourceOrigin.SourceString s,
config,
parsingOptions,
sharedChecker.Value
)
let! isValid = isValidAndHasNoWarnings fileName formatted parsingOptions
if not isValid then
failwithf "The formatted result is not valid F# code or contains warnings\n%s" formatted
return formatted.Replace("\r\n", "\n")
}
|> Async.RunSynchronously
let formatSourceStringWithDefines defines (s: string) config =
// On Linux/Mac this will exercise different line endings
let s = s.Replace("\r\n", Environment.NewLine)
let fileName = "/src.fsx"
let formatContext =
CodeFormatterImpl.createFormatContext fileName (SourceOrigin.SourceString s)
let parsingOptions =
FakeHelpers.createParsingOptionsFromFile fileName
|> fun p ->
{ p with
ConditionalCompilationDefines = defines }
let result =
async {
let! asts = CodeFormatterImpl.parse sharedChecker.Value parsingOptions formatContext
let ast, hashTokens =
Array.filter (fun (_, d, _) -> d = defines) asts
|> Array.head
|> fun (ast, _, hashTokens) -> ast, hashTokens
return CodeFormatterImpl.formatWith ast defines hashTokens formatContext config
}
|> Async.RunSynchronously
// merge with itself to make #if go on beginning of line
String.merge config.EndOfLine.NewLineString result result
|> String.normalizeNewLine
let formatSelectionOnly isFsiFile r (s: string) config =
let s = s.Replace("\r\n", Environment.NewLine)
let fileName =
if isFsiFile then
"/tmp.fsi"
else
"/tmp.fsx"
CodeFormatter.FormatSelectionAsync(
fileName,
r,
SourceOrigin.SourceString s,
config,
FakeHelpers.createParsingOptionsFromFile fileName,
sharedChecker.Value
)
|> Async.RunSynchronously
|> fun s -> s.Replace("\r\n", "\n")
let isValidFSharpCode isFsiFile s =
let fileName =
if isFsiFile then
"/tmp.fsi"
else
"/tmp.fsx"
CodeFormatter.IsValidFSharpCodeAsync(
fileName,
SourceOrigin.SourceString s,
FakeHelpers.createParsingOptionsFromFile fileName,
sharedChecker.Value
)
|> Async.RunSynchronously
let parse isFsiFile s =
let fileName =
if isFsiFile then
"/tmp.fsi"
else
"/tmp.fsx"
CodeFormatterImpl.createFormatContext fileName (SourceOrigin.SourceString s)
|> CodeFormatterImpl.parse sharedChecker.Value (FakeHelpers.createParsingOptionsFromFile fileName)
|> Async.RunSynchronously
let formatAST a s c =
CodeFormatter.FormatASTAsync(a, "/tmp.fsx", [], s, c)
|> Async.RunSynchronously
let makeRange l1 c1 l2 c2 =
CodeFormatter.MakeRange("/tmp.fsx", l1, c1, l2, c2)
let makePos l1 c1 = CodeFormatter.MakePos(l1, c1)
let equal x =
let x =
match box x with
| :? String as s -> s.Replace("\r\n", "\n") |> box
| x -> x
equal x
let inline prepend s content = s + content
let inline append s content = content + s
let printAST isFsiFile sourceCode =
let ast = parse isFsiFile sourceCode
printfn "AST:"
printfn "%A" ast
let zero = range.Zero
type Input = Input of string
let toSynExprs (Input s) =
let ast =
try
parse false s
|> Array.map (fun (ast, _, _) -> ast)
|> Some
with
| _ -> None
match ast with
| Some [| (ParsedInput.ImplFile (ParsedImplFileInput ("/tmp.fsx",
_,
QualifiedNameOfFile _,
[],
[],
[ SynModuleOrNamespace (_,
false,
AnonModule,
exprs,
_,
_,
_,
_) ],
_))) |] ->
List.choose
(function
| SynModuleDecl.DoExpr (_, expr, _) -> Some expr
| _ -> None)
exprs
| _ -> []
let tryFormatAST ast sourceCode config =
try
formatAST ast sourceCode config
with
| _ -> ""
let formatConfig =
{ FormatConfig.Default with
StrictMode = true }
// Regenerate inputs from expression ASTs
// Might suffer from bugs in formatting phase
let fromSynExpr expr =
let ast =
let ident = Ident("Tmp", zero)
ParsedInput.ImplFile(
ParsedImplFileInput(
"/tmp.fsx",
true,
QualifiedNameOfFile ident,
[],
[],
[ SynModuleOrNamespace(
[ ident ],
false,
AnonModule,
[ SynModuleDecl.DoExpr(NoDebugPointAtDoBinding, expr, zero) ],
PreXmlDoc.Empty,
[],
None,
zero
) ],
(true, true)
)
)
Input(tryFormatAST ast None formatConfig)
let shouldNotChangeAfterFormat source =
formatSourceString false source config
|> prepend newline
|> should equal source
let (==) actual expected = Assert.AreEqual(expected, actual)
let fail () = Assert.Fail()
let pass () = Assert.Pass()
/// An FsCheck runner which reports FsCheck test results to NUnit.
type NUnitRunner() =
interface IRunner with
member __.OnStartFixture _ = ()
member __.OnArguments(_ntest, _args, _every) =
//stdout.Write(every ntest args)
()
member __.OnShrink(_args, _everyShrink) =
//stdout.Write(everyShrink args)
()
member __.OnFinished(name, result) =
match result with
| TestResult.True (_data, _) ->
// TODO : Log the result data.
Runner.onFinishedToString name result
|> stdout.WriteLine
| TestResult.Exhausted _data ->
// TODO : Log the result data.
Runner.onFinishedToString name result
|> Assert.Inconclusive
| TestResult.False _ ->
// TODO : Log more information about the test failure.
Runner.onFinishedToString name result
|> Assert.Fail
let private getTempFolder () = Path.GetTempPath()
let private mkConfigPath fileName folder =
match folder with
| Some folder ->
let folderPath = Path.Combine(getTempFolder (), folder)
Directory.CreateDirectory(folderPath) |> ignore
Path.Combine(folderPath, fileName)
| None -> Path.Combine(getTempFolder (), fileName)
let mkConfigFromContent fileName folder content =
let file = mkConfigPath fileName folder
File.WriteAllText(file, content)
file
type TemporaryFileCodeSample internal (codeSnippet: string) =
let filename =
Path.Join(Path.GetTempPath(), Guid.NewGuid().ToString() + ".fs")
do File.WriteAllText(filename, codeSnippet)
member __.Filename: string = filename
interface IDisposable with
member this.Dispose() : unit = File.Delete(filename)