diff options
Diffstat (limited to 'tools/src/lib.rs')
-rw-r--r-- | tools/src/lib.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/tools/src/lib.rs b/tools/src/lib.rs new file mode 100644 index 000000000..157818bdf --- /dev/null +++ b/tools/src/lib.rs | |||
@@ -0,0 +1,50 @@ | |||
1 | extern crate itertools; | ||
2 | use itertools::Itertools; | ||
3 | |||
4 | #[derive(Debug, Eq)] | ||
5 | pub struct Test { | ||
6 | pub start_line: usize, | ||
7 | pub name: String, | ||
8 | pub text: String, | ||
9 | } | ||
10 | |||
11 | impl PartialEq for Test { | ||
12 | fn eq(&self, other: &Test) -> bool { | ||
13 | self.name.eq(&other.name) | ||
14 | } | ||
15 | } | ||
16 | |||
17 | pub fn collect_tests(s: &str) -> Vec<Test> { | ||
18 | let mut res = vec![]; | ||
19 | let prefix = "// "; | ||
20 | let comment_blocks = s | ||
21 | .lines() | ||
22 | .map(str::trim_left) | ||
23 | .enumerate() | ||
24 | .group_by(|(idx, line)| line.starts_with(prefix)); | ||
25 | |||
26 | 'outer: for (is_comment, block) in comment_blocks.into_iter() { | ||
27 | if !is_comment { | ||
28 | continue; | ||
29 | } | ||
30 | let mut block = block.map(|(idx, line)| (idx, &line[prefix.len()..])); | ||
31 | |||
32 | let (start_line, name) = loop { | ||
33 | match block.next() { | ||
34 | Some((idx, line)) if line.starts_with("test ") => { | ||
35 | break (idx, line["test ".len()..].to_string()) | ||
36 | }, | ||
37 | Some(_) => (), | ||
38 | None => continue 'outer, | ||
39 | } | ||
40 | }; | ||
41 | let text: String = itertools::join( | ||
42 | block.map(|(_, line)| line) | ||
43 | .chain(::std::iter::once("")), | ||
44 | "\n" | ||
45 | ); | ||
46 | assert!(!text.trim().is_empty() && text.ends_with("\n")); | ||
47 | res.push(Test { start_line, name, text }) | ||
48 | } | ||
49 | res | ||
50 | } | ||