use cpp_formatter::config::Style;
const INPUT: &str = "\
void a() {
g();
}
void b() {
g();
h();
}
class C {
void m() {
g();
}
};
void d() {
g();
} // trailing
void e() {
f([](int v) {
return v;
});
}
";
fn check(style: Style, 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\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 all_joins_every_fitting_body() {
let expected = "\
void a() { g(); }
void b() {
g();
h();
}
class C {
void m() { g(); }
};
void d() { g(); } // trailing
void e() {
f([](int v) { return v; });
}
";
check(Style::preset("LLVM").expect("LLVM is a preset"), expected);
}
#[test]
fn none_splits_a_one_line_body() {
let expected = "\
void a() {
g();
}
void b() {
g();
h();
}
class C {
void m() {
g();
}
};
void d() {
g();
} // trailing
void e() {
f([](int v) { return v; });
}
";
let mut style = Style::preset("LLVM").expect("LLVM is a preset");
style.set_from_text("AllowShortFunctionsOnASingleLine", "None");
check(style, expected);
}
#[test]
fn inline_joins_only_class_methods() {
let expected = "\
void a() {
g();
}
void b() {
g();
h();
}
class C {
void m() { g(); }
};
void d() {
g();
} // trailing
void e() {
f([](int v) { return v; });
}
";
let mut style = Style::preset("LLVM").expect("LLVM is a preset");
style.set_from_text("AllowShortFunctionsOnASingleLine", "Inline");
check(style, expected);
}