Skip to content

Commit

Permalink
Add tests for textDocument/definition (#201)
Browse files Browse the repository at this point in the history
* Add tests for textDocument/definition

* .github/workflows: bump sdk to 8.0.404

* tests/CSharpLanguageServer.Tests/Tooling.fs: don't delete temporary project dir when under GitHub Actions C/I

* tests/CSharpLanguageServer.Tests/Tooling.fs: bump timeout to 60 secs in WaitForProgressEnd
  • Loading branch information
razzmatazz authored Nov 22, 2024
1 parent aab7156 commit bd0f4bf
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-24.04]
dotnet: [8.0.403]
dotnet: [8.0.404]
runs-on: ${{ matrix.os }}

steps:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ name: Tests

on:
push:
branches: [master]
branches: [main]
pull_request:

jobs:
build:
strategy:
matrix:
os: [windows-latest, ubuntu-24.04]
dotnet: [8.0.403]
dotnet: [8.0.404]
fail-fast: false

runs-on: ${{ matrix.os }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Compile Include="HoverTests.fs" />
<Compile Include="InitializationTests.fs" />
<Compile Include="ReferenceTests.fs" />
<Compile Include="DefinitionTests.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
76 changes: 76 additions & 0 deletions tests/CSharpLanguageServer.Tests/DefinitionTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module CSharpLanguageServer.Tests.DefinitionTests

open NUnit.Framework
open Ionide.LanguageServerProtocol.Types

open CSharpLanguageServer.Tests.Tooling
open System

[<TestCase>]
let testDefinitionWorks () =
use client = setupServerClient defaultClientProfile "TestData/testDefinitionWorks"
client.StartAndWaitForSolutionLoad()

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

let definitionParams0: DefinitionParams =
{ TextDocument = { Uri = classFile.Uri }
Position = { Line = 0u; Character = 0u }
WorkDoneToken = None
PartialResultToken = None
}

let declaration0: Declaration option = classFile.Request("textDocument/definition", definitionParams0)
Assert.IsTrue(declaration0.IsNone)

let definitionParams1: DefinitionParams =
{ TextDocument = { Uri = classFile.Uri }
Position = { Line = 2u; Character = 16u }
WorkDoneToken = None
PartialResultToken = None
}

let declaration1: Declaration option = classFile.Request("textDocument/definition", definitionParams1)

match declaration1.Value with
| U2.C1 _ -> failwith "Location[] was expected"
| U2.C2 declaration1Locations ->
let expectedLocations1: Location array =
[|
{ Uri = classFile.Uri
Range = { Start = { Line = 2u; Character = 16u }
End = { Line = 2u; Character = 23u } }
}
|]

Assert.AreEqual(expectedLocations1, declaration1Locations)

[<TestCase>]
let testDefinitionWorksInAspNetProject () =
use client = setupServerClient defaultClientProfile
"TestData/testDefinitionWorksInAspNetProject"
client.StartAndWaitForSolutionLoad()

use indexCsHtmlCs = client.Open("Project/Pages/Index.cshtml.cs")

let definitionParams1: DefinitionParams =
{ TextDocument = { Uri = indexCsHtmlCs.Uri }
Position = { Line = 7u; Character = 8u }
WorkDoneToken = None
PartialResultToken = None
}

let declaration1: Declaration option = indexCsHtmlCs.Request("textDocument/definition", definitionParams1)

match declaration1.Value with
| U2.C1 _ -> failwith "Location[] was expected"
| U2.C2 declaration1Locations ->
let expectedLocations1: Location array =
[|
{ Uri = indexCsHtmlCs.Uri
Range = { Start = { Line = 4u; Character = 19u }
End = { Line = 4u; Character = 24u } }
}
|]

Assert.AreEqual(expectedLocations1, declaration1Locations)

Check warning on line 76 in tests/CSharpLanguageServer.Tests/DefinitionTests.fs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0.404)

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

Check warning on line 76 in tests/CSharpLanguageServer.Tests/DefinitionTests.fs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0.404)

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

Check warning on line 76 in tests/CSharpLanguageServer.Tests/DefinitionTests.fs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 8.0.404)

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

Check warning on line 76 in tests/CSharpLanguageServer.Tests/DefinitionTests.fs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 8.0.404)

Main module of program is empty: nothing will happen when it is run
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Class
{
public void MethodA(string arg)
{
}

public void MethodB(string arg)
{
MethodA(arg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@page
@model IndexModel
<div>@Model.Value</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace test_csharp_web.Pages;
public class IndexModel : PageModel
{
public string? Value { get; set; }
public void OnGet()
{
Value = "test";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@using test_csharp_web
@namespace test_csharp_web.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();

var app = builder.Build();
app.UseRouting();
app.MapRazorPages();
app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>test_csharp_web</RootNamespace>
</PropertyGroup>
</Project>
15 changes: 12 additions & 3 deletions tests/CSharpLanguageServer.Tests/Tooling.fs
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,17 @@ type ClientController (client: MailboxProcessor<ClientEvent>, testDataDir: Direc
client.PostAndReply(fun rc -> ServerStopRequest rc)
logMessage "Dispose" "OK, ServerStopRequest has finished"

match projectDir with
| Some projectDir ->
// We don't care about cleanup when running under GitHub Actions C/I,
// also Windows version of C/I would fail from time to time due to
// another process locking the files on the temporary dir for some reason.
let runningInGithubActions =
System.Environment.GetEnvironmentVariable("GITHUB_ACTIONS")
|> Option.ofObj
|> Option.map (fun v -> v = "true")
|> Option.defaultValue false

match projectDir, runningInGithubActions with
| Some projectDir, false ->
logMessage "Dispose" (sprintf "Removing files on project dir \"%s\".." projectDir)
deleteDirectory projectDir
| _ -> ()
Expand Down Expand Up @@ -566,7 +575,7 @@ type ClientController (client: MailboxProcessor<ClientEvent>, testDataDir: Direc
this.WaitForProgressEnd("OK, 1 project file(s) loaded")

member __.WaitForProgressEnd(message: string) =
let timeoutMS = 10 * 1000
let timeoutMS = 60 * 1000
let start = DateTime.Now

let progressEndMatch m =
Expand Down

0 comments on commit bd0f4bf

Please sign in to comment.