-
Notifications
You must be signed in to change notification settings - Fork 806
/
Copy pathInteractiveCheckerTests.fs
95 lines (80 loc) · 3.93 KB
/
InteractiveCheckerTests.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
module FSharp.Compiler.Service.Tests.InteractiveCheckerTests
open Xunit
open FSharp.Test.Assert
open System
open FSharp.Compiler.Service.Tests.Common
open FSharp.Compiler.Syntax
open FSharp.Compiler.Text
open FSharp.Compiler.Text.Range
let internal longIdentToString (longIdent: LongIdent) =
String.Join(".", longIdent |> List.map (fun ident -> ident.ToString()))
let internal longIdentWithDotsToString (SynLongIdent (longIdent, _, _)) = longIdentToString longIdent
let internal posToTuple (pos: pos) = (pos.Line, pos.Column)
let internal rangeToTuple (range: range) = (posToTuple range.Start, posToTuple range.End)
let internal identsAndRanges (input: ParsedInput) =
let identAndRange ident (range: range) =
(ident, rangeToTuple range)
let extractFromComponentInfo (componentInfo: SynComponentInfo) =
let (SynComponentInfo.SynComponentInfo(_attrs, _typarDecls, _typarConstraints, longIdent, _, _, _, range)) = componentInfo
// TODO : attrs, typarDecls and typarConstraints
[identAndRange (longIdentToString longIdent) range]
let extractFromTypeDefn (typeDefn: SynTypeDefn) =
let (SynTypeDefn(typeInfo=componentInfo)) = typeDefn
// TODO : repr and members
extractFromComponentInfo componentInfo
let rec extractFromModuleDecl (moduleDecl: SynModuleDecl) =
match moduleDecl with
| SynModuleDecl.Types(typeDefns, _) -> (typeDefns |> List.collect extractFromTypeDefn)
| SynModuleDecl.ModuleAbbrev(ident, _, range) -> [ identAndRange (ident.ToString()) range ]
| SynModuleDecl.NestedModule(moduleInfo=componentInfo; decls=decls) -> (extractFromComponentInfo componentInfo) @ (decls |> List.collect extractFromModuleDecl)
| SynModuleDecl.Let _ -> failwith "Not implemented yet"
| SynModuleDecl.Expr _ -> failwith "Not implemented yet"
| SynModuleDecl.Exception _ -> failwith "Not implemented yet"
| SynModuleDecl.Open(SynOpenDeclTarget.ModuleOrNamespace (lid, range), _) -> [ identAndRange (longIdentToString lid.LongIdent) range ]
| SynModuleDecl.Open(SynOpenDeclTarget.Type _, _) -> failwith "Not implemented yet"
| SynModuleDecl.Attributes _ -> failwith "Not implemented yet"
| SynModuleDecl.HashDirective _ -> failwith "Not implemented yet"
| SynModuleDecl.NamespaceFragment(moduleOrNamespace) -> extractFromModuleOrNamespace moduleOrNamespace
and extractFromModuleOrNamespace (SynModuleOrNamespace(longId = longIdent; decls = moduleDecls)) =
let xs = moduleDecls |> List.collect extractFromModuleDecl
if longIdent.IsEmpty then xs
else
(identAndRange (longIdentToString longIdent) (longIdent |> List.map (fun id -> id.idRange) |> List.reduce unionRanges)) :: xs
match input with
| ParsedInput.ImplFile(ParsedImplFileInput(contents = modulesOrNamespaces)) ->
modulesOrNamespaces |> List.collect extractFromModuleOrNamespace
| ParsedInput.SigFile _ -> []
let internal parseAndExtractRanges code =
let file = "Test.fs"
let result = parseSourceCode (file, code)
result |> identsAndRanges
let input =
"""
namespace N
type Sample () = class end
"""
[<Fact>]
let ``Test ranges - namespace`` () =
let res = parseAndExtractRanges input
printfn "Test ranges - namespace, res = %A" res
res |> shouldEqual [("N", ((2, 14), (2, 15))); ("Sample", ((4, 9), (4, 15)))]
let input2 =
"""
module M
type Sample () = class end
"""
[<Fact>]
let ``Test ranges - module`` () =
let res = parseAndExtractRanges input2
printfn "Test ranges - module, res = %A" res
res |> shouldEqual [("M", ((2, 11), (2, 12))); ("Sample", ((4, 9), (4, 15)))]
let input3 =
"""
namespace global
type Sample () = class end
"""
[<Fact>]
let ``Test ranges - global namespace`` () =
let res = parseAndExtractRanges input3
printfn "Test ranges - global namespace, res = %A" res
res |> shouldEqual [("Sample", ((4, 9), (4, 15)))]