소스 검색

Extended File class

- added "getParent" method
- extended tests for File class
Patrick 4 년 전
부모
커밋
1b1ab7a2f8
3개의 변경된 파일21개의 추가작업 그리고 0개의 파일을 삭제
  1. 14 0
      source/io/File.cpp
  2. 1 0
      source/io/File.hpp
  3. 6 0
      test/cases/io/FileTest.cpp

+ 14 - 0
source/io/File.cpp

@@ -85,6 +85,20 @@ std::string ls_std::File::getName()
   return std::string(base, copy.end());
 }
 
+std::string ls_std::File::getParent()
+{
+  std::string parent {};
+  std::vector<std::string> subDirectoryNames = ls_std::File::_splitIntoSubDirectoryNames(this->absoluteFilePath);
+  const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
+  subDirectoryNames.pop_back();
+
+  for(auto const& subDirectoryName : subDirectoryNames) {
+    parent += subDirectoryName + separator;
+  }
+
+  return parent;
+}
+
 long ls_std::File::getSize()
 {
   std::streampos fileSize {};

+ 1 - 0
source/io/File.hpp

@@ -33,6 +33,7 @@ namespace ls_std {
       bool exists();
       std::string getAbsoluteFilePath();
       std::string getName();
+      std::string getParent();
       long getSize();
       bool isDirectory();
       bool isFile();

+ 6 - 0
test/cases/io/FileTest.cpp

@@ -117,6 +117,12 @@ namespace {
     ASSERT_STREQ("resources", directory.getName().c_str());
   }
 
+  TEST_F(FileTest, getParent)
+  {
+    ls_std::File file {this->fileLocation};
+    ASSERT_STREQ(TestHelper::getResourcesFolderLocation().c_str(), file.getParent().c_str());
+  }
+
   TEST_F(FileTest, getSize)
   {
     ls_std::File file {this->fileLocation};