prefer inline tests

This commit is contained in:
gfreezy 2019-01-18 16:29:09 +08:00 committed by Aleksey Kladov
parent 6ab6d1eaa0
commit 360167db16
4 changed files with 140 additions and 131 deletions

View File

@ -132,3 +132,139 @@ fn rename_reference(
cursor_position: None,
});
}
#[cfg(test)]
mod tests {
use insta::assert_debug_snapshot_matches;
use test_utils::assert_eq_text;
use crate::{
mock_analysis::single_file_with_position,
mock_analysis::analysis_and_position,
FileId
};
#[test]
fn test_rename_for_local() {
test_rename(
r#"
fn main() {
let mut i = 1;
let j = 1;
i = i<|> + j;
{
i = 0;
}
i = 5;
}"#,
"k",
r#"
fn main() {
let mut k = 1;
let j = 1;
k = k + j;
{
k = 0;
}
k = 5;
}"#,
);
}
#[test]
fn test_rename_for_param_inside() {
test_rename(
r#"
fn foo(i : u32) -> u32 {
i<|>
}"#,
"j",
r#"
fn foo(j : u32) -> u32 {
j
}"#,
);
}
#[test]
fn test_rename_refs_for_fn_param() {
test_rename(
r#"
fn foo(i<|> : u32) -> u32 {
i
}"#,
"new_name",
r#"
fn foo(new_name : u32) -> u32 {
new_name
}"#,
);
}
#[test]
fn test_rename_for_mut_param() {
test_rename(
r#"
fn foo(mut i<|> : u32) -> u32 {
i
}"#,
"new_name",
r#"
fn foo(mut new_name : u32) -> u32 {
new_name
}"#,
);
}
#[test]
fn test_rename_mod() {
let (analysis, position) = analysis_and_position(
"
//- /bar.rs
mod fo<|>o;
//- /bar/foo.rs
// emtpy
",
);
let new_name = "foo2";
let source_change = analysis.rename(position, new_name).unwrap();
assert_debug_snapshot_matches!("rename_mod", &source_change);
}
#[test]
fn test_rename_mod_in_dir() {
let (analysis, position) = analysis_and_position(
"
//- /lib.rs
mod fo<|>o;
//- /foo/mod.rs
// emtpy
",
);
let new_name = "foo2";
let source_change = analysis.rename(position, new_name).unwrap();
assert_debug_snapshot_matches!("rename_mod_in_dir", &source_change);
}
fn test_rename(text: &str, new_name: &str, expected: &str) {
let (analysis, position) = single_file_with_position(text);
let source_change = analysis.rename(position, new_name).unwrap();
let mut text_edit_bulder = ra_text_edit::TextEditBuilder::default();
let mut file_id: Option<FileId> = None;
if let Some(change) = source_change {
for edit in change.source_file_edits {
file_id = Some(edit.file_id);
for atom in edit.edit.as_atoms() {
text_edit_bulder.replace(atom.delete, atom.insert.clone());
}
}
}
let result = text_edit_bulder
.finish()
.apply(&*analysis.file_text(file_id.unwrap()));
assert_eq_text!(expected, &*result);
}
}

View File

@ -1,6 +1,6 @@
Created: 2019-01-16T14:12:39.379431+00:00
Created: 2019-01-18T08:26:43.427092+00:00
Creator: insta@0.1.4
Source: crates/ra_ide_api/tests/test/main.rs
Source: crates/ra_ide_api/src/rename.rs
Some(
SourceChange {

View File

@ -1,6 +1,6 @@
Created: 2019-01-16T14:12:39.379358+00:00
Created: 2019-01-18T08:26:43.427095+00:00
Creator: insta@0.1.4
Source: crates/ra_ide_api/tests/test/main.rs
Source: crates/ra_ide_api/src/rename.rs
Some(
SourceChange {

View File

@ -2,9 +2,7 @@ use ra_ide_api::{
AnalysisChange,
CrateGraph, FileId, mock_analysis::{MockAnalysis, single_file, single_file_with_position}, Query,
};
use ra_ide_api::mock_analysis::analysis_and_position;
use ra_syntax::TextRange;
use test_utils::assert_eq_text;
use insta::assert_debug_snapshot_matches;
#[test]
@ -92,131 +90,6 @@ fn test_find_all_refs_for_fn_param() {
assert_eq!(refs.len(), 2);
}
#[test]
fn test_rename_for_local() {
test_rename(
r#"
fn main() {
let mut i = 1;
let j = 1;
i = i<|> + j;
{
i = 0;
}
i = 5;
}"#,
"k",
r#"
fn main() {
let mut k = 1;
let j = 1;
k = k + j;
{
k = 0;
}
k = 5;
}"#,
);
}
#[test]
fn test_rename_for_param_inside() {
test_rename(
r#"
fn foo(i : u32) -> u32 {
i<|>
}"#,
"j",
r#"
fn foo(j : u32) -> u32 {
j
}"#,
);
}
#[test]
fn test_rename_refs_for_fn_param() {
test_rename(
r#"
fn foo(i<|> : u32) -> u32 {
i
}"#,
"new_name",
r#"
fn foo(new_name : u32) -> u32 {
new_name
}"#,
);
}
#[test]
fn test_rename_for_mut_param() {
test_rename(
r#"
fn foo(mut i<|> : u32) -> u32 {
i
}"#,
"new_name",
r#"
fn foo(mut new_name : u32) -> u32 {
new_name
}"#,
);
}
#[test]
fn test_rename_mod() {
let (analysis, position) = analysis_and_position(
"
//- /bar.rs
mod fo<|>o;
//- /bar/foo.rs
// emtpy
",
);
let new_name = "foo2";
let source_change = analysis.rename(position, new_name).unwrap();
assert_debug_snapshot_matches!("rename_mod", &source_change);
}
#[test]
fn test_rename_mod_in_dir() {
let (analysis, position) = analysis_and_position(
"
//- /lib.rs
mod fo<|>o;
//- /foo/mod.rs
// emtpy
",
);
let new_name = "foo2";
let source_change = analysis.rename(position, new_name).unwrap();
assert_debug_snapshot_matches!("rename_mod_in_dir", &source_change);
}
fn test_rename(text: &str, new_name: &str, expected: &str) {
let (analysis, position) = single_file_with_position(text);
let source_change = analysis.rename(position, new_name).unwrap();
let mut text_edit_bulder = ra_text_edit::TextEditBuilder::default();
let mut file_id: Option<FileId> = None;
if let Some(change) = source_change {
for edit in change.source_file_edits {
file_id = Some(edit.file_id);
for atom in edit.edit.as_atoms() {
text_edit_bulder.replace(atom.delete, atom.insert.clone());
}
}
}
let result = text_edit_bulder
.finish()
.apply(&*analysis.file_text(file_id.unwrap()));
assert_eq_text!(expected, &*result);
}
#[test]
fn world_symbols_include_stuff_from_macros() {
let (analysis, _) = single_file(