Skip to content

Commit 8477137

Browse files
authored
Merge pull request #678 from dsyme/add-test1
add compile tests
2 parents 00a87c1 + c550d12 commit 8477137

File tree

9 files changed

+92
-37
lines changed

9 files changed

+92
-37
lines changed

docs/content/compiler.fsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@ First, we need to reference the libraries that contain F# interactive service:
2828
*)
2929

3030
#r "FSharp.Compiler.Service.dll"
31-
open Microsoft.FSharp.Compiler.SimpleSourceCodeServices
3231
open System.IO
32+
open Microsoft.FSharp.Compiler.SourceCodeServices
3333

34-
let scs = SimpleSourceCodeServices()
34+
// Create an interactive checker instance
35+
let checker = FSharpChecker.Create()
3536

3637
(**
3738
Now write content to a temporary file:
3839
3940
*)
4041
let fn = Path.GetTempFileName()
41-
let fn2 = Path.ChangeExtension(fn, ".fs")
42+
let fn2 = Path.ChangeExtension(fn, ".fsx")
4243
let fn3 = Path.ChangeExtension(fn, ".dll")
4344

4445
File.WriteAllText(fn2, """
@@ -54,7 +55,7 @@ let x = 3 + 4
5455
Now invoke the compiler:
5556
*)
5657

57-
let errors1, exitCode1 = scs.Compile([| "fsc.exe"; "-o"; fn3; "-a"; fn2 |])
58+
let errors1, exitCode1 = checker.Compile([| "fsc.exe"; "-o"; fn3; "-a"; fn2 |])
5859

5960
(**
6061
@@ -67,7 +68,7 @@ module M
6768
let x = 1.0 + "" // a type error
6869
""")
6970

70-
let errors1b, exitCode1b = scs.Compile([| "fsc.exe"; "-o"; fn3; "-a"; fn2 |])
71+
let errors1b, exitCode1b = checker.Compile([| "fsc.exe"; "-o"; fn3; "-a"; fn2 |])
7172

7273
(**
7374
@@ -83,11 +84,11 @@ You still have to pass the "-o" option to name the output file, but the output f
8384
The 'None' option indicates that the initiatlization code for the assembly is not executed.
8485
*)
8586
let errors2, exitCode2, dynAssembly2 =
86-
scs.CompileToDynamicAssembly([| "-o"; fn3; "-a"; fn2 |], execute=None)
87+
checker.CompileToDynamicAssembly([| "-o"; fn3; "-a"; fn2 |], execute=None)
8788

8889
(*
8990
Passing 'Some' for the 'execute' parameter executes the initiatlization code for the assembly.
9091
*)
9192
let errors3, exitCode3, dynAssembly3 =
92-
scs.CompileToDynamicAssembly([| "-o"; fn3; "-a"; fn2 |], Some(stdout,stderr))
93+
checker.CompileToDynamicAssembly([| "-o"; fn3; "-a"; fn2 |], Some(stdout,stderr))
9394

docs/content/tokenizer.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ file name of the source code. The defined symbols are required because the
2828
tokenizer handles `#if` directives. The file name is required only to specify
2929
locations of the source code (and it does not have to exist):
3030
*)
31-
let sourceTok = FSharpSourceTokenizer([], "C:\\test.fsx")
31+
let sourceTok = FSharpSourceTokenizer([], Some "C:\\test.fsx")
3232
(**
3333
Using the `sourceTok` object, we can now (repeatedly) tokenize lines of
3434
F# source code.

docs/content/typedtree.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ let rec visitExpr f (e:FSharpExpr) =
241241
visitExpr f baseCallExpr
242242
List.iter (visitObjMember f) overrides
243243
List.iter (snd >> List.iter (visitObjMember f)) interfaceImplementations
244-
| BasicPatterns.TraitCall(sourceTypes, traitName, typeArgs, typeInstantiation, argExprs) ->
244+
| BasicPatterns.TraitCall(sourceTypes, traitName, typeArgs, typeInstantiation, argTypes, argExprs) ->
245245
visitExprs f argExprs
246246
| BasicPatterns.ValueSet(valToSet, valueExpr) ->
247247
visitExpr f valueExpr

paket.dependencies

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
frameworks: net46, netstandard16, netcoreapp10
2+
13
source https://www.myget.org/F/dotnet-core/api/v3/index.json
24
source https://www.nuget.org/api/v2/
35

samples/FscExe/FscMain.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ open System.IO
88
open System.Reflection
99
open System.Runtime.CompilerServices
1010
open Microsoft.FSharp.Compiler
11+
open Microsoft.FSharp.Compiler.SourceCodeServices
1112
open Microsoft.FSharp.Compiler.AbstractIL.IL // runningOnMono
1213
open Microsoft.FSharp.Compiler.AbstractIL.Internal.Library
1314
open Microsoft.FSharp.Compiler.ErrorLogger
14-
open Microsoft.FSharp.Compiler.SimpleSourceCodeServices
1515
open Microsoft.FSharp.Compiler.Range
1616

1717
#if FX_RESHAPED_REFLECTION
@@ -83,7 +83,7 @@ module FSharpResidentCompiler =
8383
let! (pwd,argv, reply: AsyncReplyChannel<_>) = inbox.Receive()
8484
if !progress then printfn "server agent: got compilation request, argv = %A" argv
8585
Environment.CurrentDirectory <- pwd
86-
let errors, exitCode = SimpleSourceCodeServices().Compile (argv);
86+
let errors, exitCode = FSharpChecker.Create().Compile (argv);
8787
for error in errors do eprintfn "%s" (error.ToString())
8888
if !progress then printfn "server: finished compilation request, argv = %A" argv
8989
let output = outputCollector.GetTextAndClear()
@@ -286,7 +286,7 @@ module Driver =
286286
match exitCodeOpt with
287287
| Some exitCode -> exitCode
288288
| None ->
289-
let errors, exitCode = SimpleSourceCodeServices().Compile (argv)
289+
let errors, exitCode = FSharpChecker.Create().Compile (argv)
290290
for error in errors do eprintfn "%s" (error.ToString())
291291
exitCode
292292

@@ -295,7 +295,7 @@ module Driver =
295295
0
296296

297297
else
298-
let errors, exitCode = SimpleSourceCodeServices().Compile (argv)
298+
let errors, exitCode = FSharpChecker.Create().Compile (argv)
299299
for error in errors do eprintfn "%s" (error.ToString())
300300
exitCode
301301

tests/service/EditorTests.fs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ open System
3131
open System.IO
3232
open Microsoft.FSharp.Compiler
3333
open Microsoft.FSharp.Compiler.SourceCodeServices
34-
open Microsoft.FSharp.Compiler.SimpleSourceCodeServices
3534
open FSharp.Compiler.Service.Tests.Common
3635

3736
let stringMethods =

tests/service/FscTests.fs

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ open System.IO
1515

1616
open Microsoft.FSharp.Compiler
1717
open Microsoft.FSharp.Compiler.SourceCodeServices
18-
open Microsoft.FSharp.Compiler.SimpleSourceCodeServices
1918
open FSharp.Compiler.Service.Tests
2019
open FSharp.Compiler.Service.Tests.Common
2120

@@ -111,7 +110,6 @@ type DebugMode =
111110
| Full
112111

113112
let checker = FSharpChecker.Create()
114-
let compiler = new SimpleSourceCodeServices()
115113

116114
/// Ensures the default FSharp.Core referenced by the F# compiler service (if none is
117115
/// provided explicitly) is available in the output directory.
@@ -127,10 +125,10 @@ let ensureDefaultFSharpCoreAvailable tmpDir =
127125
File.Copy(fsCoreDefaultReference(), Path.Combine(tmpDir, Path.GetFileName(fsCoreDefaultReference())), overwrite = true)
128126
#endif
129127

130-
let compile isDll debugMode (assemblyName : string) (code : string) (dependencies : string list) =
128+
let compile isDll debugMode (assemblyName : string) (ext: string) (code : string) (dependencies : string list) (extraArgs: string list) =
131129
let tmp = Path.Combine(Path.GetTempPath(),"test"+string(hash (isDll,debugMode,assemblyName,code,dependencies)))
132130
try Directory.CreateDirectory(tmp) |> ignore with _ -> ()
133-
let sourceFile = Path.Combine(tmp, assemblyName + ".fs")
131+
let sourceFile = Path.Combine(tmp, assemblyName + "." + ext)
134132
let outFile = Path.Combine(tmp, assemblyName + if isDll then ".dll" else ".exe")
135133
let pdbFile = Path.Combine(tmp, assemblyName + pdbExtension isDll)
136134
do File.WriteAllText(sourceFile, code)
@@ -158,23 +156,26 @@ let compile isDll debugMode (assemblyName : string) (code : string) (dependencie
158156

159157
yield sprintf "--out:%s" outFile
160158

159+
yield! extraArgs
160+
161161
yield sourceFile
162+
162163
|]
163164

164165
ensureDefaultFSharpCoreAvailable tmp
165166

166167
printfn "args: %A" args
167-
let errorInfo, id = compiler.Compile args
168+
let errorInfo, id = checker.Compile args
168169
for err in errorInfo do
169170
printfn "error: %A" err
170171
if id <> 0 then raise <| CompilationError(assemblyName, id, errorInfo)
171172
Assert.AreEqual (errorInfo.Length, 0)
172173
outFile
173174

174175
//sizeof<nativeint>
175-
let compileAndVerify isDll debugMode assemblyName code dependencies =
176+
let compileAndVerify isDll debugMode assemblyName ext code dependencies =
176177
let verifier = new PEVerifier ()
177-
let outFile = compile isDll debugMode assemblyName code dependencies
178+
let outFile = compile isDll debugMode assemblyName ext code dependencies []
178179
verifier.Verify outFile
179180
outFile
180181

@@ -186,7 +187,7 @@ let compileAndVerifyAst (name : string, ast : Ast.ParsedInput list, references :
186187

187188
ensureDefaultFSharpCoreAvailable outDir
188189

189-
let errors, id = compiler.Compile(ast, name, outFile, references, executable = false)
190+
let errors, id = checker.Compile(ast, name, outFile, references, executable = false)
190191
for err in errors do printfn "error: %A" err
191192
Assert.AreEqual (errors.Length, 0)
192193
if id <> 0 then raise <| CompilationError(name, id, errors)
@@ -225,7 +226,7 @@ module Foo
225226
printfn "done!" // make the code have some initialization effect
226227
"""
227228

228-
compileAndVerify true PdbOnly "Foo" code [] |> ignore
229+
compileAndVerify true PdbOnly "Foo" "fs" code [] |> ignore
229230

230231
[<Test>]
231232
let ``3. Simple FSC executable test`` () =
@@ -236,7 +237,7 @@ module Bar
236237
let main _ = printfn "Hello, World!" ; 42
237238
238239
"""
239-
let outFile = compileAndVerify false PdbOnly "Bar" code []
240+
let outFile = compileAndVerify false PdbOnly "Bar" "fs" code []
240241

241242
use proc = Process.Start(outFile, "")
242243
proc.WaitForExit()
@@ -295,7 +296,8 @@ module Bar
295296
296297
"""
297298
try
298-
compile false PdbOnly "Bar" code [] |> ignore
299+
let outFile : string = compile false PdbOnly "Bar" "fs" code [] []
300+
()
299301
with
300302
| :? CompilationError as exn ->
301303
Assert.AreEqual(6,exn.Data2.[0].StartLineAlternate)
@@ -307,19 +309,51 @@ let ``Check cols are indexed by 1`` () =
307309
let code = "let x = 1 + a"
308310

309311
try
310-
compile false PdbOnly "Foo" code [] |> ignore
312+
let outFile : string = compile false PdbOnly "Foo" "fs" code [] []
313+
()
311314
with
312315
| :? CompilationError as exn ->
313316
Assert.True(exn.Data2.[0].ToString().Contains("Foo.fs (1,13)-(1,14)"))
314317
| _ -> failwith "No compilation error"
315318

316319

320+
[<Test>]
321+
let ``Check compile of bad fsx`` () =
322+
let code = """
323+
#load "missing.fsx"
324+
#r "missing.dll"
325+
"""
326+
327+
try
328+
let outFile : string = compile false PdbOnly "Foo" "fsx" code [] []
329+
()
330+
with
331+
| :? CompilationError as exn ->
332+
Assert.True(exn.Data2.[0].ToString().Contains("Could not load file '"))
333+
Assert.True(exn.Data2.[0].ToString().Contains("missing.fsx"))
334+
Assert.True(exn.Data2.[1].ToString().Contains("Could not locate the assembly \"missing.dll\""))
335+
| _ -> failwith "No compilation error"
336+
337+
338+
[<Test>]
339+
let ``Check compile of good fsx with bad option`` () =
340+
let code = """
341+
let x = 1
342+
"""
343+
344+
try
345+
let outFile : string = compile false PdbOnly "Foo" "fsx" code [] ["-r:missing.dll"]
346+
()
347+
with
348+
| :? CompilationError as exn ->
349+
Assert.True(exn.Data2.[0].ToString().Contains("startup (1,1)-(1,81) parameter error Could not resolve this reference"))
350+
| _ -> failwith "No compilation error"
351+
317352

318353
#if STRESS
319354
// For this stress test the aim is to check if we have a memory leak
320355

321356
module StressTest1 =
322-
open Microsoft.FSharp.Compiler.SimpleSourceCodeServices
323357
open System.IO
324358

325359
[<Test>]
@@ -335,7 +369,8 @@ type C() =
335369
let x = 3 + 4
336370
"""
337371

338-
compile true PdbOnly "Foo" code [] |> ignore
372+
let outFile : string = compile true PdbOnly "Foo" "fs" code [] []
373+
()
339374

340375
#endif
341376

tests/service/ProjectOptionsTests.fs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -439,24 +439,40 @@ let ``Project file parsing -- report files``() =
439439
for f in Directory.EnumerateFiles(dir41,"*",SearchOption.AllDirectories) do
440440
printfn "File: %s" f
441441

442+
[<Test>]
443+
let ``Test OtherOptions order for GetProjectOptionsFromScript`` () =
444+
let test scriptName expected2 =
445+
let scriptPath = __SOURCE_DIRECTORY__ + @"/data/ScriptProject/" + scriptName + ".fsx"
446+
let scriptSource = File.ReadAllText scriptPath
447+
let projOpts = checker.GetProjectOptionsFromScript(scriptPath, scriptSource) |> Async.RunSynchronously
448+
449+
projOpts.OtherOptions
450+
|> Array.map Path.GetFileNameWithoutExtension
451+
|> shouldEqual expected2
452+
let otherArgs = [|"--noframework"; "3"; "System.Numerics"; "mscorlib"; "FSharp.Core"; "System"; "System.Xml"; "System.Runtime.Remoting"; "System.Runtime.Serialization.Formatters.Soap"; "System.Data"; "System.Drawing"; "System.Core"; "System.Runtime"; "System.Linq"; "System.Reflection"; "System.Linq.Expressions"; "System.Threading.Tasks"; "System.IO"; "System.Net.Requests"; "System.Collections"; "System.Runtime.Numerics"; "System.Threading"; "System.Web"; "System.Web.Services"; "System.Windows.Forms"; "FSharp.Compiler.Interactive.Settings"|]
453+
test "Main1" otherArgs
454+
test "Main2" otherArgs
455+
test "Main3" otherArgs
456+
test "Main4" otherArgs
457+
test "MainBad" otherArgs
458+
459+
442460
#endif
443461

444462
[<Test>]
445463
let ``Test ProjectFileNames order for GetProjectOptionsFromScript`` () = // See #594
446464
let test scriptName expected =
447465
let scriptPath = __SOURCE_DIRECTORY__ + @"/data/ScriptProject/" + scriptName + ".fsx"
448466
let scriptSource = File.ReadAllText scriptPath
449-
let projOpts =
450-
checker.GetProjectOptionsFromScript(scriptPath, scriptSource)
451-
|> Async.RunSynchronously
467+
let projOpts = checker.GetProjectOptionsFromScript(scriptPath, scriptSource) |> Async.RunSynchronously
452468
projOpts.ProjectFileNames
453469
|> Array.map Path.GetFileNameWithoutExtension
454-
|> (=) expected
455-
|> shouldEqual true
456-
test "Main1" [|"BaseLib1"; "Lib1"; "Lib2"; "Main1"|]
457-
test "Main2" [|"BaseLib1"; "Lib1"; "Lib2"; "Lib3"; "Main2"|]
458-
test "Main3" [|"Lib3"; "Lib4"; "Main3"|]
459-
test "Main4" [|"BaseLib2"; "Lib5"; "BaseLib1"; "Lib1"; "Lib2"; "Main4"|]
470+
|> shouldEqual expected
471+
test "Main1" [|"BaseLib1"; "Lib1"; "Lib2"; "Main1"|]
472+
test "Main2" [|"BaseLib1"; "Lib1"; "Lib2"; "Lib3"; "Main2"|]
473+
test "Main3" [|"Lib3"; "Lib4"; "Main3"|]
474+
test "Main4" [|"BaseLib2"; "Lib5"; "BaseLib1"; "Lib1"; "Lib2"; "Main4"|]
475+
test "MainBad" [|"MainBad"|]
460476

461477
#endif
462478

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#load "NotExist1.fsx"
2+
#r "NotExist.dll"

0 commit comments

Comments
 (0)