Skip to content

Commit

Permalink
test textDocument/declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
mnxn authored and rgrinberg committed Nov 8, 2020
1 parent 6012bc4 commit d73095c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ocaml-lsp-server/test/e2e/__tests__/declaration_files/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(library
(name declaration_files))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 2.5)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val x : int
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let y = Lib.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { promises as fs } from "fs";
import * as path from "path";
import * as child_process from "child_process";
import * as LanguageServer from "./../src/LanguageServer";
import * as Types from "vscode-languageserver-types";
import { testUri, toEqualUri } from "./../src/LanguageServer";
expect.extend({
toEqualUri,
});
declare global {
namespace jest {
interface Matchers<R> {
toEqualUri(uri: string): R;
}
}
}

describe("textDocument/declaration", () => {
let languageServer = null;

let testWorkspacePath = path.join(__dirname, "declaration_files/");

let createPathForFile = (filename: string) =>
path.join(testWorkspacePath, filename);

beforeEach(async () => {
languageServer = await LanguageServer.startAndInitialize();
});

afterEach(async () => {
await LanguageServer.exit(languageServer);
languageServer = null;
});

async function openDocument(filepath) {
let source = await fs.readFile(filepath);
await languageServer.sendNotification("textDocument/didOpen", {
textDocument: Types.TextDocumentItem.create(
testUri(filepath),
"ocaml",
0,
source.toString(),
),
});
}

async function queryDeclaration(filepath, position) {
return await languageServer.sendRequest("textDocument/declaration", {
textDocument: Types.TextDocumentIdentifier.create(testUri(filepath)),
position,
});
}

it("returns location of a declaration", async () => {
child_process.execSync("dune build", { cwd: testWorkspacePath });

await openDocument(createPathForFile("main.ml"));

let result = await queryDeclaration(
createPathForFile("main.ml"),
Types.Position.create(0, 13),
);

expect(result.length).toBe(1);
expect(result[0].range).toMatchObject({
end: { character: 0, line: 0 },
start: { character: 0, line: 0 },
});
expect(result[0].uri).toEqualUri(testUri(createPathForFile("lib.ml")));
});
});

0 comments on commit d73095c

Please sign in to comment.