about summary refs log tree commit diff
path: root/crates/test_utils/src
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2018-10-31 21:37:32 +0300
committerAleksey Kladov <aleksey.kladov@gmail.com>2018-10-31 21:37:40 +0300
commit64ce895ef0beea75e9ecfcdf5b4e226a8a6336d8 (patch)
treeaf07f03610a2ef1f8a49d773170146d0c1774750 /crates/test_utils/src
parentb58ca6b1a68471f6944893b94f09cd56dc28f837 (diff)
downloadrust-64ce895ef0beea75e9ecfcdf5b4e226a8a6336d8.tar.gz
rust-64ce895ef0beea75e9ecfcdf5b4e226a8a6336d8.zip
extract fixture parsing
Diffstat (limited to 'crates/test_utils/src')
-rw-r--r--crates/test_utils/src/lib.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index dbe2997eba8..562dbcbb3ed 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<FixtureEntry> {
+    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
+}