Skip to content

Commit

Permalink
Test LaTeX linting using ChkTeX
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed Dec 4, 2019
1 parent 701cf1a commit ab4a046
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/scenarios/diagnostics/latex/disabled.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\foo
1 change: 1 addition & 0 deletions tests/scenarios/diagnostics/latex/on_change.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\foo{}
1 change: 1 addition & 0 deletions tests/scenarios/diagnostics/latex/on_open.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\foo
1 change: 1 addition & 0 deletions tests/scenarios/diagnostics/latex/on_save.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\foo{}
15 changes: 14 additions & 1 deletion tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ impl MockLspClient {
pub fn new() -> Self {
Self::default()
}

pub async fn verify_no_diagnostics(&self, uri: &Uri) {
let diagnostics_by_uri = self.diagnostics_by_uri.lock().await;
assert_eq!(
diagnostics_by_uri
.get(uri.into())
.map(Vec::len)
.unwrap_or(0),
0
);
}
}

impl LspClient for MockLspClient {
Expand Down Expand Up @@ -147,7 +158,9 @@ impl Scenario {
pub async fn read(&self, name: &'static str) -> String {
let mut path = self.directory.path().to_owned();
path.push(name);
let data = tokio::fs::read(path).await.expect("failed to read scenario file");
let data = tokio::fs::read(path)
.await
.expect("failed to read scenario file");
let text = String::from_utf8_lossy(&data);
text.replace('\r', "")
}
Expand Down
147 changes: 147 additions & 0 deletions tests/test_diagnostics_latex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
pub mod support;

use lsp_types::*;
use std::sync::Arc;
use support::*;
use tex::DistributionKind::{Miktex, Texlive};
use texlab::diagnostics::LatexLintOptions;

#[tokio::test]
async fn disabled() {
let _ = tex::with_distro(&[Texlive, Miktex], |distro| {
async move {
let scenario = Scenario::new("diagnostics/latex", Arc::new(distro));
scenario.client.options.lock().await.latex_lint = Some(LatexLintOptions {
on_change: Some(false),
on_save: Some(false),
});

scenario
.initialize(&capabilities::CLIENT_FULL_CAPABILITIES)
.await;
scenario.open("disabled.tex").await;
scenario
.client
.verify_no_diagnostics(&scenario.uri("disabled.tex"))
.await;
}
})
.await;
}

#[tokio::test]
async fn on_open() {
let _ = tex::with_distro(&[Texlive, Miktex], |distro| {
async move {
let scenario = Scenario::new("diagnostics/latex", Arc::new(distro));
scenario.client.options.lock().await.latex_lint = Some(LatexLintOptions {
on_change: Some(false),
on_save: Some(true),
});

scenario
.initialize(&capabilities::CLIENT_FULL_CAPABILITIES)
.await;
scenario.open("on_open.tex").await;
{
let diagnostics_by_uri = scenario.client.diagnostics_by_uri.lock().await;
let diagnostics = &diagnostics_by_uri[&scenario.uri("on_open.tex")];
assert_eq!(diagnostics.len(), 1);
assert_eq!(diagnostics[0].message, "Command terminated with space.");
}
}
})
.await;
}

#[tokio::test]
async fn on_save() {
let _ = tex::with_distro(&[Texlive, Miktex], |distro| {
async move {
let scenario = Scenario::new("diagnostics/latex", Arc::new(distro));
scenario.client.options.lock().await.latex_lint = Some(LatexLintOptions {
on_change: Some(false),
on_save: Some(true),
});

scenario
.initialize(&capabilities::CLIENT_FULL_CAPABILITIES)
.await;
scenario.open("on_save.tex").await;
let uri = scenario.uri("on_save.tex");
scenario.client.verify_no_diagnostics(&uri).await;

let text_document = VersionedTextDocumentIdentifier::new(uri.clone().into(), 0);
let content_change = TextDocumentContentChangeEvent {
range: None,
range_length: None,
text: "\\foo\n".into(),
};
let did_change_params = DidChangeTextDocumentParams {
text_document,
content_changes: vec![content_change],
};
scenario
.server
.execute(|svr| svr.did_change(did_change_params))
.await;
scenario.client.verify_no_diagnostics(&uri).await;

let text_document = TextDocumentIdentifier::new(uri.clone().into());
let did_save_params = DidSaveTextDocumentParams { text_document };
scenario
.server
.execute(|svr| svr.did_save(did_save_params))
.await;
{
let diagnostics_by_uri = scenario.client.diagnostics_by_uri.lock().await;
let diagnostics = &diagnostics_by_uri[&uri];
assert_eq!(diagnostics.len(), 1);
assert_eq!(diagnostics[0].message, "Command terminated with space.");
}
}
})
.await;
}

#[tokio::test]
async fn on_change() {
let _ = tex::with_distro(&[Texlive, Miktex], |distro| {
async move {
let scenario = Scenario::new("diagnostics/latex", Arc::new(distro));
scenario.client.options.lock().await.latex_lint = Some(LatexLintOptions {
on_change: Some(true),
on_save: Some(true),
});

scenario
.initialize(&capabilities::CLIENT_FULL_CAPABILITIES)
.await;
scenario.open("on_change.tex").await;
let uri = scenario.uri("on_change.tex");
scenario.client.verify_no_diagnostics(&uri).await;

let text_document = VersionedTextDocumentIdentifier::new(uri.clone().into(), 0);
let content_change = TextDocumentContentChangeEvent {
range: None,
range_length: None,
text: "\\foo\n".into(),
};
let did_change_params = DidChangeTextDocumentParams {
text_document,
content_changes: vec![content_change],
};
scenario
.server
.execute(|svr| svr.did_change(did_change_params))
.await;
{
let diagnostics_by_uri = scenario.client.diagnostics_by_uri.lock().await;
let diagnostics = &diagnostics_by_uri[&uri];
assert_eq!(diagnostics.len(), 1);
assert_eq!(diagnostics[0].message, "Command terminated with space.");
}
}
})
.await;
}

0 comments on commit ab4a046

Please sign in to comment.