utility: added FilePath abstraction and rewrote data management to use it

The utility class FilePath wraps an instance of boost::filesystem::path. Using FilePath allows for easy comparision of
file paths on different platforms using absolute or relative paths. Whereever file paths are compared, this wrapper
should be used.
This commit is contained in:
Eberhard Graether
2015-03-09 01:51:32 +01:00
parent 7014c8ac4c
commit df888e4151
37 changed files with 387 additions and 126 deletions
+2 -1
View File
@@ -5,7 +5,7 @@ add_files(
helper/TestFileManager.h
helper/TestStorage.cpp
helper/TestStorage.h
TestSuiteFixture.cpp
TestSuiteFixture.h
@@ -14,6 +14,7 @@ add_files(
DataTypeTestSuite.h
DictionaryTestSuite.h
FileManagerTestSuite.h
FilePathTestSuite.h
FileSystemTestSuite.h
GraphTestSuite.h
GraphFilterTestSuite.h
+3 -3
View File
@@ -1687,9 +1687,9 @@ public:
TestParserClient client;
CxxParser parser(&client, &fm);
std::vector<std::string> filePaths;
filePaths.push_back("data/CxxParserTestSuite/header.h");
filePaths.push_back("data/CxxParserTestSuite/code.cpp");
std::vector<FilePath> filePaths;
filePaths.push_back(FilePath("data/CxxParserTestSuite/header.h"));
filePaths.push_back(FilePath("data/CxxParserTestSuite/code.cpp"));
parser.parseFiles(filePaths, std::vector<std::string>(), std::vector<std::string>());
TS_ASSERT_EQUALS(client.errors.size(), 0);
+127
View File
@@ -0,0 +1,127 @@
#include "cxxtest/TestSuite.h"
#include "utility/file/FilePath.h"
class FilePathTestSuite : public CxxTest::TestSuite
{
public:
void test_file_path_gets_created_with_char_array()
{
FilePath path("data/FilePathTestSuite/main.cpp");
TS_ASSERT_EQUALS(path.str(), "data/FilePathTestSuite/main.cpp");
}
void test_file_path_gets_created_with_string()
{
std::string str("data/FilePathTestSuite/main.cpp");
FilePath path(str);
TS_ASSERT_EQUALS(path.str(), str);
}
void test_file_path_gets_created_other_file_path()
{
FilePath path("data/FilePathTestSuite/main.cpp");
FilePath path2(path);
TS_ASSERT_EQUALS(path, path2);
}
void test_file_path_exists()
{
FilePath path("data/FilePathTestSuite/a.cpp");
TS_ASSERT(path.exists());
}
void test_file_path_not_exists()
{
FilePath path("data/FilePathTestSuite/a.h");
TS_ASSERT(!path.exists());
}
void test_file_path_file_name()
{
FilePath path("data/FilePathTestSuite/abc.h");
TS_ASSERT_EQUALS(path.fileName(), "abc.h");
}
void test_file_path_extension()
{
FilePath path("data/FilePathTestSuite/a.h");
TS_ASSERT_EQUALS(path.extension(), ".h");
}
void test_file_path_without_extension()
{
FilePath path("data/FilePathTestSuite/a.h");
TS_ASSERT_EQUALS(path.withoutExtension(), "data/FilePathTestSuite/a");
}
void test_file_path_has_extension()
{
std::vector<std::string> extensions;
extensions.push_back(".h");
extensions.push_back(".cpp");
extensions.push_back(".cc");
TS_ASSERT(FilePath("data/FilePathTestSuite/a.h").hasExtension(extensions));
TS_ASSERT(FilePath("data/FilePathTestSuite/b.cpp").hasExtension(extensions));
TS_ASSERT(!FilePath("data/FilePathTestSuite/a.m").hasExtension(extensions));
}
void test_file_path_equals_file_with_different_relative_paths()
{
FilePath pathA("data/FilePathTestSuite/a.cpp");
FilePath pathA2("data/../data/FilePathTestSuite/./a.cpp");
TS_ASSERT_EQUALS(pathA, pathA2);
}
void test_file_path_equals_relative_and_absolute_paths()
{
FilePath pathA("data/FilePathTestSuite/a.cpp");
FilePath pathA2(pathA.absoluteStr());
TS_ASSERT_EQUALS(pathA, pathA2);
}
void test_file_path_compares_paths_with_posix_and_windows_format()
{
#ifdef _WIN32
FilePath pathB("data/FilePathTestSuite/b.cc");
FilePath pathB2("data\\FilePathTestSuite\\b.cc");
TS_ASSERT_EQUALS(pathB, pathB2);
#endif
}
void test_file_path_differs_for_different_existing_files()
{
FilePath pathA("data/FilePathTestSuite/a.cpp");
FilePath pathB("data/FilePathTestSuite/b.cc");
TS_ASSERT_DIFFERS(pathA, pathB);
}
void test_file_path_differs_for_different_nonexisting_files()
{
FilePath pathA("data/FilePathTestSuite/a.h");
FilePath pathB("data/FilePathTestSuite/b.c");
TS_ASSERT_DIFFERS(pathA, pathB);
}
void test_file_path_differs_for_existing_and_nonexisting_files()
{
FilePath pathA("data/FilePathTestSuite/a.h");
FilePath pathB("data/FilePathTestSuite/b.cc");
TS_ASSERT_DIFFERS(pathA, pathB);
}
};
+13 -13
View File
@@ -573,8 +573,8 @@ public:
TS_ASSERT_EQUALS(storage.tokenLocationCollection().getTokenLocations().size(), 4);
TS_ASSERT_EQUALS(storage.searchIndex().getNodeCount(), 3);
std::set<std::string> files;
files.insert(m_filePath);
std::set<FilePath> files;
files.insert(FilePath(m_filePath));
storage.clearFileData(files);
TS_ASSERT_EQUALS(storage.graph().getNodeCount(), 0);
@@ -604,8 +604,8 @@ public:
TS_ASSERT_EQUALS(storage.tokenLocationCollection().getTokenLocations().size(), 9);
TS_ASSERT_EQUALS(storage.searchIndex().getNodeCount(), 6);
std::set<std::string> files;
files.insert("file.cpp");
std::set<FilePath> files;
files.insert(FilePath("file.cpp"));
storage.clearFileData(files);
TS_ASSERT_EQUALS(storage.graph().getNodeCount(), 3);
@@ -635,8 +635,8 @@ public:
TS_ASSERT_EQUALS(storage.tokenLocationCollection().getTokenLocations().size(), 9);
TS_ASSERT_EQUALS(storage.searchIndex().getNodeCount(), 5);
std::set<std::string> files;
files.insert("file.h");
std::set<FilePath> files;
files.insert(FilePath("file.h"));
storage.clearFileData(files);
TS_ASSERT_EQUALS(storage.graph().getNodeCount(), 4);
@@ -666,9 +666,9 @@ public:
TS_ASSERT_EQUALS(storage.tokenLocationCollection().getTokenLocations().size(), 9);
TS_ASSERT_EQUALS(storage.searchIndex().getNodeCount(), 5);
std::set<std::string> filePaths;
filePaths.insert("file.cpp");
filePaths.insert("file.h");
std::set<FilePath> filePaths;
filePaths.insert(FilePath("file.cpp"));
filePaths.insert(FilePath("file.h"));
storage.clearFileData(filePaths);
TS_ASSERT_EQUALS(storage.graph().getNodeCount(), 0);
@@ -722,12 +722,12 @@ public:
std::string name1 = storage.getNodeWithId(id2)->getFullName();
std::string name2 = storage.getNodeWithId(id3)->getFullName();
std::set<std::string> filePaths;
filePaths.insert(name1);
std::set<std::string> dependingFilePaths = storage.getDependingFilePathsAndRemoveFileNodes(filePaths);
std::set<FilePath> filePaths;
filePaths.insert(FilePath(name1));
std::set<FilePath> dependingFilePaths = storage.getDependingFilePathsAndRemoveFileNodes(filePaths);
TS_ASSERT_EQUALS(dependingFilePaths.size(), 1);
TS_ASSERT_EQUALS(*dependingFilePaths.begin(), name2);
TS_ASSERT_EQUALS(dependingFilePaths.begin()->str(), name2);
TS_ASSERT(storage.getNodeWithId(id1));
TS_ASSERT(!storage.getNodeWithId(id2));
+1 -1
View File
@@ -69,7 +69,7 @@ public:
TS_ASSERT_EQUALS(3, a->getColumnNumber());
TS_ASSERT_EQUALS(4, a->getOtherTokenLocation()->getLineNumber());
TS_ASSERT_EQUALS(5, a->getOtherTokenLocation()->getColumnNumber());
TS_ASSERT_EQUALS("file.c", a->getFilePath());
TS_ASSERT_EQUALS("file.c", a->getFilePath().str());
}
void test_finding_token_locations_by_id()
+3 -3
View File
@@ -10,17 +10,17 @@ TestFileManager::TestFileManager()
{
}
bool TestFileManager::hasFilePath(const std::string& filePath) const
bool TestFileManager::hasFilePath(const FilePath& filePath) const
{
return true;
}
bool TestFileManager::hasSourceExtension(const std::string& filePath) const
bool TestFileManager::hasSourceExtension(const FilePath& filePath) const
{
return true;
}
bool TestFileManager::hasIncludeExtension(const std::string& filePath) const
bool TestFileManager::hasIncludeExtension(const FilePath& filePath) const
{
return true;
}
+3 -3
View File
@@ -8,9 +8,9 @@ class TestFileManager: public FileManager
public:
TestFileManager();
virtual bool hasFilePath(const std::string& filePath) const;
virtual bool hasSourceExtension(const std::string& filePath) const;
virtual bool hasIncludeExtension(const std::string& filePath) const;
virtual bool hasFilePath(const FilePath& filePath) const;
virtual bool hasSourceExtension(const FilePath& filePath) const;
virtual bool hasIncludeExtension(const FilePath& filePath) const;
};
#endif // TEST_FILE_MANAGER_H