From 9ec668277758849be1cf6809dbb702765929ee94 Mon Sep 17 00:00:00 2001 From: patrick96 Date: Mon, 21 May 2018 15:10:24 +0200 Subject: [PATCH] tests: Convert string tests to gtest The sstream test was removed because it only tested standard library behvaior --- tests/unit_tests/utils/string.cpp | 237 +++++++++++++++--------------- 1 file changed, 120 insertions(+), 117 deletions(-) diff --git a/tests/unit_tests/utils/string.cpp b/tests/unit_tests/utils/string.cpp index e52e52dd..48fad050 100644 --- a/tests/unit_tests/utils/string.cpp +++ b/tests/unit_tests/utils/string.cpp @@ -3,122 +3,125 @@ #include "common/test.hpp" #include "utils/string.hpp" -int main() { - using namespace polybar; +using namespace polybar; - "upper"_test = [] { expect(string_util::upper("FOO") == "FOO"); }; - - "lower"_test = [] { expect(string_util::lower("BAR") == "bar"); }; - - "compare"_test = [] { - expect(string_util::compare("foo", "foo")); - expect(!string_util::compare("foo", "bar")); - }; - - "replace"_test = [] { - expect(string_util::replace("abc", "b", ".") == "a.c"); - expect(string_util::replace("aaa", "a", ".", 1, 2) == "a.a"); - expect(string_util::replace("aaa", "a", ".", 0, 2) == ".aa"); - expect(string_util::replace("Foo bar baz", "a", "x") == "Foo bxr baz"); - expect(string_util::replace("foooobar", "o", "x", 2, 3) == "foxoobar"); - expect(string_util::replace("foooobar", "o", "x", 0, 1) == "foooobar"); - }; - - "replace_all"_test = [] { - expect(string_util::replace_all("Foo bar baza", "a", "x") == "Foo bxr bxzx"); - expect(string_util::replace_all("hehehe", "he", "hoo") == "hoohoohoo"); - expect(string_util::replace_all("hehehe", "he", "hoo", 0, 2) == "hoohehe"); - expect(string_util::replace_all("hehehe", "he", "hoo", 4) == "hehehoo"); - expect(string_util::replace_all("hehehe", "he", "hoo", 0, 1) == "hehehe"); - expect(string_util::replace_all("131313", "3", "13") == "113113113"); - }; - - "squeeze"_test = [] { - expect(string_util::squeeze("Squeeeeeze", 'e') == "Squeze"); - expect(string_util::squeeze("bar baz foobar", ' ') == "bar baz foobar"); - }; - - "strip"_test = [] { - expect(string_util::strip("Striip", 'i') == "Strp"); - expect(string_util::strip_trailing_newline("test\n\n") == "test\n"); - }; - - "trim"_test = [] { - expect(string_util::trim(" x x ") == "x x"); - expect(string_util::ltrim("xxtestxx", 'x') == "testxx"); - expect(string_util::rtrim("xxtestxx", 'x') == "xxtest"); - expect(string_util::trim("xxtestxx", 'x') == "test"); - }; - - "join"_test = [] { expect(string_util::join({"A", "B", "C"}, ", ") == "A, B, C"); }; - - "split_into"_test = [] { - vector strings; - string_util::split_into("A,B,C", ',', strings); - expect(strings.size() == size_t(3)); - expect(strings[0] == "A"); - expect(strings[2] == "C"); - }; - - "split"_test = [] { - vector strings{"foo", "bar"}; - vector result{string_util::split("foo,bar", ',')}; - expect(result.size() == strings.size()); - expect(result[0] == strings[0]); - expect(result[1] == "bar"); - }; - - "find_nth"_test = [] { - expect(string_util::find_nth("foobarfoobar", 0, "f", 1) == size_t{0}); - expect(string_util::find_nth("foobarfoobar", 0, "f", 2) == size_t{6}); - expect(string_util::find_nth("foobarfoobar", 0, "o", 3) == size_t{7}); - }; - - "hash"_test = [] { - 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(hashA1 == hashA2); - expect(hashA1 != hashB1); - expect(hashA1 != hashB2); - expect(hashB1 != hashB2); - }; - - "floating_point"_test = [] { - expect(string_util::floating_point(1.2599, 2) == "1.26"); - expect(string_util::floating_point(1.7, 0) == "2"); - expect(string_util::floating_point(1.777, 10) == "1.7770000000"); - }; - - "filesize"_test = [] { - expect(string_util::filesize_mb(3 * 1024, 3) == "3.000 MB"); - expect(string_util::filesize_mb(3 * 1024 + 200, 3) == "3.195 MB"); - expect(string_util::filesize_mb(3 * 1024 + 400) == "3 MB"); - expect(string_util::filesize_mb(3 * 1024 + 800) == "4 MB"); - expect(string_util::filesize_gb(3 * 1024 * 1024 + 200 * 1024, 3) == "3.195 GB"); - expect(string_util::filesize_gb(3 * 1024 * 1024 + 400 * 1024) == "3 GB"); - expect(string_util::filesize_gb(3 * 1024 * 1024 + 800 * 1024) == "4 GB"); - expect(string_util::filesize(3) == "3 B"); - expect(string_util::filesize(3 * 1024) == "3 KB"); - expect(string_util::filesize(3 * 1024 * 1024) == "3 MB"); - expect(string_util::filesize((unsigned long long)3 * 1024 * 1024 * 1024) == "3 GB"); - expect(string_util::filesize((unsigned long long)3 * 1024 * 1024 * 1024 * 1024) == "3 TB"); - }; - - "sstream"_test = [] { - string s; - expect((s = (sstream() << "test")) == "test"s); - expect((s = (sstream() << std::setprecision(2) << std::fixed << 1.25)).erase(0, 2) == "25"s); - }; - - "operators"_test = [] { - string foo = "foobar"; - expect(foo - "bar" == "foo"); - string baz = "bazbaz"; - expect(baz - "ba" == "bazbaz"); - expect(baz - "bazbz" == "bazbaz"); - string aaa = "aaa"; - expect(aaa - "aaaaa" == "aaa"); - }; +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, splitInto) { + vector strings; + string_util::split_into("A,B,C", ',', strings); + EXPECT_EQ(3, strings.size()); + EXPECT_EQ("A", strings[0]); + EXPECT_EQ("C", strings[2]); +} + +TEST(String, split) { + vector strings{"foo", "bar"}; + vector result{string_util::split("foo,bar", ',')}; + EXPECT_EQ(strings.size(), result.size()); + EXPECT_EQ(strings[0], result[0]); + EXPECT_EQ("bar", result[1]); +} + +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"); }