#include "gtest/gtest.h"
#include "Path.h"
#include <algorithm>

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"));
}

TEST(Path_join, allows_arbitrary_number_of_arguments)
{
    EXPECT_EQ("/usr/local/share/jes/runtime", Path::join("/usr/local/", "share", "jes/", "runtime"));
}


TEST(Path_is_file, returns_true_for_file)
{
    EXPECT_TRUE(Path::is_file("test/src/test_Path.cc"));
}

TEST(Path_is_file, returns_false_for_directory)
{
    EXPECT_FALSE(Path::is_file("test"));
}

TEST(Path_is_file, returns_false_for_nonexistent_path)
{
    EXPECT_FALSE(Path::is_file("non.existent"));
}


TEST(Path_is_dir, returns_true_for_directory)
{
    EXPECT_TRUE(Path::is_dir("test/src"));
}

TEST(Path_is_dir, returns_false_for_file)
{
    EXPECT_FALSE(Path::is_dir("test/src/test_Path.cc"));
}

TEST(Path_is_dir, returns_false_for_nonexistent_path)
{
    EXPECT_FALSE(Path::is_dir("non.existent"));
}


TEST(Path_listdir, returns_list_of_directory_contents)
{
    auto dir_ents = Path::listdir("test");
    EXPECT_NE(dir_ents->end(), std::find_if(dir_ents->begin(), dir_ents->end(), [](std::shared_ptr<std::string> & s) { return *s == "."; }));
    EXPECT_NE(dir_ents->end(), std::find_if(dir_ents->begin(), dir_ents->end(), [](std::shared_ptr<std::string> & s) { return *s == ".."; }));
    EXPECT_NE(dir_ents->end(), std::find_if(dir_ents->begin(), dir_ents->end(), [](std::shared_ptr<std::string> & s) { return *s == "src"; }));
    EXPECT_EQ(dir_ents->end(), std::find_if(dir_ents->begin(), dir_ents->end(), [](std::shared_ptr<std::string> & s) { return *s == "foo"; }));
}
