Merge pull request #67 from marcusklaas/test-diff

Show diffs for failing tests
This commit is contained in:
Nick Cameron 2015-05-13 13:03:11 +12:00
commit 5286663fe2
3 changed files with 41 additions and 0 deletions

6
Cargo.lock generated
View file

@ -2,9 +2,15 @@
name = "rustfmt"
version = "0.0.1"
dependencies = [
"diff 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"strings 0.0.1 (git+https://github.com/nrc/strings.rs.git)",
]
[[package]]
name = "diff"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "strings"
version = "0.0.1"

View file

@ -11,3 +11,6 @@ license = "Apache-2.0/MIT"
[dependencies.strings]
strings = "0.0.1"
git = "https://github.com/nrc/strings.rs.git"
[dev-dependencies]
diff = "0.1.0"

View file

@ -11,6 +11,7 @@
#![feature(catch_panic)]
extern crate rustfmt;
extern crate diff;
use std::collections::HashMap;
use std::fs;
@ -89,6 +90,7 @@ fn handle_result(result: HashMap<String, String>) {
// TODO: speedup by running through bytes iterator
f.read_to_string(&mut text).unwrap();
if fmt_text != text {
show_diff(&file_name, &fmt_text, &text);
failures.insert(file_name, fmt_text);
}
}
@ -96,3 +98,33 @@ fn handle_result(result: HashMap<String, String>) {
panic!(failures);
}
}
fn show_diff(file_name: &str, expected: &str, actual: &str) {
let mut line_number = 1;
let mut prev_both = true;
for result in diff::lines(expected, actual) {
match result {
diff::Result::Left(str) => {
if prev_both {
println!("Mismatch @ {}:{}", file_name, line_number);
}
println!("-{}", str);
prev_both = false;
}
diff::Result::Right(str) => {
if prev_both {
println!("Mismatch @ {}:{}", file_name, line_number);
}
println!("+{}", str);
prev_both = false;
line_number += 1;
}
diff::Result::Both(..) => {
line_number += 1;
prev_both = true;
}
}
}
}