Skip to content

Commit

Permalink
Add integration tests for completion request
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed May 25, 2019
1 parent 774ebc6 commit 55fb8ba
Show file tree
Hide file tree
Showing 38 changed files with 298 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
**/*.rs.bk
/.idea
tarpaulin-report.html
3 changes: 3 additions & 0 deletions scenarios/completion/citation/bar.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@article{foo,}

@article{bar,}
3 changes: 3 additions & 0 deletions scenarios/completion/citation/baz/qux.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@article{baz,}

@comment{qux,}
4 changes: 4 additions & 0 deletions scenarios/completion/citation/foo.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\addbibresource{bar.bib}
\addbibresource{baz/qux.bib}

\cite{}
2 changes: 2 additions & 0 deletions scenarios/completion/color/foo.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
\color{bla}
\definecolor{foo}{}
4 changes: 4 additions & 0 deletions scenarios/completion/entry_type/foo.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@artic
@preamble
@string
@comment
3 changes: 3 additions & 0 deletions scenarios/completion/field_name/foo.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@article{foo,
aut
}
Empty file.
3 changes: 3 additions & 0 deletions scenarios/completion/include/bar/baz.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\bibliography{./}
\includegraphics{}
\includesvg{}
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions scenarios/completion/include/foo.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
\include{}
\input{bar/}
Empty file.
3 changes: 3 additions & 0 deletions scenarios/completion/kernel/foo.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@article{foo,
title = {\LaT}
}
7 changes: 7 additions & 0 deletions scenarios/completion/kernel/foo.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
\documentclass{article}

\usep

\begin{doc}

\end{doc}
File renamed without changes.
1 change: 1 addition & 0 deletions scenarios/completion/label/baz.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\label{baz}
6 changes: 6 additions & 0 deletions scenarios/completion/label/foo.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
\include{bar}
\input{baz.tex}

\label{foo}

\ref{}
1 change: 1 addition & 0 deletions scenarios/completion/pgf_library/foo.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\usepgflibrary{arr}
2 changes: 2 additions & 0 deletions scenarios/completion/symbol/foo.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
\vareps
\mathbb{}
1 change: 1 addition & 0 deletions scenarios/completion/tikz_library/foo.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\usetikzlibrary{arr}
9 changes: 9 additions & 0 deletions scenarios/completion/user/foo.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\foo

\fo

\begin{foo}

\end{foo}

\begin{fo}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions scenarios/definition/label/baz.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\label{bar}
File renamed without changes.
6 changes: 4 additions & 2 deletions src/completion/latex/citation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ impl LatexCitationCompletionProvider {
for document in &request.related_documents {
if let SyntaxTree::Bibtex(tree) = &document.tree {
for entry in &tree.entries() {
if let Some(key) = &entry.key {
items.push(factory::create_citation(entry, key.text()));
if !entry.is_comment() {
if let Some(key) = &entry.key {
items.push(factory::create_citation(entry, key.text()));
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/completion/latex/pgf_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl LatexPgfLibraryCompletionProvider {

const COMMANDS: &[&str] = &["\\usepgflibrary"];

const LIBRARIES: &[&str] = &[
static LIBRARIES: &[&str] = &[
"arrows",
"arrows.meta",
"arrows.spaced",
Expand Down
2 changes: 1 addition & 1 deletion src/completion/latex/tikz_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl LatexTikzCommandCompletionProvider {
}
}

const COMMANDS: &[&str] = &[
static COMMANDS: &[&str] = &[
"afterdecoration",
"anchor",
"anchorborder",
Expand Down
2 changes: 1 addition & 1 deletion src/completion/latex/tikz_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl LatexTikzLibraryCompletionProvider {

const COMMANDS: &[&str] = &["\\usetikzlibrary"];

const LIBRARIES: &[&str] = &[
static LIBRARIES: &[&str] = &[
"3d",
"angles",
"arrows",
Expand Down
4 changes: 4 additions & 0 deletions src/data/bibtex_entry_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"name": "string",
"documentation": null
},
{
"name": "comment",
"documentation": null
},
{
"name": "article",
"documentation": "An article in a journal, magazine, newspaper, or other periodical which forms a \n self-contained unit with its own title. The title of the periodical is given in the \n journaltitle field. If the issue has its own title in addition to the main title of \n the periodical, it goes in the issuetitle field. Note that editor and related \n fields refer to the journal while translator and related fields refer to the article.\n\nRequired fields: `author`, `title`, `journaltitle`, `year/date`"
Expand Down
13 changes: 10 additions & 3 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use futures::prelude::*;
use jsonrpc::client::FutureResult;
use jsonrpc::server::EventHandler;
use lsp_types::*;
use std::fs::remove_dir;
use std::path::PathBuf;
use std::sync::Arc;
use tempfile::TempDir;
use texlab::client::LspClient;
use texlab::server::LatexLspServer;
use std::fs::remove_dir;

#[derive(Debug, Default)]
pub struct LspClientMock {
Expand Down Expand Up @@ -80,15 +80,22 @@ impl Scenario {
Uri::from_file_path(path).unwrap()
}

pub async fn open(&self, name: &'static str, language_id: &'static str) {
pub async fn open(&self, name: &'static str) {
let mut path = self.directory.path().to_owned();
path.push(name);
let text = std::fs::read_to_string(path).unwrap();
let language_id = if name.ends_with(".tex") {
"latex"
} else {
"bibtex"
};

self.server.did_open(DidOpenTextDocumentParams {
text_document: TextDocumentItem {
uri: self.uri(name),
version: 0,
language_id: language_id.to_owned(),
text: std::fs::read_to_string(path).unwrap(),
text,
},
})
}
Expand Down
205 changes: 205 additions & 0 deletions tests/completion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
#![feature(await_macro, async_await)]

mod common;

use crate::common::Scenario;
use futures::executor::block_on;
use itertools::Itertools;
use lsp_types::*;

async fn run(scenario: &'static str, file: &'static str, position: Position) -> Vec<String> {
let scenario = format!("completion/{}", scenario);
let scenario = await!(Scenario::new(&scenario));
await!(scenario.open(file));

let params = CompletionParams {
text_document: TextDocumentIdentifier::new(scenario.uri(file)),
position,
context: None,
};
await!(scenario.server.completion(params))
.unwrap()
.items
.into_iter()
.map(|item| item.label.into_owned())
.sorted()
.collect()
}

#[test]
fn test_kernel_command() {
block_on(async move {
let items = await!(run("kernel", "foo.tex", Position::new(2, 5)));
assert_eq!(items.iter().any(|item| item == "usepackage"), true);
});
}

#[test]
fn test_kernel_command_bibtex() {
block_on(async move {
let items = await!(run("kernel", "foo.bib", Position::new(1, 17)));
assert_eq!(items.iter().any(|item| item == "LaTeX"), true);
});
}

#[test]
fn test_kernel_environment() {
block_on(async move {
let items = await!(run("kernel", "foo.tex", Position::new(4, 10)));
assert_eq!(items.iter().any(|item| item == "document"), true);
});
}

#[test]
fn test_user_command() {
block_on(async move {
let items = await!(run("user", "foo.tex", Position::new(2, 3)));
assert_eq!(items.iter().all(|item| item != "fo"), true);
assert_eq!(items.iter().any(|item| item == "foo"), true);
});
}

#[test]
fn test_label() {
block_on(async move {
let items = await!(run("label", "foo.tex", Position::new(5, 5)));
assert_eq!(items, vec!["bar", "baz", "foo"]);
});
}

#[test]
fn test_citation() {
block_on(async move {
let items = await!(run("citation", "foo.tex", Position::new(3, 6)));
assert_eq!(items, vec!["bar", "baz", "foo"]);
});
}

#[test]
fn test_symbol_command_kernel() {
block_on(async move {
let items = await!(run("symbol", "foo.tex", Position::new(0, 1)));
assert_eq!(items.iter().any(|item| item == "varepsilon"), true);
});
}

#[test]
fn test_symbol_argument() {
block_on(async move {
let items = await!(run("symbol", "foo.tex", Position::new(1, 8)));
assert_eq!(items.len(), 26);
assert_eq!(items[0], "A");
});
}

#[test]
fn test_color() {
block_on(async move {
let items = await!(run("color", "foo.tex", Position::new(0, 10)));
assert_eq!(items.iter().any(|item| item == "black"), true);
});
}

#[test]
fn test_color_model() {
block_on(async move {
let items = await!(run("color", "foo.tex", Position::new(1, 18)));
assert_eq!(items.iter().any(|item| item == "rgb"), true);
});
}

#[test]
fn test_include_top_level() {
block_on(async move {
let items = await!(run("include", "foo.tex", Position::new(0, 9)));
assert_eq!(items, vec!["bar", "foo", "qux"]);
});
}

#[test]
fn test_include_directory() {
block_on(async move {
let items = await!(run("include", "foo.tex", Position::new(1, 11)));
assert_eq!(items, vec!["bar.tex", "baz.tex"]);
});
}

#[test]
fn test_include_bibliography() {
block_on(async move {
let items = await!(run("include", "bar/baz.tex", Position::new(0, 16)));
assert_eq!(items, vec!["foo.bib"]);
});
}

#[test]
fn test_include_graphics() {
block_on(async move {
let items = await!(run("include", "bar/baz.tex", Position::new(1, 17)));
assert_eq!(items, vec!["image1.png", "image2.jpg"]);
});
}

#[test]
fn test_include_graphics_svg() {
block_on(async move {
let items = await!(run("include", "bar/baz.tex", Position::new(2, 12)));
assert_eq!(items, vec!["image3"]);
});
}

#[test]
fn test_pgf_library() {
block_on(async move {
let items = await!(run("pgf_library", "foo.tex", Position::new(0, 18)));
assert_eq!(items.iter().any(|item| item == "arrows"), true);
});
}

#[test]
fn test_tikz_library() {
block_on(async move {
let items = await!(run("tikz_library", "foo.tex", Position::new(0, 19)));
assert_eq!(items.iter().any(|item| item == "arrows"), true);
});
}

#[test]
fn test_entry_type() {
block_on(async move {
let items = await!(run("entry_type", "foo.bib", Position::new(0, 1)));
assert_eq!(items.iter().any(|item| item == "article"), true);
});
}

#[test]
fn test_entry_type_preamble() {
block_on(async move {
let items = await!(run("entry_type", "foo.bib", Position::new(1, 3)));
assert_eq!(items.iter().any(|item| item == "preamble"), true);
});
}

#[test]
fn test_entry_type_string() {
block_on(async move {
let items = await!(run("entry_type", "foo.bib", Position::new(2, 3)));
assert_eq!(items.iter().any(|item| item == "string"), true);
});
}

#[test]
fn test_entry_type_comment() {
block_on(async move {
let items = await!(run("entry_type", "foo.bib", Position::new(3, 3)));
assert_eq!(items.iter().any(|item| item == "comment"), true);
});
}

#[test]
fn test_field_name() {
block_on(async move {
let items = await!(run("field_name", "foo.bib", Position::new(1, 7)));
assert_eq!(items.iter().any(|item| item == "author"), true);
});
}
Loading

0 comments on commit 55fb8ba

Please sign in to comment.