2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-08-07 22:07:02 +03:00
|
|
|
#include <AK/TestSuite.h>
|
|
|
|
|
2020-05-26 14:52:44 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
2019-09-06 16:34:26 +03:00
|
|
|
#include <AK/String.h>
|
2019-08-07 22:07:02 +03:00
|
|
|
|
|
|
|
TEST_CASE(construct)
|
|
|
|
{
|
2020-05-26 14:52:44 +03:00
|
|
|
EXPECT_EQ(LexicalPath().is_valid(), false);
|
2019-08-07 22:07:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(basic)
|
|
|
|
{
|
2020-05-26 14:52:44 +03:00
|
|
|
LexicalPath path("/abc/def/ghi.txt");
|
2019-08-07 22:07:02 +03:00
|
|
|
EXPECT_EQ(path.is_valid(), true);
|
|
|
|
EXPECT_EQ(path.basename(), "ghi.txt");
|
|
|
|
EXPECT_EQ(path.title(), "ghi");
|
|
|
|
EXPECT_EQ(path.extension(), "txt");
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(path.parts().size(), 3u);
|
2019-08-07 22:07:02 +03:00
|
|
|
EXPECT_EQ(path.parts(), Vector<String>({ "abc", "def", "ghi.txt" }));
|
|
|
|
EXPECT_EQ(path.string(), "/abc/def/ghi.txt");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(dotdot_coalescing)
|
|
|
|
{
|
2020-05-26 14:52:44 +03:00
|
|
|
EXPECT_EQ(LexicalPath("/home/user/../../not/home").string(), "/not/home");
|
|
|
|
EXPECT_EQ(LexicalPath("/../../../../").string(), "/");
|
2019-08-07 22:07:02 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 12:12:18 +03:00
|
|
|
TEST_CASE(has_extension)
|
|
|
|
{
|
|
|
|
{
|
2020-08-02 20:48:48 +03:00
|
|
|
LexicalPath path("/tmp/simple.png");
|
2020-05-26 12:12:18 +03:00
|
|
|
EXPECT(path.has_extension(".png"));
|
|
|
|
EXPECT(path.has_extension(".pnG"));
|
|
|
|
EXPECT(path.has_extension(".PNG"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-08-02 20:48:48 +03:00
|
|
|
LexicalPath path("/TMP/SIMPLE.PNG");
|
2020-05-26 12:12:18 +03:00
|
|
|
EXPECT(path.has_extension(".png"));
|
|
|
|
EXPECT(path.has_extension(".pnG"));
|
|
|
|
EXPECT(path.has_extension(".PNG"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-08-02 20:48:48 +03:00
|
|
|
LexicalPath path(".png");
|
2020-05-26 12:12:18 +03:00
|
|
|
EXPECT(path.has_extension(".png"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-08-02 20:48:48 +03:00
|
|
|
LexicalPath path;
|
2020-05-26 12:12:18 +03:00
|
|
|
EXPECT_EQ(path.has_extension(".png"), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-08-02 20:48:48 +03:00
|
|
|
LexicalPath path("png");
|
2020-05-26 12:12:18 +03:00
|
|
|
EXPECT_EQ(path.has_extension(".png"), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 01:48:26 +03:00
|
|
|
TEST_CASE(relative_path)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt", "/tmp"), "abc.txt");
|
|
|
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt", "/tmp/"), "abc.txt");
|
|
|
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt", "/"), "tmp/abc.txt");
|
|
|
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt", "/usr"), "/tmp/abc.txt");
|
|
|
|
|
|
|
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/foo.txt", "tmp"), String {});
|
|
|
|
EXPECT_EQ(LexicalPath::relative_path("tmp/foo.txt", "/tmp"), String {});
|
|
|
|
}
|
|
|
|
|
2020-05-26 14:52:44 +03:00
|
|
|
TEST_MAIN(LexicalPath)
|