From 14a23d1bde8493df9e5196973132144060a61709 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Sat, 17 Aug 2019 07:37:37 +0100 Subject: [PATCH] Strip indents and empty lines in check_apply_diagnostic_fix_from_position --- crates/ra_ide_api/src/diagnostics.rs | 91 +++++++++++++++++----------- 1 file changed, 55 insertions(+), 36 deletions(-) diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 4e1f47db6b..1a4882824f 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -184,6 +184,7 @@ fn check_struct_shorthand_initialization( #[cfg(test)] mod tests { use insta::assert_debug_snapshot_matches; + use join_to_string::join; use ra_syntax::SourceFile; use test_utils::assert_eq_text; @@ -228,9 +229,30 @@ mod tests { 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); - assert_eq_text!(after, &actual); + + // Strip indent and empty lines from `after`, to match the behaviour of + // `parse_fixture` called from `analysis_and_position`. + let margin = fixture + .lines() + .filter(|it| it.trim_start().starts_with("//-")) + .map(|it| it.len() - it.trim_start().len()) + .next() + .expect("empty fixture"); + let after = join(after.lines().filter_map(|line| { + if line.len() > margin { + Some(&line[margin..]) + } else { + None + } + })) + .separator("\n") + .suffix("\n") + .to_string(); + + assert_eq_text!(&after, &actual); assert!( - diagnostic.range.start() <= file_position.offset && diagnostic.range.end() >= file_position.offset, + diagnostic.range.start() <= file_position.offset + && diagnostic.range.end() >= file_position.offset, "diagnostic range {} does not touch cursor position {}", diagnostic.range, file_position.offset @@ -281,17 +303,16 @@ mod tests { pub enum Result { Ok(T), Err(E) } } "#; - // The formatting here is a bit odd due to how the parse_fixture function works in test_utils - - // it strips empty lines and leading whitespace. The important part of this test is that the final - // `x / y` expr is now wrapped in `Ok(..)` - let after = r#"use std::{string::String, result::Result::{self, Ok, Err}}; -fn div(x: i32, y: i32) -> Result { - if y == 0 { - return Err("div by zero".into()); - } - Ok(x / y) -} -"#; + let after = r#" + use std::{string::String, result::Result::{self, Ok, Err}}; + + fn div(x: i32, y: i32) -> Result { + if y == 0 { + return Err("div by zero".into()); + } + Ok(x / y) + } + "#; check_apply_diagnostic_fix_from_position(before, after); } @@ -313,17 +334,16 @@ fn div(x: i32, y: i32) -> Result { pub enum Result { Ok(T), Err(E) } } "#; - // The formatting here is a bit odd due to how the parse_fixture function works in test_utils - - // it strips empty lines and leading whitespace. The important part of this test is that the final - // expr is now wrapped in `Ok(..)` - let after = r#"use std::result::Result::{self, Ok, Err}; -fn div(x: T) -> Result { - if x == 0 { - return Err(7); - } - Ok(x) -} -"#; + let after = r#" + use std::result::Result::{self, Ok, Err}; + + fn div(x: T) -> Result { + if x == 0 { + return Err(7); + } + Ok(x) + } + "#; check_apply_diagnostic_fix_from_position(before, after); } @@ -350,18 +370,17 @@ fn div(x: T) -> Result { pub enum Result { Ok(T), Err(E) } } "#; - // The formatting here is a bit odd due to how the parse_fixture function works in test_utils - - // it strips empty lines and leading whitespace. The important part of this test is that the final - // `x / y` expr is now wrapped in `Ok(..)` - let after = r#"use std::{string::String, result::Result::{self, Ok, Err}}; -type MyResult = Result; -fn div(x: i32, y: i32) -> MyResult { - if y == 0 { - return Err("div by zero".into()); - } - Ok(x / y) -} -"#; + let after = r#" + use std::{string::String, result::Result::{self, Ok, Err}}; + + type MyResult = Result; + fn div(x: i32, y: i32) -> MyResult { + if y == 0 { + return Err("div by zero".into()); + } + Ok(x / y) + } + "#; check_apply_diagnostic_fix_from_position(before, after); }