Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
<Compile Include="$(FSharpSourcesRoot)\..\tests\service\TokenizerTests.fs">
<Link>TokenizerTests.fs</Link>
</Compile>
<Compile Include="$(FSharpSourcesRoot)\..\tests\service\ServiceUntypedParseTests.fs">
<Link>ServiceUntypedParseTests.fs</Link>
</Compile>
<Compile Include="$(FSharpSourcesRoot)\..\tests\service\TreeVisitorTests.fs">
<Link>TreeVisitorTests.fs</Link>
</Compile>
<Compile Include="$(FSharpSourcesRoot)\..\tests\service\Program.fs" Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
<Link>Program.fs</Link>
</Compile>
Expand Down
9 changes: 6 additions & 3 deletions src/fsharp/service/ServiceParseTreeWalk.fs
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,12 @@ module public AstTraversal =
dive synExpr2 synExpr2.Range traverseSynExpr
dive synExpr3 synExpr3.Range traverseSynExpr]
|> pick expr
| SynExpr.TypeTest(synExpr, _synType, _range) -> traverseSynExpr synExpr
| SynExpr.Upcast(synExpr, _synType, _range) -> traverseSynExpr synExpr
| SynExpr.Downcast(synExpr, _synType, _range) -> traverseSynExpr synExpr
| SynExpr.TypeTest(synExpr, synType, _range)
| SynExpr.Upcast(synExpr, synType, _range)
| SynExpr.Downcast(synExpr, synType, _range) ->
[dive synExpr synExpr.Range traverseSynExpr
dive synType synType.Range traverseSynType]
|> pick expr
| SynExpr.InferredUpcast(synExpr, _range) -> traverseSynExpr synExpr
| SynExpr.InferredDowncast(synExpr, _range) -> traverseSynExpr synExpr
| SynExpr.Null(_range) -> None
Expand Down
17 changes: 9 additions & 8 deletions tests/service/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,18 @@ let parseAndCheckScript (file, input) =
| FSharpCheckFileAnswer.Succeeded(res) -> parseResult, res
| res -> failwithf "Parsing did not finish... (%A)" res

let parseSourceCode (name: string, code: string) =
let location = Path.Combine(Path.GetTempPath(),"test"+string(hash (name, code)))
try Directory.CreateDirectory(location) |> ignore with _ -> ()
let parseSource (source: string) =
let location = Path.GetTempFileName()
let filePath = Path.Combine(location, ".fs")
let dllPath = Path.Combine(location, ".dll")

let projPath = Path.Combine(location, name + ".fsproj")
let filePath = Path.Combine(location, name + ".fs")
let dllPath = Path.Combine(location, name + ".dll")
let args = mkProjectCommandLineArgs(dllPath, [filePath])
let options, errors = checker.GetParsingOptionsFromCommandLineArgs(List.ofArray args)
let parseResults = checker.ParseFile(filePath, code, options) |> Async.RunSynchronously
parseResults.ParseTree
let parseResults = checker.ParseFile(filePath, source, options) |> Async.RunSynchronously

match parseResults.ParseTree with
| Some parseTree -> parseTree
| None -> failwithf "Expected there to be a parse tree for source:\n%s" source

/// Extract range info
let tups (m:Range.range) = (m.StartLine, m.StartColumn), (m.EndLine, m.EndColumn)
Expand Down
6 changes: 1 addition & 5 deletions tests/service/InteractiveCheckerTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ let internal identsAndRanges (input: Ast.ParsedInput) =
| Ast.ParsedInput.SigFile _ -> []

let internal parseAndExtractRanges code =
let file = "Test"
let result = parseSourceCode (file, code)
match result with
| Some tree -> tree |> identsAndRanges
| None -> failwith "fail to parse..."
parseSource code |> identsAndRanges

let input =
"""
Expand Down
14 changes: 6 additions & 8 deletions tests/service/ServiceUntypedParseTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ let private (=>) (source: string) (expected: CompletionContext option) =
match markerPos with
| None -> failwithf "Marker '%s' was not found in the source code" Marker
| Some markerPos ->
match parseSourceCode("C:\\test.fs", source) with
| None -> failwith "No parse tree"
| Some parseTree ->
let actual = UntypedParseImpl.TryGetCompletionContext(markerPos, parseTree, lines.[Line.toZ markerPos.Line])
try Assert.AreEqual(expected, actual)
with e ->
printfn "ParseTree: %A" parseTree
reraise()
let parseTree = parseSource source
let actual = UntypedParseImpl.TryGetCompletionContext(markerPos, parseTree, lines.[Line.toZ markerPos.Line])
try Assert.AreEqual(expected, actual)
with e ->
printfn "ParseTree: %A" parseTree
reraise()

module AttributeCompletion =
[<Test>]
Expand Down
26 changes: 11 additions & 15 deletions tests/service/StructureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,19 @@ let (=>) (source: string) (expectedRanges: (Range * Range) list) =

let getRange (r: range) = (r.StartLine, r.StartColumn, r.EndLine, r.EndColumn)

let ast = parseSourceCode(fileName, source)

let tree = parseSource source
try
match ast with
| Some tree ->
let actual =
Structure.getOutliningRanges lines tree
|> Seq.filter (fun sr -> sr.Range.StartLine <> sr.Range.EndLine)
|> Seq.map (fun sr -> getRange sr.Range, getRange sr.CollapseRange)
|> Seq.sort
|> List.ofSeq
let expected = List.sort expectedRanges
if actual <> expected then
failwithf "Expected %s, but was %s" (formatList expected) (formatList actual)
| None -> failwithf "Expected there to be a parse tree for source:\n%s" source
let actual =
Structure.getOutliningRanges lines tree
|> Seq.filter (fun sr -> sr.Range.StartLine <> sr.Range.EndLine)
|> Seq.map (fun sr -> getRange sr.Range, getRange sr.CollapseRange)
|> Seq.sort
|> List.ofSeq
let expected = List.sort expectedRanges
if actual <> expected then
failwithf "Expected %s, but was %s" (formatList expected) (formatList actual)
with _ ->
printfn "AST:\n%+A" ast
printfn "AST:\n%+A" tree
reraise()

[<Test>]
Expand Down
22 changes: 22 additions & 0 deletions tests/service/TreeVisitorTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Tests.Service.TreeVisitorTests

open FSharp.Compiler.Service.Tests.Common
open Microsoft.FSharp.Compiler.Range
open Microsoft.FSharp.Compiler.SourceCodeServices.AstTraversal
open NUnit.Framework

[<Test>]
let ``Visit type test`` () =
let visitor =
{ new AstVisitorBase<_>() with
member x.VisitExpr(_, _, defaultTraverse, expr) = defaultTraverse expr
member x.VisitType(_, _) = Some () }

let source = "123 :? int"
let parseTree = parseSource source

Traverse(mkPos 1 11, parseTree, visitor)
|> Option.defaultWith (fun _ -> failwith "Did not visit type")

Traverse(mkPos 1 3, parseTree, visitor)
|> Option.iter (fun _ -> failwith "Should not visit type")
3 changes: 3 additions & 0 deletions vsintegration/tests/UnitTests/VisualFSharp.UnitTests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@
<Compile Include="UnusedOpensTests.fs">
<Link>CompilerService\UnusedOpensTests.fs</Link>
</Compile>
<Compile Include="..\..\..\tests\service\TreeVisitorTests.fs">
<Link>CompilerService\TreeVisitorTests.fs</Link>
</Compile>
<Compile Include="SyntacticColorizationServiceTests.fs">
<Link>Roslyn\SyntacticColorizationServiceTests.fs</Link>
</Compile>
Expand Down