diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/ConfusingTypeName.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/ConfusingTypeName.fs index 4b06c4b7b37..4e56519ffa6 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/ConfusingTypeName.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/ConfusingTypeName.fs @@ -1,12 +1,9 @@ // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. -namespace FSharp.Compiler.ErrorMessages.ComponentTests +namespace FSharp.Compiler.ComponentTests.ErrorMessages open Xunit -open FSharp.Test.Utilities open FSharp.Test.Utilities.Compiler -open FSharp.Test.Utilities.Utilities -open FSharp.Compiler.SourceCodeServices module ``Confusing Type Name`` = diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/ConstructorTests.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/ConstructorTests.fs index dbad94ed4b8..1806defe4e4 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/ConstructorTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/ConstructorTests.fs @@ -17,8 +17,8 @@ doSomething {Record.field1=0; field2=0} |> typecheck |> shouldFail |> withDiagnostics [ - (Error 1, Line 4, Col 13, Line 4, Col 40, "This expression was expected to have type\n 'Record list' \nbut here has type\n 'Record' ") - (Warning 20, Line 4, Col 1, Line 4, Col 40, "The result of this expression has type 'int list' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.")] + (Warning 20, Line 4, Col 1, Line 4, Col 40, "The result of this expression has type 'int list' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.") + (Error 1, Line 4, Col 13, Line 4, Col 40, "This expression was expected to have type\n 'Record list' \nbut here has type\n 'Record' ")] [] let ``Comma In Rec Ctor``() = @@ -46,8 +46,8 @@ let p = |> typecheck |> shouldFail |> withDiagnostics [ - (Error 39, Line 7, Col 12, Line 7, Col 16, "The value or constructor 'Name' is not defined. Maybe you want one of the following:" + System.Environment.NewLine + " nan") (Warning 20, Line 7, Col 12, Line 7, Col 25, "The result of this equality expression has type 'bool' and is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'.") + (Error 39, Line 7, Col 12, Line 7, Col 16, "The value or constructor 'Name' is not defined. Maybe you want one of the following:" + System.Environment.NewLine + " nan") (Error 39, Line 8, Col 12, Line 8, Col 15, "The value or constructor 'Age' is not defined.") (Error 501, Line 7, Col 5, Line 8, Col 21, "The object constructor 'Person' takes 0 argument(s) but is here given 1. The required signature is 'new : unit -> Person'. If some of the arguments are meant to assign values to properties, consider separating those arguments with a comma (',').")] diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/ElseBranchHasWrongTypeTests.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/ElseBranchHasWrongTypeTests.fs index e986cc6b722..8c412854488 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/ElseBranchHasWrongTypeTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/ElseBranchHasWrongTypeTests.fs @@ -166,6 +166,6 @@ else |> typecheck |> shouldFail |> withDiagnostics [ + (Warning 20, Line 3, Col 1, Line 6, Col 13, "The result of this expression has type 'bool' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.") (Error 1, Line 5, Col 19, Line 5, Col 22, "All branches of an 'if' expression must return values of the same type as the first branch, which here is 'bool'. This branch returns a value of type 'string'.") - (Error 1, Line 6, Col 10, Line 6, Col 13, "All branches of an 'if' expression must return values of the same type as the first branch, which here is 'bool'. This branch returns a value of type 'string'.") - (Warning 20, Line 3, Col 1, Line 6, Col 13, "The result of this expression has type 'bool' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.")] + (Error 1, Line 6, Col 10, Line 6, Col 13, "All branches of an 'if' expression must return values of the same type as the first branch, which here is 'bool'. This branch returns a value of type 'string'.")] diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/TypeMismatchTests.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/TypeMismatchTests.fs index 0d2d1152c88..8d2d86d1d7c 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/TypeMismatchTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/TypeMismatchTests.fs @@ -64,8 +64,8 @@ match x with |> typecheck |> shouldFail |> withDiagnostics [ - (Error 1, Line 4, Col 10, Line 4, Col 13, "A pattern match guard must be of type 'bool', but this 'when' expression is of type 'string'.") - (Warning 20, Line 3, Col 1, Line 5, Col 13, "The result of this expression has type 'bool' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.")] + (Warning 20, Line 3, Col 1, Line 5, Col 13, "The result of this expression has type 'bool' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.") + (Error 1, Line 4, Col 10, Line 4, Col 13, "A pattern match guard must be of type 'bool', but this 'when' expression is of type 'string'.")] [] let ``Runtime Type Test In Pattern``() = diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/WarnExpressionTests.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/WarnExpressionTests.fs index 1380869d383..28487c97aa6 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/WarnExpressionTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/WarnExpressionTests.fs @@ -113,7 +113,7 @@ let view model dispatch = div [] [] ] """ - |> withLangVersion46 + |> withOptions ["--langversion:4.6"] |> typecheck |> shouldFail |> withSingleDiagnostic (Warning 3221, Line 9, Col 8, Line 9, Col 17, @@ -137,7 +137,7 @@ let view model dispatch = ] ] """ - |> withLangVersion46 + |> withOptions ["--langversion:4.6"] |> typecheck |> shouldFail |> withSingleDiagnostic (Warning 3222, Line 13, Col 19, Line 13, Col 41, @@ -161,7 +161,7 @@ let view model dispatch = ] ] """ - |> withLangVersion46 + |> withOptions ["--langversion:4.6"] |> typecheck |> shouldFail |> withSingleDiagnostic (Warning 20, Line 13, Col 19, Line 13, Col 41, diff --git a/tests/FSharp.Compiler.ComponentTests/Interop/SimpleInteropTests.fs b/tests/FSharp.Compiler.ComponentTests/Interop/SimpleInteropTests.fs index 350388cea62..6f728c05934 100644 --- a/tests/FSharp.Compiler.ComponentTests/Interop/SimpleInteropTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/Interop/SimpleInteropTests.fs @@ -1,9 +1,8 @@ // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. -namespace FSharp.Compiler.Interop.ComponentTests +namespace FSharp.Compiler.ComponentTests.Interop open Xunit -open FSharp.Test.Utilities open FSharp.Test.Utilities.Compiler module ``C# <-> F# basic interop`` = diff --git a/tests/FSharp.Test.Utilities/Compiler.fs b/tests/FSharp.Test.Utilities/Compiler.fs index ccfcd4858bd..b31da1d4866 100644 --- a/tests/FSharp.Test.Utilities/Compiler.fs +++ b/tests/FSharp.Test.Utilities/Compiler.fs @@ -2,8 +2,6 @@ namespace FSharp.Test.Utilities -open FSharp.Compiler.Interactive.Shell -open FSharp.Compiler.Scripting open FSharp.Compiler.SourceCodeServices open FSharp.Test.Utilities open FSharp.Test.Utilities.Assert @@ -17,31 +15,24 @@ open System.IO module rec Compiler = - type Baseline = - { SourceFilename: string option - OutputBaseline: string option - ILBaseline: string option } - type TestType = | Text of string | Path of string + | Baseline of (string * string) type CompilationUnit = | FS of FSharpCompilationSource | CS of CSharpCompilationSource | IL of ILCompilationSource - override this.ToString() = match this with | FS fs -> fs.ToString() | _ -> (sprintf "%A" this ) type FSharpCompilationSource = { Source: TestType - Baseline: Baseline option Options: string list OutputType: CompileOutput SourceKind: SourceKind Name: string option IgnoreWarnings: bool References: CompilationUnit list } - override this.ToString() = match this.Name with | Some n -> n | _ -> (sprintf "%A" this) type CSharpCompilationSource = { Source: TestType @@ -60,33 +51,28 @@ module rec Compiler = type Col = Col of int type Range = - { StartLine: int - StartColumn: int - EndLine: int - EndColumn: int } + { StartLine: int + StartColumn: int + EndLine: int + EndColumn: int } type ErrorInfo = { Error: ErrorType Range: Range Message: string } - type EvalOutput = Result - type ExecutionOutput = { ExitCode: int StdOut: string StdErr: string } - type RunOutput = - | EvalOutput of EvalOutput - | ExecutionOutput of ExecutionOutput - type Output = { OutputPath: string option Dependencies: string list Adjust: int - Diagnostics: ErrorInfo list - Output: RunOutput option } + Errors: ErrorInfo list + Warnings: ErrorInfo list + Output: ExecutionOutput option } type TestResult = | Success of Output @@ -99,13 +85,13 @@ module rec Compiler = match src with | Text t -> t | Path p -> System.IO.File.ReadAllText p + | Baseline (d, f) -> System.IO.File.ReadAllText (System.IO.Path.Combine(d, f)) let private fsFromString (source: string) (kind: SourceKind) : FSharpCompilationSource = match source with | null -> failwith "Source cannot be null" | _ -> { Source = Text source - Baseline = None Options = defaultOptions OutputType = Library SourceKind = kind @@ -119,11 +105,11 @@ module rec Compiler = | _ -> { Source = Text source LangVersion = CSharpLanguageVersion.CSharp8 - TargetFramework = TargetFramework.NetCoreApp31 + TargetFramework = TargetFramework.NetCoreApp30 Name = None References = [] } - let private fromFSharpErrorInfo (errors: FSharpErrorInfo[]) : ErrorInfo list = + let private fromFSharpErrorInfo (errors: FSharpErrorInfo[]) : (ErrorInfo list * ErrorInfo list) = let toErrorInfo (e: FSharpErrorInfo) : ErrorInfo = let errorNumber = e.ErrorNumber let severity = e.Severity @@ -142,12 +128,7 @@ module rec Compiler = |> List.ofArray |> List.distinctBy (fun e -> e.Severity, e.ErrorNumber, e.StartLineAlternate, e.StartColumn, e.EndLineAlternate, e.EndColumn, e.Message) |> List.map toErrorInfo - - let private partitionErrors diagnostics = diagnostics |> List.partition (fun e -> match e.Error with Error _ -> true | _ -> false) - - let private getErrors diagnostics = diagnostics |> List.filter (fun e -> match e.Error with Error _ -> true | _ -> false) - - let private getWarnings diagnostics = diagnostics |> List.filter (fun e -> match e.Error with Warning _ -> true | _ -> false) + |> List.partition (fun e -> match e.Error with Error _ -> true | _ -> false) let private adjustRange (range: Range) (adjust: int) : Range = { range with @@ -162,6 +143,19 @@ module rec Compiler = let FSharp (source: string) : CompilationUnit = fsFromString source SourceKind.Fs |> FS + let baseline (dir: string, file: string) : CompilationUnit = + match (dir, file) with + | dir, _ when String.IsNullOrWhiteSpace dir -> failwith "Baseline tests directory cannot be null or empty." + | _, file when String.IsNullOrWhiteSpace file -> failwith "Baseline source file name cannot be null or empty." + | _ -> + { Source = Baseline (dir, file) + Options = defaultOptions + OutputType = Library + SourceKind = Fs + Name = None + IgnoreWarnings = false + References = [] } |> FS + let CSharp (source: string) : CompilationUnit = csFromString source |> CS @@ -177,36 +171,10 @@ module rec Compiler = | CS cs -> CS { cs with References = cs.References @ references } | IL _ -> failwith "References are not supported in IL" - let private withOptionsHelper (options: string list) (message:string) (cUnit: CompilationUnit) : CompilationUnit = - match cUnit with - | FS fs -> FS { fs with Options = fs.Options @ options } - | _ -> failwith message - let withOptions (options: string list) (cUnit: CompilationUnit) : CompilationUnit = - withOptionsHelper options "withOptions is only supported for F#" cUnit - - let withErrorRanges (cUnit: CompilationUnit) : CompilationUnit = - withOptionsHelper [ "--test:ErrorRanges" ] "withErrorRanges is only supported on F#" cUnit - - let withLangVersion46 (cUnit: CompilationUnit) : CompilationUnit = - withOptionsHelper [ "--langversion:4.6" ] "withLangVersion46 is only supported on F#" cUnit - - let withLangVersion47 (cUnit: CompilationUnit) : CompilationUnit = - withOptionsHelper [ "--langversion:4.7" ] "withLangVersion47 is only supported on F#" cUnit - - let withLangVersion50 (cUnit: CompilationUnit) : CompilationUnit = - withOptionsHelper [ "--langversion:5.0" ] "withLangVersion50 is only supported on F#" cUnit - - let withLangVersionPreview (cUnit: CompilationUnit) : CompilationUnit = - withOptionsHelper [ "--langversion:preview" ] "withLangVersionPreview is only supported on F#" cUnit - - /// Turns on checks that check integrity of XML doc comments - let withXmlCommentChecking (cUnit: CompilationUnit) : CompilationUnit = - withOptionsHelper [ "--warnon:3390" ] "withXmlCommentChecking is only supported for F#" cUnit - - /// Turns on checks that force the documentation of all parameters - let withXmlCommentStrictParamChecking (cUnit: CompilationUnit) : CompilationUnit = - withOptionsHelper [ "--warnon:3391" ] "withXmlCommentChecking is only supported for F#" cUnit + match cUnit with + | FS fs -> FS { fs with Options = options } + | _ -> failwith "withOptions is only supported n F#" let asLibrary (cUnit: CompilationUnit) : CompilationUnit = match cUnit with @@ -229,7 +197,7 @@ module rec Compiler = let result = compileFSharpCompilation cmpl false match result with | Failure f -> - let message = sprintf "Operation failed (expected to succeed).\n All errors:\n%A" (f.Diagnostics) + let message = sprintf "Operation failed (expected to succeed).\n All errors:\n%A" (f.Errors @ f.Warnings) failwith message | Success s -> match s.OutputPath with @@ -261,24 +229,25 @@ module rec Compiler = let private compileFSharpCompilation compilation ignoreWarnings : TestResult = - let ((err: FSharpErrorInfo[], outputFilePath: string), deps) = CompilerAssert.CompileRaw(compilation, ignoreWarnings) + let ((err: FSharpErrorInfo[], outputFilePath: string), deps) = CompilerAssert.CompileRaw(compilation) - let diagnostics = err |> fromFSharpErrorInfo + let (errors, warnings) = err |> fromFSharpErrorInfo let result = { OutputPath = None Dependencies = deps Adjust = 0 - Diagnostics = diagnostics + Warnings = warnings + Errors = errors Output = None } - let (errors, warnings) = partitionErrors diagnostics - // Treat warnings as errors if "IgnoreWarnings" is false if errors.Length > 0 || (warnings.Length > 0 && not ignoreWarnings) then - Failure result + Failure { result with Warnings = warnings + Errors = errors } else - Success { result with OutputPath = Some outputFilePath } + Success { result with Warnings = warnings + OutputPath = Some outputFilePath } let private compileFSharp (fsSource: FSharpCompilationSource) : TestResult = @@ -300,7 +269,6 @@ module rec Compiler = Directory.CreateDirectory(outputPath) |> ignore let filename = compilation.AssemblyName - let output = Path.Combine(outputPath, Path.ChangeExtension(filename, ".dll")) let cmplResult = compilation.Emit (output) @@ -309,7 +277,8 @@ module rec Compiler = { OutputPath = None Dependencies = [] Adjust = 0 - Diagnostics = [] + Warnings = [] + Errors = [] Output = None } if cmplResult.Success then @@ -354,13 +323,14 @@ module rec Compiler = let parseResults = CompilerAssert.Parse source let failed = parseResults.ParseHadErrors - let diagnostics = parseResults.Errors |> fromFSharpErrorInfo + let (errors, warnings) = parseResults.Errors |> fromFSharpErrorInfo let result = { OutputPath = None Dependencies = [] Adjust = 0 - Diagnostics = diagnostics + Warnings = errors + Errors = warnings Output = None } if failed then @@ -373,39 +343,45 @@ module rec Compiler = | FS fs -> parseFSharp fs | _ -> failwith "Parsing only supported for F#." - let private typecheckFSharpSourceAndReturnErrors (fsSource: FSharpCompilationSource) : FSharpErrorInfo [] = - let source = getSource fsSource.Source - let options = fsSource.Options |> Array.ofList - - let name = match fsSource.Name with | None -> "test.fs" | Some n -> n + let private typecheckFSharpWithBaseline (options: string list) (dir: string) (file: string) : TestResult = + // Since TypecheckWithErrorsAndOptionsAgainsBaseLine throws if doesn't match expected baseline, + // We return a successfull TestResult if it succeeds. + CompilerAssert.TypeCheckWithErrorsAndOptionsAgainstBaseLine (Array.ofList options) dir file - let (err: FSharpErrorInfo []) = CompilerAssert.TypeCheckWithOptionsAndName options name source - - err + Success + { OutputPath = None + Dependencies = [] + Adjust = 0 + Warnings = [] + Errors = [] + Output = None } let private typecheckFSharpSource (fsSource: FSharpCompilationSource) : TestResult = + let source = getSource fsSource.Source + let options = fsSource.Options |> Array.ofList - let (err: FSharpErrorInfo []) = typecheckFSharpSourceAndReturnErrors fsSource + let (err: FSharpErrorInfo []) = CompilerAssert.TypeCheckWithOptions options source - let diagnostics = err |> fromFSharpErrorInfo + let (errors, warnings) = err |> fromFSharpErrorInfo let result = { OutputPath = None Dependencies = [] Adjust = 0 - Diagnostics = diagnostics + Warnings = warnings + Errors = errors Output = None } - let (errors, warnings) = partitionErrors diagnostics - // Treat warnings as errors if "IgnoreWarnings" is false; if errors.Length > 0 || (warnings.Length > 0 && not fsSource.IgnoreWarnings) then - Failure result + Failure { result with Warnings = warnings + Errors = errors } else - Success result + Success { result with Warnings = warnings } let private typecheckFSharp (fsSource: FSharpCompilationSource) : TestResult = match fsSource.Source with + | Baseline (f, d) -> typecheckFSharpWithBaseline fsSource.Options f d | _ -> typecheckFSharpSource fsSource let typecheck (cUnit: CompilationUnit) : TestResult = @@ -415,13 +391,13 @@ module rec Compiler = let run (result: TestResult) : TestResult = match result with - | Failure f -> failwith (sprintf "Compilation should be successfull in order to run.\n Errors: %A" (f.Diagnostics)) + | Failure f -> failwith (sprintf "Compilation should be successfull in order to run.\n Errors: %A" (f.Errors @ f.Warnings)) | Success s -> match s.OutputPath with | None -> failwith "Compilation didn't produce any output. Unable to run. (did you forget to set output type to Exe?)" | Some p -> let (exitCode, output, errors) = CompilerAssert.ExecuteAndReturnResult (p, s.Dependencies, false) - let executionResult = { s with Output = Some (ExecutionOutput { ExitCode = exitCode; StdOut = output; StdErr = errors }) } + let executionResult = { s with Output = Some { ExitCode = exitCode; StdOut = output; StdErr = errors } } if exitCode = 0 then Success executionResult else @@ -431,132 +407,6 @@ module rec Compiler = let compileExeAndRun = asExe >> compileAndRun - let private evalFSharp (fs: FSharpCompilationSource) : TestResult = - let source = getSource fs.Source - let options = fs.Options |> Array.ofList - - use script = new FSharpScript(additionalArgs=options) - - let ((evalresult: Result), (err: FSharpErrorInfo[])) = script.Eval(source) - - let diagnostics = err |> fromFSharpErrorInfo - - let result = - { OutputPath = None - Dependencies = [] - Adjust = 0 - Diagnostics = diagnostics - Output = Some(EvalOutput evalresult) } - - let (errors, warnings) = partitionErrors diagnostics - - let evalError = match evalresult with Ok _ -> false | _ -> true - - if evalError || errors.Length > 0 || (warnings.Length > 0 && not fs.IgnoreWarnings) then - Failure result - else - Success result - - let eval (cUnit: CompilationUnit) : TestResult = - match cUnit with - | FS fs -> evalFSharp fs - | _ -> failwith "Script evaluation is only supported for F#." - - let runFsi (cUnit: CompilationUnit) : TestResult = - match cUnit with - | FS fs -> - let source = getSource fs.Source - - let options = fs.Options |> Array.ofList - - let errors = CompilerAssert.RunScriptWithOptionsAndReturnResult options source - - let result = - { OutputPath = None - Dependencies = [] - Adjust = 0 - Diagnostics = [] - Output = None } - - if errors.Count > 0 then - let output = ExecutionOutput { - ExitCode = -1 - StdOut = String.Empty - StdErr = ((errors |> String.concat "\n").Replace("\r\n","\n")) } - Failure { result with Output = Some output } - else - Success result - | _ -> failwith "FSI running only supports F#." - - - let private createBaselineErrors (baseline: Baseline) actualErrors extension : unit = - match baseline.SourceFilename with - | Some f -> File.WriteAllText(Path.ChangeExtension(f, extension), actualErrors) - | _ -> () - - let private verifyFSBaseline (fs) : unit = - match fs.Baseline with - | None -> failwith "Baseline was not provided." - | Some bsl -> - let errorsExpectedBaseLine = - match bsl.OutputBaseline with - | Some b -> b.Replace("\r\n","\n") - | None -> String.Empty - - let typecheckDiagnostics = fs |> typecheckFSharpSourceAndReturnErrors - - let errorsActual = (typecheckDiagnostics |> Array.map (sprintf "%A") |> String.concat "\n").Replace("\r\n","\n") - - if errorsExpectedBaseLine <> errorsActual then - createBaselineErrors bsl errorsActual "fs.bsl.err" - - Assert.AreEqual(errorsExpectedBaseLine, errorsActual) - - /// Check the typechecker output against the baseline, if invoked with empty baseline, will expect no error/warnings output. - let verifyBaseline (cUnit: CompilationUnit) : CompilationUnit = - match cUnit with - | FS fs -> (verifyFSBaseline fs) |> ignore - | _ -> failwith "Baseline tests are only supported for F#." - - cUnit - - let verifyIL (il: string list) (result: TestResult) : unit = - match result with - | Success s -> - match s.OutputPath with - | None -> failwith "Operation didn't produce any output!" - | Some p -> ILChecker.checkIL p il - | Failure _ -> failwith "Result should be \"Success\" in order to get IL." - - let private verifyFSILBaseline (baseline: Baseline option) (result: Output) : unit = - match baseline with - | None -> failwith "Baseline was not provided." - | Some bsl -> - match result.OutputPath with - | None -> failwith "Operation didn't produce any output!" - | Some p -> - let expectedIL = - match bsl.ILBaseline with - | Some b -> b.Replace("\r\n","\n") - | None -> String.Empty - let (success, errorMsg, actualIL) = ILChecker.verifyILAndReturnActual p expectedIL - - if not success then - createBaselineErrors bsl actualIL "fs.il.err" - Assert.Fail(errorMsg) - - let verifyILBaseline (cUnit: CompilationUnit) : CompilationUnit = - match cUnit with - | FS fs -> - match fs |> compileFSharp |> shouldSucceed with - | Failure _ -> failwith "Result should be \"Success\" in order to get IL." - | Success s -> verifyFSILBaseline fs.Baseline s - | _ -> failwith "Baseline tests are only supported for F#." - - cUnit - - let verifyBaselines = verifyBaseline >> verifyILBaseline - [] module Assertions = let private getErrorNumber (error: ErrorType) : int = @@ -588,7 +438,7 @@ module rec Compiler = let inline checkEqual k a b = if a <> b then - Assert.AreEqual(a, b, sprintf "%s: Mismatch in %s, expected '%A', got '%A'.\nAll errors:\n%A\nExpected errors:\n%A" what k a b errors expected) + Assert.AreEqual(a, b, sprintf "%s: Mismatch in %s, expected '%A', got '%A'.\nAll errors:\n%A" what k a b errors) // TODO: Check all "categories", collect all results and print alltogether. checkEqual "Errors count" expected.Length errors.Length @@ -611,7 +461,7 @@ module rec Compiler = match result with | Success _ -> result | Failure r -> - let message = sprintf "Operation failed (expected to succeed).\n All errors:\n%A" (r.Diagnostics) + let message = sprintf "Operation failed (expected to succeed).\n All errors:\n%A" (r.Errors @ r.Warnings) failwith message let shouldFail (result: TestResult) : TestResult = @@ -621,12 +471,12 @@ module rec Compiler = let private assertResultsCategory (what: string) (selector: Output -> ErrorInfo list) (expected: ErrorInfo list) (result: TestResult) : TestResult = match result with - | Success r | Failure r -> + | Success r | Failure r -> assertErrors what r.Adjust (selector r) expected result let withResults (expectedResults: ErrorInfo list) result : TestResult = - assertResultsCategory "Results" (fun r -> r.Diagnostics) expectedResults result + assertResultsCategory "Results" (fun r -> r.Warnings @ r.Errors) expectedResults result let withResult (expectedResult: ErrorInfo ) (result: TestResult) : TestResult = withResults [expectedResult] result @@ -650,7 +500,7 @@ module rec Compiler = withDiagnostics [expected] result let withErrors (expectedErrors: ErrorInfo list) (result: TestResult) : TestResult = - assertResultsCategory "Errors" (fun r -> getErrors r.Diagnostics) expectedErrors result + assertResultsCategory "Errors" (fun r -> r.Errors) expectedErrors result let withError (expectedError: ErrorInfo) (result: TestResult) : TestResult = withErrors [expectedError] result @@ -662,19 +512,19 @@ module rec Compiler = result let withErrorCodes (expectedCodes: int list) (result: TestResult) : TestResult = - checkCodes expectedCodes (fun r -> getErrors r.Diagnostics) result + checkCodes expectedCodes (fun r -> r.Errors) result let withErrorCode (expectedCode: int) (result: TestResult) : TestResult = withErrorCodes [expectedCode] result let withWarnings (expectedWarnings: ErrorInfo list) (result: TestResult) : TestResult = - assertResultsCategory "Warnings" (fun r -> getWarnings r.Diagnostics) expectedWarnings result + assertResultsCategory "Warnings" (fun r -> r.Warnings) expectedWarnings result let withWarning (expectedWarning: ErrorInfo) (result: TestResult) : TestResult = withWarnings [expectedWarning] result let withWarningCodes (expectedCodes: int list) (result: TestResult) : TestResult = - checkCodes expectedCodes (fun r -> getWarnings r.Diagnostics) result + checkCodes expectedCodes (fun r -> r.Warnings) result let withWarningCode (expectedCode: int) (result: TestResult) : TestResult = withWarningCodes [expectedCode] result @@ -685,19 +535,19 @@ module rec Compiler = result let withMessages (messages: string list) (result: TestResult) : TestResult = - checkErrorMessages messages (fun r -> r.Diagnostics) result + checkErrorMessages messages (fun r -> r.Warnings @ r.Errors) result let withMessage (message: string) (result: TestResult) : TestResult = withMessages [message] result let withErrorMessages (messages: string list) (result: TestResult) : TestResult = - checkErrorMessages messages (fun r -> getErrors r.Diagnostics) result + checkErrorMessages messages (fun r -> r.Errors) result let withErrorMessage (message: string) (result: TestResult) : TestResult = withErrorMessages [message] result let withWarningMessages (messages: string list) (result: TestResult) : TestResult = - checkErrorMessages messages (fun r -> getWarnings r.Diagnostics) result + checkErrorMessages messages (fun r -> r.Warnings) result let withWarningMessage (message: string) (result: TestResult) : TestResult = withWarningMessages [message] result @@ -707,10 +557,7 @@ module rec Compiler = | Success r | Failure r -> match r.Output with | None -> failwith "Execution output is missing, cannot check exit code." - | Some o -> - match o with - | ExecutionOutput e -> Assert.AreEqual(e.ExitCode, expectedExitCode, sprintf "Exit code was expected to be: %A, but got %A." expectedExitCode e.ExitCode) - | _ -> failwith "Cannot check exit code on this run result." + | Some o -> Assert.AreEqual(o.ExitCode, expectedExitCode, sprintf "Exit code was expected to be: %A, but got %A." expectedExitCode o.ExitCode) result let private checkOutput (category: string) (substring: string) (selector: ExecutionOutput -> string) (result: TestResult) : TestResult = @@ -719,12 +566,9 @@ module rec Compiler = match r.Output with | None -> failwith (sprintf "Execution output is missing cannot check \"%A\"" category) | Some o -> - match o with - | ExecutionOutput e -> - let where = selector e - if not (where.Contains(substring)) then - failwith (sprintf "\nThe following substring:\n %A\nwas not found in the %A\nOutput:\n %A" substring category where) - | _ -> failwith "Cannot check output on this run result." + let where = selector o + if not (where.Contains(substring)) then + failwith (sprintf "\nThe following substring:\n %A\nwas not found in the %A\nOutput:\n %A" substring category where) result let withOutputContains (substring: string) (result: TestResult) : TestResult = @@ -735,31 +579,3 @@ module rec Compiler = let withStdErrContains (substring: string) (result: TestResult) : TestResult = checkOutput "STDERR" substring (fun o -> o.StdErr) result - - // TODO: probably needs a bit of simplification, + need to remove that pyramid of doom. - let private assertEvalOutput (selector: FsiValue -> 'T) (value: 'T) (result: TestResult) : TestResult = - match result with - | Success r | Failure r -> - match r.Output with - | None -> failwith "Execution output is missing cannot check value." - | Some o -> - match o with - | EvalOutput e -> - match e with - | Ok v -> - match v with - | None -> failwith "Cannot assert value of evaluation, since it is None." - | Some e -> Assert.AreEqual(value, (selector e)) - | Result.Error ex -> raise ex - | _ -> failwith "Only 'eval' output is supported." - result - - // TODO: Need to support for: - // STDIN, to test completions - // Contains - // Cancellation - let withEvalValueEquals (value: 'T) (result: TestResult) : TestResult = - assertEvalOutput (fun (x: FsiValue) -> x.ReflectionValue :?> 'T) value result - - let withEvalTypeEquals t (result: TestResult) : TestResult = - assertEvalOutput (fun (x: FsiValue) -> x.ReflectionType) t result \ No newline at end of file diff --git a/tests/FSharp.Test.Utilities/CompilerAssert.fs b/tests/FSharp.Test.Utilities/CompilerAssert.fs index c32ae04417d..f18022a0159 100644 --- a/tests/FSharp.Test.Utilities/CompilerAssert.fs +++ b/tests/FSharp.Test.Utilities/CompilerAssert.fs @@ -678,10 +678,13 @@ let main argv = 0""" static member RunScript source expectedErrorMessages = CompilerAssert.RunScriptWithOptions [||] source expectedErrorMessages - static member ParseWithErrors (source: string) expectedParseErrors = + static member Parse (source: string) = let sourceFileName = "test.fs" let parsingOptions = { FSharpParsingOptions.Default with SourceFiles = [| sourceFileName |] } - let parseResults = checker.ParseFile(sourceFileName, SourceText.ofString source, parsingOptions) |> Async.RunSynchronously + checker.ParseFile(sourceFileName, SourceText.ofString source, parsingOptions) |> Async.RunSynchronously + + static member ParseWithErrors (source: string) expectedParseErrors = + let parseResults = CompilerAssert.Parse source Assert.True(parseResults.ParseHadErrors)