blob: 522f4bc69ea01e8fac1851820cce0b0cf9be635b [file] [log] [blame]
Pavel Labathcc0e87c2016-03-11 08:44:44 +00001//===-- FileSpecTest.cpp ----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "gtest/gtest.h"
11
12#include "lldb/Host/FileSpec.h"
13
14using namespace lldb_private;
15
16TEST(FileSpecTest, FileAndDirectoryComponents)
17{
18 FileSpec fs_posix("/foo/bar", false, FileSpec::ePathSyntaxPosix);
19 EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
20 EXPECT_STREQ("/foo", fs_posix.GetDirectory().GetCString());
21 EXPECT_STREQ("bar", fs_posix.GetFilename().GetCString());
22
23 FileSpec fs_windows("F:\\bar", false, FileSpec::ePathSyntaxWindows);
24 EXPECT_STREQ("F:\\bar", fs_windows.GetCString());
Pavel Labatha212c582016-04-14 09:38:06 +000025 // EXPECT_STREQ("F:\\", fs_windows.GetDirectory().GetCString()); // It returns "F:/"
Pavel Labathcc0e87c2016-03-11 08:44:44 +000026 EXPECT_STREQ("bar", fs_windows.GetFilename().GetCString());
27
28 FileSpec fs_posix_root("/", false, FileSpec::ePathSyntaxPosix);
29 EXPECT_STREQ("/", fs_posix_root.GetCString());
30 EXPECT_EQ(nullptr, fs_posix_root.GetDirectory().GetCString());
31 EXPECT_STREQ("/", fs_posix_root.GetFilename().GetCString());
32
Pavel Labath144119b2016-04-04 14:39:12 +000033 FileSpec fs_windows_drive("F:", false, FileSpec::ePathSyntaxWindows);
34 EXPECT_STREQ("F:", fs_windows_drive.GetCString());
35 EXPECT_EQ(nullptr, fs_windows_drive.GetDirectory().GetCString());
36 EXPECT_STREQ("F:", fs_windows_drive.GetFilename().GetCString());
37
38 FileSpec fs_windows_root("F:\\", false, FileSpec::ePathSyntaxWindows);
39 EXPECT_STREQ("F:\\", fs_windows_root.GetCString());
40 EXPECT_STREQ("F:", fs_windows_root.GetDirectory().GetCString());
Pavel Labatha212c582016-04-14 09:38:06 +000041 // EXPECT_STREQ("\\", fs_windows_root.GetFilename().GetCString()); // It returns "/"
Pavel Labathcc0e87c2016-03-11 08:44:44 +000042
43 FileSpec fs_posix_long("/foo/bar/baz", false, FileSpec::ePathSyntaxPosix);
44 EXPECT_STREQ("/foo/bar/baz", fs_posix_long.GetCString());
45 EXPECT_STREQ("/foo/bar", fs_posix_long.GetDirectory().GetCString());
46 EXPECT_STREQ("baz", fs_posix_long.GetFilename().GetCString());
47
48 FileSpec fs_windows_long("F:\\bar\\baz", false, FileSpec::ePathSyntaxWindows);
49 EXPECT_STREQ("F:\\bar\\baz", fs_windows_long.GetCString());
Pavel Labatha212c582016-04-14 09:38:06 +000050 // EXPECT_STREQ("F:\\bar", fs_windows_long.GetDirectory().GetCString()); // It returns "F:/bar"
Pavel Labathcc0e87c2016-03-11 08:44:44 +000051 EXPECT_STREQ("baz", fs_windows_long.GetFilename().GetCString());
52
53 FileSpec fs_posix_trailing_slash("/foo/bar/", false, FileSpec::ePathSyntaxPosix);
54 EXPECT_STREQ("/foo/bar/.", fs_posix_trailing_slash.GetCString());
55 EXPECT_STREQ("/foo/bar", fs_posix_trailing_slash.GetDirectory().GetCString());
56 EXPECT_STREQ(".", fs_posix_trailing_slash.GetFilename().GetCString());
57
58 FileSpec fs_windows_trailing_slash("F:\\bar\\", false, FileSpec::ePathSyntaxWindows);
59 EXPECT_STREQ("F:\\bar\\.", fs_windows_trailing_slash.GetCString());
Pavel Labatha212c582016-04-14 09:38:06 +000060 // EXPECT_STREQ("F:\\bar", fs_windows_trailing_slash.GetDirectory().GetCString()); // It returns "F:/bar"
Pavel Labathcc0e87c2016-03-11 08:44:44 +000061 EXPECT_STREQ(".", fs_windows_trailing_slash.GetFilename().GetCString());
62}
63
64TEST(FileSpecTest, AppendPathComponent)
65{
66 FileSpec fs_posix("/foo", false, FileSpec::ePathSyntaxPosix);
67 fs_posix.AppendPathComponent("bar");
68 EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
69 EXPECT_STREQ("/foo", fs_posix.GetDirectory().GetCString());
70 EXPECT_STREQ("bar", fs_posix.GetFilename().GetCString());
71
Jim Ingham291fd352016-08-23 17:13:33 +000072 FileSpec fs_posix_2("/foo", false, FileSpec::ePathSyntaxPosix);
73 fs_posix_2.AppendPathComponent("//bar/baz");
74 EXPECT_STREQ("/foo/bar/baz", fs_posix_2.GetCString());
75 EXPECT_STREQ("/foo/bar", fs_posix_2.GetDirectory().GetCString());
76 EXPECT_STREQ("baz", fs_posix_2.GetFilename().GetCString());
77
Pavel Labath144119b2016-04-04 14:39:12 +000078 FileSpec fs_windows("F:\\bar", false, FileSpec::ePathSyntaxWindows);
79 fs_windows.AppendPathComponent("baz");
80 EXPECT_STREQ("F:\\bar\\baz", fs_windows.GetCString());
Pavel Labatha212c582016-04-14 09:38:06 +000081 // EXPECT_STREQ("F:\\bar", fs_windows.GetDirectory().GetCString()); // It returns "F:/bar"
Pavel Labath144119b2016-04-04 14:39:12 +000082 EXPECT_STREQ("baz", fs_windows.GetFilename().GetCString());
Pavel Labathcc0e87c2016-03-11 08:44:44 +000083
84 FileSpec fs_posix_root("/", false, FileSpec::ePathSyntaxPosix);
85 fs_posix_root.AppendPathComponent("bar");
86 EXPECT_STREQ("/bar", fs_posix_root.GetCString());
87 EXPECT_STREQ("/", fs_posix_root.GetDirectory().GetCString());
88 EXPECT_STREQ("bar", fs_posix_root.GetFilename().GetCString());
89
Pavel Labath144119b2016-04-04 14:39:12 +000090 FileSpec fs_windows_root("F:\\", false, FileSpec::ePathSyntaxWindows);
Pavel Labathcc0e87c2016-03-11 08:44:44 +000091 fs_windows_root.AppendPathComponent("bar");
92 EXPECT_STREQ("F:\\bar", fs_windows_root.GetCString());
Pavel Labatha212c582016-04-14 09:38:06 +000093 // EXPECT_STREQ("F:\\", fs_windows_root.GetDirectory().GetCString()); // It returns "F:/"
Pavel Labathcc0e87c2016-03-11 08:44:44 +000094 EXPECT_STREQ("bar", fs_windows_root.GetFilename().GetCString());
95}
96
97TEST(FileSpecTest, CopyByAppendingPathComponent)
98{
99 FileSpec fs = FileSpec("/foo", false, FileSpec::ePathSyntaxPosix).CopyByAppendingPathComponent("bar");
100 EXPECT_STREQ("/foo/bar", fs.GetCString());
101 EXPECT_STREQ("/foo", fs.GetDirectory().GetCString());
102 EXPECT_STREQ("bar", fs.GetFilename().GetCString());
103}
Pavel Labatha212c582016-04-14 09:38:06 +0000104
105TEST(FileSpecTest, Equal)
106{
107 FileSpec backward("C:\\foo\\bar", false, FileSpec::ePathSyntaxWindows);
108 FileSpec forward("C:/foo/bar", false, FileSpec::ePathSyntaxWindows);
109 EXPECT_EQ(forward, backward);
110
111 const bool full_match = true;
112 const bool remove_backup_dots = true;
113 EXPECT_TRUE(FileSpec::Equal(forward, backward, full_match, remove_backup_dots));
114 EXPECT_TRUE(FileSpec::Equal(forward, backward, full_match, !remove_backup_dots));
115 EXPECT_TRUE(FileSpec::Equal(forward, backward, !full_match, remove_backup_dots));
116 EXPECT_TRUE(FileSpec::Equal(forward, backward, !full_match, !remove_backup_dots));
117}