use cpp_formatter::config::Style;
fn style() -> Style {
let mut style = Style::preset("LLVM").expect("LLVM is a preset");
style.set_from_text("IndentWidth", "2");
style.set_from_text("ContinuationIndentWidth", "4");
style
}
fn check(input: &str, expected: &str) {
let path = std::path::Path::new("snippet.cpp");
let actual = cpp_formatter::format_file(path, input, &style());
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");
}
#[test]
fn a_fitting_statement_joins() {
check(
"void f() {\n int x = foo(a,\n b);\n auto y = obj.method(1,\n 2);\n}\n",
"void f() {\n int x = foo(a, b);\n auto y = obj.method(1, 2);\n}\n",
);
}
#[test]
fn a_statement_that_does_not_fit_keeps_its_lines() {
let input = "void f() {\n int x = some_function_with_a_long_name(first_argument, second_argument,\n third_argument, fourth_argument);\n}\n";
check(input, input);
}
#[test]
fn a_statement_with_a_comment_is_not_joined() {
let input = "void f() {\n int x = foo(a, // why\n b);\n}\n";
check(input, input);
}
#[test]
fn a_statement_with_a_brace_is_not_joined() {
let input = "int values[] = {1,\n 2};\n";
check(input, input);
}
#[test]
fn a_run_of_macro_invocations_is_not_joined() {
let input = "void f() {\n NUMBER_OPTION(a)\n NUMBER_OPTION(b)\n}\n";
check(input, input);
}
#[test]
fn a_labeled_statement_is_not_joined() {
let input = "int f() {\nendLoop:\n return words;\n}\n";
check(input, input);
}