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

using namespace jes;

TEST(PathTest, construct_from_const_char_ptr)
{
    Path p("/a/b/c");
    EXPECT_EQ("/a/b/c", p.to_s());
}

TEST(PathTest, construct_from_std_string)
{
    std::string s("file.c");
    Path p(s);
    EXPECT_EQ(p.to_s(), "file.c");
}

TEST(PathTest, convert_backslashes_to_forward_slashes)
{
    Path p("C:\\foo\\bar.txt");
    EXPECT_EQ("C:/foo/bar.txt", p.to_s());
}

TEST(PathTest, joins_two_paths_together)
{
    EXPECT_EQ("C:/foo/../baz", Path("C:\\foo").join("../baz")->to_s());
    EXPECT_EQ("/xyz/f.txt", Path("/xyz/").join("f.txt")->to_s());
}

TEST(PathTest, dirname)
{
    EXPECT_EQ("", Path("").dirname()->to_s());
    EXPECT_EQ("/one/two", Path("/one/two/f.c").dirname()->to_s());
    EXPECT_EQ("C:/a", Path("C:\\a\\b\\").dirname()->to_s());
    EXPECT_EQ("/", Path("/").dirname()->to_s());
    EXPECT_EQ("/", Path("///foo").dirname()->to_s());
    EXPECT_EQ(".", Path("file.txt").dirname()->to_s());
    EXPECT_EQ(".", Path("folder//").dirname()->to_s());
    EXPECT_EQ("a", Path("a/b").dirname()->to_s());
}

TEST(PathTest, ext)
{
    EXPECT_EQ("", Path("").ext("")->to_s());
    EXPECT_EQ("", Path("").ext("h")->to_s());
    EXPECT_EQ("file.h", Path("file.c").ext(".h")->to_s());
    EXPECT_EQ("file.h", Path("file.c").ext("h")->to_s());
    EXPECT_EQ("/some/crazy/dir/foo.txt", Path("/some/crazy/dir/foo").ext(".txt")->to_s());
    EXPECT_EQ("/some/crazy/dir/foo.txt", Path("/some/crazy/dir/foo").ext("txt")->to_s());
    EXPECT_EQ("C:/baz", Path("C:\\baz.xyz").ext("")->to_s());
}

TEST(PathTest, exists)
{
    EXPECT_TRUE(Path("runtime").exists());
    EXPECT_FALSE(Path("foobar").exists());
}

TEST(PathTest, dir_entries)
{
    auto dir_entries = Path(".").dir_entries();
    EXPECT_TRUE(std::find(dir_entries.begin(), dir_entries.end(), "runtime") != dir_entries.end());
    EXPECT_FALSE(std::find(dir_entries.begin(), dir_entries.end(), "foobar") != dir_entries.end());
}
