Skip to content

Commit

Permalink
Merge pull request #37 from Rookfighter/feature/add-formatting
Browse files Browse the repository at this point in the history
Adds clang format config and CI steps
  • Loading branch information
Rookfighter authored May 16, 2024
2 parents 8895335 + dabfea4 commit 14a29da
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 36 deletions.
107 changes: 107 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
#AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBinaryOperators: None
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: true
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Never
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
13 changes: 12 additions & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
name: CMake

on: [push, pull_request]
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
format:
name: Check C++ Code Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check Format
run: 'find test/ examples/ include/ -iname "*.h" -o -iname "*.cpp" -print0 | xargs -0 clang-format --dry-run --Werror'
build:
name: "${{matrix.config.name}} C++${{matrix.cxx}}"
runs-on: ${{matrix.config.os}}
Expand Down
8 changes: 3 additions & 5 deletions examples/custom_type_conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace ini
{
/** Conversion functor to parse std::vectors from an ini field-
* The generic template can be passed down to the vector. */
* The generic template can be passed down to the vector. */
template<typename T>
struct Convert<std::vector<T>>
{
Expand Down Expand Up @@ -49,7 +49,6 @@ namespace ini
Convert<T> conv;
conv.decode(tmp, decoded);
result.push_back(decoded);

}
}

Expand Down Expand Up @@ -106,11 +105,10 @@ int main()

std::cout << std::endl;


// create another ini file for encoding
ini::IniFile outputIni;
outputIni["Bar"]["floatList"] =std::vector<float>{1.0f, 9.3f, 3.256f};
outputIni["Bar"]["boolList"] =std::vector<bool>{true, false, false, true};
outputIni["Bar"]["floatList"] = std::vector<float>{1.0f, 9.3f, 3.256f};
outputIni["Bar"]["boolList"] = std::vector<bool>{true, false, false, true};

std::cout << "Encoded ini file" << std::endl;
std::cout << "================" << std::endl;
Expand Down
55 changes: 25 additions & 30 deletions test/test_inifile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ TEST_CASE("parse comment only file", "IniFile")
REQUIRE(inif.size() == 0);
}


TEST_CASE("parse empty section", "IniFile")
{
std::istringstream ss("[Foo]");
Expand Down Expand Up @@ -220,8 +219,8 @@ TEST_CASE("parse field as const char*", "IniFile")

REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 2);
REQUIRE(std::strcmp(inif["Foo"]["bar1"].as<const char*>(), "hello") == 0);
REQUIRE(std::strcmp(inif["Foo"]["bar2"].as<const char*>(), "world") == 0);
REQUIRE(std::strcmp(inif["Foo"]["bar1"].as<const char *>(), "hello") == 0);
REQUIRE(std::strcmp(inif["Foo"]["bar2"].as<const char *>(), "world") == 0);
}

#ifdef __cpp_lib_string_view
Expand Down Expand Up @@ -327,17 +326,14 @@ TEST_CASE("comments are allowed after escaped comments", "IniFile")
REQUIRE(inif["Foo"]["more"].as<std::string>() == "of this # #");
}

TEST_CASE(
"escape char right before a comment prefix escapes all the comment prefix",
"IniFile")
TEST_CASE("escape char right before a comment prefix escapes all the comment prefix", "IniFile")
{
std::istringstream ss("[Foo]\n"
"weird1=note \\### this is not a comment\n"
"weird2=but \\#### this is a comment");
ini::IniFile inif(ss, '=', {"##"});

REQUIRE(inif["Foo"]["weird1"].as<std::string>() ==
"note ### this is not a comment");
REQUIRE(inif["Foo"]["weird1"].as<std::string>() == "note ### this is not a comment");
REQUIRE(inif["Foo"]["weird2"].as<std::string>() == "but ##");
}

Expand All @@ -351,18 +347,16 @@ TEST_CASE("escape comment when writing", "IniFile")

std::string str = inif.encode();

REQUIRE(str ==
"[Fo\\#o]\n"
"he\\#llo=world\n"
"world=he\\#llo\n");
REQUIRE(str == "[Fo\\#o]\n"
"he\\#llo=world\n"
"world=he\\#llo\n");
}


TEST_CASE("decode what we encoded", "IniFile")
{
std::string content = "[Fo\\#o]\n"
"he\\REMllo=worl\\REMd\n"
"world=he\\//llo\n";
"he\\REMllo=worl\\REMd\n"
"world=he\\//llo\n";

ini::IniFile inif('=', {"#", "REM", "//"});

Expand Down Expand Up @@ -391,7 +385,6 @@ TEST_CASE("decode what we encoded", "IniFile")
REQUIRE(inif["Fo#o"]["heREMllo"].as<std::string>() == "worlREMd");
REQUIRE(inif["Fo#o"]["world"].as<std::string>() == "he//llo");


auto actual2 = inif.encode();

REQUIRE(content == actual2);
Expand Down Expand Up @@ -507,7 +500,6 @@ TEST_CASE("save with float fields", "IniFile")
REQUIRE(result == "[Foo]\nbar1=1.2\nbar2=-2.4\n");
}


TEST_CASE("save with std::string fields", "IniFile")
{
ini::IniFile inif;
Expand All @@ -521,8 +513,8 @@ TEST_CASE("save with std::string fields", "IniFile")
TEST_CASE("save with const char* fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = static_cast<const char*>("hello");
inif["Foo"]["bar2"] = static_cast<const char*>("world");
inif["Foo"]["bar1"] = static_cast<const char *>("hello");
inif["Foo"]["bar2"] = static_cast<const char *>("world");

std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=hello\nbar2=world\n");
Expand All @@ -533,8 +525,8 @@ TEST_CASE("save with char* fields", "IniFile")
ini::IniFile inif;
char bar1[6] = "hello";
char bar2[6] = "world";
inif["Foo"]["bar1"] = static_cast<char*>(bar1);
inif["Foo"]["bar2"] = static_cast<char*>(bar2);
inif["Foo"]["bar1"] = static_cast<char *>(bar1);
inif["Foo"]["bar2"] = static_cast<char *>(bar2);

std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=hello\nbar2=world\n");
Expand Down Expand Up @@ -666,7 +658,7 @@ TEST_CASE("multi-line values should not be parsed when disabled", "IniFile")
inif.decode(ss);

REQUIRE(inif["Foo"]["bar"].as<std::string>() == "Hello");
REQUIRE(inif["Foo"]["baz"].as<std::string>() == "world!");
REQUIRE(inif["Foo"]["baz"].as<std::string>() == "world!");
}

TEST_CASE("multi-line values should be parsed when enabled, even when the continuation contains =", "IniFile")
Expand All @@ -679,7 +671,7 @@ TEST_CASE("multi-line values should be parsed when enabled, even when the contin
inif.decode(ss);

REQUIRE(inif["Foo"]["bar"].as<std::string>() == "Hello\nbaz=world!");
REQUIRE(inif["Foo"]["baz"].as<std::string>() == "");
REQUIRE(inif["Foo"]["baz"].as<std::string>() == "");
}

TEST_CASE("when multi-line values are enabled, write newlines as multi-line value continuations", "IniFile")
Expand All @@ -692,13 +684,14 @@ TEST_CASE("when multi-line values are enabled, write newlines as multi-line valu

std::string str = inif.encode();

REQUIRE(str ==
"[Foo]\n"
"bar=Hello\n"
"\tworld!\n");
REQUIRE(str == "[Foo]\n"
"bar=Hello\n"
"\tworld!\n");
}

TEST_CASE("stringInsensitiveLess operator() returns true if and only if first parameter is less than the second ignoring sensitivity", "StringInsensitiveLessFunctor")
TEST_CASE("stringInsensitiveLess operator() returns true if and only if first parameter is less than the second "
"ignoring sensitivity",
"StringInsensitiveLessFunctor")
{
ini::StringInsensitiveLess cc;

Expand All @@ -707,14 +700,16 @@ TEST_CASE("stringInsensitiveLess operator() returns true if and only if first pa
REQUIRE(cc("aaa", "aaB"));
}

TEST_CASE("stringInsensitiveLess operator() returns false when words differs only in case", "StringInsensitiveLessFunctor")
TEST_CASE(
"stringInsensitiveLess operator() returns false when words differs only in case", "StringInsensitiveLessFunctor")
{
ini::StringInsensitiveLess cc;

REQUIRE(cc("AA", "aa") == false);
}

TEST_CASE("stringInsensitiveLess operator() has a case insensitive strict weak ordering policy", "StringInsensitiveLessFunctor")
TEST_CASE("stringInsensitiveLess operator() has a case insensitive strict weak ordering policy",
"StringInsensitiveLessFunctor")
{
ini::StringInsensitiveLess cc;

Expand Down

0 comments on commit 14a29da

Please sign in to comment.