Skip to content

Commit

Permalink
Test "textDocument/documentLink" request
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed Mar 10, 2020
1 parent 8ba5283 commit eeab661
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 27 deletions.
21 changes: 0 additions & 21 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,27 +295,6 @@ impl<C: LspClient + Send + Sync + 'static> LatexLspServer<C> {
Err(msg)
}
}
/*
let workspace = self.workspace_manager.get();
let client_capabilities = self
.client_capabilities
.get()
.expect("Failed to retrieve client capabilities");
if let Some(document) = workspace.find(&uri) {
let options = self.configuration(true).await;
Ok(FeatureRequest {
params,
view: DocumentView::new(workspace, document, &options),
client_capabilities: Arc::clone(&client_capabilities),
distribution: Arc::clone(&self.distribution),
options,
})
} else {
let msg = format!("Unknown document: {}", uri);
Err(msg)
}
*/
}

async fn pull_configuration(&self) -> Options {
Expand Down
35 changes: 29 additions & 6 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,8 @@ impl TestBedBuilder {
Self::default()
}

pub fn file<P, S>(&mut self, path: P, text: S) -> &mut Self
where
P: Into<PathBuf>,
S: Into<String>,
{
self.files.push((path.into(), text.into()));
pub fn file<P: Into<PathBuf>>(&mut self, path: P, text: &str) -> &mut Self {
self.files.push((path.into(), text.trim().into()));
self
}

Expand Down Expand Up @@ -117,6 +113,9 @@ impl TestBedBuilder {
let dir = tempdir().expect("failed to create temporary directory");
for (path, text) in &self.files {
let full_path = dir.path().join(path);
fs::create_dir_all(full_path.parent().unwrap())
.await
.unwrap();
fs::write(&full_path, text).await.unwrap();
}

Expand Down Expand Up @@ -260,6 +259,10 @@ impl TestBed {
Uri::from_file_path(self.path(relative_path)).unwrap()
}

pub fn identifier(&self, relative_path: &str) -> TextDocumentIdentifier {
TextDocumentIdentifier::new(self.uri(relative_path).into())
}

pub async fn open(&self, relative_path: &str) {
let full_path = self.path(relative_path);
let params = DidOpenTextDocumentParams {
Expand All @@ -277,6 +280,26 @@ impl TestBed {
self.client.did_open(params).await;
}

pub async fn edit<S: Into<String>>(&self, relative_path: &str, text: S) {
let uri = self.uri(relative_path).into();
let params = DidChangeTextDocumentParams {
text_document: VersionedTextDocumentIdentifier::new(uri, 0),
content_changes: vec![TextDocumentContentChangeEvent {
range: None,
range_length: None,
text: text.into(),
}],
};
self.client.did_change(params).await;
}

pub async fn document_link(&self, relative_path: &str) -> Option<Vec<DocumentLink>> {
let params = DocumentLinkParams {
text_document: self.identifier(relative_path),
};
self.client.document_link(params).await.ok()
}

pub async fn shutdown(&self) {
self.client.shutdown(()).await.unwrap();
self.client.exit(()).await;
Expand Down
124 changes: 124 additions & 0 deletions tests/link.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use indoc::indoc;
use texlab::{
protocol::*,
test::{TestBedBuilder, FULL_CAPABILITIES},
};

#[tokio::test]
async fn empty_document() {
let mut test_bed = TestBedBuilder::new().file("main.tex", "").build().await;
test_bed.spawn();
test_bed.initialize(FULL_CAPABILITIES.clone()).await;
test_bed.open("main.tex").await;

let links = test_bed.document_link("main.tex").await.unwrap();

test_bed.shutdown().await;

assert_eq!(links, Vec::new());
}

#[tokio::test]
async fn default_settings() {
let mut test_bed = TestBedBuilder::new()
.file(
"main.tex",
indoc!(
r#"
\include{foo/bar}
\input{qux.tex}
"#
),
)
.file("foo/bar.tex", "")
.file("qux.tex", "")
.build()
.await;

test_bed.spawn();
test_bed.initialize(FULL_CAPABILITIES.clone()).await;
test_bed.open("main.tex").await;

let links = test_bed.document_link("main.tex").await.unwrap();

test_bed.shutdown().await;

assert_eq!(
links,
vec![
DocumentLink {
range: Range::new_simple(0, 9, 0, 16),
target: test_bed.uri("foo/bar.tex").into()
},
DocumentLink {
range: Range::new_simple(1, 7, 1, 14),
target: test_bed.uri("qux.tex").into()
}
]
);
}

#[tokio::test]
async fn root_directory() {
let mut test_bed = TestBedBuilder::new()
.file("src/main.tex", r#"\include{src/foo}"#)
.file("src/foo.tex", "")
.root_dir(".")
.build()
.await;

test_bed.spawn();
test_bed.initialize(FULL_CAPABILITIES.clone()).await;
test_bed.open("src/main.tex").await;

let links = test_bed.document_link("src/main.tex").await.unwrap();

test_bed.shutdown().await;

assert_eq!(
links,
vec![DocumentLink {
range: Range::new_simple(0, 9, 0, 16),
target: test_bed.uri("src/foo.tex").into()
}]
);
}

#[tokio::test]
async fn parent_directory() {
let mut test_bed = TestBedBuilder::new()
.file("src/main.tex", r#"\addbibresource{../foo.bib}"#)
.file("foo.bib", "")
.build()
.await;

test_bed.spawn();
test_bed.initialize(FULL_CAPABILITIES.clone()).await;
test_bed.open("src/main.tex").await;

let links = test_bed.document_link("src/main.tex").await.unwrap();

test_bed.shutdown().await;

assert_eq!(
links,
vec![DocumentLink {
range: Range::new_simple(0, 16, 0, 26),
target: test_bed.uri("foo.bib").into()
}]
);
}

#[tokio::test]
async fn unknown_file() {
let mut test_bed = TestBedBuilder::new().build().await;

test_bed.spawn();
test_bed.initialize(FULL_CAPABILITIES.clone()).await;

let links = test_bed.document_link("main.tex").await;

test_bed.shutdown().await;

assert_eq!(links, None);
}

0 comments on commit eeab661

Please sign in to comment.