Skip to content

Commit

Permalink
TESTS: Add unit tests for FilePath
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Dec 21, 2016
1 parent 401659b commit 0227a90
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
170 changes: 170 additions & 0 deletions tests/common/filepath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/* 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 FilePath utility class.
*/

/* NOTE: The following methods can't be tested (at the moment?) because
* their behaviour is operating-system-dependant:
* - Common::FilePath::isAbsolute()
* - Common::FilePath::absolutize()
* - Common::FilePath::normalize()
* - Common::FilePath::canonicalize()
*
* The following methods can't be tested because their behaviour changes
* depending on the file and directory structure:
* - Common::FilePath::findSubDirectory()
* - Common::FilePath::getSubDirectories()
* - Common::FilePath::createDirectories()
*
* The following methods can't be tested because their behaviour changes
* both with the operating system and user configuration:
* - Common::FilePath::getHomeDirectory()
* - Common::FilePath::getConfigDirectory()
* - Common::FilePath::getUserDataDirectory()
* - Common::FilePath::getUserDataFile()
*/

#include <cstdio>

#include "gtest/gtest.h"

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

static const char *kFilename = "tests/common/testfile_filepath.file";
static const char *kFilenameFake = "tests/common/testfile_filepath_nope.file";
static const char *kDirectory = ".";

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);
}

GTEST_TEST(FilePath, isRegularFile) {
createTestFile(kFilename);

EXPECT_TRUE (Common::FilePath::isRegularFile(kFilename ));
EXPECT_FALSE(Common::FilePath::isRegularFile(kFilenameFake));
EXPECT_FALSE(Common::FilePath::isRegularFile(kDirectory ));
}

GTEST_TEST(FilePath, isDirectory) {
createTestFile(kFilename);

EXPECT_FALSE(Common::FilePath::isDirectory(kFilename ));
EXPECT_FALSE(Common::FilePath::isDirectory(kFilenameFake));
EXPECT_TRUE (Common::FilePath::isDirectory(kDirectory ));
}

GTEST_TEST(FilePath, getFileSize) {
createTestFile(kFilename, 23);

EXPECT_EQ(Common::FilePath::getFileSize(kFilename ), 23);
EXPECT_EQ(Common::FilePath::getFileSize(kFilenameFake), Common::kFileInvalid);
EXPECT_EQ(Common::FilePath::getFileSize(kDirectory ), Common::kFileInvalid);
}

GTEST_TEST(FilePath, getFile) {
EXPECT_STREQ(Common::FilePath::getFile("/path/to/file.ext").c_str(), "file.ext");
EXPECT_STREQ(Common::FilePath::getFile("path/to/file.ext" ).c_str(), "file.ext");
EXPECT_STREQ(Common::FilePath::getFile("file.ext" ).c_str(), "file.ext");
EXPECT_STREQ(Common::FilePath::getFile("/path/to/" ).c_str(), ".");
}

GTEST_TEST(FilePath, getStem) {
EXPECT_STREQ(Common::FilePath::getStem("/path/to/file.ext").c_str(), "file");
EXPECT_STREQ(Common::FilePath::getStem("path/to/file.ext" ).c_str(), "file");
EXPECT_STREQ(Common::FilePath::getStem("file.ext" ).c_str(), "file");
EXPECT_STREQ(Common::FilePath::getStem("file.ext.ext" ).c_str(), "file.ext");
EXPECT_STREQ(Common::FilePath::getStem("file." ).c_str(), "file");
EXPECT_STREQ(Common::FilePath::getStem("file" ).c_str(), "file");
EXPECT_STREQ(Common::FilePath::getStem("/path/to/" ).c_str(), ".");
}

GTEST_TEST(FilePath, getExtension) {
EXPECT_STREQ(Common::FilePath::getExtension("/path/to/file.ext").c_str(), ".ext");
EXPECT_STREQ(Common::FilePath::getExtension("path/to/file.ext" ).c_str(), ".ext");
EXPECT_STREQ(Common::FilePath::getExtension("file.ext" ).c_str(), ".ext");
EXPECT_STREQ(Common::FilePath::getExtension("file.ext.ext" ).c_str(), ".ext");
EXPECT_STREQ(Common::FilePath::getExtension("file." ).c_str(), ".");
EXPECT_STREQ(Common::FilePath::getExtension("file" ).c_str(), "");
EXPECT_STREQ(Common::FilePath::getExtension("/path/to/" ).c_str(), "");
}

GTEST_TEST(FilePath, getDirectory) {
EXPECT_STREQ(Common::FilePath::getDirectory("/path/to/file.ext").c_str(), "/path/to");
EXPECT_STREQ(Common::FilePath::getDirectory("path/to/file.ext" ).c_str(), "path/to");
EXPECT_STREQ(Common::FilePath::getDirectory("file.ext" ).c_str(), "");
EXPECT_STREQ(Common::FilePath::getDirectory("/path/to/" ).c_str(), "/path/to");
}

GTEST_TEST(FilePath, changeExtension) {
EXPECT_STREQ(Common::FilePath::changeExtension("/path/file.ext", ".new" ).c_str(), "/path/file.new");
EXPECT_STREQ(Common::FilePath::changeExtension("/path/file.ext", ".new.m").c_str(), "/path/file.new.m");
EXPECT_STREQ(Common::FilePath::changeExtension("/path/file.ext", "new" ).c_str(), "/path/file.new");
EXPECT_STREQ(Common::FilePath::changeExtension("/path/file.ext", "" ).c_str(), "/path/file");
EXPECT_STREQ(Common::FilePath::changeExtension("file.ext.ext" , ".new" ).c_str(), "file.ext.new");
EXPECT_STREQ(Common::FilePath::changeExtension("file.ext.ext" , ".new.m").c_str(), "file.ext.new.m");
EXPECT_STREQ(Common::FilePath::changeExtension("file.ext.ext" , "new" ).c_str(), "file.ext.new");
EXPECT_STREQ(Common::FilePath::changeExtension("file.ext.ext" , "" ).c_str(), "file.ext");
EXPECT_STREQ(Common::FilePath::changeExtension("file." , ".new" ).c_str(), "file.new");
EXPECT_STREQ(Common::FilePath::changeExtension("file." , ".new.m").c_str(), "file.new.m");
EXPECT_STREQ(Common::FilePath::changeExtension("file." , "new" ).c_str(), "file.new");
EXPECT_STREQ(Common::FilePath::changeExtension("file." , "" ).c_str(), "file");
EXPECT_STREQ(Common::FilePath::changeExtension("file" , ".new" ).c_str(), "file.new");
EXPECT_STREQ(Common::FilePath::changeExtension("file" , ".new.m").c_str(), "file.new.m");
EXPECT_STREQ(Common::FilePath::changeExtension("file" , "new" ).c_str(), "file.new");
EXPECT_STREQ(Common::FilePath::changeExtension("file" , "" ).c_str(), "file");
EXPECT_STREQ(Common::FilePath::changeExtension("/path/" , ".new" ).c_str(), "/path/.new");
EXPECT_STREQ(Common::FilePath::changeExtension("/path/" , ".new.m").c_str(), "/path/.new.m");
EXPECT_STREQ(Common::FilePath::changeExtension("/path/" , "new" ).c_str(), "/path/.new");
EXPECT_STREQ(Common::FilePath::changeExtension("/path/" , "" ).c_str(), "/path/");
}

GTEST_TEST(FilePath, relativize) {
EXPECT_STREQ(Common::FilePath::relativize("/path/to", "/path/to/file.ext").c_str(), "file.ext");
EXPECT_STREQ(Common::FilePath::relativize("/other" , "/path/to/file.ext").c_str(), "");

/* Common::FilePath::relativize() only works with normalized paths...
* These here are common failure modes.
*/
EXPECT_STREQ(Common::FilePath::relativize("/path/to/", "/path/to/file.ext" ).c_str(), "ile.ext");
EXPECT_STREQ(Common::FilePath::relativize("/path/to" , "/path/to//file.ext").c_str(), "/file.ext");
EXPECT_STREQ(Common::FilePath::relativize("/path//to", "/path/to/file.ext" ).c_str(), "");
}

GTEST_TEST(FilePath, escapeStringLiteral) {
EXPECT_STREQ(Common::FilePath::escapeStringLiteral("/file.ext").c_str(), "\\/file\\.ext");
}
6 changes: 6 additions & 0 deletions tests/common/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,9 @@ check_PROGRAMS += tests/common/test_encoding_cp950
tests_common_test_encoding_cp950_SOURCES = tests/common/encoding_cp950.cpp
tests_common_test_encoding_cp950_LDADD = $(common_LIBS)
tests_common_test_encoding_cp950_CXXFLAGS = $(test_CXXFLAGS)

check_PROGRAMS += tests/common/test_filepath
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

0 comments on commit 0227a90

Please sign in to comment.