File size: 3,502 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | #include <gtest/gtest.h>
#include <Base/FileInfo.h>
#include <Base/Stream.h>
#include <Base/TimeInfo.h>
class FileInfoTest: public ::testing::Test
{
protected:
FileInfoTest()
{
tmp.setFile(Base::FileInfo::getTempPath() + "fctest");
tmp.createDirectory();
file.setFile(tmp.filePath() + "/test.txt");
dir.setFile(tmp.filePath() + "/subdir");
}
void SetUp() override
{
Base::ofstream str(file, std::ios::out);
str << "Test\n";
str.close();
dir.createDirectory();
}
void TearDown() override
{
EXPECT_TRUE(file.deleteFile());
EXPECT_TRUE(dir.deleteDirectory());
}
protected:
Base::FileInfo tmp;
Base::FileInfo file;
Base::FileInfo dir;
};
TEST_F(FileInfoTest, TestDirectoryPath)
{
Base::FileInfo relfile("nofile.txt");
relfile.setFile(relfile.dirPath());
EXPECT_TRUE(relfile.exists());
}
TEST_F(FileInfoTest, TestExistsDirectory)
{
EXPECT_TRUE(tmp.exists());
}
TEST_F(FileInfoTest, TestCreateDirectory)
{
Base::FileInfo path;
path.setFile(tmp.filePath() + "/subdir1/subdir2");
EXPECT_FALSE(path.createDirectory());
}
TEST_F(FileInfoTest, TestDirectoryContent)
{
auto content = tmp.getDirectoryContent();
EXPECT_EQ(content.size(), 2);
EXPECT_TRUE(content[0].exists());
EXPECT_TRUE(content[1].exists());
}
TEST_F(FileInfoTest, TestCheckPermission)
{
EXPECT_TRUE(file.isReadable());
EXPECT_TRUE(file.isWritable());
}
TEST_F(FileInfoTest, TestCheckNoPermission)
{
std::string path = tmp.filePath();
Base::FileInfo nofile(path + "/nofile");
EXPECT_FALSE(nofile.isReadable());
EXPECT_FALSE(nofile.isWritable());
}
TEST_F(FileInfoTest, TestSetPermission)
{
file.setPermissions(Base::FileInfo::ReadOnly);
EXPECT_TRUE(file.isReadable());
EXPECT_FALSE(file.isWritable());
file.setPermissions(Base::FileInfo::WriteOnly);
EXPECT_FALSE(file.isReadable());
EXPECT_TRUE(file.isWritable());
file.setPermissions(Base::FileInfo::ReadWrite);
EXPECT_TRUE(file.isReadable());
EXPECT_TRUE(file.isWritable());
}
TEST_F(FileInfoTest, TestCheckFile)
{
EXPECT_TRUE(file.isFile());
EXPECT_FALSE(dir.isFile());
std::string path = tmp.filePath();
Base::FileInfo file2(path + "/file2");
EXPECT_TRUE(file2.isFile());
}
TEST_F(FileInfoTest, TestCheckDirectory)
{
EXPECT_FALSE(file.isDir());
EXPECT_TRUE(dir.isDir());
std::string path = tmp.filePath();
Base::FileInfo file2(path + "/file2");
EXPECT_FALSE(file2.isDir());
}
TEST_F(FileInfoTest, TestSize)
{
EXPECT_EQ(file.size(), 5);
}
TEST_F(FileInfoTest, TestLastModified)
{
EXPECT_FALSE(file.lastModified().isNull());
std::string path = tmp.filePath();
Base::FileInfo nofile(path + "/nofile.txt");
EXPECT_TRUE(nofile.lastModified().isNull());
}
TEST_F(FileInfoTest, TestDeleteFile)
{
std::string path = tmp.filePath();
Base::FileInfo file2(path + "/nofile.txt");
EXPECT_FALSE(file2.deleteFile());
}
TEST_F(FileInfoTest, TestRenameFile)
{
std::string path = tmp.filePath();
Base::FileInfo file2(path + "/file2");
EXPECT_FALSE(file2.renameFile((path + "/file3").c_str()));
EXPECT_TRUE(file.renameFile((path + "/file2").c_str()));
}
TEST_F(FileInfoTest, TestCopyFile)
{
std::string path = tmp.filePath();
Base::FileInfo copy(path + "/copy.txt");
EXPECT_TRUE(file.copyTo(copy.filePath().c_str()));
EXPECT_TRUE(copy.deleteFile());
}
|