summaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2023-01-05 13:55:20 +0100
committerRoland Reichwein <mail@reichwein.it>2023-01-05 13:55:20 +0100
commit7973293c311e27ff08a1488c9759c1b5b0fda30e (patch)
treeb4c622de1af43979ba1779713398bbcb6e52be31 /tests
parent0f2ac0c4311e4429bfa4ede1d96ce467b5dceb5b (diff)
Added tests
Diffstat (limited to 'tests')
-rw-r--r--tests/test-base64.cpp25
-rw-r--r--tests/test-file.cpp45
-rw-r--r--tests/test-mime.cpp31
-rw-r--r--tests/test-os.cpp16
-rw-r--r--tests/test-stringhelper.cpp45
-rw-r--r--tests/test-tempfile.cpp15
-rw-r--r--tests/test-url.cpp10
7 files changed, 178 insertions, 9 deletions
diff --git a/tests/test-base64.cpp b/tests/test-base64.cpp
index 03648d4..5b7644e 100644
--- a/tests/test-base64.cpp
+++ b/tests/test-base64.cpp
@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
-#include "file.h"
+#include "base64.h"
class Base64Test: public ::testing::Test
{
@@ -21,3 +21,26 @@ protected:
};
+TEST_F(Base64Test, encode)
+{
+ EXPECT_EQ(Reichwein::Base64::encode64(""), "");
+ EXPECT_EQ(Reichwein::Base64::encode64("a"), "YQ==");
+ EXPECT_EQ(Reichwein::Base64::encode64("ab"), "YWI=");
+ EXPECT_EQ(Reichwein::Base64::encode64("abc"), "YWJj");
+ EXPECT_EQ(Reichwein::Base64::encode64("abcd"), "YWJjZA==");
+}
+
+TEST_F(Base64Test, decode)
+{
+ EXPECT_EQ(Reichwein::Base64::decode64(""), "");
+ EXPECT_EQ(Reichwein::Base64::decode64("YQ=="), "a");
+ EXPECT_EQ(Reichwein::Base64::decode64("YQ"), "a"); // unpadded is accepted here
+ EXPECT_EQ(Reichwein::Base64::decode64("YWI="), "ab");
+ EXPECT_EQ(Reichwein::Base64::decode64("YWI"), "ab");
+ EXPECT_EQ(Reichwein::Base64::decode64("YWJj"), "abc");
+ EXPECT_EQ(Reichwein::Base64::decode64("YWJjZA=="), "abcd");
+ EXPECT_EQ(Reichwein::Base64::decode64("YWJjZA="), "abcd");
+ EXPECT_EQ(Reichwein::Base64::decode64("YWJjZA"), "abcd");
+ EXPECT_ANY_THROW({Reichwein::Base64::decode64("_");});
+ EXPECT_ANY_THROW({Reichwein::Base64::decode64("abc_");});
+}
diff --git a/tests/test-file.cpp b/tests/test-file.cpp
index 7c3b752..a175858 100644
--- a/tests/test-file.cpp
+++ b/tests/test-file.cpp
@@ -53,3 +53,48 @@ TEST_F(FileTest, getFile_proc)
EXPECT_GT(s.size(), 0);
}
+
+TEST_F(FileTest, getFile_not_found)
+{
+ EXPECT_THROW({Reichwein::File::getFile(testFilename);}, std::runtime_error);
+}
+
+TEST_F(FileTest, setFile)
+{
+ Reichwein::File::setFile(testFilename, std::string("abc"));
+
+ std::ifstream f(testFilename, std::ios::binary);
+ std::string s{" "};
+ EXPECT_EQ(f.readsome(s.data(), s.size()), static_cast<std::streamsize>(3));
+ EXPECT_EQ(s, "abc ");
+}
+
+TEST_F(FileTest, setFile_ptr)
+{
+ std::string s{"abc"};
+ Reichwein::File::setFile(testFilename, s.data(), s.size());
+
+ EXPECT_EQ(Reichwein::File::getFile(testFilename), "abc");
+}
+
+TEST_F(FileTest, setFile_vector)
+{
+ std::vector<uint8_t> v{1, 2, 3, 4};
+ Reichwein::File::setFile(testFilename, v);
+
+ EXPECT_EQ(Reichwein::File::getFile(testFilename), "\x01\x02\x03\x04");
+}
+
+TEST_F(FileTest, setFile_overwrite)
+{
+ Reichwein::File::setFile(testFilename, std::string("abc"));
+ Reichwein::File::setFile(testFilename, std::string("def"));
+
+ EXPECT_EQ(Reichwein::File::getFile(testFilename), "def");
+}
+
+TEST_F(FileTest, setFile_nonexisting_dir)
+{
+ EXPECT_THROW({Reichwein::File::setFile(testFilename/testFilename, "abc");}, std::runtime_error);
+}
+
diff --git a/tests/test-mime.cpp b/tests/test-mime.cpp
index 2d28a36..f3ca8ee 100644
--- a/tests/test-mime.cpp
+++ b/tests/test-mime.cpp
@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
-#include "file.h"
+#include "mime.h"
class MimeTest: public ::testing::Test
{
@@ -21,3 +21,32 @@ protected:
};
+TEST_F(MimeTest, mime_type)
+{
+ EXPECT_EQ(Reichwein::Mime::mime_type(""), "");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.html"), "text/html");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.htm"), "text/html");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.html"), "text/html");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.php"), "text/html");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.css"), "text/css");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.txt"), "text/plain");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.js"), "application/javascript");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.json"), "application/json");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.xml"), "application/xml");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.swf"), "application/x-shockwave-flash");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.flv"), "video/x-flv");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.png"), "image/png");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.jpe"), "image/jpeg");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.jpeg"), "image/jpeg");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.jpg"), "image/jpeg");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.gif"), "image/gif");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.bmp"), "image/bmp");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.ico"), "image/vnd.microsoft.icon");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.tiff"), "image/tiff");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.tif"), "image/tiff");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.svg"), "image/svg+xml");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.svgz"), "image/svg+xml");
+ EXPECT_EQ(Reichwein::Mime::mime_type("a/bcd.cpp"), "application/text");
+ EXPECT_EQ(Reichwein::Mime::mime_type("bla.HTML"), "text/html");
+}
+
diff --git a/tests/test-os.cpp b/tests/test-os.cpp
index 6177bfa..06eebc8 100644
--- a/tests/test-os.cpp
+++ b/tests/test-os.cpp
@@ -1,14 +1,14 @@
#include <gtest/gtest.h>
-#include "file.h"
+#include "os.h"
-class OsTest: public ::testing::Test
+class OSTest: public ::testing::Test
{
protected:
- OsTest(){
+ OSTest(){
}
- ~OsTest() override{
+ ~OSTest() override{
}
void SetUp() override
@@ -20,3 +20,11 @@ protected:
}
};
+
+TEST_F(OSTest,uptime_host) {
+ EXPECT_GT(Reichwein::OS::uptime_host().size(), 0);
+}
+
+TEST_F(OSTest,uptime_process) {
+ EXPECT_GT(Reichwein::OS::uptime_process().size(), 0);
+}
diff --git a/tests/test-stringhelper.cpp b/tests/test-stringhelper.cpp
index 3ece784..7f326af 100644
--- a/tests/test-stringhelper.cpp
+++ b/tests/test-stringhelper.cpp
@@ -1,6 +1,9 @@
#include <gtest/gtest.h>
-#include "file.h"
+#include "stringhelper.h"
+
+#include <string>
+#include <vector>
class StringhelperTest: public ::testing::Test
{
@@ -21,3 +24,43 @@ protected:
};
+TEST_F(StringhelperTest, strfmt)
+{
+ EXPECT_EQ(Reichwein::Stringhelper::strfmt(""), "");
+ EXPECT_EQ(Reichwein::Stringhelper::strfmt("abc"), "abc");
+ EXPECT_EQ(Reichwein::Stringhelper::strfmt("abc%ddef", 123), "abc123def");
+ EXPECT_EQ(Reichwein::Stringhelper::strfmt("abc%sdef", "XYZ"), "abcXYZdef");
+}
+
+TEST_F(StringhelperTest, split)
+{
+ EXPECT_EQ(Reichwein::Stringhelper::split(""), std::vector<std::string>{});
+ EXPECT_EQ(Reichwein::Stringhelper::split("abc"), std::vector<std::string>{"abc"});
+ std::vector<std::string> v{"abc", "def", "ghi"};
+ EXPECT_EQ(Reichwein::Stringhelper::split("abc def ghi"), v);
+ EXPECT_EQ(Reichwein::Stringhelper::split("abcxdefxghi", "x"), v);
+}
+
+TEST_F(StringhelperTest, join)
+{
+ EXPECT_EQ(Reichwein::Stringhelper::join({}), "");
+ EXPECT_EQ(Reichwein::Stringhelper::join({"abc"}), "abc");
+ EXPECT_EQ(Reichwein::Stringhelper::join({"abc", "def"}), "abc\ndef");
+ EXPECT_EQ(Reichwein::Stringhelper::join({"abc", "def"}, " "), "abc def");
+ EXPECT_EQ(Reichwein::Stringhelper::join({"abc", "def", "ghi"}, "12"), "abc12def12ghi");
+}
+
+TEST_F(StringhelperTest, startsWithAnyOfLower)
+{
+ EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("", {""}));
+ EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("a", {""}));
+ EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("abc", {"a"}));
+ EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("abc", {"A"}));
+ EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("Abc", {"a"}));
+ EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("Abc", {"A"}));
+ EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("Abc", {"AB"}));
+ EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("Abc", {"B", "aB"}));
+
+ EXPECT_FALSE(Reichwein::Stringhelper::istartsWithAnyOf("", {"a"}));
+ EXPECT_FALSE(Reichwein::Stringhelper::istartsWithAnyOf("Abc", {"B"}));
+}
diff --git a/tests/test-tempfile.cpp b/tests/test-tempfile.cpp
index b9bef05..782c3de 100644
--- a/tests/test-tempfile.cpp
+++ b/tests/test-tempfile.cpp
@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
-#include "file.h"
+#include "tempfile.h"
class TempfileTest: public ::testing::Test
{
@@ -21,3 +21,16 @@ protected:
};
+TEST_F(TempfileTest, getPath)
+{
+ Reichwein::Tempfile t0;
+ Reichwein::Tempfile t1{".cpp"};
+ Reichwein::Tempfile t2{".cpp"};
+
+ EXPECT_NE(t0.getPath(), t1.getPath());
+ EXPECT_NE(t1.getPath(), t2.getPath());
+
+ EXPECT_GT(t0.getPath().generic_u8string().size(), 0);
+ EXPECT_GT(t1.getPath().generic_u8string().size(), 4);
+ EXPECT_GT(t2.getPath().generic_u8string().size(), 4);
+}
diff --git a/tests/test-url.cpp b/tests/test-url.cpp
index 2f5075a..2e56d2a 100644
--- a/tests/test-url.cpp
+++ b/tests/test-url.cpp
@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
-#include "file.h"
+#include "url.h"
class URLTest: public ::testing::Test
{
@@ -20,3 +20,11 @@ protected:
}
};
+
+TEST_F(URLTest, urlDecode)
+{
+ EXPECT_EQ(Reichwein::URL::urlDecode(""), "");
+ EXPECT_EQ(Reichwein::URL::urlDecode("abc"), "abc");
+ EXPECT_EQ(Reichwein::URL::urlDecode("a+b+c"), "a b c");
+ EXPECT_EQ(Reichwein::URL::urlDecode("a+b%41c"), "a bAc");
+}