diff options
Diffstat (limited to 'tests/parser.rs')
-rw-r--r-- | tests/parser.rs | 48 |
1 files changed, 8 insertions, 40 deletions
diff --git a/tests/parser.rs b/tests/parser.rs index 43d04e491..518852bb2 100644 --- a/tests/parser.rs +++ b/tests/parser.rs | |||
@@ -1,46 +1,20 @@ | |||
1 | extern crate file; | 1 | extern crate file; |
2 | #[macro_use(assert_diff)] | ||
3 | extern crate difference; | ||
4 | extern crate libsyntax2; | 2 | extern crate libsyntax2; |
3 | extern crate testutils; | ||
5 | 4 | ||
6 | use std::path::{PathBuf, Path}; | 5 | use std::path::{Path}; |
7 | use std::fs::read_dir; | ||
8 | use std::fmt::Write; | 6 | use std::fmt::Write; |
9 | 7 | ||
10 | use libsyntax2::{tokenize, parse, Node, File}; | 8 | use libsyntax2::{tokenize, parse, Node, File}; |
9 | use testutils::{collect_tests, assert_equal_text}; | ||
11 | 10 | ||
12 | #[test] | 11 | #[test] |
13 | fn parser_tests() { | 12 | fn parser_tests() { |
14 | for test_case in parser_test_cases() { | 13 | for test_case in collect_tests(&["parser/ok", "parser/err"]) { |
15 | parser_test_case(&test_case); | 14 | parser_test_case(&test_case); |
16 | } | 15 | } |
17 | } | 16 | } |
18 | 17 | ||
19 | fn parser_test_dir() -> PathBuf { | ||
20 | let dir = env!("CARGO_MANIFEST_DIR"); | ||
21 | PathBuf::from(dir).join("tests/data/parser") | ||
22 | } | ||
23 | |||
24 | fn test_from_dir(dir: &Path) -> Vec<PathBuf> { | ||
25 | let mut acc = Vec::new(); | ||
26 | for file in read_dir(&dir).unwrap() { | ||
27 | let file = file.unwrap(); | ||
28 | let path = file.path(); | ||
29 | if path.extension().unwrap_or_default() == "rs" { | ||
30 | acc.push(path); | ||
31 | } | ||
32 | } | ||
33 | acc.sort(); | ||
34 | acc | ||
35 | } | ||
36 | |||
37 | fn parser_test_cases() -> Vec<PathBuf> { | ||
38 | let mut acc = Vec::new(); | ||
39 | acc.extend(test_from_dir(&parser_test_dir().join("ok"))); | ||
40 | acc.extend(test_from_dir(&parser_test_dir().join("err"))); | ||
41 | acc | ||
42 | } | ||
43 | |||
44 | fn parser_test_case(path: &Path) { | 18 | fn parser_test_case(path: &Path) { |
45 | let actual = { | 19 | let actual = { |
46 | let text = file::get_text(path).unwrap(); | 20 | let text = file::get_text(path).unwrap(); |
@@ -48,19 +22,13 @@ fn parser_test_case(path: &Path) { | |||
48 | let file = parse(text, &tokens); | 22 | let file = parse(text, &tokens); |
49 | dump_tree(&file) | 23 | dump_tree(&file) |
50 | }; | 24 | }; |
51 | let expected = path.with_extension("txt"); | 25 | let expected_path = path.with_extension("txt"); |
52 | let expected = file::get_text(&expected).expect( | 26 | let expected = file::get_text(&expected_path).expect( |
53 | &format!("Can't read {}", expected.display()) | 27 | &format!("Can't read {}", expected_path.display()) |
54 | ); | 28 | ); |
55 | let expected = expected.as_str(); | 29 | let expected = expected.as_str(); |
56 | let actual = actual.as_str(); | 30 | let actual = actual.as_str(); |
57 | if expected == actual { | 31 | assert_equal_text(expected, actual, &expected_path); |
58 | return | ||
59 | } | ||
60 | if expected.trim() == actual.trim() { | ||
61 | panic!("Whitespace difference! {}", path.display()) | ||
62 | } | ||
63 | assert_diff!(expected, actual, "\n", 0) | ||
64 | } | 32 | } |
65 | 33 | ||
66 | fn dump_tree(file: &File) -> String { | 34 | fn dump_tree(file: &File) -> String { |