Refactor FsppDirTest

This commit is contained in:
Sebastian Meßmer 2015-03-19 04:24:43 +01:00
parent 24a60d6cb3
commit 214b268a1c

View File

@ -15,23 +15,27 @@ public:
this->LoadDir("/mydir/mysubdir")->createDir("mysubsubdir", this->MODE_PUBLIC);
}
void EXPECT_HAS_DEFAULT_ENTRIES(const std::vector<fspp::Dir::Entry> &children) {
EXPECT_HAS_DIR_ENTRY(".", children);
EXPECT_HAS_DIR_ENTRY("..", children);
void EXPECT_HAS_ENTRIES(const std::vector<fspp::Dir::Entry> &children, const std::initializer_list<fspp::Dir::Entry> expected) {
std::vector<fspp::Dir::Entry> expectedChildren = expected;
expectedChildren.push_back(fspp::Dir::Entry(fspp::Dir::EntryType::DIR, "."));
expectedChildren.push_back(fspp::Dir::Entry(fspp::Dir::EntryType::DIR, ".."));
EXPECT_UNORDERED_EQ(expectedChildren, children);
}
void EXPECT_HAS_DIR_ENTRY(const std::string &name, const std::vector<fspp::Dir::Entry> &children) {
EXPECT_HAS_ENTRY(fspp::Dir::EntryType::DIR, name, children);
template<class Entry>
void EXPECT_UNORDERED_EQ(const std::vector<Entry> &expected, std::vector<Entry> actual) {
EXPECT_EQ(expected.size(), actual.size());
for (const Entry &expectedEntry : expected) {
removeOne(&actual, expectedEntry);
}
}
void EXPECT_HAS_FILE_ENTRY(const std::string &name, const std::vector<fspp::Dir::Entry> &children) {
EXPECT_HAS_ENTRY(fspp::Dir::EntryType::FILE, name, children);
}
void EXPECT_HAS_ENTRY(fspp::Dir::EntryType type, const std::string &name, const std::vector<fspp::Dir::Entry> &children) {
for (const auto &child : children) {
if (child.type == type && child.name == name) {
return;
template<class Entry>
void removeOne(std::vector<Entry> *entries, const Entry &toRemove) {
for (auto iter = entries->begin(); iter != entries->end(); ++iter) {
if (iter->type == toRemove.type && iter->name == toRemove.name) {
entries->erase(iter);
return;
}
}
EXPECT_TRUE(false);
@ -39,46 +43,52 @@ public:
};
TYPED_TEST_CASE_P(FsppDirTest);
fspp::Dir::Entry DirEntry(const std::string &name) {
return fspp::Dir::Entry(fspp::Dir::EntryType::DIR, name);
}
fspp::Dir::Entry FileEntry(const std::string &name) {
return fspp::Dir::Entry(fspp::Dir::EntryType::FILE, name);
}
TYPED_TEST_P(FsppDirTest, Children_RootDir_Empty) {
auto children = this->LoadDir("/")->children();
EXPECT_EQ(2, children->size());
this->EXPECT_HAS_DEFAULT_ENTRIES(*children);
this->EXPECT_HAS_ENTRIES(*children, {});
}
TYPED_TEST_P(FsppDirTest, Children_RootDir_OneFile) {
auto rootdir = this->LoadDir("/");
rootdir->createAndOpenFile("myfile", this->MODE_PUBLIC);
auto children = rootdir->children();
EXPECT_EQ(3, children->size());
this->EXPECT_HAS_DEFAULT_ENTRIES(*children);
this->EXPECT_HAS_FILE_ENTRY("myfile", *children);
this->EXPECT_HAS_ENTRIES(*children, {
FileEntry("myfile")
});
}
TYPED_TEST_P(FsppDirTest, Children_RootDir_OneDir) {
auto rootdir = this->LoadDir("/");
rootdir->createDir("mydir", this->MODE_PUBLIC);
auto children = rootdir->children();
EXPECT_EQ(3, children->size());
this->EXPECT_HAS_DEFAULT_ENTRIES(*children);
this->EXPECT_HAS_DIR_ENTRY("mydir", *children);
this->EXPECT_HAS_ENTRIES(*children, {
DirEntry("mydir")
});
}
TYPED_TEST_P(FsppDirTest, Children_RootDir_LargerStructure) {
this->InitDirStructure();
auto children = this->LoadDir("/")->children();
EXPECT_EQ(5, children->size());
this->EXPECT_HAS_DEFAULT_ENTRIES(*children);
this->EXPECT_HAS_FILE_ENTRY("myfile", *children);
this->EXPECT_HAS_DIR_ENTRY("mydir", *children);
this->EXPECT_HAS_DIR_ENTRY("myemptydir", *children);
this->EXPECT_HAS_ENTRIES(*children, {
FileEntry("myfile"),
DirEntry("mydir"),
DirEntry("myemptydir")
});
}
TYPED_TEST_P(FsppDirTest, Children_Nested_Empty) {
auto rootdir = this->LoadDir("/");
rootdir->createDir("myemptydir", this->MODE_PUBLIC);
auto children = this->LoadDir("/myemptydir")->children();
EXPECT_EQ(2, children->size());
this->EXPECT_HAS_DEFAULT_ENTRIES(*children);
this->EXPECT_HAS_ENTRIES(*children, {});
}
TYPED_TEST_P(FsppDirTest, Children_Nested_OneFile_Directly) {
@ -86,9 +96,9 @@ TYPED_TEST_P(FsppDirTest, Children_Nested_OneFile_Directly) {
auto dir = this->LoadDir("/mydir");
dir->createAndOpenFile("myfile", this->MODE_PUBLIC);
auto children = dir->children();
EXPECT_EQ(3, children->size());
this->EXPECT_HAS_DEFAULT_ENTRIES(*children);
this->EXPECT_HAS_FILE_ENTRY("myfile", *children);
this->EXPECT_HAS_ENTRIES(*children, {
FileEntry("myfile")
});
}
TYPED_TEST_P(FsppDirTest, Children_Nested_OneFile_AfterReloadingDir) {
@ -96,9 +106,9 @@ TYPED_TEST_P(FsppDirTest, Children_Nested_OneFile_AfterReloadingDir) {
this->LoadDir("/mydir")->createAndOpenFile("myfile", this->MODE_PUBLIC);
auto dir = this->LoadDir("/mydir");
auto children = dir->children();
EXPECT_EQ(3, children->size());
this->EXPECT_HAS_DEFAULT_ENTRIES(*children);
this->EXPECT_HAS_FILE_ENTRY("myfile", *children);
this->EXPECT_HAS_ENTRIES(*children, {
FileEntry("myfile")
});
}
TYPED_TEST_P(FsppDirTest, Children_Nested_OneDir_Directly) {
@ -106,9 +116,9 @@ TYPED_TEST_P(FsppDirTest, Children_Nested_OneDir_Directly) {
auto dir = this->LoadDir("/mydir");
dir->createDir("mysubdir", this->MODE_PUBLIC);
auto children = dir->children();
EXPECT_EQ(3, children->size());
this->EXPECT_HAS_DEFAULT_ENTRIES(*children);
this->EXPECT_HAS_DIR_ENTRY("mysubdir", *children);
this->EXPECT_HAS_ENTRIES(*children, {
DirEntry("mysubdir")
});
}
TYPED_TEST_P(FsppDirTest, Children_Nested_OneDir_AfterReloadingDir) {
@ -116,35 +126,34 @@ TYPED_TEST_P(FsppDirTest, Children_Nested_OneDir_AfterReloadingDir) {
this->LoadDir("/mydir")->createDir("mysubdir", this->MODE_PUBLIC);
auto dir = this->LoadDir("/mydir");
auto children = dir->children();
EXPECT_EQ(3, children->size());
this->EXPECT_HAS_DEFAULT_ENTRIES(*children);
this->EXPECT_HAS_DIR_ENTRY("mysubdir", *children);
this->EXPECT_HAS_ENTRIES(*children, {
DirEntry("mysubdir")
});
}
TYPED_TEST_P(FsppDirTest, Children_Nested_LargerStructure_Empty) {
this->InitDirStructure();
auto children = this->LoadDir("/myemptydir")->children();
EXPECT_EQ(2, children->size());
this->EXPECT_HAS_DEFAULT_ENTRIES(*children);
this->EXPECT_HAS_ENTRIES(*children, {});
}
TYPED_TEST_P(FsppDirTest, Children_Nested_LargerStructure) {
this->InitDirStructure();
auto children = this->LoadDir("/mydir")->children();
EXPECT_EQ(5, children->size());
this->EXPECT_HAS_DEFAULT_ENTRIES(*children);
this->EXPECT_HAS_FILE_ENTRY("myfile", *children);
this->EXPECT_HAS_FILE_ENTRY("myfile2", *children);
this->EXPECT_HAS_DIR_ENTRY("mysubdir", *children);
this->EXPECT_HAS_ENTRIES(*children, {
FileEntry("myfile"),
FileEntry("myfile2"),
DirEntry("mysubdir")
});
}
TYPED_TEST_P(FsppDirTest, Children_Nested2_LargerStructure) {
this->InitDirStructure();
auto children = this->LoadDir("/mydir/mysubdir")->children();
EXPECT_EQ(4, children->size());
this->EXPECT_HAS_DEFAULT_ENTRIES(*children);
this->EXPECT_HAS_FILE_ENTRY("myfile", *children);
this->EXPECT_HAS_DIR_ENTRY("mysubsubdir", *children);
this->EXPECT_HAS_ENTRIES(*children, {
FileEntry("myfile"),
DirEntry("mysubsubdir")
});
}
TYPED_TEST_P(FsppDirTest, CreateAndOpenFile_LoadAfterwards) {