use cpp_formatter::config::Style;
fn check(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");
}
#[test]
fn microsoft_wraps_by_construct() {
let input = "\
namespace ns {
class Widget {
public:
void run();
};
union U {
int i;
};
enum class Kind {
A,
B
};
void Widget::run() {
if (x) {
y();
} else {
z();
}
do {
v();
} while (u);
struct S {
};
}
}
";
let expected = "\
namespace ns
{
class Widget
{
public:
void run();
};
union U {
int i;
};
enum class Kind
{
A,
B
};
void Widget::run()
{
if (x)
{
y();
}
else
{
z();
}
do
{
v();
} while (u);
struct S
{
};
}
} // namespace ns
";
check(
Style::preset("Microsoft").expect("Microsoft is a preset"),
input,
expected,
);
}
#[test]
fn a_case_body_brace_is_attached_where_the_style_says() {
let input =
"void f() {\n switch (x) {\n case 1: {\n g();\n break;\n }\n }\n}\n";
let microsoft = "\
void f()
{
switch (x)
{
case 1: {
g();
break;
}
}
}
";
check(
Style::preset("Microsoft").expect("Microsoft is a preset"),
input,
microsoft,
);
}
#[test]
fn an_empty_body_is_placed_by_its_style() {
let input = "void f() {}\nstruct S {};\nunion U {};\n";
check(
Style::preset("LLVM").expect("LLVM is a preset"),
input,
input,
);
check(
Style::preset("Microsoft").expect("Microsoft is a preset"),
input,
"void f()\n{\n}\nstruct S\n{\n};\nunion U {};\n",
);
let mut allman = Style::preset("LLVM").expect("LLVM is a preset");
allman.set_from_text("BreakBeforeBraces", "Allman");
check(
allman,
input,
"void f() {}\nstruct S\n{\n};\nunion U\n{\n};\n",
);
}
#[test]
fn an_empty_control_body_is_split_in_every_style() {
let input = "void f() {\n if (x) {}\n try {} catch (...) {}\n auto g = []() {};\n struct T {};\n}\n";
let llvm = "\
void f() {
if (x) {
}
try {
} catch (...) {
}
auto g = []() {};
struct T {};
}
";
check(
Style::preset("LLVM").expect("LLVM is a preset"),
input,
llvm,
);
let microsoft = "\
void f()
{
if (x)
{
}
try
{
}
catch (...)
{
}
auto g = []() {};
struct T
{
};
}
";
check(
Style::preset("Microsoft").expect("Microsoft is a preset"),
input,
microsoft,
);
}
#[test]
fn the_files_own_header_sorts_first() {
let input = "#include \"b.h\"\n#include \"a.h\"\n";
let style = Style::preset("LLVM").expect("LLVM is a preset");
let named = cpp_formatter::format_named(input, Some("b"), &style);
assert_eq!(named, "#include \"b.h\"\n#include \"a.h\"\n");
let unnamed = cpp_formatter::format_named(input, None, &style);
assert_eq!(unnamed, "#include \"a.h\"\n#include \"b.h\"\n");
}
#[test]
fn attach_joins_only_when_the_style_has_a_column_limit() {
let input = "void f()\n{\n g();\n}\n";
let mut unlimited = Style::preset("LLVM").expect("LLVM is a preset");
unlimited.set_from_text("ColumnLimit", "0");
unlimited.set_from_text("AllowShortFunctionsOnASingleLine", "None");
check(unlimited, input, input);
let mut limited = Style::preset("LLVM").expect("LLVM is a preset");
limited.set_from_text("ColumnLimit", "80");
limited.set_from_text("AllowShortFunctionsOnASingleLine", "None");
check(limited, input, "void f() {\n g();\n}\n");
}
#[test]
fn a_case_body_brace_stands_at_the_label_under_allman() {
let input = "\
void f(int v) {
switch (v) {
case 1: {
g();
break;
}
}
}
";
let expected = "\
void f(int v)
{
switch (v)
{
case 1:
{
g();
break;
}
}
}
";
let mut allman = Style::preset("LLVM").expect("LLVM is a preset");
allman.set_from_text("BreakBeforeBraces", "Allman");
check(allman, input, expected);
}
#[test]
fn wrapping_20_a_control_brace_inside_a_case_body() {
let mut microsoft = Style::preset("Microsoft").expect("Microsoft is a preset");
microsoft.set_from_text("ColumnLimit", "80");
let path = std::path::Path::new("snippet.cpp");
let input = "void f(int x)\n{\n switch (x)\n {\n default:\n if (JSON_HEDLEY_UNLIKELY(!scalar(p)))\n {\n return false;\n }\n goto done;\n }\n}\n";
assert_eq!(cpp_formatter::format_file(path, input, µsoft), input);
}
#[test]
fn wrapping_21_a_braced_case_body_after_a_comment() {
let mut microsoft = Style::preset("Microsoft").expect("Microsoft is a preset");
microsoft.set_from_text("ColumnLimit", "80");
let path = std::path::Path::new("snippet.cpp");
let input = "void f(int x)\n{\n switch (x)\n {\n case 1:\n break;\n default: // end\n {\n goto endLoop;\n }\n }\n}\n";
assert_eq!(cpp_formatter::format_file(path, input, µsoft), input);
}