Skip to content

Commit

Permalink
TESTS: Add unit tests for FileList
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Dec 21, 2016
1 parent 0227a90 commit 5b6abbc
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 0 deletions.
182 changes: 182 additions & 0 deletions tests/common/filelist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* xoreos is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with xoreos. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* Unit tests for the FileList class.
*/

#include <cstdio>

#include "gtest/gtest.h"

#include "src/common/util.h"
#include "src/common/filelist.h"

static const char *kFilenameDirectory = "tests/common/testfile_filelist.file";
static const char *kFilename = "testfile_filelist.file";
static const char *kFilenameGlob = ".*/testfile_filelist.file";
static const char *kFilenameFake = "testfile_filelist_nope.file";
static const char *kFilenameFakeGlob = ".*/testfile_filelist_nope.file";
static const char *kDirectory = "tests/common/";

static void createTestFile(const char *filename, size_t size = 0) {
FILE *testFile = std::fopen(filename, "wb");
ASSERT_NE(testFile, static_cast<FILE *>(0));

static const byte kBuf[32] = { 0 };
while (size > 0) {
const size_t toWrite = MIN(size, sizeof(kBuf));

ASSERT_EQ(std::fwrite(kBuf, toWrite, 1, testFile), 1);

size -= toWrite;
}

ASSERT_EQ(std::fflush(testFile), 0);

std::fclose(testFile);
}

static const char *findPathFile(const char *path) {
const char *f = strrchr(path, '/');

return f ? (f + 1) : path;
}

GTEST_TEST(FileList, addDirectory) {
createTestFile(kFilenameDirectory);

Common::FileList list;

EXPECT_TRUE(list.empty());
EXPECT_EQ(list.size(), 0);

list.addDirectory(kDirectory);

EXPECT_FALSE(list.empty());
EXPECT_GE(list.size(), 1);
}

GTEST_TEST(FileList, copy) {
createTestFile(kFilenameDirectory);

const Common::FileList list1(kDirectory);
const Common::FileList list2(list1);

EXPECT_EQ(list1.size(), list2.size());

Common::FileList::const_iterator i1 = list1.begin();
Common::FileList::const_iterator i2 = list2.begin();
for ( ; i1 != list1.end() && i2 != list2.end(); ++i1, ++i2)
EXPECT_STREQ(i1->c_str(), i2->c_str());

EXPECT_EQ(i1, list1.end());
EXPECT_EQ(i2, list2.end());
}

GTEST_TEST(FileList, clear) {
createTestFile(kFilenameDirectory);

Common::FileList list(kDirectory);

EXPECT_FALSE(list.empty());
EXPECT_GE(list.size(), 1);

list.clear();

EXPECT_TRUE(list.empty());
EXPECT_EQ(list.size(), 0);
}

GTEST_TEST(FileList, contains) {
createTestFile(kFilenameDirectory);

const Common::FileList list(kDirectory);

EXPECT_TRUE (list.contains(kFilename , true));
EXPECT_FALSE(list.contains(kFilenameFake, true));
}

GTEST_TEST(FileList, containsGlob) {
createTestFile(kFilenameDirectory);

const Common::FileList list(kDirectory);

EXPECT_TRUE (list.containsGlob(kFilenameGlob , true));
EXPECT_FALSE(list.containsGlob(kFilenameFakeGlob, true));
}

GTEST_TEST(FileList, findFirst) {
createTestFile(kFilenameDirectory);

const Common::FileList list(kDirectory);
EXPECT_STREQ(list.findFirst(kFilenameFake, true).c_str(), "");

EXPECT_STREQ(findPathFile(list.findFirst(kFilename, true).c_str()), kFilename);
}

GTEST_TEST(FileList, findFirstGlob) {
createTestFile(kFilenameDirectory);

const Common::FileList list(kDirectory);
EXPECT_STREQ(list.findFirstGlob(kFilenameFakeGlob, true).c_str(), "");

EXPECT_STREQ(findPathFile(list.findFirstGlob(kFilenameGlob, true).c_str()), kFilename);
}

GTEST_TEST(FileList, getSubList) {
createTestFile(kFilenameDirectory);

const Common::FileList list(kDirectory);

Common::FileList subList1;
EXPECT_TRUE(list.getSubList(kFilename, true, subList1));

EXPECT_FALSE(subList1.empty());
EXPECT_GE(subList1.size(), 1);

EXPECT_TRUE(subList1.contains(kFilename, true));

Common::FileList subList2;
EXPECT_FALSE(list.getSubList(kFilenameFake, true, subList2));

EXPECT_TRUE(subList2.empty());
EXPECT_EQ(subList2.size(), 0);
}

GTEST_TEST(FileList, getSubListGlob) {
createTestFile(kFilenameDirectory);

const Common::FileList list(kDirectory);

Common::FileList subList1;
EXPECT_TRUE(list.getSubListGlob(kFilenameGlob, true, subList1));

EXPECT_FALSE(subList1.empty());
EXPECT_GE(subList1.size(), 1);

EXPECT_TRUE(subList1.contains(kFilename, true));

Common::FileList subList2;
EXPECT_FALSE(list.getSubListGlob(kFilenameFakeGlob, true, subList2));

EXPECT_TRUE(subList2.empty());
EXPECT_EQ(subList2.size(), 0);
}
6 changes: 6 additions & 0 deletions tests/common/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,9 @@ tests_common_test_filepath_SOURCES = tests/common/filepath.cpp
tests_common_test_filepath_LDADD = $(common_LIBS)
tests_common_test_filepath_CXXFLAGS = $(test_CXXFLAGS)
CLEANFILES += tests/common/testfile_filepath.file

check_PROGRAMS += tests/common/test_filelist
tests_common_test_filelist_SOURCES = tests/common/filelist.cpp
tests_common_test_filelist_LDADD = $(common_LIBS)
tests_common_test_filelist_CXXFLAGS = $(test_CXXFLAGS)
CLEANFILES += tests/common/testfile_filelist.file

0 comments on commit 5b6abbc

Please sign in to comment.