use cpp_formatter::config::Style;
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
"##;
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;
}
"##;
#[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");
}
fn style(configuration: &str) -> 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");
for line in configuration.lines() {
if let Some((key, value)) = line.split_once(':') {
style.set_from_text(key.trim(), value.trim());
}
}
style
}
#[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
"#,
);
}
#[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;
}
"#,
);
}
#[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
"#,
);
}
#[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;
}
"#,
);
}
#[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
"#,
);
}