polybar/0000755000175000017500000000000013657333602012750 5ustar patrick96patrick96polybar/version.txt0000644000175000017500000000021613657333575015206 0ustar patrick96patrick96# Polybar version information # Update this on every release # This is used to create the version string if a git repo is not available 3.4.3 polybar/tests/0000755000175000017500000000000013657333575014123 5ustar patrick96patrick96polybar/tests/unit_tests/0000755000175000017500000000000013657333575016324 5ustar patrick96patrick96polybar/tests/unit_tests/utils/0000755000175000017500000000000013657333575017464 5ustar patrick96patrick96polybar/tests/unit_tests/utils/string.cpp0000644000175000017500000001156513657333575021506 0ustar patrick96patrick96#include #include "common/test.hpp" #include "utils/string.hpp" using namespace polybar; TEST(String, upper) { EXPECT_EQ("FOO", string_util::upper("FOO")); EXPECT_EQ("FOO", string_util::upper("FoO")); EXPECT_EQ("FOO", string_util::upper("FOo")); EXPECT_EQ("FOO", string_util::upper("Foo")); } TEST(String, lower) { EXPECT_EQ("bar", string_util::lower("BAR")); } TEST(String, compare) { EXPECT_TRUE(string_util::compare("foo", "foo")); EXPECT_TRUE(string_util::compare("foo", "Foo")); EXPECT_FALSE(string_util::compare("foo", "bar")); } TEST(String, replace) { EXPECT_EQ("a.c", string_util::replace("abc", "b", ".")); EXPECT_EQ("a.a", string_util::replace("aaa", "a", ".", 1, 2)); EXPECT_EQ(".aa", string_util::replace("aaa", "a", ".", 0, 2)); EXPECT_EQ("Foo bxr baz", string_util::replace("Foo bar baz", "a", "x")); EXPECT_EQ("foxoobar", string_util::replace("foooobar", "o", "x", 2, 3)); EXPECT_EQ("foooobar", string_util::replace("foooobar", "o", "x", 0, 1)); } TEST(String, replaceAll) { EXPECT_EQ("Foo bxr bxzx", string_util::replace_all("Foo bar baza", "a", "x")); EXPECT_EQ("hoohoohoo", string_util::replace_all("hehehe", "he", "hoo")); EXPECT_EQ("hoohehe", string_util::replace_all("hehehe", "he", "hoo", 0, 2)); EXPECT_EQ("hehehoo", string_util::replace_all("hehehe", "he", "hoo", 4)); EXPECT_EQ("hehehe", string_util::replace_all("hehehe", "he", "hoo", 0, 1)); EXPECT_EQ("113113113", string_util::replace_all("131313", "3", "13")); } TEST(String, squeeze) { EXPECT_EQ("Squeze", string_util::squeeze("Squeeeeeze", 'e')); EXPECT_EQ("bar baz foobar", string_util::squeeze("bar baz foobar", ' ')); } TEST(String, strip) { EXPECT_EQ("Strp", string_util::strip("Striip", 'i')); EXPECT_EQ("test\n", string_util::strip_trailing_newline("test\n\n")); } TEST(String, trim) { EXPECT_EQ("x x", string_util::trim(" x x ")); EXPECT_EQ("testxx", string_util::ltrim("xxtestxx", 'x')); EXPECT_EQ("xxtest", string_util::rtrim("xxtestxx", 'x')); EXPECT_EQ("test", string_util::trim("xxtestxx", 'x')); } TEST(String, join) { EXPECT_EQ("A, B, C", string_util::join({"A", "B", "C"}, ", ")); } TEST(String, split) { { vector strings = string_util::split("A,B,C", ','); EXPECT_EQ(3, strings.size()); EXPECT_EQ("A", strings[0]); EXPECT_EQ("B", strings[1]); EXPECT_EQ("C", strings[2]); } { vector strings = string_util::split(",A,,B,,C,", ','); EXPECT_EQ(3, strings.size()); EXPECT_EQ("A", strings[0]); EXPECT_EQ("B", strings[1]); EXPECT_EQ("C", strings[2]); } } TEST(String, tokenize) { { vector strings = string_util::tokenize("A,B,C", ','); EXPECT_EQ(3, strings.size()); EXPECT_EQ("A", strings[0]); EXPECT_EQ("B", strings[1]); EXPECT_EQ("C", strings[2]); } { using namespace std::string_literals; vector strings = string_util::tokenize(",A,,B,,C,", ','); vector result{""s, "A"s, ""s, "B"s, ""s, "C"s, ""s}; EXPECT_TRUE(strings == result); } } TEST(String, findNth) { EXPECT_EQ(0, string_util::find_nth("foobarfoobar", 0, "f", 1)); EXPECT_EQ(6, string_util::find_nth("foobarfoobar", 0, "f", 2)); EXPECT_EQ(7, string_util::find_nth("foobarfoobar", 0, "o", 3)); } TEST(String, hash) { unsigned long hashA1{string_util::hash("foo")}; unsigned long hashA2{string_util::hash("foo")}; unsigned long hashB1{string_util::hash("Foo")}; unsigned long hashB2{string_util::hash("Bar")}; EXPECT_EQ(hashA2, hashA1); EXPECT_NE(hashB1, hashA1); EXPECT_NE(hashB2, hashA1); EXPECT_NE(hashB2, hashB1); } TEST(String, floatingPoint) { EXPECT_EQ("1.26", string_util::floating_point(1.2599, 2)); EXPECT_EQ("2", string_util::floating_point(1.7, 0)); EXPECT_EQ("1.7770000000", string_util::floating_point(1.777, 10)); } TEST(String, filesize) { EXPECT_EQ("3.000 MB", string_util::filesize_mb(3 * 1024, 3)); EXPECT_EQ("3.195 MB", string_util::filesize_mb(3 * 1024 + 200, 3)); EXPECT_EQ("3 MB", string_util::filesize_mb(3 * 1024 + 400)); EXPECT_EQ("4 MB", string_util::filesize_mb(3 * 1024 + 800)); EXPECT_EQ("3.195 GB", string_util::filesize_gb(3 * 1024 * 1024 + 200 * 1024, 3)); EXPECT_EQ("3 GB", string_util::filesize_gb(3 * 1024 * 1024 + 400 * 1024)); EXPECT_EQ("4 GB", string_util::filesize_gb(3 * 1024 * 1024 + 800 * 1024)); EXPECT_EQ("3 B", string_util::filesize(3)); EXPECT_EQ("3 KB", string_util::filesize(3 * 1024)); EXPECT_EQ("3 MB", string_util::filesize(3 * 1024 * 1024)); EXPECT_EQ("3 GB", string_util::filesize((unsigned long long)3 * 1024 * 1024 * 1024)); EXPECT_EQ("3 TB", string_util::filesize((unsigned long long)3 * 1024 * 1024 * 1024 * 1024)); } TEST(String, operators) { string foo = "foobar"; EXPECT_EQ("foo", foo - "bar"); string baz = "bazbaz"; EXPECT_EQ("bazbaz", baz - "ba"); EXPECT_EQ("bazbaz", baz - "baZ"); EXPECT_EQ("bazbaz", baz - "bazbz"); string aaa = "aaa"; EXPECT_EQ("aaa", aaa - "aaaaa"); } polybar/tests/unit_tests/utils/scope.cpp0000644000175000017500000000062613657333575021305 0ustar patrick96patrick96#include "common/test.hpp" #include "utils/scope.hpp" using namespace polybar; TEST(Scope, onExit) { auto flag = false; { EXPECT_FALSE(flag); auto handler = scope_util::make_exit_handler<>([&] { flag = true; }); EXPECT_FALSE(flag); { auto handler = scope_util::make_exit_handler<>([&] { flag = true; }); } EXPECT_TRUE(flag); flag = false; } EXPECT_TRUE(flag); } polybar/tests/unit_tests/utils/memory.cpp0000644000175000017500000000076313657333575021506 0ustar patrick96patrick96#include "common/test.hpp" #include "utils/memory.hpp" using namespace polybar; struct mytype { int x, y, z; }; TEST(Memory, makeMallocPtr) { auto ptr = memory_util::make_malloc_ptr(); EXPECT_EQ(sizeof(mytype*), sizeof(ptr.get())); ptr.reset(); EXPECT_EQ(nullptr, ptr.get()); } TEST(Memory, countof) { mytype A[3]{{}, {}, {}}; mytype B[8]{{}, {}, {}, {}, {}, {}, {}, {}}; EXPECT_EQ(memory_util::countof(A), size_t{3}); EXPECT_EQ(memory_util::countof(B), size_t{8}); } polybar/tests/unit_tests/utils/math.cpp0000644000175000017500000000572613657333575021133 0ustar patrick96patrick96#include "common/test.hpp" #include "utils/math.hpp" using namespace polybar; TEST(Math, min) { EXPECT_EQ(2, math_util::min(2, 5)); EXPECT_EQ(-50, math_util::min(-8, -50)); EXPECT_EQ(0, math_util::min(0, -5)); } TEST(Math, max) { EXPECT_EQ(5, math_util::max(2, 5)); EXPECT_EQ(-8, math_util::max(-8, -50)); EXPECT_EQ(251, math_util::max(0, (1 << 8) - 5)); } TEST(Math, cap) { EXPECT_EQ(8, math_util::cap(8, 0, 10)); EXPECT_EQ(0, math_util::cap(-8, 0, 10)); EXPECT_EQ(10, math_util::cap(15, 0, 10)); EXPECT_EQ(20.5f, math_util::cap(20.5f, 0.0f, 30.0f)); EXPECT_EQ(1.0f, math_util::cap(1.0f, 0.0f, 2.0f)); EXPECT_EQ(-2.0f, math_util::cap(-2.0f, -5.0f, 5.0f)); EXPECT_EQ(0, math_util::cap(1.0f, 0.0f, 0.0f)); } TEST(Math, percentage) { EXPECT_EQ(55.0f, (math_util::percentage(5.5f, 0.0f, 10.0f))); EXPECT_EQ(56, (math_util::percentage(5.55f, 0.0f, 10.0f))); EXPECT_EQ(43.75f, (math_util::percentage(5.25f, 0.0f, 12.0f))); EXPECT_EQ(41, (math_util::percentage(5, 0, 12))); EXPECT_EQ(20.5f, (math_util::percentage(20.5f, 0.0f, 100.0f))); EXPECT_EQ(70.0f, (math_util::percentage(4.5f, 1.0f, 6.0f))); EXPECT_EQ(21, (math_util::percentage(20.5f, 0.0f, 100.0f))); EXPECT_EQ(50, (math_util::percentage(4, 2, 6))); EXPECT_EQ(50, (math_util::percentage(0, -10, 10))); EXPECT_EQ(0, (math_util::percentage(-10, -10, 10))); EXPECT_EQ(100, (math_util::percentage(10, -10, 10))); EXPECT_EQ(10, (math_util::percentage(10, 0, 100))); } TEST(Math, percentageToValue) { EXPECT_EQ(3, math_util::percentage_to_value(50, 5)); EXPECT_EQ(2.5f, (math_util::percentage_to_value(50, 5))); EXPECT_EQ(0, math_util::percentage_to_value(0, 5)); EXPECT_EQ(1, math_util::percentage_to_value(10, 5)); EXPECT_EQ(1, math_util::percentage_to_value(20, 5)); EXPECT_EQ(2, math_util::percentage_to_value(30, 5)); EXPECT_EQ(2, math_util::percentage_to_value(40, 5)); EXPECT_EQ(3, math_util::percentage_to_value(50, 5)); EXPECT_EQ(5, math_util::percentage_to_value(100, 5)); EXPECT_EQ(5, math_util::percentage_to_value(200, 5)); EXPECT_EQ(0, math_util::percentage_to_value(-30, 5)); } TEST(Math, rangedPercentageToValue) { EXPECT_EQ(250, math_util::percentage_to_value(50, 200, 300)); EXPECT_EQ(3, math_util::percentage_to_value(50, 1, 5)); } TEST(Math, roundToNearest10) { EXPECT_EQ(50, math_util::nearest_10(52)); EXPECT_EQ(10, math_util::nearest_10(9.1)); EXPECT_EQ(100, math_util::nearest_10(95.0)); EXPECT_EQ(90, math_util::nearest_10(94.9)); } TEST(Math, roundToNearest5) { EXPECT_EQ(50, math_util::nearest_5(52)); EXPECT_EQ(10, math_util::nearest_5(9.1)); EXPECT_EQ(95, math_util::nearest_5(95.0)); EXPECT_EQ(95, math_util::nearest_5(94.9)); EXPECT_EQ(0, math_util::nearest_5(1)); EXPECT_EQ(100, math_util::nearest_5(99.99)); } polybar/tests/unit_tests/utils/file.cpp0000644000175000017500000000053113657333575021106 0ustar patrick96patrick96#include #include #include "common/test.hpp" #include "utils/command.hpp" #include "utils/file.hpp" using namespace polybar; TEST(File, expand) { auto cmd = command_util::make_command("echo $HOME"); cmd->exec(); cmd->tail([](string home) { EXPECT_EQ(home + "/test", file_util::expand("~/test")); }); } polybar/tests/unit_tests/utils/color.cpp0000644000175000017500000000561013657333575021310 0ustar patrick96patrick96#include "common/test.hpp" #include "utils/color.hpp" using namespace polybar; TEST(String, rgb) { unsigned int color{0x123456}; EXPECT_EQ(0, color_util::alpha_channel(color)); EXPECT_EQ(0x12, color_util::red_channel(color)); EXPECT_EQ(0x34, color_util::green_channel(color)); EXPECT_EQ(0x3434, color_util::green_channel(color)); EXPECT_EQ(0x56, color_util::blue_channel(color)); EXPECT_TRUE(0x33 / 255.0 == rgb{0xFF112233}.b); EXPECT_TRUE(0x51 / 255.0 == rgb{0x88449933}.g); EXPECT_TRUE(0xff0f0f0f == rgb{0xee111111}); EXPECT_TRUE(0xff0a141e == rgb{0x99112233}); } TEST(String, rgba) { unsigned int color{0xCC123456}; EXPECT_EQ(0xCCCC, color_util::alpha_channel(color)); EXPECT_EQ(0x1212, color_util::red_channel(color)); EXPECT_EQ(0x12, color_util::red_channel(color)); EXPECT_EQ(0x3434, color_util::green_channel(color)); EXPECT_EQ(0x5656, color_util::blue_channel(color)); EXPECT_EQ(0xCC / 255.0, rgba{0xCC112233}.a); EXPECT_EQ(0x99 / 255.0, rgba{0x88449933}.g); EXPECT_EQ(0xFF111111, static_cast(rgba{0xFF111111})); EXPECT_EQ(0x00FFFFFF, static_cast(rgba{0x00FFFFFF})); } TEST(String, hex) { unsigned int colorA{0x123456}; EXPECT_EQ("#123456"s, color_util::hex(colorA)); unsigned int colorB{0xCC123456}; EXPECT_EQ("#cc123456"s, color_util::hex(colorB)); unsigned int colorC{0x00ffffff}; EXPECT_EQ("#00ffffff"s, color_util::hex(colorC)); } TEST(String, parseHex) { EXPECT_EQ("#ffffffff", color_util::parse_hex("#fff")); EXPECT_EQ("#ff112233", color_util::parse_hex("#123")); EXPECT_EQ("#ff888888", color_util::parse_hex("#888888")); EXPECT_EQ("#00aa00aa", color_util::parse_hex("#00aa00aa")); } TEST(String, parse) { EXPECT_EQ(0, color_util::parse("invalid")); EXPECT_EQ(0, color_util::parse("#f")); EXPECT_EQ(0, color_util::parse("#ff")); EXPECT_EQ(0xFF999999, color_util::parse("invalid", 0xFF999999)); EXPECT_EQ(0x00111111, color_util::parse("invalid", 0x00111111)); EXPECT_EQ(0xFF000000, color_util::parse("invalid", 0xFF000000)); EXPECT_EQ(0xffffffff, color_util::parse("#fff")); EXPECT_EQ(0xFF889900, color_util::parse("#890")); EXPECT_EQ(0x55888777, color_util::parse("#55888777")); EXPECT_EQ(0x88aaaaaa, color_util::parse("#88aaaaaa")); EXPECT_EQ(0x00aaaaaa, color_util::parse("#00aaaaaa")); EXPECT_EQ(0x00FFFFFF, color_util::parse("#00FFFFFF")); } TEST(String, simplify) { EXPECT_EQ("#111", color_util::simplify_hex("#FF111111")); EXPECT_EQ("#234", color_util::simplify_hex("#ff223344")); EXPECT_EQ("#ee223344", color_util::simplify_hex("#ee223344")); EXPECT_EQ("#234567", color_util::simplify_hex("#ff234567")); EXPECT_EQ("#00223344", color_util::simplify_hex("#00223344")); } polybar/tests/unit_tests/components/0000755000175000017500000000000013657333575020511 5ustar patrick96patrick96polybar/tests/unit_tests/components/parser.cpp0000644000175000017500000000174413657333575022517 0ustar patrick96patrick96#include "common/test.hpp" #include "events/signal_emitter.hpp" #include "components/parser.hpp" using namespace polybar; class TestableParser : public parser { using parser::parser; public: using parser::parse_action_cmd; }; class Parser : public ::testing::Test { protected: TestableParser m_parser{signal_emitter::make()}; }; /** * The first element of the pair is the expected return text, the second element * is the input to parse_action_cmd */ class ParseActionCmd : public Parser, public ::testing::WithParamInterface> {}; vector> parse_action_cmd_list = { {"abc", ":abc:\\abc"}, {"abc\\:", ":abc\\::\\abc"}, {"\\:\\:\\:", ":\\:\\:\\::\\abc"}, }; INSTANTIATE_TEST_SUITE_P(Inst, ParseActionCmd, ::testing::ValuesIn(parse_action_cmd_list)); TEST_P(ParseActionCmd, correctness) { auto input = GetParam().second; auto result = m_parser.parse_action_cmd(std::move(input)); EXPECT_EQ(GetParam().first, result); } polybar/tests/unit_tests/components/command_line.cpp0000644000175000017500000000634713657333575023654 0ustar patrick96patrick96#include "common/test.hpp" #include "components/command_line.hpp" #include "utils/string.hpp" using namespace polybar; class CommandLine : public ::testing::Test { protected: virtual void SetUp() { set_cli(); } virtual void set_cli() { cli = command_line::parser::make("cmd", get_opts()); } command_line::options get_opts() { // clang-format off return command_line::options { command_line::option{"-f", "--flag", "Flag description"}, command_line::option{"-o", "--option", "Option description", "OPTION", {"foo", "bar", "baz"}}, }; // clang-format on }; command_line::parser::make_type cli; }; TEST_F(CommandLine, hasShort) { cli->process_input(string_util::split("-f", ' ')); EXPECT_TRUE(cli->has("flag")); EXPECT_FALSE(cli->has("option")); set_cli(); cli->process_input(string_util::split("-f -o foo", ' ')); EXPECT_TRUE(cli->has("flag")); EXPECT_TRUE(cli->has("option")); set_cli(); cli->process_input(string_util::split("-o baz", ' ')); EXPECT_FALSE(cli->has("flag")); EXPECT_TRUE(cli->has("option")); } TEST_F(CommandLine, hasLong) { cli->process_input(string_util::split("--flag", ' ')); EXPECT_TRUE(cli->has("flag")); EXPECT_FALSE(cli->has("option")); set_cli(); cli->process_input(string_util::split("--flag --option=foo", ' ')); EXPECT_TRUE(cli->has("flag")); EXPECT_TRUE(cli->has("option")); set_cli(); cli->process_input(string_util::split("--option=foo --flag", ' ')); EXPECT_TRUE(cli->has("flag")); EXPECT_TRUE(cli->has("option")); set_cli(); cli->process_input(string_util::split("--option=baz", ' ')); EXPECT_FALSE(cli->has("flag")); EXPECT_TRUE(cli->has("option")); } TEST_F(CommandLine, compare) { cli->process_input(string_util::split("-o baz", ' ')); EXPECT_TRUE(cli->compare("option", "baz")); set_cli(); cli->process_input(string_util::split("--option=foo", ' ')); EXPECT_TRUE(cli->compare("option", "foo")); } TEST_F(CommandLine, get) { cli->process_input(string_util::split("--option=baz", ' ')); EXPECT_EQ("baz", cli->get("option")); set_cli(); cli->process_input(string_util::split("--option=foo", ' ')); EXPECT_EQ("foo", cli->get("option")); } TEST_F(CommandLine, missingValue) { auto input1 = string_util::split("--option", ' '); auto input2 = string_util::split("-o", ' '); auto input3 = string_util::split("--option baz", ' '); EXPECT_THROW(cli->process_input(input1), command_line::value_error); set_cli(); EXPECT_THROW(cli->process_input(input2), command_line::value_error); set_cli(); EXPECT_THROW(cli->process_input(input3), command_line::value_error); } TEST_F(CommandLine, invalidValue) { auto input1 = string_util::split("--option=invalid", ' '); auto input2 = string_util::split("-o invalid_value", ' '); EXPECT_THROW(cli->process_input(input1), command_line::value_error); set_cli(); EXPECT_THROW(cli->process_input(input2), command_line::value_error); } TEST_F(CommandLine, unrecognized) { auto input1 = string_util::split("-x", ' '); auto input2 = string_util::split("--unrecognized", ' '); EXPECT_THROW(cli->process_input(input1), command_line::argument_error); set_cli(); EXPECT_THROW(cli->process_input(input2), command_line::argument_error); } polybar/tests/unit_tests/components/builder.cpp0000644000175000017500000000351113657333575022643 0ustar patrick96patrick96#include #include "common/test.hpp" #include "components/builder.hpp" #include "components/types.hpp" #include "utils/factory.hpp" #include "drawtypes/label.hpp" using namespace polybar; using namespace std; /** * \brief Testing-only subclass of builder to change access level */ class TestableBuilder : public builder { using builder::builder; public: using builder::get_label_text; }; class Builder : public ::testing::Test { protected: /** * Generic bar settings * * Builder only needs spacing and background */ bar_settings m_bar{}; TestableBuilder m_builder{m_bar}; }; // GetLabelTextTest {{{ /** * \brief Class for parameterized tests on get_label_text * * The first element of the pair is the expected returned text, the second * element is a triple containing the original label text, m_ellipsis and * m_maxlen, in that order */ class GetLabelTextTest : public Builder, public ::testing::WithParamInterface>> {}; vector>> get_label_text_list = { {"...", make_tuple("abcd", true, 3)}, {"abc", make_tuple("abc", true, 3)}, {"abc", make_tuple("abcdefgh", false, 3)}, {"a...", make_tuple("abcdefgh", true, 4)}, {"abcd...", make_tuple("abcdefgh", true, 7)}, {"abcdefgh", make_tuple("abcdefgh", true, 8)}, }; INSTANTIATE_TEST_SUITE_P(Inst, GetLabelTextTest, ::testing::ValuesIn(get_label_text_list)); TEST_P(GetLabelTextTest, correctness) { label_t m_label = factory_util::shared