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

Tests for pull diagnostics #180

Merged
merged 2 commits into from
Jul 30, 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
6 changes: 3 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ matrix.dotnet }}

Expand All @@ -33,7 +33,7 @@ jobs:
working-directory: src/CSharpLanguageServer

- name: Upload NuGet packages as artifacts
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: packages
path: src/CSharpLanguageServer/release/
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ matrix.dotnet }}

Expand Down
109 changes: 102 additions & 7 deletions tests/CSharpLanguageServer.Tests/DiagnosticTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,122 @@

client.StartAndWaitForSolutionLoad()

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

Thread.Sleep(5000)
Thread.Sleep(4000)

let state = client.GetState()
let version, diagnosticList = state.PushDiagnostics |> Map.find classFile.Uri
let version0, diagnosticList0 = state.PushDiagnostics |> Map.find classFile.Uri

Assert.AreEqual(None, version)
Assert.AreEqual(None, version0)

Assert.AreEqual(3, diagnosticList.Length)
Assert.AreEqual(3, diagnosticList0.Length)

let diagnostic0 = diagnosticList.[0]
let diagnostic0 = diagnosticList0.[0]
Assert.AreEqual("Identifier expected", diagnostic0.Message)
Assert.AreEqual(Some DiagnosticSeverity.Error, diagnostic0.Severity)
Assert.AreEqual(0, diagnostic0.Range.Start.Line)
Assert.AreEqual(3, diagnostic0.Range.Start.Character)

let diagnostic1 = diagnosticList.[1]
let diagnostic1 = diagnosticList0.[1]
Assert.AreEqual("; expected", diagnostic1.Message)

let diagnostic2 = diagnosticList.[2]
let diagnostic2 = diagnosticList0.[2]
Assert.AreEqual(
"The type or namespace name 'XXX' could not be found (are you missing a using directive or an assembly reference?)",
diagnostic2.Message)

//
// now change the file to contain no content (and thus no diagnostics)
//
classFile.DidChange("")

Thread.Sleep(4000)

let state = client.GetState()
let version1, diagnosticList1 = state.PushDiagnostics |> Map.find classFile.Uri

Assert.AreEqual(None, version1)

Assert.AreEqual(0, diagnosticList1.Length)
()


[<TestCase>]
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"""
)
]

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

client.StartAndWaitForSolutionLoad()

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

let diagnosticParams: DocumentDiagnosticParams =
{ WorkDoneToken = None
PartialResultToken = None
TextDocument = { Uri = classFile.Uri }
Identifier = None
PreviousResultId = None }

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

match report0 with
| U2.C2 _ -> failwith "U2.C1 is expected"
| U2.C1 report ->
Assert.AreEqual("full", report.Kind)
Assert.AreEqual(None, report.ResultId)
Assert.AreEqual(3, report.Items.Length)

let diagnostic0 = report.Items.[0]
Assert.AreEqual(0, diagnostic0.Range.Start.Line)
Assert.AreEqual(3, diagnostic0.Range.Start.Character)
Assert.AreEqual(Some DiagnosticSeverity.Error, diagnostic0.Severity)
Assert.AreEqual("Identifier expected", diagnostic0.Message)
Assert.AreEqual(
"https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1001",
diagnostic0.CodeDescription.Value.Href)

let diagnostic1 = report.Items.[1]
Assert.AreEqual("; expected", diagnostic1.Message)

let diagnostic2 = report.Items.[2]
Assert.AreEqual(
"The type or namespace name 'XXX' could not be found (are you missing a using directive or an assembly reference?)",
diagnostic2.Message)

//
// 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)

match report1 with
| U2.C2 _ -> failwith "U2.C1 is expected"
| U2.C1 report ->
Assert.AreEqual("full", report.Kind)
Assert.AreEqual(None, report.ResultId)
Assert.AreEqual(0, report.Items.Length)
()

Check warning on line 151 in tests/CSharpLanguageServer.Tests/DiagnosticTests.fs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0.300)

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

Check warning on line 151 in tests/CSharpLanguageServer.Tests/DiagnosticTests.fs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0.300)

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

Check warning on line 151 in tests/CSharpLanguageServer.Tests/DiagnosticTests.fs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 8.0.300)

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

Check warning on line 151 in tests/CSharpLanguageServer.Tests/DiagnosticTests.fs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 8.0.300)

Main module of program is empty: nothing will happen when it is run
27 changes: 25 additions & 2 deletions tests/CSharpLanguageServer.Tests/Tooling.fs
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,37 @@ let rec deleteDirectory (path: string) =
Directory.Delete(path)


type FileController (_client: MailboxProcessor<ClientEvent>, filename: string, uri: string) =
type FileController (client: MailboxProcessor<ClientEvent>, filename: string, uri: string, fileText: string) =
member __.Filename = filename
member __.Uri = uri
member __.FileText = fileText

interface IDisposable with
member __.Dispose() =
let didCloseParams: DidCloseTextDocumentParams =
{ TextDocument = { Uri = uri } }

client.Post(SendServerRpcNotification ("textDocument/didClose", serialize didCloseParams))
()

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

member __.DidChange(text: string) =
let didChangeParams: DidChangeTextDocumentParams =
{
TextDocument = { Uri = uri; Version = 2 }
ContentChanges =
[|
{ Text = text } |> U2.C2
|]
}

client.Post(SendServerRpcNotification ("textDocument/didChange", serialize didChangeParams))
()


type ClientController (client: MailboxProcessor<ClientEvent>, projectFiles: Map<string, string>) =
let mutable projectDir: string option = None
Expand Down Expand Up @@ -576,7 +599,7 @@ type ClientController (client: MailboxProcessor<ClientEvent>, projectFiles: Map<

client.Post(SendServerRpcNotification ("textDocument/didOpen", serialize didOpenParams))

new FileController(client, filename, uri)
new FileController(client, filename, uri, fileText)


let setupServerClient (clientProfile: ClientProfile) (projectFiles: Map<string, string>) =
Expand Down