cpp_formatter 0.4.0

A C++ formatter with clang-format's configuration surface.
Documentation
//! **Generated by `examples/option_matrix.rs`** — do not edit by hand.
//!
//! One test per style option that this formatter honours *and* whose effect clang-format 22.1.3 shows on the
//! sink named in the test. The expected text is clang-format's, so each test is a promise about the option
//! rather than a snapshot of whatever this implementation happened to print.
//!
//! Regenerate with:
//!
//! ```text
//! cargo run --release -p cpp_formatter --example option_matrix -- <clang-format> --write
//! ```

use cpp_formatter::config::Style;

/// The sink the cases of this name were recorded on.
const SINK_MAIN: &str = r##"#include "local.h"
#include <vector>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define LONG(a)    \
    do {           \
        (void)(a); \
    } while (0)
namespace ns {
enum class Kind { A,
                  B };
class Widget : public Base {
  public:
    Widget() : a_(0), b_(1) {}
    explicit Widget(int a) : a_(a) {}
    virtual ~Widget() = default;
    void run() override;
    int get() const { return a_; }

  protected:
    int a_;

  private:
    int b_;
};
template <typename T>
concept Sized = requires(T t) { t.size(); };
template <typename T, int N = 3>
struct Buffer {
    T items[N];
};
void Widget::run() {
    int x = 1;
    int *p = &x;
    const int &r = x;
    char c = 'A';
    const char *s = "hello";
    if (x > 0) {
        x = 2;
    } else {
        x = 3;
    }
    switch (x) {
    case 1:
        break;
    default:
        break;
    }
    auto f = [&](int a) -> int { return a + x; };
    std::vector<int> v = {1, 2, 3};
    int y = 0x1F + 1000 + 1500; // after a statement
    /* block */ y += 1;
    (void)p;
    (void)r;
    (void)c;
    (void)s;
    (void)f;
    (void)v;
    (void)y;
}
} // namespace ns
"##;

/// The sink the cases of this name were recorded on.
const SINK_SPACING: &str = r##"void f() {
    int *p = 0;
    int &r = *p;
    int x = (int)1.5;
    char c = 'a';
    bool b = !x;
    int m = a ? b : c;
    int t = 1;
    int u = -t;
    auto g = [&](int v) -> int { return v; };
    struct S {};
    if (b) {
        g(t);
    }
    (void)r;
    (void)c;
    (void)m;
    (void)u;
}
"##;

/// Format `sink` under `configuration` and compare with the oracle's answer, then re-format that answer.
#[track_caller]
fn check_against_the_oracle(sink: &str, configuration: &str, expected: &str) {
    let style = style(configuration);
    let path = std::path::Path::new("sink.cpp");
    let actual = cpp_formatter::format_file(path, sink, &style);
    assert_eq!(
        actual, expected,
        "the option is not honoured the way clang-format honours it"
    );
    let again = cpp_formatter::format_file(path, expected, &style);
    assert_eq!(again, expected, "the oracle's text is not a fixed point");
}

/// The base style plus one option: the string spells out what the test exercises.
fn style(configuration: &str) -> Style {
    let mut style = Style::preset("LLVM").expect("LLVM is a preset");
    // The base the case was recorded with, so the option under test is the only difference.
    style.set_from_text("IndentWidth", "4");
    style.set_from_text("ContinuationIndentWidth", "8");
    style.set_from_text("ColumnLimit", "0");
    for line in configuration.lines() {
        if let Some((key, value)) = line.split_once(':') {
            style.set_from_text(key.trim(), value.trim());
        }
    }
    style
}

/// `AccessModifierOffset: 2` on the `main` sink
#[test]
fn option_accessmodifieroffset() {
    check_against_the_oracle(
        SINK_MAIN,
        r#"AccessModifierOffset: 2"#,
        r#"#include "local.h"
#include <vector>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define LONG(a)    \
    do {           \
        (void)(a); \
    } while (0)
namespace ns {
enum class Kind { A,
                  B };
class Widget : public Base {
      public:
    Widget() : a_(0), b_(1) {}
    explicit Widget(int a) : a_(a) {}
    virtual ~Widget() = default;
    void run() override;
    int get() const { return a_; }

      protected:
    int a_;

      private:
    int b_;
};
template <typename T>
concept Sized = requires(T t) { t.size(); };
template <typename T, int N = 3>
struct Buffer {
    T items[N];
};
void Widget::run() {
    int x = 1;
    int *p = &x;
    const int &r = x;
    char c = 'A';
    const char *s = "hello";
    if (x > 0) {
        x = 2;
    } else {
        x = 3;
    }
    switch (x) {
    case 1:
        break;
    default:
        break;
    }
    auto f = [&](int a) -> int { return a + x; };
    std::vector<int> v = {1, 2, 3};
    int y = 0x1F + 1000 + 1500; // after a statement
    /* block */ y += 1;
    (void)p;
    (void)r;
    (void)c;
    (void)s;
    (void)f;
    (void)v;
    (void)y;
}
} // namespace ns
"#,
    );
}

/// `PointerAlignment: Left` on the `spacing` sink
#[test]
fn option_pointeralignment() {
    check_against_the_oracle(
        SINK_SPACING,
        r#"PointerAlignment: Left"#,
        r#"void f() {
    int* p = 0;
    int& r = *p;
    int x = (int)1.5;
    char c = 'a';
    bool b = !x;
    int m = a ? b : c;
    int t = 1;
    int u = -t;
    auto g = [&](int v) -> int { return v; };
    struct S {};
    if (b) {
        g(t);
    }
    (void)r;
    (void)c;
    (void)m;
    (void)u;
}
"#,
    );
}

/// `SpaceAfterTemplateKeyword: false` on the `main` sink
#[test]
fn option_spaceaftertemplatekeyword() {
    check_against_the_oracle(
        SINK_MAIN,
        r#"SpaceAfterTemplateKeyword: false"#,
        r#"#include "local.h"
#include <vector>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define LONG(a)    \
    do {           \
        (void)(a); \
    } while (0)
namespace ns {
enum class Kind { A,
                  B };
class Widget : public Base {
  public:
    Widget() : a_(0), b_(1) {}
    explicit Widget(int a) : a_(a) {}
    virtual ~Widget() = default;
    void run() override;
    int get() const { return a_; }

  protected:
    int a_;

  private:
    int b_;
};
template<typename T>
concept Sized = requires(T t) { t.size(); };
template<typename T, int N = 3>
struct Buffer {
    T items[N];
};
void Widget::run() {
    int x = 1;
    int *p = &x;
    const int &r = x;
    char c = 'A';
    const char *s = "hello";
    if (x > 0) {
        x = 2;
    } else {
        x = 3;
    }
    switch (x) {
    case 1:
        break;
    default:
        break;
    }
    auto f = [&](int a) -> int { return a + x; };
    std::vector<int> v = {1, 2, 3};
    int y = 0x1F + 1000 + 1500; // after a statement
    /* block */ y += 1;
    (void)p;
    (void)r;
    (void)c;
    (void)s;
    (void)f;
    (void)v;
    (void)y;
}
} // namespace ns
"#,
    );
}

/// `SpaceBeforeParens: Always` on the `spacing` sink
#[test]
fn option_spacebeforeparens() {
    check_against_the_oracle(
        SINK_SPACING,
        r#"SpaceBeforeParens: Always"#,
        r#"void f () {
    int *p = 0;
    int &r = *p;
    int x = (int)1.5;
    char c = 'a';
    bool b = !x;
    int m = a ? b : c;
    int t = 1;
    int u = -t;
    auto g = [&] (int v) -> int { return v; };
    struct S {};
    if (b) {
        g (t);
    }
    (void)r;
    (void)c;
    (void)m;
    (void)u;
}
"#,
    );
}

/// `SpacesBeforeTrailingComments: 2` on the `main` sink
#[test]
fn option_spacesbeforetrailingcomments() {
    check_against_the_oracle(
        SINK_MAIN,
        r#"SpacesBeforeTrailingComments: 2"#,
        r#"#include "local.h"
#include <vector>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define LONG(a)    \
    do {           \
        (void)(a); \
    } while (0)
namespace ns {
enum class Kind { A,
                  B };
class Widget : public Base {
  public:
    Widget() : a_(0), b_(1) {}
    explicit Widget(int a) : a_(a) {}
    virtual ~Widget() = default;
    void run() override;
    int get() const { return a_; }

  protected:
    int a_;

  private:
    int b_;
};
template <typename T>
concept Sized = requires(T t) { t.size(); };
template <typename T, int N = 3>
struct Buffer {
    T items[N];
};
void Widget::run() {
    int x = 1;
    int *p = &x;
    const int &r = x;
    char c = 'A';
    const char *s = "hello";
    if (x > 0) {
        x = 2;
    } else {
        x = 3;
    }
    switch (x) {
    case 1:
        break;
    default:
        break;
    }
    auto f = [&](int a) -> int { return a + x; };
    std::vector<int> v = {1, 2, 3};
    int y = 0x1F + 1000 + 1500;  // after a statement
    /* block */ y += 1;
    (void)p;
    (void)r;
    (void)c;
    (void)s;
    (void)f;
    (void)v;
    (void)y;
}
}  // namespace ns
"#,
    );
}