From dfba29e4fb66457d101db295e3c356a932ac005e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 31 Oct 2018 22:34:31 +0300 Subject: Add MockAnalysis to make testing easier --- crates/test_utils/src/lib.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'crates/test_utils') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 562dbcbb3..8980f077f 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -8,6 +8,8 @@ use text_unit::{TextRange, TextUnit}; pub use self::difference::Changeset as __Changeset; +pub const CURSOR_MARKER: &str = "<|>"; + #[macro_export] macro_rules! assert_eq_text { ($expected:expr, $actual:expr) => {{ @@ -45,11 +47,10 @@ pub fn extract_offset(text: &str) -> (TextUnit, String) { } pub fn try_extract_offset(text: &str) -> Option<(TextUnit, String)> { - let cursor = "<|>"; - let cursor_pos = text.find(cursor)?; - let mut new_text = String::with_capacity(text.len() - cursor.len()); + let cursor_pos = text.find(CURSOR_MARKER)?; + let mut new_text = String::with_capacity(text.len() - CURSOR_MARKER.len()); new_text.push_str(&text[..cursor_pos]); - new_text.push_str(&text[cursor_pos + cursor.len()..]); + new_text.push_str(&text[cursor_pos + CURSOR_MARKER.len()..]); let cursor_pos = TextUnit::from(cursor_pos as u32); Some((cursor_pos, new_text)) } @@ -116,7 +117,22 @@ pub fn parse_fixture(fixture: &str) -> Vec { } }; }; - for line in fixture.lines() { + let margin = fixture.lines() + .filter(|it| it.trim_start().starts_with("//-")) + .map(|it| it.len() - it.trim_start().len()) + .next().expect("empty fixture"); + let lines = fixture.lines() + .filter_map(|line| { + if line.len() >= margin { + assert!(line[..margin].trim().is_empty()); + Some(&line[margin..]) + } else { + assert!(line.trim().is_empty()); + None + } + }); + + for line in lines { if line.starts_with("//-") { flush!(); buf.clear(); -- cgit v1.2.3