Skip to content

Commit 7d82152

Browse files
bors[bot]matklad
andauthored
Merge #5163
5163: Refactor parser tests a bit r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents a9db3d5 + 991850b commit 7d82152

File tree

3 files changed

+106
-104
lines changed

3 files changed

+106
-104
lines changed

crates/ra_syntax/src/tests.rs

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use std::{
2+
env,
23
fmt::Write,
4+
fs,
35
path::{Component, Path, PathBuf},
46
};
57

6-
use test_utils::{collect_rust_files, dir_tests, project_dir, read_text};
8+
use test_utils::{assert_eq_text, project_dir};
79

810
use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token};
911

@@ -200,3 +202,99 @@ where
200202
}
201203
});
202204
}
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.
214+
fn dir_tests<F>(test_data_dir: &Path, paths: &[&str], outfile_extension: &str, f: F)
215+
where
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`.
234+
fn 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>`.
249+
fn 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`.
265+
fn 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_EXPECT").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.
296+
fn 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+
}

crates/test_utils/src/lib.rs

Lines changed: 5 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod fixture;
1313
use std::{
1414
convert::{TryFrom, TryInto},
1515
env, fs,
16-
path::{Path, PathBuf},
16+
path::PathBuf,
1717
};
1818

1919
use serde_json::Value;
@@ -299,85 +299,6 @@ pub fn find_mismatch<'a>(expected: &'a Value, actual: &'a Value) -> Option<(&'a
299299
}
300300
}
301301

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.
310-
pub fn dir_tests<F>(test_data_dir: &Path, paths: &[&str], outfile_extension: &str, f: F)
311-
where
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`.
330-
pub 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>`.
345-
fn 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.
359-
pub 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.
375-
pub 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-
381302
/// Returns `false` if slow tests should not run, otherwise returns `true` and
382303
/// also creates a file at `./target/.slow_tests_cookie` which serves as a flag
383304
/// that slow tests did run.
@@ -392,25 +313,8 @@ pub fn skip_slow_tests() -> bool {
392313
should_skip
393314
}
394315

395-
/// Asserts that `expected` and `actual` strings are equal. If they differ only
396-
/// in trailing or leading whitespace the test won't fail and
397-
/// the contents of `actual` will be written to the file located at `path`.
398-
fn assert_equal_text(expected: &str, actual: &str, path: &Path) {
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());
316+
/// Returns the path to the root directory of `rust-analyzer` project.
317+
pub fn project_dir() -> PathBuf {
318+
let dir = env!("CARGO_MANIFEST_DIR");
319+
PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned()
416320
}

docs/dev/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,10 @@ There are two kinds of tests:
361361
The purpose of inline tests is not to achieve full coverage by test cases, but to explain to the reader of the code what each particular `if` and `match` is responsible for.
362362
If you are tempted to add a large inline test, it might be a good idea to leave only the simplest example in place, and move the test to a manual `parser/ok` test.
363363

364-
To update test data, run with `UPDATE_EXPECTATIONS` variable:
364+
To update test data, run with `UPDATE_EXPECT` variable:
365365

366366
```bash
367-
env UPDATE_EXPECTATIONS=1 cargo qt
367+
env UPDATE_EXPECT=1 cargo qt
368368
```
369369

370370
After adding a new inline test you need to run `cargo xtest codegen` and also update the test data as described above.

0 commit comments

Comments
 (0)