Skip to content

Commit

Permalink
* Fixing clang tidy comments
Browse files Browse the repository at this point in the history
* Fixing missing includes
* Fixing formatting
  • Loading branch information
dpayne committed Jan 28, 2024
1 parent 9556b0f commit 10cb418
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 56 deletions.
16 changes: 7 additions & 9 deletions include/util/css_reload_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

#include <functional>
#include <string>
#include <unordered_map>
#include <vector>

#include "glibmm/refptr.h"
#include "giomm/file.h"
#include "giomm/filemonitor.h"
#include "glibmm/refptr.h"

struct pollfd;

Expand All @@ -20,24 +21,21 @@ class CssReloadHelper {
protected:
std::vector<std::string> parseImports(const std::string& cssFile);

void parseImports(const std::string& cssFile,
std::unordered_map<std::string, bool>& imports);

void parseImports(const std::string& cssFile, std::unordered_map<std::string, bool>& imports);

void watchFiles(const std::vector<std::string>& files);

bool handleInotifyEvents(int fd);

bool watch(int inotifyFd, pollfd * pollFd);
bool watch(int inotifyFd, pollfd* pollFd);

virtual std::string getFileContents(const std::string& filename);

virtual std::string findPath(const std::string& filename);

void handleFileChange(
Glib::RefPtr<Gio::File> const& file,
Glib::RefPtr<Gio::File> const& other_type,
Gio::FileMonitorEvent event_type);
void handleFileChange(Glib::RefPtr<Gio::File> const& file,
Glib::RefPtr<Gio::File> const& other_type,
Gio::FileMonitorEvent event_type);

private:
std::string m_cssFile;
Expand Down
6 changes: 3 additions & 3 deletions src/util/css_reload_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <fstream>
#include <regex>
#include <unordered_map>
#include "glibmm/refptr.h"
#include "giomm/file.h"

#include "config.hpp"
#include "giomm/file.h"
#include "glibmm/refptr.h"

namespace {
const std::regex IMPORT_REGEX(R"(@import\s+(?:url\()?(?:"|')([^"')]+)(?:"|')\)?;)");
Expand All @@ -30,7 +30,7 @@ std::string waybar::CssReloadHelper::getFileContents(const std::string& filename
return {};
}

return std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return {(std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()};
}

std::string waybar::CssReloadHelper::findPath(const std::string& filename) {
Expand Down
63 changes: 19 additions & 44 deletions test/css_reload_helper.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "util/css_reload_helper.hpp"

#include <map>
#include <fstream>

#if __has_include(<catch2/catch_test_macros.hpp>)
#include <catch2/catch_test_macros.hpp>
Expand All @@ -9,59 +9,39 @@
#include <catch2/catch.hpp>
#endif

class CssReloadHelperTest : public waybar::CssReloadHelper {
public:
CssReloadHelperTest() : CssReloadHelper("/tmp/waybar_test.css", [this]() { callback(); }) {}

class CssReloadHelperTest : public waybar::CssReloadHelper
{
public:
CssReloadHelperTest()
: CssReloadHelper("/tmp/waybar_test.css", [this]() {callback();})
{
}

void callback()
{
m_callbackCounter++;
}
void callback() { m_callbackCounter++; }

protected:

std::string getFileContents(const std::string& filename) override
{
protected:
std::string getFileContents(const std::string& filename) override {
return m_fileContents[filename];
}

std::string findPath(const std::string& filename) override
{
return filename;
}
std::string findPath(const std::string& filename) override { return filename; }

void setFileContents(const std::string& filename, const std::string& contents)
{
void setFileContents(const std::string& filename, const std::string& contents) {
m_fileContents[filename] = contents;
}

int getCallbackCounter() const
{
return m_callbackCounter;
}
int getCallbackCounter() const { return m_callbackCounter; }

private:
private:
int m_callbackCounter{};
std::map<std::string, std::string> m_fileContents;
};

TEST_CASE_METHOD(CssReloadHelperTest, "parse_imports", "[util][css_reload_helper]")
{
SECTION("no imports")
{
TEST_CASE_METHOD(CssReloadHelperTest, "parse_imports", "[util][css_reload_helper]") {

Check warning on line 36 in test/css_reload_helper.cpp

View workflow job for this annotation

GitHub Actions / build

test/css_reload_helper.cpp:36:1 [readability-function-cognitive-complexity]

function 'test' has cognitive complexity of 127 (threshold 25)
SECTION("no imports") {
setFileContents("/tmp/waybar_test.css", "body { color: red; }");
auto files = parseImports("/tmp/waybar_test.css");
REQUIRE(files.size() == 1);
CHECK(files[0] == "/tmp/waybar_test.css");
}

SECTION("single import")
{
SECTION("single import") {
setFileContents("/tmp/waybar_test.css", "@import 'test.css';");
setFileContents("test.css", "body { color: red; }");
auto files = parseImports("/tmp/waybar_test.css");
Expand All @@ -71,8 +51,7 @@ TEST_CASE_METHOD(CssReloadHelperTest, "parse_imports", "[util][css_reload_helper
CHECK(files[1] == "test.css");
}

SECTION("multiple imports")
{
SECTION("multiple imports") {
setFileContents("/tmp/waybar_test.css", "@import 'test.css'; @import 'test2.css';");
setFileContents("test.css", "body { color: red; }");
setFileContents("test2.css", "body { color: blue; }");
Expand All @@ -84,8 +63,7 @@ TEST_CASE_METHOD(CssReloadHelperTest, "parse_imports", "[util][css_reload_helper
CHECK(files[2] == "test2.css");
}

SECTION("nested imports")
{
SECTION("nested imports") {
setFileContents("/tmp/waybar_test.css", "@import 'test.css';");
setFileContents("test.css", "@import 'test2.css';");
setFileContents("test2.css", "body { color: red; }");
Expand All @@ -97,8 +75,7 @@ TEST_CASE_METHOD(CssReloadHelperTest, "parse_imports", "[util][css_reload_helper
CHECK(files[2] == "test2.css");
}

SECTION("circular imports")
{
SECTION("circular imports") {
setFileContents("/tmp/waybar_test.css", "@import 'test.css';");
setFileContents("test.css", "@import 'test2.css';");
setFileContents("test2.css", "@import 'test.css';");
Expand All @@ -110,16 +87,14 @@ TEST_CASE_METHOD(CssReloadHelperTest, "parse_imports", "[util][css_reload_helper
CHECK(files[2] == "test2.css");
}

SECTION("empty")
{
SECTION("empty") {
setFileContents("/tmp/waybar_test.css", "");
auto files = parseImports("/tmp/waybar_test.css");
REQUIRE(files.size() == 1);
CHECK(files[0] == "/tmp/waybar_test.css");
}

SECTION("empty name")
{
SECTION("empty name") {
auto files = parseImports("");
REQUIRE(files.empty());
}
Expand Down

0 comments on commit 10cb418

Please sign in to comment.