aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--xtask/src/codegen/gen_parser_tests.rs27
1 files changed, 13 insertions, 14 deletions
diff --git a/xtask/src/codegen/gen_parser_tests.rs b/xtask/src/codegen/gen_parser_tests.rs
index 0f550d948..e6eeb29a1 100644
--- a/xtask/src/codegen/gen_parser_tests.rs
+++ b/xtask/src/codegen/gen_parser_tests.rs
@@ -56,16 +56,16 @@ struct Tests {
56 pub err: HashMap<String, Test>, 56 pub err: HashMap<String, Test>,
57} 57}
58 58
59fn collect_tests(s: &str) -> Vec<(usize, Test)> { 59fn collect_tests(s: &str) -> Vec<Test> {
60 let mut res = vec![]; 60 let mut res = vec![];
61 let prefix = "// "; 61 let prefix = "// ";
62 let lines = s.lines().map(str::trim_start).enumerate(); 62 let lines = s.lines().map(str::trim_start);
63 63
64 let mut block = vec![]; 64 let mut block = vec![];
65 for (line_idx, line) in lines { 65 for line in lines {
66 let is_comment = line.starts_with(prefix); 66 let is_comment = line.starts_with(prefix);
67 if is_comment { 67 if is_comment {
68 block.push((line_idx, &line[prefix.len()..])); 68 block.push(&line[prefix.len()..]);
69 } else { 69 } else {
70 process_block(&mut res, &block); 70 process_block(&mut res, &block);
71 block.clear(); 71 block.clear();
@@ -74,29 +74,28 @@ fn collect_tests(s: &str) -> Vec<(usize, Test)> {
74 process_block(&mut res, &block); 74 process_block(&mut res, &block);
75 return res; 75 return res;
76 76
77 fn process_block(acc: &mut Vec<(usize, Test)>, block: &[(usize, &str)]) { 77 fn process_block(acc: &mut Vec<Test>, block: &[&str]) {
78 if block.is_empty() { 78 if block.is_empty() {
79 return; 79 return;
80 } 80 }
81 let mut ok = true; 81 let mut ok = true;
82 let mut block = block.iter(); 82 let mut block = block.iter();
83 let (start_line, name) = loop { 83 let name = loop {
84 match block.next() { 84 match block.next() {
85 Some(&(idx, line)) if line.starts_with("test ") => { 85 Some(line) if line.starts_with("test ") => {
86 break (idx, line["test ".len()..].to_string()); 86 break line["test ".len()..].to_string();
87 } 87 }
88 Some(&(idx, line)) if line.starts_with("test_err ") => { 88 Some(line) if line.starts_with("test_err ") => {
89 ok = false; 89 ok = false;
90 break (idx, line["test_err ".len()..].to_string()); 90 break line["test_err ".len()..].to_string();
91 } 91 }
92 Some(_) => (), 92 Some(_) => (),
93 None => return, 93 None => return,
94 } 94 }
95 }; 95 };
96 let text: String = 96 let text: String = block.copied().chain(std::iter::once("")).collect::<Vec<_>>().join("\n");
97 block.map(|(_, line)| *line).chain(std::iter::once("")).collect::<Vec<_>>().join("\n");
98 assert!(!text.trim().is_empty() && text.ends_with('\n')); 97 assert!(!text.trim().is_empty() && text.ends_with('\n'));
99 acc.push((start_line, Test { name, text, ok })) 98 acc.push(Test { name, text, ok })
100 } 99 }
101} 100}
102 101
@@ -118,7 +117,7 @@ fn tests_from_dir(dir: &Path) -> Result<Tests> {
118 fn process_file(res: &mut Tests, path: &Path) -> Result<()> { 117 fn process_file(res: &mut Tests, path: &Path) -> Result<()> {
119 let text = fs::read_to_string(path)?; 118 let text = fs::read_to_string(path)?;
120 119
121 for (_, test) in collect_tests(&text) { 120 for test in collect_tests(&text) {
122 if test.ok { 121 if test.ok {
123 if let Some(old_test) = res.ok.insert(test.name.clone(), test) { 122 if let Some(old_test) = res.ok.insert(test.name.clone(), test) {
124 Err(format!("Duplicate test: {}", old_test.name))? 123 Err(format!("Duplicate test: {}", old_test.name))?