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 --- Cargo.lock | 1 + crates/ra_lsp_server/Cargo.toml | 1 + crates/ra_lsp_server/tests/heavy_tests/support.rs | 29 ++++------------ crates/test_utils/src/lib.rs | 42 +++++++++++++++++++++++ 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 76391eff4..bf937d205 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 2bf073074..f29dafc17 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 004d7e8ad..b90d21135 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 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