aboutsummaryrefslogtreecommitdiff
path: root/tools/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-07-30 14:16:58 +0100
committerAleksey Kladov <[email protected]>2018-07-30 14:16:58 +0100
commit3b6a6f6673041cf9ee315c00f9b0e24e2c067091 (patch)
tree001e556601e9dd37556338f877759466846c7af0 /tools/src
parentd39198490f878a9ae395af1cf923fb7375de4548 (diff)
Add render test functionality
Diffstat (limited to 'tools/src')
-rw-r--r--tools/src/lib.rs50
-rw-r--r--tools/src/main.rs (renamed from tools/src/bin/main.rs)45
2 files changed, 53 insertions, 42 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 @@
1extern crate itertools;
2use itertools::Itertools;
3
4#[derive(Debug, Eq)]
5pub struct Test {
6 pub start_line: usize,
7 pub name: String,
8 pub text: String,
9}
10
11impl PartialEq for Test {
12 fn eq(&self, other: &Test) -> bool {
13 self.name.eq(&other.name)
14 }
15}
16
17pub 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}
diff --git a/tools/src/bin/main.rs b/tools/src/main.rs
index f4f6e82ae..671f05388 100644
--- a/tools/src/bin/main.rs
+++ b/tools/src/main.rs
@@ -1,14 +1,14 @@
1extern crate clap; 1extern crate clap;
2#[macro_use] 2#[macro_use]
3extern crate failure; 3extern crate failure;
4extern crate itertools;
5extern crate ron; 4extern crate ron;
6extern crate tera; 5extern crate tera;
7extern crate walkdir; 6extern crate walkdir;
7extern crate tools;
8 8
9use clap::{App, Arg, SubCommand};
10use itertools::Itertools;
11use std::{collections::HashSet, fs, path::Path}; 9use std::{collections::HashSet, fs, path::Path};
10use clap::{App, Arg, SubCommand};
11use tools::{collect_tests, Test};
12 12
13type Result<T> = ::std::result::Result<T, failure::Error>; 13type Result<T> = ::std::result::Result<T, failure::Error>;
14 14
@@ -96,17 +96,6 @@ fn gen_tests(verify: bool) -> Result<()> {
96 Ok(()) 96 Ok(())
97} 97}
98 98
99#[derive(Debug, Eq)]
100struct Test {
101 name: String,
102 text: String,
103}
104
105impl PartialEq for Test {
106 fn eq(&self, other: &Test) -> bool {
107 self.name.eq(&other.name)
108 }
109}
110 99
111impl ::std::hash::Hash for Test { 100impl ::std::hash::Hash for Test {
112 fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) { 101 fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
@@ -135,34 +124,6 @@ fn tests_from_dir(dir: &Path) -> Result<HashSet<Test>> {
135 Ok(res) 124 Ok(res)
136} 125}
137 126
138fn collect_tests(s: &str) -> Vec<Test> {
139 let mut res = vec![];
140 let prefix = "// ";
141 let comment_blocks = s
142 .lines()
143 .map(str::trim_left)
144 .group_by(|line| line.starts_with(prefix));
145
146 'outer: for (is_comment, block) in comment_blocks.into_iter() {
147 if !is_comment {
148 continue;
149 }
150 let mut block = block.map(|line| &line[prefix.len()..]);
151
152 let name = loop {
153 match block.next() {
154 Some(line) if line.starts_with("test ") => break line["test ".len()..].to_string(),
155 Some(_) => (),
156 None => continue 'outer,
157 }
158 };
159 let text: String = itertools::join(block.chain(::std::iter::once("")), "\n");
160 assert!(!text.trim().is_empty() && text.ends_with("\n"));
161 res.push(Test { name, text })
162 }
163 res
164}
165
166fn existing_tests(dir: &Path) -> Result<HashSet<Test>> { 127fn existing_tests(dir: &Path) -> Result<HashSet<Test>> {
167 let mut res = HashSet::new(); 128 let mut res = HashSet::new();
168 for file in fs::read_dir(dir)? { 129 for file in fs::read_dir(dir)? {