Collect the tests from the given source text.
(text: &str)
| 209 | |
| 210 | /// Collect the tests from the given source text. |
| 211 | fn collect_tests(text: &str) -> Vec<Test> { |
| 212 | let mut tests = Vec::new(); |
| 213 | |
| 214 | for comment_block in extract_comment_blocks(text) { |
| 215 | let first_line = &comment_block[0]; |
| 216 | |
| 217 | let (kind, name) = match first_line.split_once(' ') { |
| 218 | Some(("test_ok", suffix)) => (TestKind::Ok, suffix), |
| 219 | Some(("test_err", suffix)) => (TestKind::Err, suffix), |
| 220 | _ => continue, |
| 221 | }; |
| 222 | |
| 223 | let text: String = comment_block[1..] |
| 224 | .iter() |
| 225 | .cloned() |
| 226 | .chain([String::new()]) |
| 227 | .collect::<Vec<_>>() |
| 228 | .join("\n"); |
| 229 | |
| 230 | assert!(!text.trim().is_empty() && text.ends_with('\n')); |
| 231 | |
| 232 | tests.push(Test { |
| 233 | name: name.to_string(), |
| 234 | contents: text, |
| 235 | kind, |
| 236 | }); |
| 237 | } |
| 238 | |
| 239 | tests |
| 240 | } |
| 241 | |
| 242 | #[derive(Debug, Default)] |
| 243 | struct CommentBlock(Vec<String>); |