Skip to content

Commit

Permalink
Refactor rename tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed May 3, 2019
1 parent 6a4da67 commit b978332
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 260 deletions.
2 changes: 1 addition & 1 deletion src/completion/latex/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const COLOR_NAMES: &'static [&'static str] = &[
mod tests {
use super::*;
use crate::completion::latex::data::types::LatexComponentDatabase;
use crate::feature::{FeatureSpec, FeatureTester};
use crate::feature::FeatureSpec;
use crate::test_feature;
use lsp_types::Position;

Expand Down
131 changes: 14 additions & 117 deletions src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,130 +186,27 @@ impl Into<FeatureRequest<ReferenceParams>> for FeatureSpec {
}

#[cfg(test)]
#[macro_export]
macro_rules! test_feature {
($provider:tt, $spec: expr) => {{
futures::executor::block_on($provider::execute(&$spec.into()))
}};
}

#[cfg(test)]
pub struct FeatureTester {
workspace: Arc<Workspace>,
document: Arc<Document>,
document_id: TextDocumentIdentifier,
position: Position,
new_name: String,
component_database: Arc<LatexComponentDatabase>,
}

#[cfg(test)]
impl FeatureTester {
pub fn new(workspace: Workspace, uri: Url, line: u64, character: u64, new_name: &str) -> Self {
let document = workspace.find(&uri).unwrap();
FeatureTester {
workspace: Arc::new(workspace),
document,
document_id: TextDocumentIdentifier::new(uri),
position: Position::new(line, character),
new_name: new_name.to_owned(),
component_database: Arc::new(LatexComponentDatabase::default()),
}
}
}

#[cfg(test)]
impl Into<FeatureRequest<TextDocumentPositionParams>> for FeatureTester {
fn into(self) -> FeatureRequest<TextDocumentPositionParams> {
let params = TextDocumentPositionParams::new(self.document_id, self.position);
FeatureRequest::new(
params,
self.workspace,
self.document,
self.component_database,
)
}
}

#[cfg(test)]
impl Into<FeatureRequest<CompletionParams>> for FeatureTester {
fn into(self) -> FeatureRequest<CompletionParams> {
let params = CompletionParams {
text_document: self.document_id,
position: self.position,
context: None,
};
FeatureRequest::new(
params,
self.workspace,
self.document,
self.component_database,
)
}
}

#[cfg(test)]
impl Into<FeatureRequest<FoldingRangeParams>> for FeatureTester {
fn into(self) -> FeatureRequest<FoldingRangeParams> {
let params = FoldingRangeParams {
text_document: self.document_id,
};
FeatureRequest::new(
params,
self.workspace,
self.document,
self.component_database,
)
}
}

#[cfg(test)]
impl Into<FeatureRequest<DocumentLinkParams>> for FeatureTester {
fn into(self) -> FeatureRequest<DocumentLinkParams> {
let params = DocumentLinkParams {
text_document: self.document_id,
};
FeatureRequest::new(
params,
self.workspace,
self.document,
self.component_database,
)
}
}

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

#[cfg(test)]
impl Into<FeatureRequest<RenameParams>> for FeatureTester {
fn into(self) -> FeatureRequest<RenameParams> {
let params = RenameParams {
text_document: self.document_id,
position: self.position,
new_name: self.new_name,
};
FeatureRequest::new(
params,
self.workspace,
self.document,
self.component_database,
)
}
#[macro_export]
macro_rules! test_feature {
($provider:tt, $spec: expr) => {{
futures::executor::block_on($provider::execute(&$spec.into()))
}};
}
116 changes: 65 additions & 51 deletions src/rename/bibtex_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::syntax::bibtex::BibtexSyntaxTree;
use crate::syntax::latex::analysis::citation::LatexCitationAnalyzer;
use crate::syntax::latex::ast::LatexVisitor;
use crate::syntax::latex::LatexSyntaxTree;
use crate::syntax::text::Span;
use crate::syntax::text::SyntaxNode;
use crate::workspace::SyntaxTree;
use lsp_types::RenameParams;
Expand Down Expand Up @@ -86,63 +85,78 @@ impl BibtexEntryRenameProvider {
#[cfg(test)]
mod tests {
use super::*;
use crate::feature::FeatureTester;
use crate::completion::latex::data::types::LatexComponentDatabase;
use crate::feature::FeatureSpec;
use crate::range;
use crate::workspace::WorkspaceBuilder;
use futures::executor::block_on;
use crate::test_feature;
use lsp_types::Position;

#[test]
fn test() {
struct Row {
index: usize,
line: u64,
character: u64,
};

for row in vec![
Row {
index: 0,
line: 0,
character: 9,
},
Row {
index: 1,
line: 1,
character: 6,
},
] {
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}");
let uri = vec![&uri1, &uri2][row.index].clone();
let request =
FeatureTester::new(builder.workspace, uri, row.line, row.character, "qux").into();

let changes = block_on(BibtexEntryRenameProvider::execute(&request))
.unwrap()
.changes
.unwrap();
fn test_entry() {
let edit = test_feature!(
BibtexEntryRenameProvider,
FeatureSpec {
files: vec![
FeatureSpec::file("foo.bib", "@article{foo, bar = baz}"),
FeatureSpec::file("bar.tex", "\\addbibresource{foo.bib}\n\\cite{foo}"),
],
main_file: "foo.bib",
position: Position::new(0, 9),
new_name: "qux",
component_database: LatexComponentDatabase::default(),
}
);
let mut changes = HashMap::new();
changes.insert(
FeatureSpec::uri("foo.bib"),
vec![TextEdit::new(range::create(0, 9, 0, 12), "qux".to_owned())],
);
changes.insert(
FeatureSpec::uri("bar.tex"),
vec![TextEdit::new(range::create(1, 6, 1, 9), "qux".to_owned())],
);
assert_eq!(edit, Some(WorkspaceEdit::new(changes)));
}

assert_eq!(2, changes.len());
assert_eq!(
vec![TextEdit::new(range::create(0, 9, 0, 12), "qux".to_owned())],
*changes.get(&uri1).unwrap()
);
assert_eq!(
vec![TextEdit::new(range::create(1, 6, 1, 9), "qux".to_owned())],
*changes.get(&uri2).unwrap()
);
}
#[test]
fn test_citation() {
let edit = test_feature!(
BibtexEntryRenameProvider,
FeatureSpec {
files: vec![
FeatureSpec::file("foo.bib", "@article{foo, bar = baz}"),
FeatureSpec::file("bar.tex", "\\addbibresource{foo.bib}\n\\cite{foo}"),
],
main_file: "bar.tex",
position: Position::new(1, 6),
new_name: "qux",
component_database: LatexComponentDatabase::default(),
}
);
let mut changes = HashMap::new();
changes.insert(
FeatureSpec::uri("foo.bib"),
vec![TextEdit::new(range::create(0, 9, 0, 12), "qux".to_owned())],
);
changes.insert(
FeatureSpec::uri("bar.tex"),
vec![TextEdit::new(range::create(1, 6, 1, 9), "qux".to_owned())],
);
assert_eq!(edit, Some(WorkspaceEdit::new(changes)));
}

#[test]
fn test_field_name() {
let mut builder = WorkspaceBuilder::new();
let uri = builder.document("foo.bib", "@article{foo, bar = baz}");
let request = FeatureTester::new(builder.workspace, uri, 0, 14, "qux").into();

let edit = block_on(BibtexEntryRenameProvider::execute(&request));

assert_eq!(None, edit);
let edit = test_feature!(
BibtexEntryRenameProvider,
FeatureSpec {
files: vec![FeatureSpec::file("foo.bib", "@article{foo, bar = baz}")],
main_file: "foo.bib",
position: Position::new(0, 14),
new_name: "qux",
component_database: LatexComponentDatabase::default(),
}
);
assert_eq!(edit, None);
}
}
60 changes: 35 additions & 25 deletions src/rename/latex_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,41 +52,51 @@ impl LatexCommandRenameProvider {
#[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", "\\include{bar.tex}\n\\baz");
let uri2 = builder.document("bar.tex", "\\baz");
let request = FeatureTester::new(builder.workspace, uri1.clone(), 1, 2, "qux").into();

let changes = block_on(LatexCommandRenameProvider::execute(&request))
.unwrap()
.changes
.unwrap();

assert_eq!(2, changes.len());
assert_eq!(
let edit = test_feature!(
LatexCommandRenameProvider,
FeatureSpec {
files: vec![
FeatureSpec::file("foo.tex", "\\include{bar.tex}\n\\baz"),
FeatureSpec::file("bar.tex", "\\baz"),
],
main_file: "foo.tex",
position: Position::new(1, 2),
new_name: "qux",
component_database: LatexComponentDatabase::default(),
}
);
let mut changes = HashMap::new();
changes.insert(
FeatureSpec::uri("foo.tex"),
vec![TextEdit::new(range::create(1, 0, 1, 4), "\\qux".to_owned())],
*changes.get(&uri1).unwrap()
);
assert_eq!(
changes.insert(
FeatureSpec::uri("bar.tex"),
vec![TextEdit::new(range::create(0, 0, 0, 4), "\\qux".to_owned())],
*changes.get(&uri2).unwrap()
);
assert_eq!(edit, Some(WorkspaceEdit::new(changes)));
}

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

let edit = block_on(LatexCommandRenameProvider::execute(&request));

assert_eq!(None, edit);
let edit = test_feature!(
LatexCommandRenameProvider,
FeatureSpec {
files: vec![FeatureSpec::file("foo.bib", "@article{foo, bar = baz}")],
main_file: "foo.bib",
position: Position::new(0, 14),
new_name: "qux",
component_database: LatexComponentDatabase::default(),
}
);
assert_eq!(edit, None);
}
}
Loading

0 comments on commit b978332

Please sign in to comment.