diff options
author | Aleksey Kladov <[email protected]> | 2019-10-24 17:29:38 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-10-24 17:29:38 +0100 |
commit | a40d02c9eb1c7226bc7db87b014dc827e77f2a08 (patch) | |
tree | 40108b506592ca6ae0fd4597f8b2cb7bdf414bfa /xtask/src/codegen | |
parent | a409a12f1b3c7aa6c09405bf8e28f73b9761fd18 (diff) |
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 e6eeb29a1..db1e59dac 100644 --- a/xtask/src/codegen/gen_parser_tests.rs +++ b/xtask/src/codegen/gen_parser_tests.rs | |||
@@ -3,12 +3,12 @@ | |||
3 | 3 | ||
4 | use std::{ | 4 | use std::{ |
5 | collections::HashMap, | 5 | collections::HashMap, |
6 | fs, | 6 | fs, iter, |
7 | path::{Path, PathBuf}, | 7 | path::{Path, PathBuf}, |
8 | }; | 8 | }; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | codegen::{self, update, Mode}, | 11 | codegen::{self, extract_comment_blocks, update, Mode}, |
12 | project_root, Result, | 12 | project_root, Result, |
13 | }; | 13 | }; |
14 | 14 | ||
@@ -57,46 +57,28 @@ struct Tests { | |||
57 | } | 57 | } |
58 | 58 | ||
59 | fn collect_tests(s: &str) -> Vec<Test> { | 59 | fn collect_tests(s: &str) -> Vec<Test> { |
60 | let mut res = vec![]; | 60 | let mut res = Vec::new(); |
61 | let prefix = "// "; | 61 | for comment_block in extract_comment_blocks(s) { |
62 | let lines = s.lines().map(str::trim_start); | 62 | let first_line = &comment_block[0]; |
63 | 63 | let (name, ok) = if first_line.starts_with("test ") { | |
64 | let mut block = vec![]; | 64 | let name = first_line["test ".len()..].to_string(); |
65 | for line in lines { | 65 | (name, true) |
66 | let is_comment = line.starts_with(prefix); | 66 | } else if first_line.starts_with("test_err ") { |
67 | if is_comment { | 67 | let name = first_line["test_err ".len()..].to_string(); |
68 | block.push(&line[prefix.len()..]); | 68 | (name, false) |
69 | } else { | 69 | } else { |
70 | process_block(&mut res, &block); | 70 | continue; |
71 | block.clear(); | ||
72 | } | ||
73 | } | ||
74 | process_block(&mut res, &block); | ||
75 | return res; | ||
76 | |||
77 | fn process_block(acc: &mut Vec<Test>, block: &[&str]) { | ||
78 | if block.is_empty() { | ||
79 | return; | ||
80 | } | ||
81 | let mut ok = true; | ||
82 | let mut block = block.iter(); | ||
83 | let name = loop { | ||
84 | match block.next() { | ||
85 | Some(line) if line.starts_with("test ") => { | ||
86 | break line["test ".len()..].to_string(); | ||
87 | } | ||
88 | Some(line) if line.starts_with("test_err ") => { | ||
89 | ok = false; | ||
90 | break line["test_err ".len()..].to_string(); | ||
91 | } | ||
92 | Some(_) => (), | ||
93 | None => return, | ||
94 | } | ||
95 | }; | 71 | }; |
96 | let text: String = block.copied().chain(std::iter::once("")).collect::<Vec<_>>().join("\n"); | 72 | let text: String = comment_block[1..] |
73 | .iter() | ||
74 | .cloned() | ||
75 | .chain(iter::once(String::new())) | ||
76 | .collect::<Vec<_>>() | ||
77 | .join("\n"); | ||
97 | assert!(!text.trim().is_empty() && text.ends_with('\n')); | 78 | assert!(!text.trim().is_empty() && text.ends_with('\n')); |
98 | acc.push(Test { name, text, ok }) | 79 | res.push(Test { name, text, ok }) |
99 | } | 80 | } |
81 | res | ||
100 | } | 82 | } |
101 | 83 | ||
102 | fn tests_from_dir(dir: &Path) -> Result<Tests> { | 84 | fn tests_from_dir(dir: &Path) -> Result<Tests> { |