Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Hover #183

Merged
merged 2 commits into from
Aug 12, 2024
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 @@ -11,6 +11,7 @@
<Compile Include="DocumentationTests.fs" />
<Compile Include="InitializationTests.fs" />
<Compile Include="DiagnosticTests.fs" />
<Compile Include="HoverTests.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
54 changes: 15 additions & 39 deletions tests/CSharpLanguageServer.Tests/DiagnosticTests.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module CSharpLanguageServer.Tests.Diagnostics
module CSharpLanguageServer.Tests.DiagnosticTests

open System.Threading

Expand All @@ -11,29 +11,17 @@ open CSharpLanguageServer.Tests.Tooling
let testPushDiagnosticsWork () =
let projectFiles =
Map.ofList [
("Project/Project.csproj",
"""<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
""");
("Project/Class.cs",
"""XXX"""
)
("Project/Project.csproj", dotnet8PExeProjectCsproj)
("Project/Class.cs", """XXX""")
]

use client = setupServerClient
{ defaultClientProfile with LoggingEnabled = false }
projectFiles

use client = setupServerClient defaultClientProfile projectFiles
client.StartAndWaitForSolutionLoad()

//
// open Class.cs file and wait for diagnostics to be pushed
//
let classFile = client.Open("Project/Class.cs")
use classFile = client.Open("Project/Class.cs")

Thread.Sleep(4000)

Expand Down Expand Up @@ -78,29 +66,17 @@ let testPushDiagnosticsWork () =
let testPullDiagnosticsWork () =
let projectFiles =
Map.ofList [
("Project/Project.csproj",
"""<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
""");
("Project/Class.cs",
"""XXX"""
)
("Project/Project.csproj", dotnet8PExeProjectCsproj)
("Project/Class.cs", """XXX""" )
]

use client = setupServerClient
{ defaultClientProfile with LoggingEnabled = false }
projectFiles

use client = setupServerClient defaultClientProfile projectFiles
client.StartAndWaitForSolutionLoad()

//
// open Class.cs file and pull diagnostics
//
let classFile = client.Open("Project/Class.cs")
use classFile = client.Open("Project/Class.cs")

let diagnosticParams: DocumentDiagnosticParams =
{ WorkDoneToken = None
Expand All @@ -109,11 +85,10 @@ let testPullDiagnosticsWork () =
Identifier = None
PreviousResultId = None }

let report0: DocumentDiagnosticReport = classFile.Request("textDocument/diagnostic", diagnosticParams)
let report0: DocumentDiagnosticReport option = classFile.Request("textDocument/diagnostic", diagnosticParams)

match report0 with
| U2.C2 _ -> failwith "U2.C1 is expected"
| U2.C1 report ->
| Some (U2.C1 report) ->
Assert.AreEqual("full", report.Kind)
Assert.AreEqual(None, report.ResultId)
Assert.AreEqual(3, report.Items.Length)
Expand All @@ -134,18 +109,19 @@ let testPullDiagnosticsWork () =
Assert.AreEqual(
"The type or namespace name 'XXX' could not be found (are you missing a using directive or an assembly reference?)",
diagnostic2.Message)
| _ -> failwith "U2.C1 is expected"

//
// now try to do the same but with file fixed to contain no content (and thus no diagnostics)
//
classFile.DidChange("")

let report1: DocumentDiagnosticReport = classFile.Request("textDocument/diagnostic", diagnosticParams)
let report1: DocumentDiagnosticReport option = classFile.Request("textDocument/diagnostic", diagnosticParams)

match report1 with
| U2.C2 _ -> failwith "U2.C1 is expected"
| U2.C1 report ->
| Some (U2.C1 report) ->
Assert.AreEqual("full", report.Kind)
Assert.AreEqual(None, report.ResultId)
Assert.AreEqual(0, report.Items.Length)
| _ -> failwith "U2.C1 is expected"
()
62 changes: 62 additions & 0 deletions tests/CSharpLanguageServer.Tests/HoverTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module CSharpLanguageServer.Tests.HoverTests

open NUnit.Framework
open Ionide.LanguageServerProtocol.Types

open CSharpLanguageServer.Tests.Tooling

[<TestCase>]
let testHoverWorks() =
let classCsContents = """class Class
{
public void Method(string arg)
{
}
}
"""
let projectFiles =
Map.ofList [
("Project/Project.csproj", dotnet8PExeProjectCsproj)
("Project/Class.cs", classCsContents)
]

use client = setupServerClient defaultClientProfile projectFiles
client.StartAndWaitForSolutionLoad()

use classFile = client.Open("Project/Class.cs")

//
// check hover at method name
//
let hover0Params: HoverParams =
{ TextDocument = { Uri = classFile.Uri }
Position = { Line = 2u; Character = 16u }
WorkDoneToken = None
}

let hover0: Hover option = classFile.Request("textDocument/hover", hover0Params)
Assert.IsTrue(hover0.IsSome)

match hover0 with
| Some hover ->
match hover.Contents with
| U3.C1 c ->
Assert.AreEqual(MarkupKind.Markdown, c.Kind)
Assert.AreEqual("`` void Class.Method(string arg) ``", c.Value)
| _ -> failwith "C1 was expected"

Assert.IsTrue(hover.Range.IsNone)

| _ -> failwith "Some (U3.C1 c) was expected"

//
// check hover at beginning of the file (nothing should come up)
//
let hover1Params: HoverParams =
{ TextDocument = { Uri = classFile.Uri }
Position = { Line = 0u; Character = 0u }
WorkDoneToken = None
}

let hover1: Hover option = classFile.Request("textDocument/hover", hover1Params)
Assert.IsTrue(hover1.IsNone)

Check warning on line 62 in tests/CSharpLanguageServer.Tests/HoverTests.fs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0.303)

Main module of program is empty: nothing will happen when it is run

Check warning on line 62 in tests/CSharpLanguageServer.Tests/HoverTests.fs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0.303)

Main module of program is empty: nothing will happen when it is run

Check warning on line 62 in tests/CSharpLanguageServer.Tests/HoverTests.fs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 8.0.303)

Main module of program is empty: nothing will happen when it is run

Check warning on line 62 in tests/CSharpLanguageServer.Tests/HoverTests.fs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 8.0.303)

Main module of program is empty: nothing will happen when it is run
22 changes: 3 additions & 19 deletions tests/CSharpLanguageServer.Tests/InitializationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,11 @@ open Ionide.LanguageServerProtocol.Types
let testServerRegistersCapabilitiesWithTheClient () =
let projectFiles =
Map.ofList [
("Project/Project.csproj",
"""<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
""");
("Project/Class.cs",
"""using System;
class Class
{
}
"""
)
("Project/Project.csproj", dotnet8PExeProjectCsproj)
("Project/Class.cs", """class Class {}""")
]

use client = setupServerClient
{ defaultClientProfile with LoggingEnabled = false }
projectFiles

use client = setupServerClient defaultClientProfile projectFiles
client.StartAndWaitForSolutionLoad()

let serverInfo = client.GetState().ServerInfo.Value
Expand Down
27 changes: 18 additions & 9 deletions tests/CSharpLanguageServer.Tests/Tooling.fs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type ClientServerRpcRequestInfo =
{
Method: string
RpcRequestMsg: JObject
ResultReplyChannel: option<AsyncReplyChannel<JObject>>
ResultReplyChannel: option<AsyncReplyChannel<JObject option>>
}

type RpcEndpoint = Server | Client
Expand Down Expand Up @@ -128,7 +128,7 @@ type ClientEvent =
| GetState of AsyncReplyChannel<ClientState>
| ServerStderrLineRead of string option
| RpcMessageReceived of Result<JObject option, Exception>
| SendServerRpcRequest of string * JToken * option<AsyncReplyChannel<JObject>>
| SendServerRpcRequest of string * JToken * option<AsyncReplyChannel<JObject option>>
| ServerRpcCallResult of JObject
| SendServerRpcNotification of string * JToken
| ClientRpcCall of JValue * string * JObject
Expand Down Expand Up @@ -339,7 +339,7 @@ let processClientEvent (state: ClientState) (post: ClientEvent -> unit) msg : As

match rpcRequest.ResultReplyChannel with
| Some rc ->
rc.Reply(result["result"].ToObject())
rc.Reply(result["result"].ToObject() |> Option.ofObj)
| None -> ()

return { state with OutstandingServerRpcReqs = newOutstandingServerRpcReqs }
Expand Down Expand Up @@ -461,10 +461,10 @@ type FileController (client: MailboxProcessor<ClientEvent>, filename: string, ur
client.Post(SendServerRpcNotification ("textDocument/didClose", serialize didCloseParams))
()

member __.Request<'Request, 'Response>(method: string, request: 'Request): 'Response =
member __.Request<'Request, 'Response>(method: string, request: 'Request): option<'Response> =
let requestJObject = request |> serialize
let responseJObject = client.PostAndReply<JObject>(fun rc -> SendServerRpcRequest (method, requestJObject, Some rc))
responseJObject |> deserialize<'Response>
let responseJObject = client.PostAndReply<JObject option>(fun rc -> SendServerRpcRequest (method, requestJObject, Some rc))
responseJObject |> Option.map deserialize<'Response>

member __.DidChange(text: string) =
let didChangeParams: DidChangeTextDocumentParams =
Expand Down Expand Up @@ -521,15 +521,16 @@ type ClientController (client: MailboxProcessor<ClientEvent>, projectFiles: Map<
}

let initializeResult =
client.PostAndReply<JObject>(fun rc -> SendServerRpcRequest ("initialize", serialize initializeParams, Some rc))
client.PostAndReply<JObject option>(fun rc -> SendServerRpcRequest ("initialize", serialize initializeParams, Some rc))
|> _.Value
|> deserialize<InitializeResult>

client.Post(UpdateState (fun s -> { s with ServerInfo = initializeResult.ServerInfo
ServerCapabilities = Some initializeResult.Capabilities }))

log "OK, 'initialize' request complete"

let _ = client.PostAndReply<JObject>(fun rc -> SendServerRpcRequest ("initialized", JObject(), Some rc))
let _ = client.PostAndReply<JObject option>(fun rc -> SendServerRpcRequest ("initialized", JObject(), Some rc))
log "OK, 'initialized' request complete"

this.WaitForProgressEnd("OK, 1 project file(s) loaded")
Expand All @@ -555,7 +556,7 @@ type ClientController (client: MailboxProcessor<ClientEvent>, projectFiles: Map<
Thread.Sleep(25)

member __.Shutdown () =
let _ = client.PostAndReply<JObject>(fun rc -> SendServerRpcRequest ("shutdown", JObject(), Some rc))
let _ = client.PostAndReply<JObject option>(fun rc -> SendServerRpcRequest ("shutdown", JObject(), Some rc))
client.Post(SendServerRpcRequest ("exit", JObject(), None))

member __.Stop () =
Expand Down Expand Up @@ -607,3 +608,11 @@ let setupServerClient (clientProfile: ClientProfile) (projectFiles: Map<string,
let clientActor = MailboxProcessor.Start(clientEventLoop initialState)
clientActor.Post(SetupWithProfile clientProfile)
new ClientController (clientActor, projectFiles)

let dotnet8PExeProjectCsproj = """<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
"""