extract fixture parsing

This commit is contained in:
Aleksey Kladov 2018-10-31 21:37:32 +03:00
parent b58ca6b1a6
commit 64ce895ef0
4 changed files with 51 additions and 22 deletions

1
Cargo.lock generated
View file

@ -661,6 +661,7 @@ dependencies = [
"serde_json 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "walkdir 2.2.6 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -32,3 +32,4 @@ gen_lsp_server = { path = "../gen_lsp_server" }
[dev-dependencies] [dev-dependencies]
tempdir = "0.3.7" tempdir = "0.3.7"
test_utils = { path = "../test_utils" }

View file

@ -17,6 +17,7 @@ use languageserver_types::{
use serde::Serialize; use serde::Serialize;
use serde_json::{from_str, to_string_pretty, Value}; use serde_json::{from_str, to_string_pretty, Value};
use tempdir::TempDir; use tempdir::TempDir;
use test_utils::parse_fixture;
use ra_lsp_server::{ use ra_lsp_server::{
main_loop, req, 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()); INIT.call_once(|| Logger::with_env_or_str(crate::LOG).start().unwrap());
let tmp_dir = TempDir::new("test-project").unwrap(); let tmp_dir = TempDir::new("test-project").unwrap();
let mut buf = String::new();
let mut file_name = None;
let mut paths = vec![]; let mut paths = vec![];
macro_rules! flush {
() => { for entry in parse_fixture(fixture) {
if let Some(file_name) = file_name { let path = tmp_dir.path().join(entry.meta);
let path = tmp_dir.path().join(file_name); fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::create_dir_all(path.parent().unwrap()).unwrap(); fs::write(path.as_path(), entry.text.as_bytes()).unwrap();
fs::write(path.as_path(), buf.as_bytes()).unwrap(); paths.push((path, entry.text));
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');
} }
flush!();
Server::new(tmp_dir, paths) Server::new(tmp_dir, paths)
} }

View file

@ -87,3 +87,45 @@ pub fn add_cursor(text: &str, offset: TextUnit) -> String {
res.push_str(&text[offset..]); res.push_str(&text[offset..]);
res 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
}