Extract helper functions for testing.
These functions help reduce duplication in the test harness and make it easier to add tests for other write-modes in the future.
This commit is contained in:
parent
9c275833fc
commit
e9e5621307
1 changed files with 27 additions and 24 deletions
|
@ -19,7 +19,7 @@ use std::io::{self, Read, BufRead, BufReader};
|
|||
use std::path::Path;
|
||||
|
||||
use rustfmt::*;
|
||||
use rustfmt::filemap::write_system_newlines;
|
||||
use rustfmt::filemap::{write_system_newlines, FileMap};
|
||||
use rustfmt::config::{Config, ReportTactic, WriteMode};
|
||||
use rustfmt::rustfmt_diff::*;
|
||||
|
||||
|
@ -65,12 +65,24 @@ fn coverage_tests() {
|
|||
|
||||
#[test]
|
||||
fn checkstyle_test() {
|
||||
let filename = "tests/source/fn-single-line.rs".to_string();
|
||||
let expected = "tests/writemode/checkstyle.xml";
|
||||
let filename = "tests/source/fn-single-line.rs";
|
||||
let expected_filename = "tests/writemode/checkstyle.xml";
|
||||
assert_output(filename, expected_filename, WriteMode::Checkstyle);
|
||||
}
|
||||
|
||||
let output = run_rustfmt(filename.clone(), WriteMode::Checkstyle);
|
||||
|
||||
let mut expected_file = fs::File::open(&expected)
|
||||
// Helper function for comparing the results of rustfmt
|
||||
// to a known output file generated by one of the write modes.
|
||||
fn assert_output(source: &str, expected_filename: &str, write_mode: WriteMode) {
|
||||
let config = read_config(&source);
|
||||
let file_map = run_rustfmt(source.to_string(), write_mode);
|
||||
|
||||
// Populate output by writing to a vec.
|
||||
let mut out = vec![];
|
||||
let _ = filemap::write_all_files(&file_map, &mut out, write_mode, &config);
|
||||
let output = String::from_utf8(out).unwrap();
|
||||
|
||||
let mut expected_file = fs::File::open(&expected_filename)
|
||||
.ok()
|
||||
.expect("Couldn't open target.");
|
||||
let mut expected_text = String::new();
|
||||
|
@ -81,7 +93,7 @@ fn checkstyle_test() {
|
|||
let compare = make_diff(&expected_text, &output, DIFF_CONTEXT_SIZE);
|
||||
if compare.len() > 0 {
|
||||
let mut failures = HashMap::new();
|
||||
failures.insert(filename, compare);
|
||||
failures.insert(source.to_string(), compare);
|
||||
print_mismatches(failures);
|
||||
assert!(false, "Text does not match expected output");
|
||||
}
|
||||
|
@ -169,7 +181,7 @@ fn print_mismatches(result: HashMap<String, Vec<Mismatch>>) {
|
|||
assert!(t.reset().unwrap());
|
||||
}
|
||||
|
||||
pub fn run_rustfmt(filename: String, write_mode: WriteMode) -> String {
|
||||
fn read_config(filename: &str) -> Config {
|
||||
let sig_comments = read_significant_comments(&filename);
|
||||
let mut config = get_config(sig_comments.get("config").map(|x| &(*x)[..]));
|
||||
|
||||
|
@ -181,30 +193,21 @@ pub fn run_rustfmt(filename: String, write_mode: WriteMode) -> String {
|
|||
|
||||
// Don't generate warnings for to-do items.
|
||||
config.report_todo = ReportTactic::Never;
|
||||
config
|
||||
}
|
||||
|
||||
// Simulate run()
|
||||
let mut out = Vec::new();
|
||||
let file_map = format(Path::new(&filename), &config, write_mode);
|
||||
let _ = filemap::write_all_files(&file_map, &mut out, write_mode, &config);
|
||||
String::from_utf8(out).unwrap()
|
||||
// Simulate run()
|
||||
fn run_rustfmt(filename: String, write_mode: WriteMode) -> FileMap {
|
||||
let config = read_config(&filename);
|
||||
format(Path::new(&filename), &config, write_mode)
|
||||
}
|
||||
|
||||
pub fn idempotent_check(filename: String,
|
||||
write_mode: WriteMode)
|
||||
-> Result<FormatReport, HashMap<String, Vec<Mismatch>>> {
|
||||
let sig_comments = read_significant_comments(&filename);
|
||||
let mut config = get_config(sig_comments.get("config").map(|x| &(*x)[..]));
|
||||
|
||||
for (key, val) in &sig_comments {
|
||||
if key != "target" && key != "config" {
|
||||
config.override_value(key, val);
|
||||
}
|
||||
}
|
||||
|
||||
// Don't generate warnings for to-do items.
|
||||
config.report_todo = ReportTactic::Never;
|
||||
|
||||
let mut file_map = format(Path::new(&filename), &config, write_mode);
|
||||
let config = read_config(&filename);
|
||||
let mut file_map = run_rustfmt(filename, write_mode);
|
||||
let format_report = fmt_lines(&mut file_map, &config);
|
||||
|
||||
let mut write_result = HashMap::new();
|
||||
|
|
Loading…
Reference in a new issue