From 64ce895ef0beea75e9ecfcdf5b4e226a8a6336d8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 31 Oct 2018 21:37:32 +0300 Subject: extract fixture parsing --- crates/test_utils/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'crates/test_utils/src') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index dbe2997eb..562dbcbb3 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -87,3 +87,45 @@ pub fn add_cursor(text: &str, offset: TextUnit) -> String { res.push_str(&text[offset..]); res } + + +#[derive(Debug)] +pub struct FixtureEntry { + pub meta: String, + pub text: String, +} + +/// Parses text wich looks like this: +/// +/// ```notrust +/// //- some meta +/// line 1 +/// line 2 +/// // - other meta +/// ``` +pub fn parse_fixture(fixture: &str) -> Vec { + let mut res = Vec::new(); + let mut buf = String::new(); + let mut meta: Option<&str> = None; + + macro_rules! flush { + () => { + if let Some(meta) = meta { + res.push(FixtureEntry { meta: meta.to_string(), text: buf.clone() }); + buf.clear(); + } + }; + }; + for line in fixture.lines() { + if line.starts_with("//-") { + flush!(); + buf.clear(); + meta = Some(line["//-".len()..].trim()); + continue; + } + buf.push_str(line); + buf.push('\n'); + } + flush!(); + res +} -- cgit v1.2.3