diff options
author | Aleksey Kladov <[email protected]> | 2018-07-30 12:06:22 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-07-30 12:06:22 +0100 |
commit | 6983091d6d255bcfd17c4f8c14015d8abc77928d (patch) | |
tree | 153359a05ca3fd887b59fe47ee84a60be0e6dfaf /tools/src/bin/collect-tests.rs | |
parent | 9a4957d143397256dc04f715660f758a65fcb9d1 (diff) |
Cleanup tools
Diffstat (limited to 'tools/src/bin/collect-tests.rs')
-rw-r--r-- | tools/src/bin/collect-tests.rs | 133 |
1 files changed, 0 insertions, 133 deletions
diff --git a/tools/src/bin/collect-tests.rs b/tools/src/bin/collect-tests.rs deleted file mode 100644 index a52e7b119..000000000 --- a/tools/src/bin/collect-tests.rs +++ /dev/null | |||
@@ -1,133 +0,0 @@ | |||
1 | extern crate file; | ||
2 | extern crate itertools; | ||
3 | extern crate walkdir; | ||
4 | |||
5 | use walkdir::WalkDir; | ||
6 | use itertools::Itertools; | ||
7 | |||
8 | use std::path::{Path, PathBuf}; | ||
9 | use std::collections::HashSet; | ||
10 | use std::fs; | ||
11 | |||
12 | fn main() { | ||
13 | let verify = ::std::env::args().any(|arg| arg == "--verify"); | ||
14 | |||
15 | let d = grammar_dir(); | ||
16 | let tests = tests_from_dir(&d); | ||
17 | let existing = existing_tests(); | ||
18 | |||
19 | for t in existing.difference(&tests) { | ||
20 | panic!("Test is deleted: {}\n{}", t.name, t.text); | ||
21 | } | ||
22 | |||
23 | let new_tests = tests.difference(&existing); | ||
24 | for (i, t) in new_tests.enumerate() { | ||
25 | if verify { | ||
26 | panic!("Inline test is not recorded: {}", t.name); | ||
27 | } | ||
28 | |||
29 | let name = format!("{:04}_{}.rs", existing.len() + i + 1, t.name); | ||
30 | println!("Creating {}", name); | ||
31 | let path = inline_tests_dir().join(name); | ||
32 | file::put_text(&path, &t.text).unwrap(); | ||
33 | } | ||
34 | } | ||
35 | |||
36 | #[derive(Debug, Eq)] | ||
37 | struct Test { | ||
38 | name: String, | ||
39 | text: String, | ||
40 | } | ||
41 | |||
42 | impl PartialEq for Test { | ||
43 | fn eq(&self, other: &Test) -> bool { | ||
44 | self.name.eq(&other.name) | ||
45 | } | ||
46 | } | ||
47 | |||
48 | impl ::std::hash::Hash for Test { | ||
49 | fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) { | ||
50 | self.name.hash(state) | ||
51 | } | ||
52 | } | ||
53 | |||
54 | fn tests_from_dir(dir: &Path) -> HashSet<Test> { | ||
55 | let mut res = HashSet::new(); | ||
56 | for entry in WalkDir::new(dir) { | ||
57 | let entry = entry.unwrap(); | ||
58 | if !entry.file_type().is_file() { | ||
59 | continue; | ||
60 | } | ||
61 | if entry.path().extension().unwrap_or_default() != "rs" { | ||
62 | continue; | ||
63 | } | ||
64 | let text = file::get_text(entry.path()).unwrap(); | ||
65 | |||
66 | for test in collect_tests(&text) { | ||
67 | if let Some(old_test) = res.replace(test) { | ||
68 | panic!("Duplicate test: {}", old_test.name) | ||
69 | } | ||
70 | } | ||
71 | } | ||
72 | res | ||
73 | } | ||
74 | |||
75 | fn collect_tests(s: &str) -> Vec<Test> { | ||
76 | let mut res = vec![]; | ||
77 | let prefix = "// "; | ||
78 | let comment_blocks = s.lines() | ||
79 | .map(str::trim_left) | ||
80 | .group_by(|line| line.starts_with(prefix)); | ||
81 | |||
82 | 'outer: for (is_comment, block) in comment_blocks.into_iter() { | ||
83 | if !is_comment { | ||
84 | continue; | ||
85 | } | ||
86 | let mut block = block.map(|line| &line[prefix.len()..]); | ||
87 | |||
88 | let name = loop { | ||
89 | match block.next() { | ||
90 | Some(line) if line.starts_with("test ") => break line["test ".len()..].to_string(), | ||
91 | Some(_) => (), | ||
92 | None => continue 'outer, | ||
93 | } | ||
94 | }; | ||
95 | let text: String = itertools::join(block.chain(::std::iter::once("")), "\n"); | ||
96 | assert!(!text.trim().is_empty() && text.ends_with("\n")); | ||
97 | res.push(Test { name, text }) | ||
98 | } | ||
99 | res | ||
100 | } | ||
101 | |||
102 | fn existing_tests() -> HashSet<Test> { | ||
103 | let mut res = HashSet::new(); | ||
104 | for file in fs::read_dir(&inline_tests_dir()).unwrap() { | ||
105 | let file = file.unwrap(); | ||
106 | let path = file.path(); | ||
107 | if path.extension().unwrap_or_default() != "rs" { | ||
108 | continue; | ||
109 | } | ||
110 | let name = path.file_name().unwrap().to_str().unwrap(); | ||
111 | let name = name["0000_".len()..name.len() - 3].to_string(); | ||
112 | let text = file::get_text(&path).unwrap(); | ||
113 | res.insert(Test { name, text }); | ||
114 | } | ||
115 | res | ||
116 | } | ||
117 | |||
118 | fn inline_tests_dir() -> PathBuf { | ||
119 | let res = base_dir().join("tests/data/parser/inline"); | ||
120 | if !res.is_dir() { | ||
121 | fs::create_dir_all(&res).unwrap(); | ||
122 | } | ||
123 | res | ||
124 | } | ||
125 | |||
126 | fn grammar_dir() -> PathBuf { | ||
127 | base_dir().join("src/parser/grammar") | ||
128 | } | ||
129 | |||
130 | fn base_dir() -> PathBuf { | ||
131 | let dir = env!("CARGO_MANIFEST_DIR"); | ||
132 | PathBuf::from(dir).parent().unwrap().to_owned() | ||
133 | } | ||