cpp_formatter 0.4.0

A C++ formatter with clang-format's configuration surface.
Documentation
//! **Category: classes** — inline cases, no files, no clang-format at test time.
//!
//! Each case is a pair of strings: the input as written, and the text this formatter must produce for it. The pairs
//! were taken from clang-format 22.1.3 for the same style that `style()` documents, so the expected side is the
//! oracle's answer rather than whatever this implementation happened to print.

use cpp_formatter::config::Style;

/// Format `input` with this project's style and compare against the oracle's answer, then re-format the answer.
///
/// The second half is the property a user feels: formatting text that is already formatted has to leave it alone.
#[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);
    // The trailing newline is the *source's* (the formatter preserves its absence), so the cases compare the
    // content and leave the file's final byte to `trim_end_matches` on both sides.
    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");
}

/// The style the expected strings were produced with.
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
}

/// `07_members_block`
#[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;
};"#,
    );
}

/// `08_ctor_block`
#[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_;
};"#,
    );
}

/// `09_inherit_block`
#[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;
};"#,
    );
}

/// `10_enum_block`
#[test]
fn classes_10_enum_block() {
    check(
        r#"enum class E {
    A,
    B,
};"#,
        r#"enum class E {
    A,
    B,
};"#,
    );
}

/// `11_operator_block`
#[test]
fn classes_11_operator_block() {
    check(
        r#"struct S {
    S &operator=(const S &) = default;
};"#,
        r#"struct S {
    S &operator=(const S &) = default;
};"#,
    );
}

/// `12_template_member`
#[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);
};"#,
    );
}

/// **`= default` is not a `default:` label, and a template head is a line of its own** — both measured on the
/// nlohmann json corpus under `Google` (where the two styles' `AccessModifierOffset`/`IndentWidth` made the label
/// mistake visible: every member after `C() = default;` came out two columns too deep).
#[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
    );
}

/// **A template head that spans lines aligns its arguments under the `<`**, and the declaration after it starts a new
/// line at the block's column — clang-format 23.1.5 (`LLVM`) writes this fixture byte for byte. Two rules were missing:
/// `template <typename …>` / `template <class …>` did not mark the angle at all, and the "the line after a template's
/// `>` is a new declaration" test looked at the line's start instead of the declaration's.
#[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);
}