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 comments_01_trailing_line() {
check(r#"int x = 1; // note"#, r#"int x = 1; // note"#);
}
#[test]
fn comments_02_trailing_block() {
check(r#"int x = 1; /* note */"#, r#"int x = 1; /* note */"#);
}
#[test]
fn comments_03_own_line() {
check(
r#"// heading
int x = 1;"#,
r#"// heading
int x = 1;"#,
);
}
#[test]
fn comments_04_between() {
check(
r#"void f() {
// step
g();
}"#,
r#"void f() {
// step
g();
}"#,
);
}
#[test]
fn comments_05_doc() {
check(
r#"/// doc
int f();"#,
r#"/// doc
int f();"#,
);
}
#[test]
fn comments_06_string() {
check(
r#"const char* s = "// not a comment";"#,
r#"const char *s = "// not a comment";"#,
);
}
#[test]
fn comments_13_prefix_spacing() {
check(
"//using enum X;\n///doc\n//NOLINT\nvoid f() {\n int x = 1; //trailing\n}\n",
"// using enum X;\n/// doc\n// NOLINT\nvoid f() {\n int x = 1; // trailing\n}\n",
);
}
#[test]
fn comments_14_trailing_comment_then_an_enum_item() {
check(
"enum E {\n A, // one\n B, ///<two\n};\n",
"enum E {\n A, // one\n B, ///< two\n};\n",
);
}