aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_syntax/src/tests.rs100
-rw-r--r--crates/test_utils/src/lib.rs106
2 files changed, 104 insertions, 102 deletions
diff --git a/crates/ra_syntax/src/tests.rs b/crates/ra_syntax/src/tests.rs
index 959967b79..f14f23628 100644
--- a/crates/ra_syntax/src/tests.rs
+++ b/crates/ra_syntax/src/tests.rs
@@ -1,9 +1,11 @@
1use std::{ 1use std::{
2 env,
2 fmt::Write, 3 fmt::Write,
4 fs,
3 path::{Component, Path, PathBuf}, 5 path::{Component, Path, PathBuf},
4}; 6};
5 7
6use test_utils::{collect_rust_files, dir_tests, project_dir, read_text}; 8use test_utils::{assert_eq_text, project_dir};
7 9
8use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token}; 10use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token};
9 11
@@ -200,3 +202,99 @@ where
200 } 202 }
201 }); 203 });
202} 204}
205
206/// Calls callback `f` with input code and file paths for each `.rs` file in `test_data_dir`
207/// subdirectories defined by `paths`.
208///
209/// If the content of the matching output file differs from the output of `f()`
210/// the test will fail.
211///
212/// If there is no matching output file it will be created and filled with the
213/// output of `f()`, but the test will fail.
214fn dir_tests<F>(test_data_dir: &Path, paths: &[&str], outfile_extension: &str, f: F)
215where
216 F: Fn(&str, &Path) -> String,
217{
218 for (path, input_code) in collect_rust_files(test_data_dir, paths) {
219 let actual = f(&input_code, &path);
220 let path = path.with_extension(outfile_extension);
221 if !path.exists() {
222 println!("\nfile: {}", path.display());
223 println!("No .txt file with expected result, creating...\n");
224 println!("{}\n{}", input_code, actual);
225 fs::write(&path, &actual).unwrap();
226 panic!("No expected result");
227 }
228 let expected = read_text(&path);
229 assert_equal_text(&expected, &actual, &path);
230 }
231}
232
233/// Collects all `.rs` files from `dir` subdirectories defined by `paths`.
234fn collect_rust_files(root_dir: &Path, paths: &[&str]) -> Vec<(PathBuf, String)> {
235 paths
236 .iter()
237 .flat_map(|path| {
238 let path = root_dir.to_owned().join(path);
239 rust_files_in_dir(&path).into_iter()
240 })
241 .map(|path| {
242 let text = read_text(&path);
243 (path, text)
244 })
245 .collect()
246}
247
248/// Collects paths to all `.rs` files from `dir` in a sorted `Vec<PathBuf>`.
249fn rust_files_in_dir(dir: &Path) -> Vec<PathBuf> {
250 let mut acc = Vec::new();
251 for file in fs::read_dir(&dir).unwrap() {
252 let file = file.unwrap();
253 let path = file.path();
254 if path.extension().unwrap_or_default() == "rs" {
255 acc.push(path);
256 }
257 }
258 acc.sort();
259 acc
260}
261
262/// Asserts that `expected` and `actual` strings are equal. If they differ only
263/// in trailing or leading whitespace the test won't fail and
264/// the contents of `actual` will be written to the file located at `path`.
265fn assert_equal_text(expected: &str, actual: &str, path: &Path) {
266 if expected == actual {
267 return;
268 }
269 let dir = project_dir();
270 let pretty_path = path.strip_prefix(&dir).unwrap_or_else(|_| path);
271 if expected.trim() == actual.trim() {
272 println!("whitespace difference, rewriting");
273 println!("file: {}\n", pretty_path.display());
274 fs::write(path, actual).unwrap();
275 return;
276 }
277 if env::var("UPDATE_EXPECTATIONS").is_ok() {
278 println!("rewriting {}", pretty_path.display());
279 fs::write(path, actual).unwrap();
280 return;
281 }
282 assert_eq_text!(expected, actual, "file: {}", pretty_path.display());
283}
284
285/// Read file and normalize newlines.
286///
287/// `rustc` seems to always normalize `\r\n` newlines to `\n`:
288///
289/// ```
290/// let s = "
291/// ";
292/// assert_eq!(s.as_bytes(), &[10]);
293/// ```
294///
295/// so this should always be correct.
296fn read_text(path: &Path) -> String {
297 fs::read_to_string(path)
298 .unwrap_or_else(|_| panic!("File at {:?} should be valid", path))
299 .replace("\r\n", "\n")
300}
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index e32a0a0c3..fba5f4281 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -13,7 +13,7 @@ mod fixture;
13use std::{ 13use std::{
14 convert::{TryFrom, TryInto}, 14 convert::{TryFrom, TryInto},
15 env, fs, 15 env, fs,
16 path::{Path, PathBuf}, 16 path::PathBuf,
17}; 17};
18 18
19use serde_json::Value; 19use serde_json::Value;
@@ -299,85 +299,6 @@ pub fn find_mismatch<'a>(expected: &'a Value, actual: &'a Value) -> Option<(&'a
299 } 299 }
300} 300}
301 301
302/// Calls callback `f` with input code and file paths for each `.rs` file in `test_data_dir`
303/// subdirectories defined by `paths`.
304///
305/// If the content of the matching output file differs from the output of `f()`
306/// the test will fail.
307///
308/// If there is no matching output file it will be created and filled with the
309/// output of `f()`, but the test will fail.
310pub fn dir_tests<F>(test_data_dir: &Path, paths: &[&str], outfile_extension: &str, f: F)
311where
312 F: Fn(&str, &Path) -> String,
313{
314 for (path, input_code) in collect_rust_files(test_data_dir, paths) {
315 let actual = f(&input_code, &path);
316 let path = path.with_extension(outfile_extension);
317 if !path.exists() {
318 println!("\nfile: {}", path.display());
319 println!("No .txt file with expected result, creating...\n");
320 println!("{}\n{}", input_code, actual);
321 fs::write(&path, &actual).unwrap();
322 panic!("No expected result");
323 }
324 let expected = read_text(&path);
325 assert_equal_text(&expected, &actual, &path);
326 }
327}
328
329/// Collects all `.rs` files from `dir` subdirectories defined by `paths`.
330pub fn collect_rust_files(root_dir: &Path, paths: &[&str]) -> Vec<(PathBuf, String)> {
331 paths
332 .iter()
333 .flat_map(|path| {
334 let path = root_dir.to_owned().join(path);
335 rust_files_in_dir(&path).into_iter()
336 })
337 .map(|path| {
338 let text = read_text(&path);
339 (path, text)
340 })
341 .collect()
342}
343
344/// Collects paths to all `.rs` files from `dir` in a sorted `Vec<PathBuf>`.
345fn rust_files_in_dir(dir: &Path) -> Vec<PathBuf> {
346 let mut acc = Vec::new();
347 for file in fs::read_dir(&dir).unwrap() {
348 let file = file.unwrap();
349 let path = file.path();
350 if path.extension().unwrap_or_default() == "rs" {
351 acc.push(path);
352 }
353 }
354 acc.sort();
355 acc
356}
357
358/// Returns the path to the root directory of `rust-analyzer` project.
359pub fn project_dir() -> PathBuf {
360 let dir = env!("CARGO_MANIFEST_DIR");
361 PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned()
362}
363
364/// Read file and normalize newlines.
365///
366/// `rustc` seems to always normalize `\r\n` newlines to `\n`:
367///
368/// ```
369/// let s = "
370/// ";
371/// assert_eq!(s.as_bytes(), &[10]);
372/// ```
373///
374/// so this should always be correct.
375pub fn read_text(path: &Path) -> String {
376 fs::read_to_string(path)
377 .unwrap_or_else(|_| panic!("File at {:?} should be valid", path))
378 .replace("\r\n", "\n")
379}
380
381/// Returns `false` if slow tests should not run, otherwise returns `true` and 302/// Returns `false` if slow tests should not run, otherwise returns `true` and
382/// also creates a file at `./target/.slow_tests_cookie` which serves as a flag 303/// also creates a file at `./target/.slow_tests_cookie` which serves as a flag
383/// that slow tests did run. 304/// that slow tests did run.
@@ -392,25 +313,8 @@ pub fn skip_slow_tests() -> bool {
392 should_skip 313 should_skip
393} 314}
394 315
395/// Asserts that `expected` and `actual` strings are equal. If they differ only 316/// Returns the path to the root directory of `rust-analyzer` project.
396/// in trailing or leading whitespace the test won't fail and 317pub fn project_dir() -> PathBuf {
397/// the contents of `actual` will be written to the file located at `path`. 318 let dir = env!("CARGO_MANIFEST_DIR");
398fn assert_equal_text(expected: &str, actual: &str, path: &Path) { 319 PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned()
399 if expected == actual {
400 return;
401 }
402 let dir = project_dir();
403 let pretty_path = path.strip_prefix(&dir).unwrap_or_else(|_| path);
404 if expected.trim() == actual.trim() {
405 println!("whitespace difference, rewriting");
406 println!("file: {}\n", pretty_path.display());
407 fs::write(path, actual).unwrap();
408 return;
409 }
410 if env::var("UPDATE_EXPECTATIONS").is_ok() {
411 println!("rewriting {}", pretty_path.display());
412 fs::write(path, actual).unwrap();
413 return;
414 }
415 assert_eq_text!(expected, actual, "file: {}", pretty_path.display());
416} 320}