Commit 1e300054 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Add/fix files utils & add unit tests

parent 2f50c910
......@@ -9,11 +9,27 @@ namespace file
std::string extension( const std::string& fileName )
{
auto pos = fileName.find_last_of( '.' );
if ( pos == std::string::npos || pos == fileName.length() )
if ( pos == std::string::npos )
return {};
return fileName.substr( pos + 1 );
}
std::string directory( const std::string& filePath )
{
auto pos = filePath.find_last_of( '/' );
if ( pos == std::string::npos )
return {};
return filePath.substr( 0, pos + 1 );
}
std::string fileName(const std::string& filePath)
{
auto pos = filePath.find_last_of( '/' );
if ( pos == std::string::npos )
return filePath;
return filePath.substr( pos + 1 );
}
}
}
......@@ -8,6 +8,8 @@ namespace utils
namespace file
{
std::string extension( const std::string& fileName );
std::string directory( const std::string& filePath );
std::string fileName( const std::string& filePath );
}
}
......@@ -43,6 +43,7 @@ list(APPEND TEST_SRCS
VideoTracks.cpp
AudioTracks.cpp
VLCMetadataServices.cpp
FsUtilsTests.cpp
)
add_executable(unittest ${TEST_SRCS})
......
#include "gtest/gtest.h"
#include "Utils.h"
TEST( FsUtils, extension )
{
ASSERT_EQ( "ext", utils::file::extension( "file.ext" ) );
ASSERT_EQ( "", utils::file::extension( "file." ) );
ASSERT_EQ( "ext2", utils::file::extension( "file.ext.ext2" ) );
ASSERT_EQ( "", utils::file::extension( "" ) );
ASSERT_EQ( "", utils::file::extension( "file.ext." ) );
}
TEST( FsUtils, directory )
{
ASSERT_EQ( "/a/b/c/", utils::file::directory( "/a/b/c/d.e" ) );
ASSERT_EQ( "", utils::file::directory( "" ) );
ASSERT_EQ( "", utils::file::directory( "file.test" ) );
}
TEST( FsUtils, fileName )
{
ASSERT_EQ( "d.e", utils::file::fileName( "/a/b/c/d.e" ) );
ASSERT_EQ( "noextfile", utils::file::fileName( "/a/b/noextfile" ) );
ASSERT_EQ( "file.test", utils::file::fileName( "file.test" ) );
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment