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 an_integer_array_is_a_table_on_the_brace_line() {
let input = "static const int n[] = {111, 2222, 33, 44444, 5, 666, 7777, 88, 999, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};\n";
let expected = "\
static const int n[] = {111, 2222, 33, 44444, 5, 666, 7777, 88, 999,
10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25};
";
check(input, expected);
}
#[test]
fn the_operator_names_are_five_by_five_on_the_next_line() {
let mut input = String::from("std::set<std::string, std::less<>> X = {\n");
input.push_str(" \"__add\", \"__sub\", \"__mul\", \"__div\", \"__mod\", \"__pow\",\n");
input.push_str(" \"__unm\", \"__idiv\", \"__band\", \"__bor\", \"__bxor\", \"__bnot\", \"__shl\",\n");
input.push_str(" \"__shr\", \"__concat\", \"__len\", \"__eq\", \"__lt\", \"__index\", \"__newindex\",\n");
input.push_str(" \"__call\", \"__gc\", \"__close\", \"__mode\", \"__name\"};\n");
let expected = "\
std::set<std::string, std::less<>> X = {
\"__add\", \"__sub\", \"__mul\", \"__div\", \"__mod\",
\"__pow\", \"__unm\", \"__idiv\", \"__band\", \"__bor\",
\"__bxor\", \"__bnot\", \"__shl\", \"__shr\", \"__concat\",
\"__len\", \"__eq\", \"__lt\", \"__index\", \"__newindex\",
\"__call\", \"__gc\", \"__close\", \"__mode\", \"__name\"};
";
check(&input, expected);
}
#[test]
fn a_fitting_list_is_one_line() {
check(
"static const int n[] = {1, 2, 3};\n",
"static const int n[] = {1, 2, 3};\n",
);
}