use cpp_formatter::config::Style;
#[track_caller]
fn check(input: &str, expected: &str) {
let style = style();
let path = std::path::Path::new("snippet.cpp");
let actual = cpp_formatter::format_file(path, input, &style);
let actual = actual.trim_end_matches('\n');
let expected = expected.trim_end_matches('\n');
assert_eq!(
actual, expected,
"formatting drifted\ninput:\n{input}\nexpected:\n{expected}\nactual:\n{actual}"
);
let again = cpp_formatter::format_file(path, expected, &style);
assert_eq!(again, expected, "the expected text is not a fixed point");
}
fn style() -> Style {
let mut style = Style::preset("LLVM").expect("LLVM is a preset");
style.set_from_text("IndentWidth", "4");
style.set_from_text("ContinuationIndentWidth", "8");
style.set_from_text("ColumnLimit", "0");
style
}
#[test]
fn spacing_01_call() {
check(r#"int x=f(a,b);"#, r#"int x = f(a, b);"#);
}
#[test]
fn spacing_02_pointer() {
check(r#"int *p = &x;"#, r#"int *p = &x;"#);
}
#[test]
fn spacing_03_cast() {
check(r#"void f() { (void)i; }"#, r#"void f() { (void)i; }"#);
}
#[test]
fn spacing_04_operators() {
check(r#"int y = a+b*c-d/e;"#, r#"int y = a + b * c - d / e;"#);
}
#[test]
fn spacing_05_compare() {
check(
r#"bool ok = a==b && c!=d;"#,
r#"bool ok = a == b && c != d;"#,
);
}
#[test]
fn spacing_06_template_args() {
check(
r#"std::map<int,std::string> m;"#,
r#"std::map<int, std::string> m;"#,
);
}
#[test]
fn spacing_07_angle_space() {
check(r#"std::vector<int> v;"#, r#"std::vector<int> v;"#);
}
#[test]
fn spacing_08_lambda_bracket() {
check(
r#"auto f =[&](int a){ return a; };"#,
r#"auto f = [&](int a) { return a; };"#,
);
}
#[test]
fn spacing_09_braced_list() {
check(r#"int a[3] = {1,2,3};"#, r#"int a[3] = {1, 2, 3};"#);
}
#[test]
fn spacing_10_ternary() {
check(r#"int t = a ? b : c;"#, r#"int t = a ? b : c;"#);
}