diff options
Diffstat (limited to 'crates/ra_syntax/tests/test.rs')
-rw-r--r-- | crates/ra_syntax/tests/test.rs | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/crates/ra_syntax/tests/test.rs b/crates/ra_syntax/tests/test.rs index 67acc9020..c17b6ffa6 100644 --- a/crates/ra_syntax/tests/test.rs +++ b/crates/ra_syntax/tests/test.rs | |||
@@ -6,7 +6,7 @@ extern crate walkdir; | |||
6 | use std::{ | 6 | use std::{ |
7 | fmt::Write, | 7 | fmt::Write, |
8 | fs, | 8 | fs, |
9 | path::{Path, PathBuf}, | 9 | path::{Path, PathBuf, Component}, |
10 | }; | 10 | }; |
11 | 11 | ||
12 | use ra_syntax::{ | 12 | use ra_syntax::{ |
@@ -37,6 +37,47 @@ fn parser_fuzz_tests() { | |||
37 | } | 37 | } |
38 | } | 38 | } |
39 | 39 | ||
40 | /// Test that Rust-analyzer can parse and validate the rust-analyser | ||
41 | /// TODO: Use this as a benchmark | ||
42 | #[test] | ||
43 | fn self_hosting_parsing() { | ||
44 | use std::ffi::OsStr; | ||
45 | let empty_vec = vec![]; | ||
46 | let dir = project_dir().join("crates"); | ||
47 | let mut count = 0; | ||
48 | for entry in walkdir::WalkDir::new(dir) | ||
49 | .into_iter() | ||
50 | .filter_entry(|entry| { | ||
51 | !entry | ||
52 | .path() | ||
53 | .components() | ||
54 | // TODO: this more neatly | ||
55 | .any(|component| { | ||
56 | // Get all files which are not in the crates/ra_syntax/tests/data folder | ||
57 | component == Component::Normal(OsStr::new("data")) | ||
58 | }) | ||
59 | }) | ||
60 | .map(|e| e.unwrap()) | ||
61 | .filter(|entry| { | ||
62 | // Get all `.rs ` files | ||
63 | !entry.path().is_dir() && (entry.path().extension() == Some(OsStr::new("rs"))) | ||
64 | }) | ||
65 | { | ||
66 | count += 1; | ||
67 | let text = read_text(entry.path()); | ||
68 | let node = SourceFileNode::parse(&text); | ||
69 | let errors = node.errors(); | ||
70 | assert_eq!( | ||
71 | errors, empty_vec, | ||
72 | "There should be no errors in the file {:?}", | ||
73 | entry | ||
74 | ); | ||
75 | } | ||
76 | assert!( | ||
77 | count > 30, | ||
78 | "self_hosting_parsing found too few files - is it running in the right directory?" | ||
79 | ) | ||
80 | } | ||
40 | /// Read file and normalize newlines. | 81 | /// Read file and normalize newlines. |
41 | /// | 82 | /// |
42 | /// `rustc` seems to always normalize `\r\n` newlines to `\n`: | 83 | /// `rustc` seems to always normalize `\r\n` newlines to `\n`: |
@@ -49,7 +90,9 @@ fn parser_fuzz_tests() { | |||
49 | /// | 90 | /// |
50 | /// so this should always be correct. | 91 | /// so this should always be correct. |
51 | fn read_text(path: &Path) -> String { | 92 | fn read_text(path: &Path) -> String { |
52 | fs::read_to_string(path).unwrap().replace("\r\n", "\n") | 93 | fs::read_to_string(path) |
94 | .expect(&format!("File at {:?} should be valid", path)) | ||
95 | .replace("\r\n", "\n") | ||
53 | } | 96 | } |
54 | 97 | ||
55 | pub fn dir_tests<F>(paths: &[&str], f: F) | 98 | pub fn dir_tests<F>(paths: &[&str], f: F) |