add initial Path class
This commit is contained in:
parent
b58c344604
commit
1f9a981e6a
33
src/core/Path.cc
Normal file
33
src/core/Path.cc
Normal file
@ -0,0 +1,33 @@
|
||||
#include "Path.h"
|
||||
|
||||
std::string Path::dirname(const std::string & s)
|
||||
{
|
||||
size_t pos = s.find_last_of("/\\");
|
||||
if (pos == std::string::npos)
|
||||
{
|
||||
return ".";
|
||||
}
|
||||
if ((pos == 0u) || (pos == 2u && s[1] == ':'))
|
||||
{
|
||||
return std::string(s, 0, pos + 1u);
|
||||
}
|
||||
return std::string(s, 0, pos);
|
||||
}
|
||||
|
||||
std::string Path::join(const std::string & first, const std::string & second)
|
||||
{
|
||||
if (first == "")
|
||||
{
|
||||
return second;
|
||||
}
|
||||
if (second == "")
|
||||
{
|
||||
return first;
|
||||
}
|
||||
char last = first[first.size() - 1u];
|
||||
if ((last == '/') || (last == '\\'))
|
||||
{
|
||||
return first + second;
|
||||
}
|
||||
return first + "/" + second;
|
||||
}
|
13
src/core/Path.h
Normal file
13
src/core/Path.h
Normal file
@ -0,0 +1,13 @@
|
||||
#ifndef PATH_H
|
||||
#define PATH_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class Path
|
||||
{
|
||||
public:
|
||||
static std::string dirname(const std::string & s);
|
||||
static std::string join(const std::string & first, const std::string & second);
|
||||
};
|
||||
|
||||
#endif
|
63
test/src/test_Path.cc
Normal file
63
test/src/test_Path.cc
Normal file
@ -0,0 +1,63 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "Path.h"
|
||||
|
||||
TEST(Path_dirname, returns_dot_when_no_directory_component)
|
||||
{
|
||||
EXPECT_EQ(".", Path::dirname("a.file"));
|
||||
}
|
||||
|
||||
TEST(Path_dirname, returns_dirname_for_relative_path)
|
||||
{
|
||||
EXPECT_EQ("a/dir", Path::dirname("a/dir/file"));
|
||||
}
|
||||
|
||||
TEST(Path_dirname, returns_single_slash_for_short_absolute_path)
|
||||
{
|
||||
EXPECT_EQ("/", Path::dirname("/usr"));
|
||||
}
|
||||
|
||||
TEST(Path_dirname, returns_full_absolute_dirname_for_long_absolute_path)
|
||||
{
|
||||
EXPECT_EQ("/usr/share", Path::dirname("/usr/share/pixmaps"));
|
||||
}
|
||||
|
||||
TEST(Path_dirname, returns_drive_letter_for_forward_slash_windows_path)
|
||||
{
|
||||
EXPECT_EQ("C:/", Path::dirname("C:/Data"));
|
||||
}
|
||||
|
||||
TEST(Path_dirname, returns_drive_letter_for_windows_path)
|
||||
{
|
||||
EXPECT_EQ("C:\\", Path::dirname("C:\\Data"));
|
||||
}
|
||||
|
||||
TEST(Path_dirname, returns_dirname_for_absolute_windows_path)
|
||||
{
|
||||
EXPECT_EQ("X:\\a\\b\\c", Path::dirname("X:\\a\\b\\c\\d"));
|
||||
}
|
||||
|
||||
TEST(Path_dirname, returns_single_slash_for_windows_absolute_path)
|
||||
{
|
||||
EXPECT_EQ("\\", Path::dirname("\\foo"));
|
||||
}
|
||||
|
||||
|
||||
TEST(Path_join, returns_joined_path)
|
||||
{
|
||||
EXPECT_EQ("path1/path2", Path::join("path1", "path2"));
|
||||
}
|
||||
|
||||
TEST(Path_join, returns_first_path_when_second_is_empty)
|
||||
{
|
||||
EXPECT_EQ("path1", Path::join("path1", ""));
|
||||
}
|
||||
|
||||
TEST(Path_join, returns_second_path_when_first_is_empty)
|
||||
{
|
||||
EXPECT_EQ("path2", Path::join("", "path2"));
|
||||
}
|
||||
|
||||
TEST(Path_join, does_not_add_extra_slash_when_first_path_already_ends_with_one)
|
||||
{
|
||||
EXPECT_EQ("/var/run", Path::join("/var/", "run"));
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user