diff options
| author | Aleksey Kladov <aleksey.kladov@gmail.com> | 2019-10-24 19:29:38 +0300 |
|---|---|---|
| committer | Aleksey Kladov <aleksey.kladov@gmail.com> | 2019-10-24 19:29:38 +0300 |
| commit | a40d02c9eb1c7226bc7db87b014dc827e77f2a08 (patch) | |
| tree | 40108b506592ca6ae0fd4597f8b2cb7bdf414bfa /xtask/src/codegen | |
| parent | a409a12f1b3c7aa6c09405bf8e28f73b9761fd18 (diff) | |
| download | rust-a40d02c9eb1c7226bc7db87b014dc827e77f2a08.tar.gz rust-a40d02c9eb1c7226bc7db87b014dc827e77f2a08.zip | |
refactor comment extraction from tasks
Diffstat (limited to 'xtask/src/codegen')
| -rw-r--r-- | xtask/src/codegen/gen_parser_tests.rs | 58 |
1 files changed, 20 insertions, 38 deletions
diff --git a/xtask/src/codegen/gen_parser_tests.rs b/xtask/src/codegen/gen_parser_tests.rs index e6eeb29a1ea..db1e59daccc 100644 --- a/xtask/src/codegen/gen_parser_tests.rs +++ b/xtask/src/codegen/gen_parser_tests.rs @@ -3,12 +3,12 @@ use std::{ collections::HashMap, - fs, + fs, iter, path::{Path, PathBuf}, }; use crate::{ - codegen::{self, update, Mode}, + codegen::{self, extract_comment_blocks, update, Mode}, project_root, Result, }; @@ -57,46 +57,28 @@ struct Tests { } fn collect_tests(s: &str) -> Vec<Test> { - let mut res = vec![]; - let prefix = "// "; - let lines = s.lines().map(str::trim_start); - - let mut block = vec![]; - for line in lines { - let is_comment = line.starts_with(prefix); - if is_comment { - block.push(&line[prefix.len()..]); + let mut res = Vec::new(); + for comment_block in extract_comment_blocks(s) { + let first_line = &comment_block[0]; + let (name, ok) = if first_line.starts_with("test ") { + let name = first_line["test ".len()..].to_string(); + (name, true) + } else if first_line.starts_with("test_err ") { + let name = first_line["test_err ".len()..].to_string(); + (name, false) } else { - process_block(&mut res, &block); - block.clear(); - } - } - process_block(&mut res, &block); - return res; - - fn process_block(acc: &mut Vec<Test>, block: &[&str]) { - if block.is_empty() { - return; - } - let mut ok = true; - let mut block = block.iter(); - let name = loop { - match block.next() { - Some(line) if line.starts_with("test ") => { - break line["test ".len()..].to_string(); - } - Some(line) if line.starts_with("test_err ") => { - ok = false; - break line["test_err ".len()..].to_string(); - } - Some(_) => (), - None => return, - } + continue; }; - let text: String = block.copied().chain(std::iter::once("")).collect::<Vec<_>>().join("\n"); + let text: String = comment_block[1..] + .iter() + .cloned() + .chain(iter::once(String::new())) + .collect::<Vec<_>>() + .join("\n"); assert!(!text.trim().is_empty() && text.ends_with('\n')); - acc.push(Test { name, text, ok }) + res.push(Test { name, text, ok }) } + res } fn tests_from_dir(dir: &Path) -> Result<Tests> { |
