summaryrefslogtreecommitdiff
path: root/Tests/AK/TestLexicalPath.cpp
blob: 044f5f157a3ecfd8f63690d48567ac34d456f3b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibTest/TestCase.h>

#include <AK/LexicalPath.h>
#include <AK/String.h>

TEST_CASE(construct)
{
    EXPECT_EQ(LexicalPath().is_valid(), false);
}

TEST_CASE(basic)
{
    LexicalPath path("/abc/def/ghi.txt");
    EXPECT_EQ(path.is_valid(), true);
    EXPECT_EQ(path.basename(), "ghi.txt");
    EXPECT_EQ(path.title(), "ghi");
    EXPECT_EQ(path.extension(), "txt");
    EXPECT_EQ(path.parts().size(), 3u);
    EXPECT_EQ(path.parts(), Vector<String>({ "abc", "def", "ghi.txt" }));
    EXPECT_EQ(path.string(), "/abc/def/ghi.txt");
}

TEST_CASE(dotdot_coalescing)
{
    EXPECT_EQ(LexicalPath("/home/user/../../not/home").string(), "/not/home");
    EXPECT_EQ(LexicalPath("/../../../../").string(), "/");
}

TEST_CASE(has_extension)
{
    {
        LexicalPath path("/tmp/simple.png");
        EXPECT(path.has_extension(".png"));
        EXPECT(path.has_extension(".pnG"));
        EXPECT(path.has_extension(".PNG"));
    }

    {
        LexicalPath path("/TMP/SIMPLE.PNG");
        EXPECT(path.has_extension(".png"));
        EXPECT(path.has_extension(".pnG"));
        EXPECT(path.has_extension(".PNG"));
    }

    {
        LexicalPath path(".png");
        EXPECT(path.has_extension(".png"));
    }

    {
        LexicalPath path;
        EXPECT_EQ(path.has_extension(".png"), false);
    }

    {
        LexicalPath path("png");
        EXPECT_EQ(path.has_extension(".png"), false);
    }
}

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