2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-06-29 15:31:42 +00:00
|
|
|
* Copyright (c) 2021, Max Wipfli <max.wipfli@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-04-25 05:53:23 +00:00
|
|
|
#include <LibTest/TestCase.h>
|
2019-08-07 19:07:02 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-05-26 11:52:44 +00:00
|
|
|
#include <AK/LexicalPath.h>
|
2019-08-07 19:07:02 +00:00
|
|
|
|
2021-06-29 15:31:42 +00:00
|
|
|
TEST_CASE(relative_path)
|
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt"sv, "/tmp"sv), "abc.txt"sv);
|
|
|
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt"sv, "/tmp/"sv), "abc.txt"sv);
|
|
|
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt"sv, "/"sv), "tmp/abc.txt"sv);
|
2022-12-11 23:33:34 +00:00
|
|
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt"sv, "/usr"sv), "../tmp/abc.txt"sv);
|
2021-06-29 15:31:42 +00:00
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/foo.txt"sv, "tmp"sv), ""sv);
|
|
|
|
EXPECT_EQ(LexicalPath::relative_path("tmp/foo.txt"sv, "/tmp"sv), ""sv);
|
2022-12-11 23:33:34 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/foo/bar/baz.txt"sv, "/tmp/bar/foo/"sv), "../../foo/bar/baz.txt"sv);
|
2021-06-29 15:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(regular_absolute_path)
|
2019-08-07 19:07:02 +00:00
|
|
|
{
|
2021-06-29 15:31:42 +00:00
|
|
|
LexicalPath path("/home/anon/foo.txt");
|
|
|
|
EXPECT_EQ(path.string(), "/home/anon/foo.txt");
|
|
|
|
EXPECT_EQ(path.dirname(), "/home/anon");
|
|
|
|
EXPECT_EQ(path.basename(), "foo.txt");
|
|
|
|
EXPECT_EQ(path.title(), "foo");
|
2019-08-07 19:07:02 +00:00
|
|
|
EXPECT_EQ(path.extension(), "txt");
|
2022-07-11 17:32:29 +00:00
|
|
|
EXPECT(path.has_extension(".txt"sv));
|
|
|
|
EXPECT(path.has_extension("txt"sv));
|
|
|
|
EXPECT(!path.has_extension("txxt"sv));
|
2021-06-29 15:31:42 +00:00
|
|
|
EXPECT_EQ(path.parts_view().size(), 3u);
|
|
|
|
EXPECT_EQ(path.parts_view()[0], "home");
|
|
|
|
EXPECT_EQ(path.parts_view()[1], "anon");
|
|
|
|
EXPECT_EQ(path.parts_view()[2], "foo.txt");
|
2019-08-07 19:07:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 15:31:42 +00:00
|
|
|
TEST_CASE(regular_relative_path)
|
2019-08-07 19:07:02 +00:00
|
|
|
{
|
2021-06-29 15:31:42 +00:00
|
|
|
LexicalPath path("anon/foo.txt");
|
|
|
|
EXPECT_EQ(path.dirname(), "anon");
|
|
|
|
EXPECT_EQ(path.basename(), "foo.txt");
|
|
|
|
EXPECT_EQ(path.parts_view().size(), 2u);
|
|
|
|
EXPECT_EQ(path.parts_view()[0], "anon");
|
|
|
|
EXPECT_EQ(path.parts_view()[1], "foo.txt");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(single_dot)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LexicalPath path("/home/./anon/foo.txt");
|
|
|
|
EXPECT_EQ(path.string(), "/home/anon/foo.txt");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
LexicalPath path("./test.txt");
|
|
|
|
EXPECT_EQ(path.string(), "test.txt");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
LexicalPath path("./../test.txt");
|
|
|
|
EXPECT_EQ(path.string(), "../test.txt");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(relative_path_with_dotdot)
|
|
|
|
{
|
|
|
|
LexicalPath path("anon/../../foo.txt");
|
|
|
|
EXPECT_EQ(path.string(), "../foo.txt");
|
|
|
|
EXPECT_EQ(path.dirname(), "..");
|
|
|
|
EXPECT_EQ(path.basename(), "foo.txt");
|
|
|
|
EXPECT_EQ(path.parts_view().size(), 2u);
|
|
|
|
EXPECT_EQ(path.parts_view()[0], "..");
|
|
|
|
EXPECT_EQ(path.parts_view()[1], "foo.txt");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(absolute_path_with_dotdot)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LexicalPath path("/test/../foo.txt");
|
|
|
|
EXPECT_EQ(path.string(), "/foo.txt");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
LexicalPath path("/../../foo.txt");
|
|
|
|
EXPECT_EQ(path.string(), "/foo.txt");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(more_dotdot_paths)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(LexicalPath::canonicalized_path("/home/user/../../not/home"), "/not/home");
|
|
|
|
EXPECT_EQ(LexicalPath::canonicalized_path("/../../../../"), "/");
|
|
|
|
EXPECT_EQ(LexicalPath::canonicalized_path("./../../../../"), "../../../..");
|
|
|
|
EXPECT_EQ(LexicalPath::canonicalized_path("../../../../../"), "../../../../..");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(the_root_path)
|
|
|
|
{
|
|
|
|
LexicalPath path("/");
|
|
|
|
EXPECT_EQ(path.dirname(), "/");
|
|
|
|
EXPECT_EQ(path.basename(), "/");
|
|
|
|
EXPECT_EQ(path.title(), "/");
|
|
|
|
EXPECT_EQ(path.parts_view().size(), 0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(the_dot_path)
|
|
|
|
{
|
|
|
|
LexicalPath path(".");
|
|
|
|
EXPECT_EQ(path.string(), ".");
|
|
|
|
EXPECT_EQ(path.dirname(), ".");
|
|
|
|
EXPECT_EQ(path.basename(), ".");
|
|
|
|
EXPECT_EQ(path.title(), ".");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(double_slash)
|
|
|
|
{
|
|
|
|
LexicalPath path("//home/anon/foo.txt");
|
|
|
|
EXPECT_EQ(path.string(), "/home/anon/foo.txt");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(trailing_slash)
|
|
|
|
{
|
|
|
|
LexicalPath path("/home/anon/");
|
|
|
|
EXPECT_EQ(path.string(), "/home/anon");
|
|
|
|
EXPECT_EQ(path.dirname(), "/home");
|
|
|
|
EXPECT_EQ(path.basename(), "anon");
|
|
|
|
EXPECT_EQ(path.parts_view().size(), 2u);
|
2019-08-07 19:07:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 10:55:07 +00:00
|
|
|
TEST_CASE(resolve_absolute_path)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(LexicalPath::absolute_path("/home/anon", "foo.txt"), "/home/anon/foo.txt");
|
|
|
|
EXPECT_EQ(LexicalPath::absolute_path("/home/anon/", "foo.txt"), "/home/anon/foo.txt");
|
|
|
|
EXPECT_EQ(LexicalPath::absolute_path("/home/anon", "././foo.txt"), "/home/anon/foo.txt");
|
|
|
|
EXPECT_EQ(LexicalPath::absolute_path("/home/anon/quux", "../foo.txt"), "/home/anon/foo.txt");
|
|
|
|
EXPECT_EQ(LexicalPath::absolute_path("/home/anon/quux", "../test/foo.txt"), "/home/anon/test/foo.txt");
|
|
|
|
EXPECT_EQ(LexicalPath::absolute_path("quux", "../test/foo.txt"), "test/foo.txt");
|
|
|
|
EXPECT_EQ(LexicalPath::absolute_path("quux", "../../test/foo.txt"), "../test/foo.txt");
|
|
|
|
EXPECT_EQ(LexicalPath::absolute_path("quux/bar", "../../test/foo.txt"), "test/foo.txt");
|
|
|
|
EXPECT_EQ(LexicalPath::absolute_path("quux/bar/", "../../test/foo.txt"), "test/foo.txt");
|
|
|
|
}
|
|
|
|
|
2020-05-26 09:12:18 +00:00
|
|
|
TEST_CASE(has_extension)
|
|
|
|
{
|
|
|
|
{
|
2020-08-02 17:48:48 +00:00
|
|
|
LexicalPath path("/tmp/simple.png");
|
2022-07-11 17:32:29 +00:00
|
|
|
EXPECT(path.has_extension(".png"sv));
|
|
|
|
EXPECT(path.has_extension(".pnG"sv));
|
|
|
|
EXPECT(path.has_extension(".PNG"sv));
|
2020-05-26 09:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-08-02 17:48:48 +00:00
|
|
|
LexicalPath path("/TMP/SIMPLE.PNG");
|
2022-07-11 17:32:29 +00:00
|
|
|
EXPECT(path.has_extension(".png"sv));
|
|
|
|
EXPECT(path.has_extension(".pnG"sv));
|
|
|
|
EXPECT(path.has_extension(".PNG"sv));
|
2020-05-26 09:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-08-02 17:48:48 +00:00
|
|
|
LexicalPath path(".png");
|
2022-07-11 17:32:29 +00:00
|
|
|
EXPECT(path.has_extension(".png"sv));
|
2020-05-26 09:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-08-02 17:48:48 +00:00
|
|
|
LexicalPath path("png");
|
2022-07-11 17:32:29 +00:00
|
|
|
EXPECT_EQ(path.has_extension(".png"sv), false);
|
2020-05-26 09:12:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 15:31:42 +00:00
|
|
|
TEST_CASE(join)
|
2021-04-03 22:48:26 +00:00
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
EXPECT_EQ(LexicalPath::join("anon"sv, "foo.txt"sv).string(), "anon/foo.txt"sv);
|
|
|
|
EXPECT_EQ(LexicalPath::join("/home"sv, "anon/foo.txt"sv).string(), "/home/anon/foo.txt"sv);
|
|
|
|
EXPECT_EQ(LexicalPath::join("/"sv, "foo.txt"sv).string(), "/foo.txt"sv);
|
|
|
|
EXPECT_EQ(LexicalPath::join("/home"sv, "anon"sv, "foo.txt"sv).string(), "/home/anon/foo.txt"sv);
|
2021-04-03 22:48:26 +00:00
|
|
|
}
|
2021-05-27 12:03:55 +00:00
|
|
|
|
2021-06-29 15:31:42 +00:00
|
|
|
TEST_CASE(append)
|
2021-05-27 12:03:55 +00:00
|
|
|
{
|
2021-06-29 15:55:12 +00:00
|
|
|
LexicalPath path("/home/anon/");
|
2022-07-11 17:32:29 +00:00
|
|
|
auto new_path = path.append("foo.txt"sv);
|
2021-06-29 15:55:12 +00:00
|
|
|
EXPECT_EQ(new_path.string(), "/home/anon/foo.txt");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(parent)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LexicalPath path("/home/anon/foo.txt");
|
|
|
|
auto parent = path.parent();
|
|
|
|
EXPECT_EQ(parent.string(), "/home/anon");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
LexicalPath path("anon/foo.txt");
|
|
|
|
auto parent = path.parent();
|
|
|
|
EXPECT_EQ(parent.string(), "anon");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
LexicalPath path("foo.txt");
|
|
|
|
auto parent = path.parent();
|
|
|
|
EXPECT_EQ(parent.string(), ".");
|
|
|
|
auto parent_of_parent = parent.parent();
|
|
|
|
EXPECT_EQ(parent_of_parent.string(), "..");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
LexicalPath path("/");
|
|
|
|
auto parent = path.parent();
|
|
|
|
EXPECT_EQ(parent.string(), "/");
|
|
|
|
}
|
2021-05-27 12:03:55 +00:00
|
|
|
}
|
2022-06-23 22:40:21 +00:00
|
|
|
|
|
|
|
TEST_CASE(is_child_of)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LexicalPath parent("/a/parent/directory");
|
|
|
|
LexicalPath child("/a/parent/directory/a/child");
|
|
|
|
LexicalPath mismatching("/not/a/child/directory");
|
|
|
|
EXPECT(child.is_child_of(parent));
|
|
|
|
EXPECT(child.is_child_of(child));
|
|
|
|
EXPECT(parent.is_child_of(parent));
|
|
|
|
EXPECT(!parent.is_child_of(child));
|
|
|
|
EXPECT(!mismatching.is_child_of(parent));
|
|
|
|
|
|
|
|
EXPECT(parent.is_child_of(parent.parent()));
|
|
|
|
EXPECT(child.parent().parent().is_child_of(parent));
|
|
|
|
EXPECT(!child.parent().parent().parent().is_child_of(parent));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
LexicalPath root("/");
|
|
|
|
EXPECT(LexicalPath("/").is_child_of(root));
|
|
|
|
EXPECT(LexicalPath("/any").is_child_of(root));
|
|
|
|
EXPECT(LexicalPath("/child/directory").is_child_of(root));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
LexicalPath relative("folder");
|
|
|
|
LexicalPath relative_child("folder/sub");
|
|
|
|
LexicalPath absolute("/folder");
|
|
|
|
LexicalPath absolute_child("/folder/sub");
|
|
|
|
EXPECT(relative_child.is_child_of(relative));
|
|
|
|
EXPECT(absolute_child.is_child_of(absolute));
|
|
|
|
|
|
|
|
EXPECT(relative.is_child_of(absolute));
|
|
|
|
EXPECT(relative.is_child_of(absolute_child));
|
|
|
|
EXPECT(relative_child.is_child_of(absolute));
|
|
|
|
EXPECT(relative_child.is_child_of(absolute_child));
|
|
|
|
|
|
|
|
EXPECT(!absolute.is_child_of(relative));
|
|
|
|
EXPECT(!absolute_child.is_child_of(relative));
|
|
|
|
EXPECT(!absolute_child.is_child_of(relative_child));
|
|
|
|
}
|
|
|
|
}
|