use cpp_formatter::config::Style;
#[track_caller]
fn check(input: &str, expected: &str) {
check_with(style(), input, expected);
}
#[track_caller]
fn check_with(style: Style, input: &str, expected: &str) {
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", "2");
style.set_from_text("ContinuationIndentWidth", "4");
style
}
#[test]
fn an_empty_function_body_is_one_pair_of_braces() {
check("void f() {\n}\n", "void f() {}\n");
}
#[test]
fn an_empty_constructor_body_is_one_pair_of_braces() {
check(
"class T {\npublic:\n T() {\n }\n};\n",
"class T {\npublic:\n T() {}\n};\n",
);
}
#[test]
fn an_empty_if_body_is_a_brace_pair_on_two_lines() {
check(
"void f() {\n if (x) {\n }\n}\n",
"void f() {\n if (x) {\n }\n}\n",
);
check(
"void f() {\n if (x) {}\n}\n",
"void f() {\n if (x) {\n }\n}\n",
);
}
#[test]
fn an_empty_record_is_one_pair_of_braces() {
check("struct S {\n};\n", "struct S {};\n");
}
#[test]
fn a_block_holding_a_comment_keeps_its_lines() {
let input = "void f() {\n /* nothing */\n}\n";
let formatted =
cpp_formatter::format_file(std::path::Path::new("snippet.cpp"), input, &style());
assert!(
formatted.contains("{\n /* nothing */\n}"),
"a comment is not an empty block: {formatted:?}"
);
}
#[test]
fn without_a_column_limit_an_empty_body_keeps_the_authors_lines() {
let mut unlimited = style();
unlimited.set_from_text("ColumnLimit", "0");
check_with(unlimited.clone(), "void f() {\n}\n", "void f() {\n}\n");
check_with(unlimited, "void f() {}\n", "void f() {}\n");
}
#[test]
fn blocks_21_an_unbraced_control_body() {
check(
"static int f(int level) {\n if (level == 0)\n return 0;\n return 1;\n}\n",
"static int f(int level) {\n if (level == 0)\n return 0;\n return 1;\n}\n",
);
check(
"void f(int a, int b) {\n if (a)\n if (b)\n g();\n h();\n}\n",
"void f(int a, int b) {\n if (a)\n if (b)\n g();\n h();\n}\n",
);
check(
"void f() {\n do {\n g();\n } while (x);\n h();\n}\n",
"void f() {\n do {\n g();\n } while (x);\n h();\n}\n",
);
}
#[test]
fn blocks_22_short_namespace_lines() {
check(
"namespace detail {\nvoid f();\n}\n",
"namespace detail {\nvoid f();\n}\n",
);
check(
"namespace detail {\nvoid f();\nvoid g();\n}\n",
"namespace detail {\nvoid f();\nvoid g();\n} // namespace detail\n",
);
}
#[test]
fn blocks_23_a_break_for_the_sake_of_a_trailing_comment() {
check(
"class json_sax_dom_error_state {\npublic:\n json_sax_dom_error_state(json_sax_dom_error_state &&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)\n};\n",
"class json_sax_dom_error_state {\npublic:\n json_sax_dom_error_state(json_sax_dom_error_state &&) =\n default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)\n};\n",
);
}