From 1f9a981e6a76401d88809808fbec8adc10a1e8d1 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 6 Jul 2016 19:21:35 -0400 Subject: [PATCH] add initial Path class --- src/core/Path.cc | 33 +++++++++++++++++++++++ src/core/Path.h | 13 +++++++++ test/src/test_Path.cc | 63 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 src/core/Path.cc create mode 100644 src/core/Path.h create mode 100644 test/src/test_Path.cc diff --git a/src/core/Path.cc b/src/core/Path.cc new file mode 100644 index 0000000..366e851 --- /dev/null +++ b/src/core/Path.cc @@ -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; +} diff --git a/src/core/Path.h b/src/core/Path.h new file mode 100644 index 0000000..b21f37a --- /dev/null +++ b/src/core/Path.h @@ -0,0 +1,13 @@ +#ifndef PATH_H +#define PATH_H + +#include + +class Path +{ +public: + static std::string dirname(const std::string & s); + static std::string join(const std::string & first, const std::string & second); +}; + +#endif diff --git a/test/src/test_Path.cc b/test/src/test_Path.cc new file mode 100644 index 0000000..2212905 --- /dev/null +++ b/test/src/test_Path.cc @@ -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")); +}