diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-24 17:30:15 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-24 17:30:15 +0100 |
commit | a3a10c59e00906265ab177042948621540067f3b (patch) | |
tree | dbbb4a6c5a762661cde00d5e7b1650ab984d885a /xtask/src/codegen/gen_parser_tests.rs | |
parent | 81677c3232fc4e6a9fb52889b4e82a8afdee9c7e (diff) | |
parent | a40d02c9eb1c7226bc7db87b014dc827e77f2a08 (diff) |
Merge #2062
2062: refactor comment extraction from tasks r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'xtask/src/codegen/gen_parser_tests.rs')
-rw-r--r-- | xtask/src/codegen/gen_parser_tests.rs | 63 |
1 files changed, 22 insertions, 41 deletions
diff --git a/xtask/src/codegen/gen_parser_tests.rs b/xtask/src/codegen/gen_parser_tests.rs index 0f550d948..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 | ||
@@ -56,48 +56,29 @@ struct Tests { | |||
56 | pub err: HashMap<String, Test>, | 56 | pub err: HashMap<String, Test>, |
57 | } | 57 | } |
58 | 58 | ||
59 | fn collect_tests(s: &str) -> Vec<(usize, 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).enumerate(); | 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_idx, 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_idx, &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<(usize, Test)>, block: &[(usize, &str)]) { | ||
78 | if block.is_empty() { | ||
79 | return; | ||
80 | } | ||
81 | let mut ok = true; | ||
82 | let mut block = block.iter(); | ||
83 | let (start_line, name) = loop { | ||
84 | match block.next() { | ||
85 | Some(&(idx, line)) if line.starts_with("test ") => { | ||
86 | break (idx, line["test ".len()..].to_string()); | ||
87 | } | ||
88 | Some(&(idx, line)) if line.starts_with("test_err ") => { | ||
89 | ok = false; | ||
90 | break (idx, line["test_err ".len()..].to_string()); | ||
91 | } | ||
92 | Some(_) => (), | ||
93 | None => return, | ||
94 | } | ||
95 | }; | 71 | }; |
96 | let text: String = | 72 | let text: String = comment_block[1..] |
97 | block.map(|(_, line)| *line).chain(std::iter::once("")).collect::<Vec<_>>().join("\n"); | 73 | .iter() |
74 | .cloned() | ||
75 | .chain(iter::once(String::new())) | ||
76 | .collect::<Vec<_>>() | ||
77 | .join("\n"); | ||
98 | assert!(!text.trim().is_empty() && text.ends_with('\n')); | 78 | assert!(!text.trim().is_empty() && text.ends_with('\n')); |
99 | acc.push((start_line, Test { name, text, ok })) | 79 | res.push(Test { name, text, ok }) |
100 | } | 80 | } |
81 | res | ||
101 | } | 82 | } |
102 | 83 | ||
103 | fn tests_from_dir(dir: &Path) -> Result<Tests> { | 84 | fn tests_from_dir(dir: &Path) -> Result<Tests> { |
@@ -118,7 +99,7 @@ fn tests_from_dir(dir: &Path) -> Result<Tests> { | |||
118 | fn process_file(res: &mut Tests, path: &Path) -> Result<()> { | 99 | fn process_file(res: &mut Tests, path: &Path) -> Result<()> { |
119 | let text = fs::read_to_string(path)?; | 100 | let text = fs::read_to_string(path)?; |
120 | 101 | ||
121 | for (_, test) in collect_tests(&text) { | 102 | for test in collect_tests(&text) { |
122 | if test.ok { | 103 | if test.ok { |
123 | if let Some(old_test) = res.ok.insert(test.name.clone(), test) { | 104 | if let Some(old_test) = res.ok.insert(test.name.clone(), test) { |
124 | Err(format!("Duplicate test: {}", old_test.name))? | 105 | Err(format!("Duplicate test: {}", old_test.name))? |