use std::fs;
use ec4rs::{
PropertyKey,
property::{self, Charset},
};
use line_ending::LineEnding;
use super::super::IndentationHandler;
use crate::test_utils::{TestFileHelper, indentation::get_infos_from_file_name};
#[test]
fn test_fix_indentations_on_test_files_for_indent_style() {
test_fix_indentations_on_test_files::<property::IndentStyle>();
}
#[test]
fn test_fix_indentations_on_test_files_for_indent_size() {
test_fix_indentations_on_test_files::<property::IndentSize>();
}
fn test_fix_indentations_on_test_files<P: PropertyKey>() {
for test_entry in TestFileHelper::new::<P>().get_test_file_paths() {
let (indent_style, indent_size, tab_width, actual_lines_with_wrong_indentation) =
get_infos_from_file_name(&test_entry);
let handler = IndentationHandler {
charset: Charset::Utf8, line_ending: LineEnding::from_current_platform(), indent_style: Some(indent_style),
indent_size: Some(indent_size),
tab_width: Some(tab_width),
};
let expect_change = !actual_lines_with_wrong_indentation.is_empty();
let mut content = fs::read_to_string(&test_entry).expect("Test file needs to be readable");
let content_orginal = content.clone();
let changed = handler.fix_indentations(&mut content);
assert_eq!(
changed,
content != content_orginal,
"The return value has to indicate if the content was changed or not"
);
assert_eq!(
changed, expect_change,
"The fix has to change if and only if there are lines with wrong indentation"
);
if changed {
assert!(
handler
.get_lines_with_wrong_indentation(&content)
.next()
.is_none(),
"There are still wrong indented lines after the fix",
)
}
}
}