diff options
author | Aleksey Kladov <[email protected]> | 2018-01-07 11:56:08 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-01-07 11:56:08 +0000 |
commit | 9e4052cc2ee12751ba94909ff479bd03df141ac4 (patch) | |
tree | 2e7c3a063369c5151fd851910c997e5d1020a164 /tests/testutils/src | |
parent | 18f9e50b2d1aaf91992be9fd2f2a7e1866a943d3 (diff) |
Test utils
Diffstat (limited to 'tests/testutils/src')
-rw-r--r-- | tests/testutils/src/lib.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/tests/testutils/src/lib.rs b/tests/testutils/src/lib.rs new file mode 100644 index 000000000..9fc85cc24 --- /dev/null +++ b/tests/testutils/src/lib.rs | |||
@@ -0,0 +1,64 @@ | |||
1 | extern crate difference; | ||
2 | extern crate file; | ||
3 | |||
4 | use std::path::{PathBuf, Path}; | ||
5 | use std::fs::read_dir; | ||
6 | |||
7 | use difference::Changeset; | ||
8 | |||
9 | pub fn assert_equal_text( | ||
10 | expected: &str, | ||
11 | actual: &str, | ||
12 | path: &Path | ||
13 | ) { | ||
14 | if expected != actual { | ||
15 | print_difference(expected, actual, path) | ||
16 | } | ||
17 | } | ||
18 | |||
19 | pub fn collect_tests(paths: &[&str]) -> Vec<PathBuf> { | ||
20 | paths.iter().flat_map(|path| { | ||
21 | let path = test_data_dir().join(path); | ||
22 | test_from_dir(&path).into_iter() | ||
23 | }).collect() | ||
24 | } | ||
25 | |||
26 | fn test_from_dir(dir: &Path) -> Vec<PathBuf> { | ||
27 | let mut acc = Vec::new(); | ||
28 | for file in read_dir(&dir).unwrap() { | ||
29 | let file = file.unwrap(); | ||
30 | let path = file.path(); | ||
31 | if path.extension().unwrap_or_default() == "rs" { | ||
32 | acc.push(path); | ||
33 | } | ||
34 | } | ||
35 | acc.sort(); | ||
36 | acc | ||
37 | } | ||
38 | |||
39 | fn print_difference(expected: &str, actual: &str, path: &Path) { | ||
40 | let dir = project_dir(); | ||
41 | let path = path.strip_prefix(&dir).unwrap_or_else(|_| path); | ||
42 | println!("\nfile: {}", path.display()); | ||
43 | if expected.trim() == actual.trim() { | ||
44 | println!("whitespace difference"); | ||
45 | println!("rewriting the file"); | ||
46 | file::put_text(path, actual).unwrap(); | ||
47 | } else { | ||
48 | let changeset = Changeset::new(actual, expected, "\n"); | ||
49 | println!("{}", changeset); | ||
50 | } | ||
51 | panic!("Comparison failed") | ||
52 | } | ||
53 | |||
54 | fn project_dir() -> PathBuf { | ||
55 | let dir = env!("CARGO_MANIFEST_DIR"); | ||
56 | PathBuf::from(dir) | ||
57 | .parent().unwrap() | ||
58 | .parent().unwrap() | ||
59 | .to_owned() | ||
60 | } | ||
61 | |||
62 | fn test_data_dir() -> PathBuf { | ||
63 | project_dir().join("tests/data") | ||
64 | } \ No newline at end of file | ||