Skip to content

Commit

Permalink
Update unit test according to new world.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Nov 10, 2023
1 parent 58837c3 commit 6812c7b
Showing 1 changed file with 60 additions and 63 deletions.
123 changes: 60 additions & 63 deletions tests/service/Symbols.fs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,14 @@ let f2 b1 b2 b3 b4 b5 =
| _ -> ()

module FSharpMemberOrFunctionOrValue =
let private chooseMemberOrFunctionOrValue (su: FSharpSymbolUse) =
match su.Symbol with :? FSharpMemberOrFunctionOrValue as mfv -> Some mfv | _ -> None

let private pickPropertySymbol (su: FSharpSymbolUse) =
match su.Symbol with
| :? FSharpMemberOrFunctionOrValue as mfv when mfv.IsProperty -> Some (mfv, su.Range)
| _ -> None

[<Test>]
let ``Both Set and Get symbols are present`` () =
let _, checkResults = getParseAndCheckResults """
Expand All @@ -441,16 +449,13 @@ type Foo =
and set (a: int) (b: float) = ()
"""

// "X" resolves a symbol but it will either be the get or set symbol.
// "X" resolves a symbol but it will either be the property, get or set symbol.
// Use get_ or set_ to differentiate.
let xSymbol = checkResults.GetSymbolUsesAtLocation(5, 14, " member _.X", [ "X" ]) |> List.exactlyOne

match xSymbol.Symbol with
| :? FSharpMemberOrFunctionOrValue as mfv ->
Assert.True mfv.IsProperty
Assert.True mfv.HasGetterMethod
Assert.True mfv.HasSetterMethod
| symbol-> Assert.Fail $"Expected {symbol} to be FSharpMemberOrFunctionOrValue"
let xPropertySymbol, _ = checkResults.GetSymbolUsesAtLocation(5, 14, " member _.X", [ "X" ]) |> List.pick pickPropertySymbol

Assert.True xPropertySymbol.IsProperty
Assert.True xPropertySymbol.HasGetterMethod
Assert.True xPropertySymbol.HasSetterMethod

let getSymbol = findSymbolUseByName "get_X" checkResults
match getSymbol.Symbol with
Expand All @@ -465,67 +470,64 @@ type Foo =
| symbol -> Assert.Fail $"Expected {symbol} to be FSharpMemberOrFunctionOrValue"

[<Test>]
let ``AutoProperty with get,set has a single symbol!`` () =
let ``AutoProperty with get,set has property symbol!`` () =
let _, checkResults = getParseAndCheckResults """
namespace Foo
type Foo =
member val AutoPropGetSet = 0 with get, set
"""

let autoPropertySymbolUse =
checkResults.GetSymbolUsesAtLocation(5, 29, " member val AutoPropGetSet = 0 with get, set", ["AutoPropGetSet"])
|> List.exactlyOne

match autoPropertySymbolUse.Symbol with
| :? FSharpMemberOrFunctionOrValue as mfv ->
Assert.True mfv.IsProperty
Assert.True mfv.HasGetterMethod
Assert.True mfv.HasSetterMethod
Assert.True (mfv.GetterMethod.CompiledName.StartsWith("get_"))
Assert.True (mfv.SetterMethod.CompiledName.StartsWith("set_"))
assertRange (5, 15) (5, 29) autoPropertySymbolUse.Range
let symbols = checkResults.GetSymbolUsesAtLocation(5, 29, " member val AutoPropGetSet = 0 with get, set", ["AutoPropGetSet"])

| _ -> Assert.Fail "Symbol was not FSharpMemberOrFunctionOrValue"
let autoPropertySymbol, mAutoPropSymbolUse =
symbols
|> List.pick pickPropertySymbol

let getSymbol =
checkResults.GetSymbolUsesAtLocation(5, 42, " member val AutoPropGetSet = 0 with get, set", ["get"])
|> List.map (fun su -> su.Symbol)
|> List.exactlyOne

// Two symbols for the setter: the set function and the compiler generated v parameter
let setSymbols =
checkResults.GetSymbolUsesAtLocation(5, 47, " member val AutoPropGetSet = 0 with get, set", ["set"])
|> List.map (fun su -> su.Symbol)
Assert.True autoPropertySymbol.IsProperty
Assert.True autoPropertySymbol.HasGetterMethod
Assert.True autoPropertySymbol.HasSetterMethod
Assert.True (autoPropertySymbol.GetterMethod.CompiledName.StartsWith("get_"))
Assert.True (autoPropertySymbol.SetterMethod.CompiledName.StartsWith("set_"))
assertRange (5, 15) (5, 29) mAutoPropSymbolUse

match getSymbol, setSymbols with
| :? FSharpMemberOrFunctionOrValue as getMfv,
[ :? FSharpMemberOrFunctionOrValue as setVMfv
:? FSharpMemberOrFunctionOrValue as setMfv ] ->
let symbols =
symbols
|> List.choose chooseMemberOrFunctionOrValue

match symbols with
| [ propMfv
setMfv
getMfv ] ->
Assert.True propMfv.IsProperty
Assert.True(getMfv.CompiledName.StartsWith("get_"))
Assert.AreEqual("v", setVMfv.DisplayName)
Assert.True(setMfv.CompiledName.StartsWith("set_"))
| _ -> Assert.Fail "Expected symbols to be FSharpMemberOrFunctionOrValue"
| _ -> Assert.Fail $"Expected three symbols, got %A{symbols}"

// The setter should have a symbol for the generated parameter `v`.
let setVMfv =
checkResults.GetSymbolUsesAtLocation(5, 29, " member val AutoPropGetSet = 0 with get, set", ["v"])
|> List.tryExactlyOne
|> Option.map chooseMemberOrFunctionOrValue

if Option.isNone setVMfv then Assert.Fail "No generated v symbol for the setter was found"

[<Test>]
let ``Single symbol is resolved for property`` () =
let ``Property symbol is resolved for property`` () =
let source = """
type X(y: string) =
member val Y = y with get, set
"""

let _, checkResults = getParseAndCheckResults source
let symbolUses =
let propSymbol, _ =
checkResults.GetSymbolUsesAtLocation(3, 16, " member val Y = y with get, set", [ "Y" ])
|> List.map (fun su -> su.Symbol)
|> List.pick pickPropertySymbol

match symbolUses with
| [ :? FSharpMemberOrFunctionOrValue as mfv ] ->
Assert.True mfv.IsProperty
Assert.True mfv.HasGetterMethod
Assert.True mfv.HasSetterMethod
assertRange (3, 15) (3, 16) mfv.SignatureLocation.Value
| _ -> Assert.Fail "Expected symbols"
Assert.True propSymbol.IsProperty
Assert.True propSymbol.HasGetterMethod
Assert.True propSymbol.HasSetterMethod
assertRange (3, 15) (3, 16) propSymbol.SignatureLocation.Value

[<Test>]
let ``Multiple relevant symbols for type name`` () =
Expand Down Expand Up @@ -627,17 +629,14 @@ type Foo() =
member this.Count with set (v:int) = b <- v and get () = b
"""

let getSymbolUses =
let propSymbol, _ =
checkResults.GetSymbolUsesAtLocation(6, 21, " member this.Count with set (v:int) = b <- v", ["Count"])
|> List.map (fun su -> su.Symbol)
|> List.pick pickPropertySymbol

match getSymbolUses with
| [ :? FSharpMemberOrFunctionOrValue as mfv ] ->
Assert.True mfv.IsProperty
Assert.True mfv.HasGetterMethod
Assert.True mfv.HasSetterMethod
assertRange (6, 16) (6, 21) mfv.SignatureLocation.Value
| symbols -> Assert.Fail $"Unexpected symbols, got %A{symbols}"
Assert.True propSymbol.IsProperty
Assert.True propSymbol.HasGetterMethod
Assert.True propSymbol.HasSetterMethod
assertRange (6, 16) (6, 21) propSymbol.SignatureLocation.Value

[<Test>]
let ``Property usage is reported properly`` () =
Expand All @@ -653,16 +652,14 @@ type Foo() =
ignore (Foo().Name)
"""

let propertySymbolUse =
let propertySymbolUse, _ =
checkResults.GetSymbolUsesAtLocation(6, 17, " member x.Name", ["Name"])
|> List.map (fun su -> su.Symbol)
|> List.exactlyOne
|> List.pick pickPropertySymbol

let usages = checkResults.GetUsesOfSymbolInFile(propertySymbolUse)
Assert.AreEqual(3, usages.Length)
Assert.AreEqual(2, usages.Length)
Assert.True usages.[0].IsFromDefinition
Assert.True usages.[1].IsFromDefinition
Assert.True usages.[2].IsFromUse
Assert.True usages.[1].IsFromUse

module GetValSignatureText =
let private assertSignature (expected:string) source (lineNumber, column, line, identifier) =
Expand Down

0 comments on commit 6812c7b

Please sign in to comment.