Skip to content

Check code format on PR and main branch #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
---
Language: Cpp
Language: Cpp
AccessModifierOffset: -4
AlignAfterOpenBracket: BlockIndent
AlignEscapedNewlinesLeft: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: All
BreakBeforeBraces: Attach
ColumnLimit: 100
ConstructorInitializerAllOnOneLineOrOnePerLine: true
BreakConstructorInitializers: AfterColon
ColumnLimit: 100
FixNamespaceComments: true
IncludeCategories:
# The autodetect header should always be included last
- Regex: '^<cucumber-cpp/autodetect\.hpp>$'
Expand All @@ -18,14 +21,19 @@ IncludeCategories:
Priority: 1
- Regex: '.*'
Priority: 2
IndentWidth: 4
IndentPPDirectives: BeforeHash
IndentWidth: 4
NamespaceIndentation: None
PackConstructorInitializers: Never
PenaltyBreakString: 1000
PenaltyExcessCharacter: 10000
PenaltyReturnTypeOnItsOwnLine: 1000
PointerAlignment: Left
SortIncludes: false
ShortNamespaceLines: 200
SortIncludes: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
Standard: Cpp03
Standard: Cpp11
...

26 changes: 26 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: check format

on:
pull_request:
branches: [ main ]
workflow_dispatch:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v2

- name: setup environment
run: |
sudo apt-get install --no-install-recommends \
clang-format-15

- name: check code format
run: |
clang-format-15 --style=file --Werror --dry-run --verbose `find . -type d \( -name '3rdparty' \) -prune -o -regex '.*\.\(cpp\|hpp\)' -print`

5 changes: 2 additions & 3 deletions examples/Calc/src/Calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ void Calculator::push(double n) {

double Calculator::add() {
double result = 0;
for(std::list<double>::const_iterator i = values.begin(); i != values.end(); ++i) {
for (std::list<double>::const_iterator i = values.begin(); i != values.end(); ++i) {
result += *i;
}
return result;
}

double Calculator::divide() {
double result = std::numeric_limits<double>::quiet_NaN();
for(std::list<double>::const_iterator i = values.begin(); i != values.end(); ++i) {
for (std::list<double>::const_iterator i = values.begin(); i != values.end(); ++i) {
if (i == values.begin()) {
result = *i;
} else {
Expand All @@ -24,4 +24,3 @@ double Calculator::divide() {
}
return result;
}

23 changes: 15 additions & 8 deletions examples/CalcQt/features/step_definitions/CalculatorQtSteps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
/// Helper class to ensure that QApplication gets destroyed before main() returns
/// and that CalculatorWidget gets destroyed before QApplication gets destroyed.
struct CalculatorCtx {
CalculatorCtx()
: argc(0)
, app(argc, NULL)
{}
CalculatorCtx() :
argc(0),
app(argc, NULL) {
}

int argc;
QApplication app;
Expand All @@ -21,16 +21,23 @@ struct CalculatorCtx {
static int milliseconds = -1;

int millisecondsToWait() {
if (milliseconds < 0)
{
if (milliseconds < 0) {
char* envVariable = getenv("CALCQT_STEP_DELAY");
milliseconds = (0 != envVariable) ? atoi(envVariable) : 0;
}
return milliseconds;
}

std::istream& operator>> (std::istream& in, QString& val) { std::string s; in >> s; val = s.c_str(); return in; }
std::ostream& operator<< (std::ostream& out, const QString& val) { out << val.toLatin1().data(); return out; }
std::istream& operator>>(std::istream& in, QString& val) {
std::string s;
in >> s;
val = s.c_str();
return in;
}
std::ostream& operator<<(std::ostream& out, const QString& val) {
out << val.toLatin1().data();
return out;
}

GIVEN("^I just turned on the calculator$") {
cucumber::ScenarioScope<CalculatorCtx> ctx;
Expand Down
2 changes: 1 addition & 1 deletion examples/CalcQt/src/CalcQt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "CalculatorWidget.h"

int main(int argc, char *argv[]) {
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
app.setApplicationName("Qt Calculator");
CalculatorWidget widget;
Expand Down
51 changes: 31 additions & 20 deletions examples/CalcQt/src/CalculatorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#include <QPushButton>
#include <QSignalMapper>

CalculatorWidget::CalculatorWidget(QWidget *parent) : QWidget(parent) {
QGridLayout *layout = new QGridLayout(this);
CalculatorWidget::CalculatorWidget(QWidget* parent) :
QWidget(parent) {
QGridLayout* layout = new QGridLayout(this);
layout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(layout);

Expand All @@ -24,16 +25,16 @@ CalculatorWidget::CalculatorWidget(QWidget *parent) : QWidget(parent) {
displayLabel->setSizePolicy(policy);

signalMapper = new QSignalMapper(this);
QPushButton *button = new QPushButton(QString::number(0), this);
QPushButton* button = new QPushButton(QString::number(0), this);
QObject::connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, 0);
layout->addWidget(button, 4, 1);
digitButtons.push_back(button);
for (unsigned int i = 1; i < 10; ++i) {
QPushButton *button = new QPushButton(QString::number(i), this);
QPushButton* button = new QPushButton(QString::number(i), this);
QObject::connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, i);
layout->addWidget(button, 1+(9-i)/3, (i-1)%3);
layout->addWidget(button, 1 + (9 - i) / 3, (i - 1) % 3);
digitButtons.push_back(button);
}
QObject::connect(signalMapper, SIGNAL(mapped(int)), SLOT(buttonClicked(int)));
Expand Down Expand Up @@ -63,8 +64,12 @@ int CalculatorWidget::calculate(const QString& expression) {
while ((pos = regexp.indexIn(expression, pos)) != -1) {
int value = regexp.cap(1).toInt();
switch (operation) {
case '+': result += value; break;
case '-': result -= value; break;
case '+':
result += value;
break;
case '-':
result -= value;
break;
}
pos += regexp.matchedLength();
if (pos < expression.length()) {
Expand All @@ -78,21 +83,27 @@ QString CalculatorWidget::display() {
return displayLabel->text();
}

void CalculatorWidget::keyPressEvent(QKeyEvent *event) {
void CalculatorWidget::keyPressEvent(QKeyEvent* event) {
keyclickedButton = 0;
int key = event->key();
if (key >= Qt::Key_0 && key <= Qt::Key_9) {
keyclickedButton = digitButtons[key - Qt::Key_0];
}
else {
switch(key)
{
case Qt::Key_Plus: keyclickedButton = additionButton; break;
case Qt::Key_Minus: keyclickedButton = subtractionButton; break;
} else {
switch (key) {
case Qt::Key_Plus:
keyclickedButton = additionButton;
break;
case Qt::Key_Minus:
keyclickedButton = subtractionButton;
break;
case Qt::Key_Return:
case Qt::Key_Enter:
case Qt::Key_Equal: keyclickedButton = calculateButton; break;
case Qt::Key_Escape: keyclickedButton = clearButton; break;
case Qt::Key_Equal:
keyclickedButton = calculateButton;
break;
case Qt::Key_Escape:
keyclickedButton = clearButton;
break;
}
}
if (0 != keyclickedButton) {
Expand All @@ -101,7 +112,7 @@ void CalculatorWidget::keyPressEvent(QKeyEvent *event) {
}
}

void CalculatorWidget::keyReleaseEvent(QKeyEvent *event) {
void CalculatorWidget::keyReleaseEvent(QKeyEvent* event) {
Q_UNUSED(event)
if (0 != keyclickedButton) {
keyclickedButton->setDown(false);
Expand All @@ -110,11 +121,11 @@ void CalculatorWidget::keyReleaseEvent(QKeyEvent *event) {
}

void CalculatorWidget::addButtonClicked() {
displayLabel->setText(displayLabel->text()+"+");
displayLabel->setText(displayLabel->text() + "+");
}

void CalculatorWidget::buttonClicked(int index) {
displayLabel->setText(displayLabel->text()+QString::number(index));
displayLabel->setText(displayLabel->text() + QString::number(index));
}

void CalculatorWidget::calculateButtonClicked() {
Expand All @@ -126,5 +137,5 @@ void CalculatorWidget::clearButtonClicked() {
}

void CalculatorWidget::subtractButtonClicked() {
displayLabel->setText(displayLabel->text()+"-");
displayLabel->setText(displayLabel->text() + "-");
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ class ActiveActors {
public:
typedef std::string actor_name_type;
typedef unsigned short actor_year_type;

private:
typedef std::map<actor_name_type, actor_year_type> actors_type;

actors_type actors;

public:
void addActor(const actor_name_type name, const actor_year_type year) {
actors[name] = year;
Expand All @@ -27,7 +29,7 @@ class ActiveActors {
actors_type::iterator it = actors.begin();
actor_name_type name = it->first;
actor_year_type year = it->second;
while(++it != actors.end()) {
while (++it != actors.end()) {
actor_year_type currentYear = it->second;
if (year > currentYear) {
name = it->first;
Expand All @@ -41,11 +43,12 @@ GIVEN("^the following actors are still active") {
TABLE_PARAM(actorsParam);
ScenarioScope<ActiveActors> context;

const table_hashes_type & actors = actorsParam.hashes();
const table_hashes_type& actors = actorsParam.hashes();
for (table_hashes_type::const_iterator ait = actors.begin(); ait != actors.end(); ++ait) {
std::string name(ait->at("name"));
std::string yearString(ait->at("born"));
const ActiveActors::actor_year_type year = ::cucumber::internal::fromString<ActiveActors::actor_year_type>(yearString);
const ActiveActors::actor_year_type year
= ::cucumber::internal::fromString<ActiveActors::actor_year_type>(yearString);
context->addActor(name, year);
}
}
Expand Down
11 changes: 4 additions & 7 deletions examples/FeatureShowcase/features/step_definitions/TagSteps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@

#include <iostream>

using std::string;
using std::cout;
using std::endl;
using std::string;

BEFORE_ALL()
{
BEFORE_ALL() {
cout << "-------------------- (Before all scenarios)" << endl;
}

AFTER_ALL()
{
AFTER_ALL() {
cout << "-------------------- (After all scenarios)" << endl;
}

BEFORE() {
cout << "-------------------- (Before any scenario)" << endl;
}

BEFORE("@foo,@bar","@baz") {
BEFORE("@foo,@bar", "@baz") {
cout << "Before scenario (\"@foo,@baz\",\"@bar\")" << endl;
}

Expand All @@ -43,7 +41,6 @@ AFTER("@gherkin") {
cout << "After scenario (\"@gherkin\")" << endl;
}


/*
* CUKE_STEP_ is used just because the feature does not convey any
* business value. It should NEVER be used in real step definitions!
Expand Down
29 changes: 14 additions & 15 deletions features/step_definitions/RpnCalculatorSteps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
#include <boost/math/constants/constants.hpp>
#include <boost/format.hpp>

using std::string;
using cucumber::ScenarioScope;
using std::string;

namespace {

bool isCloseEnough(double value, double expected) {
return (std::abs(value - expected) > 0.00001);
}
bool isCloseEnough(double value, double expected) {
return (std::abs(value - expected) > 0.00001);
}

void verifyEqual(double value, double expected) {
if (isCloseEnough(value, expected)) {
throw (boost::format("Value %1% not equal to %2%") % value % expected).str();
}
void verifyEqual(double value, double expected) {
if (isCloseEnough(value, expected)) {
throw(boost::format("Value %1% not equal to %2%") % value % expected).str();
}
}

void verifyNotEqual(double value, double expected) {
if (!isCloseEnough(value, expected)) {
throw (boost::format("Value %1% equal to %2%") % value % expected).str();
}
void verifyNotEqual(double value, double expected) {
if (!isCloseEnough(value, expected)) {
throw(boost::format("Value %1% equal to %2%") % value % expected).str();
}
}
}

GIVEN("^a calculator") {
// nothing to do
Expand Down Expand Up @@ -72,9 +72,9 @@ WHEN("^the calculator adds up the following numbers:$") {

ScenarioScope<RpnCalculator> calc;
vector<string> numbers;
boost::split(numbers, numberString,boost::is_any_of("\n"));
boost::split(numbers, numberString, boost::is_any_of("\n"));

for (size_t i=0; i < numbers.size(); ++i) {
for (size_t i = 0; i < numbers.size(); ++i) {
calc->push(numbers[i]);
if (i != 0) {
calc->push("+");
Expand All @@ -100,4 +100,3 @@ THEN("^the calculator does not return ([\\d\\.]+)$") {
ScenarioScope<RpnCalculator> calc;
verifyNotEqual(calc->value(), expected);
}

Loading