Specify cursor position in ok-wrapping tests, and switch to using analysis_and_position function

This commit is contained in:
Phil Ellison 2019-08-17 07:12:50 +01:00 committed by Aleksey Kladov
parent 6620949cae
commit 59dd30402b
2 changed files with 31 additions and 35 deletions

View file

@ -187,7 +187,7 @@ mod tests {
use ra_syntax::SourceFile; use ra_syntax::SourceFile;
use test_utils::assert_eq_text; use test_utils::assert_eq_text;
use crate::mock_analysis::{fixture_with_target_file, single_file}; use crate::mock_analysis::{analysis_and_position, single_file};
use super::*; use super::*;
@ -216,14 +216,25 @@ mod tests {
assert_eq_text!(after, &actual); assert_eq_text!(after, &actual);
} }
fn check_apply_diagnostic_fix_for_target_file(target_file: &str, fixture: &str, after: &str) { /// Takes a multi-file input fixture with annotated cursor positions,
let (analysis, file_id, target_file_contents) = /// and checks that:
fixture_with_target_file(fixture, target_file); /// * a diagnostic is produced
let diagnostic = analysis.diagnostics(file_id).unwrap().pop().unwrap(); /// * this diagnostic touches the input cursor position
/// * that the contents of the file containing the cursor match `after` after the diagnostic fix is applied
fn check_apply_diagnostic_fix_from_position(fixture: &str, after: &str) {
let (analysis, file_position) = analysis_and_position(fixture);
let diagnostic = analysis.diagnostics(file_position.file_id).unwrap().pop().unwrap();
let mut fix = diagnostic.fix.unwrap(); let mut fix = diagnostic.fix.unwrap();
let edit = fix.source_file_edits.pop().unwrap().edit; let edit = fix.source_file_edits.pop().unwrap().edit;
let target_file_contents = analysis.file_text(file_position.file_id).unwrap();
let actual = edit.apply(&target_file_contents); let actual = edit.apply(&target_file_contents);
assert_eq_text!(after, &actual); assert_eq_text!(after, &actual);
assert!(
diagnostic.range.start() <= file_position.offset && diagnostic.range.end() >= file_position.offset,
"diagnostic range {} does not touch cursor position {}",
diagnostic.range,
file_position.offset
);
} }
fn check_apply_diagnostic_fix(before: &str, after: &str) { fn check_apply_diagnostic_fix(before: &str, after: &str) {
@ -235,9 +246,11 @@ mod tests {
assert_eq_text!(after, &actual); assert_eq_text!(after, &actual);
} }
fn check_no_diagnostic_for_target_file(target_file: &str, fixture: &str) { /// Takes a multi-file input fixture with annotated cursor position and checks that no diagnostics
let (analysis, file_id, _) = fixture_with_target_file(fixture, target_file); /// apply to the file containing the cursor.
let diagnostics = analysis.diagnostics(file_id).unwrap(); fn check_no_diagnostic_for_target_file(fixture: &str) {
let (analysis, file_position) = analysis_and_position(fixture);
let diagnostics = analysis.diagnostics(file_position.file_id).unwrap();
assert_eq!(diagnostics.len(), 0); assert_eq!(diagnostics.len(), 0);
} }
@ -257,7 +270,7 @@ mod tests {
if y == 0 { if y == 0 {
return Err("div by zero".into()); return Err("div by zero".into());
} }
x / y x / y<|>
} }
//- /std/lib.rs //- /std/lib.rs
@ -279,7 +292,7 @@ fn div(x: i32, y: i32) -> Result<i32, String> {
Ok(x / y) Ok(x / y)
} }
"#; "#;
check_apply_diagnostic_fix_for_target_file("/main.rs", before, after); check_apply_diagnostic_fix_from_position(before, after);
} }
#[test] #[test]
@ -292,7 +305,7 @@ fn div(x: i32, y: i32) -> Result<i32, String> {
if x == 0 { if x == 0 {
return Err(7); return Err(7);
} }
x <|>x
} }
//- /std/lib.rs //- /std/lib.rs
@ -311,7 +324,7 @@ fn div<T>(x: T) -> Result<T, i32> {
Ok(x) Ok(x)
} }
"#; "#;
check_apply_diagnostic_fix_for_target_file("/main.rs", before, after); check_apply_diagnostic_fix_from_position(before, after);
} }
#[test] #[test]
@ -326,7 +339,7 @@ fn div<T>(x: T) -> Result<T, i32> {
if y == 0 { if y == 0 {
return Err("div by zero".into()); return Err("div by zero".into());
} }
x / y x <|>/ y
} }
//- /std/lib.rs //- /std/lib.rs
@ -349,7 +362,7 @@ fn div(x: i32, y: i32) -> MyResult<i32> {
Ok(x / y) Ok(x / y)
} }
"#; "#;
check_apply_diagnostic_fix_for_target_file("/main.rs", before, after); check_apply_diagnostic_fix_from_position(before, after);
} }
#[test] #[test]
@ -359,7 +372,7 @@ fn div(x: i32, y: i32) -> MyResult<i32> {
use std::{string::String, result::Result::{self, Ok, Err}}; use std::{string::String, result::Result::{self, Ok, Err}};
fn foo() -> Result<String, i32> { fn foo() -> Result<String, i32> {
0 0<|>
} }
//- /std/lib.rs //- /std/lib.rs
@ -370,7 +383,7 @@ fn div(x: i32, y: i32) -> MyResult<i32> {
pub enum Result<T, E> { Ok(T), Err(E) } pub enum Result<T, E> { Ok(T), Err(E) }
} }
"#; "#;
check_no_diagnostic_for_target_file("/main.rs", content); check_no_diagnostic_for_target_file(content);
} }
#[test] #[test]
@ -385,7 +398,7 @@ fn div(x: i32, y: i32) -> MyResult<i32> {
} }
fn foo() -> SomeOtherEnum { fn foo() -> SomeOtherEnum {
0 0<|>
} }
//- /std/lib.rs //- /std/lib.rs
@ -396,7 +409,7 @@ fn div(x: i32, y: i32) -> MyResult<i32> {
pub enum Result<T, E> { Ok(T), Err(E) } pub enum Result<T, E> { Ok(T), Err(E) }
} }
"#; "#;
check_no_diagnostic_for_target_file("/main.rs", content); check_no_diagnostic_for_target_file(content);
} }
#[test] #[test]

View file

@ -80,15 +80,6 @@ impl MockAnalysis {
.expect("no file in this mock"); .expect("no file in this mock");
FileId(idx as u32 + 1) FileId(idx as u32 + 1)
} }
pub fn id_and_contents_of(&self, path: &str) -> (FileId, String) {
let (idx, contents) = self
.files
.iter()
.enumerate()
.find(|(_, (p, _text))| path == p)
.expect("no file in this mock");
(FileId(idx as u32 + 1), contents.1.to_string())
}
pub fn analysis_host(self) -> AnalysisHost { pub fn analysis_host(self) -> AnalysisHost {
let mut host = AnalysisHost::default(); let mut host = AnalysisHost::default();
let source_root = SourceRootId(0); let source_root = SourceRootId(0);
@ -133,14 +124,6 @@ pub fn single_file(code: &str) -> (Analysis, FileId) {
(mock.analysis(), file_id) (mock.analysis(), file_id)
} }
/// Creates analysis from a fixture with multiple files
/// and returns the file id and contents of the target file.
pub fn fixture_with_target_file(fixture: &str, target_file: &str) -> (Analysis, FileId, String) {
let mock = MockAnalysis::with_files(fixture);
let (target_file_id, target_file_contents) = mock.id_and_contents_of(target_file);
(mock.analysis(), target_file_id, target_file_contents)
}
/// Creates analysis for a single file, returns position marked with <|>. /// Creates analysis for a single file, returns position marked with <|>.
pub fn single_file_with_position(code: &str) -> (Analysis, FilePosition) { pub fn single_file_with_position(code: &str) -> (Analysis, FilePosition) {
let mut mock = MockAnalysis::new(); let mut mock = MockAnalysis::new();