Merge pull request #908 from matklad/simplify-tests

simplify tests
This commit is contained in:
Nick Cameron 2016-04-05 16:53:37 +12:00
commit ea766777ef
6 changed files with 115 additions and 49 deletions

View File

@ -1,3 +1,4 @@
// rustfmt-write_mode: coverage
/// Here's a doc comment! /// Here's a doc comment!
fn main() { fn main() {
// foo is bar // foo is bar

View File

@ -1,3 +1,4 @@
XX XXXXXXXXXXXXXXXXXXX XXXXXXXX
/// Here's a doc comment! /// Here's a doc comment!
fn main() { fn main() {
XX XXX XX XXX XX XXX XX XXX

View File

@ -20,7 +20,7 @@ use std::path::{Path, PathBuf};
use rustfmt::*; use rustfmt::*;
use rustfmt::filemap::{write_system_newlines, FileMap}; use rustfmt::filemap::{write_system_newlines, FileMap};
use rustfmt::config::{Config, ReportTactic, WriteMode}; use rustfmt::config::{Config, ReportTactic};
use rustfmt::rustfmt_diff::*; use rustfmt::rustfmt_diff::*;
const DIFF_CONTEXT_SIZE: usize = 3; const DIFF_CONTEXT_SIZE: usize = 3;
@ -44,7 +44,7 @@ fn system_tests() {
// Turn a DirEntry into a String that represents the relative path to the // Turn a DirEntry into a String that represents the relative path to the
// file. // file.
let files = files.map(get_path_string); let files = files.map(get_path_string);
let (_reports, count, fails) = check_files(files, None); let (_reports, count, fails) = check_files(files);
// Display results. // Display results.
println!("Ran {} system tests.", count); println!("Ran {} system tests.", count);
@ -55,9 +55,9 @@ fn system_tests() {
// the only difference is the coverage mode // the only difference is the coverage mode
#[test] #[test]
fn coverage_tests() { fn coverage_tests() {
let files = fs::read_dir("tests/coverage-source").expect("Couldn't read source dir"); let files = fs::read_dir("tests/coverage/source").expect("Couldn't read source dir");
let files = files.map(get_path_string); let files = files.map(get_path_string);
let (_reports, count, fails) = check_files(files, Some(WriteMode::Coverage)); let (_reports, count, fails) = check_files(files);
println!("Ran {} tests in coverage mode.", count); println!("Ran {} tests in coverage mode.", count);
assert!(fails == 0, "{} tests failed", fails); assert!(fails == 0, "{} tests failed", fails);
@ -65,16 +65,16 @@ fn coverage_tests() {
#[test] #[test]
fn checkstyle_test() { fn checkstyle_test() {
let filename = "tests/source/fn-single-line.rs"; let filename = "tests/writemode/source/fn-single-line.rs";
let expected_filename = "tests/writemode/checkstyle.xml"; let expected_filename = "tests/writemode/target/checkstyle.xml";
assert_output(filename, expected_filename, Some(WriteMode::Checkstyle)); assert_output(filename, expected_filename);
} }
// Helper function for comparing the results of rustfmt // Helper function for comparing the results of rustfmt
// to a known output file generated by one of the write modes. // to a known output file generated by one of the write modes.
fn assert_output(source: &str, expected_filename: &str, write_mode: Option<WriteMode>) { fn assert_output(source: &str, expected_filename: &str) {
let config = read_config(&source, write_mode); let config = read_config(&source);
let (file_map, _report) = format_file(source, &config); let (file_map, _report) = format_file(source, &config);
// Populate output by writing to a vec. // Populate output by writing to a vec.
@ -104,7 +104,7 @@ fn idempotence_tests() {
let files = fs::read_dir("tests/target") let files = fs::read_dir("tests/target")
.expect("Couldn't read target dir") .expect("Couldn't read target dir")
.map(get_path_string); .map(get_path_string);
let (_reports, count, fails) = check_files(files, None); let (_reports, count, fails) = check_files(files);
// Display results. // Display results.
println!("Ran {} idempotent tests.", count); println!("Ran {} idempotent tests.", count);
@ -122,7 +122,7 @@ fn self_tests() {
// Hack because there's no `IntoIterator` impl for `[T; N]`. // Hack because there's no `IntoIterator` impl for `[T; N]`.
let files = files.chain(Some("src/lib.rs".to_owned()).into_iter()); let files = files.chain(Some("src/lib.rs".to_owned()).into_iter());
let (reports, count, fails) = check_files(files, None); let (reports, count, fails) = check_files(files);
let mut warnings = 0; let mut warnings = 0;
// Display results. // Display results.
@ -141,7 +141,7 @@ fn self_tests() {
// For each file, run rustfmt and collect the output. // For each file, run rustfmt and collect the output.
// Returns the number of files checked and the number of failures. // Returns the number of files checked and the number of failures.
fn check_files<I>(files: I, write_mode: Option<WriteMode>) -> (Vec<FormatReport>, u32, u32) fn check_files<I>(files: I) -> (Vec<FormatReport>, u32, u32)
where I: Iterator<Item = String> where I: Iterator<Item = String>
{ {
let mut count = 0; let mut count = 0;
@ -151,7 +151,7 @@ fn check_files<I>(files: I, write_mode: Option<WriteMode>) -> (Vec<FormatReport>
for file_name in files.filter(|f| f.ends_with(".rs")) { for file_name in files.filter(|f| f.ends_with(".rs")) {
println!("Testing '{}'...", file_name); println!("Testing '{}'...", file_name);
match idempotent_check(file_name, write_mode) { match idempotent_check(file_name) {
Ok(report) => reports.push(report), Ok(report) => reports.push(report),
Err(msg) => { Err(msg) => {
print_mismatches(msg); print_mismatches(msg);
@ -176,7 +176,7 @@ fn print_mismatches(result: HashMap<String, Vec<Mismatch>>) {
assert!(t.reset().unwrap()); assert!(t.reset().unwrap());
} }
fn read_config(filename: &str, write_mode: Option<WriteMode>) -> Config { fn read_config(filename: &str) -> Config {
let sig_comments = read_significant_comments(&filename); let sig_comments = read_significant_comments(&filename);
let mut config = get_config(sig_comments.get("config").map(|x| &(*x)[..])); let mut config = get_config(sig_comments.get("config").map(|x| &(*x)[..]));
@ -189,10 +189,6 @@ fn read_config(filename: &str, write_mode: Option<WriteMode>) -> Config {
// Don't generate warnings for to-do items. // Don't generate warnings for to-do items.
config.report_todo = ReportTactic::Never; config.report_todo = ReportTactic::Never;
if let Some(mode) = write_mode {
config.write_mode = mode
}
config config
} }
@ -201,11 +197,9 @@ fn format_file<P: Into<PathBuf>>(filename: P, config: &Config) -> (FileMap, Form
format_input(input, &config) format_input(input, &config)
} }
pub fn idempotent_check(filename: String, pub fn idempotent_check(filename: String) -> Result<FormatReport, HashMap<String, Vec<Mismatch>>> {
write_mode: Option<WriteMode>)
-> Result<FormatReport, HashMap<String, Vec<Mismatch>>> {
let sig_comments = read_significant_comments(&filename); let sig_comments = read_significant_comments(&filename);
let config = read_config(&filename, write_mode); let config = read_config(&filename);
let (file_map, format_report) = format_file(filename, &config); let (file_map, format_report) = format_file(filename, &config);
let mut write_result = HashMap::new(); let mut write_result = HashMap::new();
@ -220,7 +214,7 @@ pub fn idempotent_check(filename: String,
let target = sig_comments.get("target").map(|x| &(*x)[..]); let target = sig_comments.get("target").map(|x| &(*x)[..]);
handle_result(write_result, target, write_mode).map(|_| format_report) handle_result(write_result, target).map(|_| format_report)
} }
// Reads test config file from comments and reads its contents. // Reads test config file from comments and reads its contents.
@ -268,14 +262,13 @@ fn read_significant_comments(file_name: &str) -> HashMap<String, String> {
// Compare output to input. // Compare output to input.
// TODO: needs a better name, more explanation. // TODO: needs a better name, more explanation.
fn handle_result(result: HashMap<String, String>, fn handle_result(result: HashMap<String, String>,
target: Option<&str>, target: Option<&str>)
write_mode: Option<WriteMode>)
-> Result<(), HashMap<String, Vec<Mismatch>>> { -> Result<(), HashMap<String, Vec<Mismatch>>> {
let mut failures = HashMap::new(); let mut failures = HashMap::new();
for (file_name, fmt_text) in result { for (file_name, fmt_text) in result {
// If file is in tests/source, compare to file with same name in tests/target. // If file is in tests/source, compare to file with same name in tests/target.
let target = get_target(&file_name, target, write_mode); let target = get_target(&file_name, target);
let mut f = fs::File::open(&target).expect("Couldn't open target"); let mut f = fs::File::open(&target).expect("Couldn't open target");
let mut text = String::new(); let mut text = String::new();
@ -297,29 +290,20 @@ fn handle_result(result: HashMap<String, String>,
} }
// Map source file paths to their target paths. // Map source file paths to their target paths.
fn get_target(file_name: &str, target: Option<&str>, write_mode: Option<WriteMode>) -> String { fn get_target(file_name: &str, target: Option<&str>) -> String {
let file_path = Path::new(file_name); if file_name.contains("source") {
let (source_path_prefix, target_path_prefix) = match write_mode { let target_file_name = file_name.replace("source", "target");
Some(WriteMode::Coverage) => { if let Some(replace_name) = target {
(Path::new("tests/coverage-source/"), "tests/coverage-target/") Path::new(&target_file_name)
.with_file_name(replace_name)
.into_os_string()
.into_string()
.unwrap()
} else {
target_file_name
} }
_ => (Path::new("tests/source/"), "tests/target/"),
};
if file_path.starts_with(source_path_prefix) {
let mut components = file_path.components();
// Can't skip(2) as the resulting iterator can't as_path()
components.next();
components.next();
let new_target = match components.as_path().to_str() {
Some(string) => string,
None => file_name,
};
let base = target.unwrap_or(new_target);
format!("{}{}", target_path_prefix, base)
} else { } else {
// This is either and idempotence check or a self check
file_name.to_owned() file_name.to_owned()
} }
} }

View File

@ -1,2 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<checkstyle version="4.3"><file name="tests/source/fn-single-line.rs"><error line="1" severity="warning" message="Should be `fn foo_expr() { 1 }`" /><error line="1" severity="warning" message="Should be `fn foo_stmt() { foo(); }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_local() { let z = 5; }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_item(x: &amp;mut i32) { x = 3; }`" /><error line="1" severity="warning" message="Should be `fn empty() {}`" /><error line="1" severity="warning" message="Should be `fn foo_return() -&gt; String { &quot;yay&quot; }`" /><error line="1" severity="warning" message="Should be `fn foo_where() -&gt; T`" /><error line="1" severity="warning" message="Should be ` where T: Sync`" /><error line="1" severity="warning" message="Should be `{`" /><error line="50" severity="warning" message="Should be `fn lots_of_space() { 1 }`" /><error line="57" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `trait CoolerTypes {`" /><error line="57" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `fn Foo&lt;T&gt;() where T: Bar {}`" /></file></checkstyle>

View File

@ -0,0 +1,80 @@
// rustfmt-fn_single_line: true
// rustfmt-write_mode: checkstyle
// Test single-line functions.
fn foo_expr() {
1
}
fn foo_stmt() {
foo();
}
fn foo_decl_local() {
let z = 5;
}
fn foo_decl_item(x: &mut i32) {
x = 3;
}
fn empty() {
}
fn foo_return() -> String {
"yay"
}
fn foo_where() -> T where T: Sync {
let x = 2;
}
fn fooblock() {
{
"inner-block"
}
}
fn fooblock2(x: i32) {
let z = match x {
_ => 2,
};
}
fn comment() {
// this is a test comment
1
}
fn comment2() {
// multi-line comment
let z = 2;
1
}
fn only_comment() {
// Keep this here
}
fn aaaaaaaaaaaaaaaaa_looooooooooooooooooooooong_name() {
let z = "aaaaaaawwwwwwwwwwwwwwwwwwwwwwwwwwww";
}
fn lots_of_space () {
1
}
fn mac() -> Vec<i32> { vec![] }
trait CoolTypes {
fn dummy(&self) {
}
}
trait CoolerTypes { fn dummy(&self) {
}
}
fn Foo<T>() where T: Bar {
}

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<checkstyle version="4.3"><file name="tests/writemode/source/fn-single-line.rs"><error line="2" severity="warning" message="Should be `fn foo_expr() { 1 }`" /><error line="2" severity="warning" message="Should be `fn foo_stmt() { foo(); }`" /><error line="2" severity="warning" message="Should be `fn foo_decl_local() { let z = 5; }`" /><error line="2" severity="warning" message="Should be `fn foo_decl_item(x: &amp;mut i32) { x = 3; }`" /><error line="2" severity="warning" message="Should be `fn empty() {}`" /><error line="2" severity="warning" message="Should be `fn foo_return() -&gt; String { &quot;yay&quot; }`" /><error line="2" severity="warning" message="Should be `fn foo_where() -&gt; T`" /><error line="2" severity="warning" message="Should be ` where T: Sync`" /><error line="2" severity="warning" message="Should be `{`" /><error line="51" severity="warning" message="Should be `fn lots_of_space() { 1 }`" /><error line="58" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="58" severity="warning" message="Should be `trait CoolerTypes {`" /><error line="58" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="58" severity="warning" message="Should be `fn Foo&lt;T&gt;() where T: Bar {}`" /></file></checkstyle>