diff options
| author | Aleksey Kladov <aleksey.kladov@gmail.com> | 2018-10-31 21:37:32 +0300 |
|---|---|---|
| committer | Aleksey Kladov <aleksey.kladov@gmail.com> | 2018-10-31 21:37:40 +0300 |
| commit | 64ce895ef0beea75e9ecfcdf5b4e226a8a6336d8 (patch) | |
| tree | af07f03610a2ef1f8a49d773170146d0c1774750 | |
| parent | b58ca6b1a68471f6944893b94f09cd56dc28f837 (diff) | |
| download | rust-64ce895ef0beea75e9ecfcdf5b4e226a8a6336d8.tar.gz rust-64ce895ef0beea75e9ecfcdf5b4e226a8a6336d8.zip | |
extract fixture parsing
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | crates/ra_lsp_server/Cargo.toml | 1 | ||||
| -rw-r--r-- | crates/ra_lsp_server/tests/heavy_tests/support.rs | 29 | ||||
| -rw-r--r-- | crates/test_utils/src/lib.rs | 42 |
4 files changed, 51 insertions, 22 deletions
diff --git a/Cargo.lock b/Cargo.lock index 76391eff43b..bf937d20506 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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)", diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index 2bf07307425..f29dafc17cd 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml @@ -32,3 +32,4 @@ gen_lsp_server = { path = "../gen_lsp_server" } [dev-dependencies] tempdir = "0.3.7" +test_utils = { path = "../test_utils" } diff --git a/crates/ra_lsp_server/tests/heavy_tests/support.rs b/crates/ra_lsp_server/tests/heavy_tests/support.rs index 004d7e8adde..b90d2113570 100644 --- a/crates/ra_lsp_server/tests/heavy_tests/support.rs +++ b/crates/ra_lsp_server/tests/heavy_tests/support.rs @@ -17,6 +17,7 @@ use languageserver_types::{ 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) } 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 +} |
