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 classes_07_members_block() {
check(
r#"class A {
public:
int x;
private:
int y;
};"#,
r#"class A {
public:
int x;
private:
int y;
};"#,
);
}
#[test]
fn classes_08_ctor_block() {
check(
r#"class B {
public:
B() : x_(0) {}
int x_;
};"#,
r#"class B {
public:
B() : x_(0) {}
int x_;
};"#,
);
}
#[test]
fn classes_09_inherit_block() {
check(
r#"class C : public A {
public:
void f() override;
};"#,
r#"class C : public A {
public:
void f() override;
};"#,
);
}
#[test]
fn classes_10_enum_block() {
check(
r#"enum class E {
A,
B,
};"#,
r#"enum class E {
A,
B,
};"#,
);
}
#[test]
fn classes_11_operator_block() {
check(
r#"struct S {
S &operator=(const S &) = default;
};"#,
r#"struct S {
S &operator=(const S &) = default;
};"#,
);
}
#[test]
fn classes_12_template_member() {
check(
r#"class D {
public:
template <typename T>
void f(T v);
};"#,
r#"class D {
public:
template <typename T>
void f(T v);
};"#,
);
}
#[test]
fn classes_20_an_equality_default_is_not_a_label() {
let mut google = Style::preset("Google").expect("Google is a preset");
google.set_from_text("ColumnLimit", "80");
let path = std::path::Path::new("snippet.cpp");
let input = "class C {\n public:\n C(int p) : x_(p) {}\n C() = default;\n int x_;\n};\n";
assert_eq!(cpp_formatter::format_file(path, input, &google), input);
let template = "template <typename T>\nclass lexer_base {\n public:\n int x;\n};\n";
assert_eq!(
cpp_formatter::format_file(path, template, &google),
template
);
}
#[test]
fn classes_21_a_template_head_that_spans_lines() {
let llvm = Style::preset("LLVM").expect("LLVM is a preset");
let path = std::path::Path::new("snippet.cpp");
let input = "template <typename BasicJsonType, typename InputAdapterType,\n bool Collect = false,\n typename LexerType = lexer<BasicJsonType, InputAdapterType>>\nclass json_sax_dom_parser : public json_sax_dom_error_state {\npublic:\n void f();\n};\n";
assert_eq!(cpp_formatter::format_file(path, input, &llvm), input);
}