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 statements_05_return() {
check(r#"int f() { return; }"#, r#"int f() { return; }"#);
}
#[test]
fn statements_07_if_block() {
check(
r#"void f(int x) {
if (x>0) {
g();
} else {
h();
}
}"#,
r#"void f(int x) {
if (x > 0) {
g();
} else {
h();
}
}"#,
);
}
#[test]
fn statements_08_for_block() {
check(
r#"void f() {
for (int i=0; i<3; ++i) {
g(i);
}
}"#,
r#"void f() {
for (int i = 0; i < 3; ++i) {
g(i);
}
}"#,
);
}
#[test]
fn statements_09_switch_block() {
check(
r#"void f(int k) {
switch (k) {
case 1:
break;
default:
break;
}
}"#,
r#"void f(int k) {
switch (k) {
case 1:
break;
default:
break;
}
}"#,
);
}
#[test]
fn statements_10_while_block() {
check(
r#"void f() {
while (x>0) {
--x;
}
}"#,
r#"void f() {
while (x > 0) {
--x;
}
}"#,
);
}
#[test]
fn statements_11_return_block() {
check(
r#"int f() {
return 0;
}"#,
r#"int f() {
return 0;
}"#,
);
}
#[test]
fn statements_12_ternary_block() {
check(
r#"int f(int a) {
return a>0 ? a : -a;
}"#,
r#"int f(int a) {
return a > 0 ? a : -a;
}"#,
);
}