]> git.lizzy.rs Git - rust.git/commitdiff
extract fixture parsing
authorAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 31 Oct 2018 18:37:32 +0000 (21:37 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 31 Oct 2018 18:37:40 +0000 (21:37 +0300)
Cargo.lock
crates/ra_lsp_server/Cargo.toml
crates/ra_lsp_server/tests/heavy_tests/support.rs
crates/test_utils/src/lib.rs

index 76391eff43bd2151ccd2a1bf6779d55d95858e12..bf937d2050676f839084276135884aa5a447bc2f 100644 (file)
@@ -661,6 +661,7 @@ dependencies = [
  "serde_json 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)",
  "smol_str 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
+ "test_utils 0.1.0",
  "text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "url_serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "walkdir 2.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
index 2bf0730742526293b2fc9314d7d7039b9d5668f4..f29dafc17cdafc116a5d10c6842729048ccad3a5 100644 (file)
@@ -32,3 +32,4 @@ gen_lsp_server = { path = "../gen_lsp_server" }
 
 [dev-dependencies]
 tempdir = "0.3.7"
+test_utils = { path = "../test_utils" }
index 004d7e8adde3a5a7df971890c66399c025fa8d89..b90d211357052b66c4c22d86b3e374430afdad61 100644 (file)
@@ -17,6 +17,7 @@
 use serde::Serialize;
 use serde_json::{from_str, to_string_pretty, Value};
 use tempdir::TempDir;
+use test_utils::parse_fixture;
 
 use ra_lsp_server::{
     main_loop, req,
@@ -28,30 +29,14 @@ pub fn project(fixture: &str) -> Server {
     INIT.call_once(|| Logger::with_env_or_str(crate::LOG).start().unwrap());
 
     let tmp_dir = TempDir::new("test-project").unwrap();
-    let mut buf = String::new();
-    let mut file_name = None;
     let mut paths = vec![];
-    macro_rules! flush {
-        () => {
-            if let Some(file_name) = file_name {
-                let path = tmp_dir.path().join(file_name);
-                fs::create_dir_all(path.parent().unwrap()).unwrap();
-                fs::write(path.as_path(), buf.as_bytes()).unwrap();
-                paths.push((path, buf.clone()));
-            }
-        };
-    };
-    for line in fixture.lines() {
-        if line.starts_with("//-") {
-            flush!();
-            buf.clear();
-            file_name = Some(line["//-".len()..].trim());
-            continue;
-        }
-        buf.push_str(line);
-        buf.push('\n');
+
+    for entry in parse_fixture(fixture) {
+        let path = tmp_dir.path().join(entry.meta);
+        fs::create_dir_all(path.parent().unwrap()).unwrap();
+        fs::write(path.as_path(), entry.text.as_bytes()).unwrap();
+        paths.push((path, entry.text));
     }
-    flush!();
     Server::new(tmp_dir, paths)
 }
 
index dbe2997eba84952021608412d0b99ac434c1682c..562dbcbb3ed195359911c600a3712b40b9d6502f 100644 (file)
@@ -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
+}