Skip to content

Commit

Permalink
Refactor reference tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed May 3, 2019
1 parent 730b8b2 commit 6a4da67
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 40 deletions.
20 changes: 20 additions & 0 deletions src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ impl Into<FeatureRequest<DocumentLinkParams>> for FeatureSpec {
}
}

#[cfg(test)]
impl Into<FeatureRequest<ReferenceParams>> for FeatureSpec {
fn into(self) -> FeatureRequest<ReferenceParams> {
let params = ReferenceParams {
text_document: self.identifier(),
position: self.position,
context: ReferenceContext {
include_declaration: false,
},
};
let (workspace, document) = self.workspace();
FeatureRequest::new(
params,
workspace,
document,
Arc::new(self.component_database),
)
}
}

#[cfg(test)]
#[macro_export]
macro_rules! test_feature {
Expand Down
57 changes: 37 additions & 20 deletions src/reference/bibtex_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,49 @@ impl BibtexEntryReferenceProvider {
#[cfg(test)]
mod tests {
use super::*;
use crate::feature::FeatureTester;
use crate::workspace::WorkspaceBuilder;
use futures::executor::block_on;
use crate::completion::latex::data::types::LatexComponentDatabase;
use crate::feature::FeatureSpec;
use crate::range;
use crate::test_feature;
use lsp_types::Position;

#[test]
fn test() {
let mut builder = WorkspaceBuilder::new();
let uri1 = builder.document("foo.bib", "@article{foo, bar = {baz}}");
let uri2 = builder.document("bar.tex", "\\addbibresource{foo.bib}\n\\cite{foo}");
builder.document("baz.tex", "\\cite{foo}");
let request = FeatureTester::new(builder.workspace, uri1, 0, 9, "").into();

let results = block_on(BibtexEntryReferenceProvider::execute(&request));

let location = Location::new(uri2, range::create(1, 0, 1, 10));
assert_eq!(vec![location], results);
let references = test_feature!(
BibtexEntryReferenceProvider,
FeatureSpec {
files: vec![
FeatureSpec::file("foo.bib", "@article{foo, bar = {baz}}"),
FeatureSpec::file("bar.tex", "\\addbibresource{foo.bib}\n\\cite{foo}"),
FeatureSpec::file("baz.tex", "\\cite{foo}")
],
main_file: "foo.bib",
position: Position::new(0, 9),
new_name: "",
component_database: LatexComponentDatabase::default(),
}
);
assert_eq!(
references,
vec![Location::new(
FeatureSpec::uri("bar.tex"),
range::create(1, 0, 1, 10)
)]
);
}

#[test]
fn test_latex() {
let mut builder = WorkspaceBuilder::new();
let uri = builder.document("foo.tex", "");
let request = FeatureTester::new(builder.workspace, uri, 0, 0, "").into();

let results = block_on(BibtexEntryReferenceProvider::execute(&request));

assert_eq!(results, Vec::new());
let references = test_feature!(
BibtexEntryReferenceProvider,
FeatureSpec {
files: vec![FeatureSpec::file("foo.tex", ""),],
main_file: "foo.tex",
position: Position::new(0, 0),
new_name: "",
component_database: LatexComponentDatabase::default(),
}
);
assert_eq!(references, Vec::new());
}
}
57 changes: 37 additions & 20 deletions src/reference/latex_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,49 @@ impl LatexLabelReferenceProvider {
#[cfg(test)]
mod tests {
use super::*;
use crate::feature::FeatureTester;
use crate::workspace::WorkspaceBuilder;
use futures::executor::block_on;
use crate::completion::latex::data::types::LatexComponentDatabase;
use crate::feature::FeatureSpec;
use crate::range;
use crate::test_feature;
use lsp_types::Position;

#[test]
fn test() {
let mut builder = WorkspaceBuilder::new();
let uri1 = builder.document("foo.tex", "\\label{foo}");
let uri2 = builder.document("bar.tex", "\\input{foo.tex}\n\\ref{foo}");
builder.document("baz.tex", "\\ref{foo}");
let request = FeatureTester::new(builder.workspace, uri1, 0, 8, "").into();

let results = block_on(LatexLabelReferenceProvider::execute(&request));

let location = Location::new(uri2, range::create(1, 0, 1, 9));
assert_eq!(vec![location], results);
let references = test_feature!(
LatexLabelReferenceProvider,
FeatureSpec {
files: vec![
FeatureSpec::file("foo.tex", "\\label{foo}"),
FeatureSpec::file("bar.tex", "\\input{foo.tex}\n\\ref{foo}"),
FeatureSpec::file("baz.tex", "\\ref{foo}")
],
main_file: "foo.tex",
position: Position::new(0, 8),
new_name: "",
component_database: LatexComponentDatabase::default(),
}
);
assert_eq!(
references,
vec![Location::new(
FeatureSpec::uri("bar.tex"),
range::create(1, 0, 1, 9)
)]
);
}

#[test]
fn test_bibtex() {
let mut builder = WorkspaceBuilder::new();
let uri = builder.document("foo.bib", "");
let request = FeatureTester::new(builder.workspace, uri, 0, 0, "").into();

let results = block_on(LatexLabelReferenceProvider::execute(&request));

assert_eq!(results, Vec::new());
let references = test_feature!(
LatexLabelReferenceProvider,
FeatureSpec {
files: vec![FeatureSpec::file("foo.bib", ""),],
main_file: "foo.bib",
position: Position::new(0, 0),
new_name: "",
component_database: LatexComponentDatabase::default(),
}
);
assert_eq!(references, Vec::new());
}
}

0 comments on commit 6a4da67

Please sign in to comment.