summaryrefslogtreecommitdiffstats
path: root/lldb/unittests/Host/FileSpecTest.cpp
blob: f8a887492f020fa56e10c761f7e73e6798dc82c9 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//===-- FileSpecTest.cpp ----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "gtest/gtest.h"

#include "lldb/Host/FileSpec.h"

using namespace lldb_private;

TEST(FileSpecTest, FileAndDirectoryComponents) {
  FileSpec fs_posix("/foo/bar", false, FileSpec::ePathSyntaxPosix);
  EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
  EXPECT_STREQ("/foo", fs_posix.GetDirectory().GetCString());
  EXPECT_STREQ("bar", fs_posix.GetFilename().GetCString());

  FileSpec fs_windows("F:\\bar", false, FileSpec::ePathSyntaxWindows);
  EXPECT_STREQ("F:\\bar", fs_windows.GetCString());
  // EXPECT_STREQ("F:\\", fs_windows.GetDirectory().GetCString()); // It returns
  // "F:/"
  EXPECT_STREQ("bar", fs_windows.GetFilename().GetCString());

  FileSpec fs_posix_root("/", false, FileSpec::ePathSyntaxPosix);
  EXPECT_STREQ("/", fs_posix_root.GetCString());
  EXPECT_EQ(nullptr, fs_posix_root.GetDirectory().GetCString());
  EXPECT_STREQ("/", fs_posix_root.GetFilename().GetCString());

  FileSpec fs_windows_drive("F:", false, FileSpec::ePathSyntaxWindows);
  EXPECT_STREQ("F:", fs_windows_drive.GetCString());
  EXPECT_EQ(nullptr, fs_windows_drive.GetDirectory().GetCString());
  EXPECT_STREQ("F:", fs_windows_drive.GetFilename().GetCString());

  FileSpec fs_windows_root("F:\\", false, FileSpec::ePathSyntaxWindows);
  EXPECT_STREQ("F:\\", fs_windows_root.GetCString());
  EXPECT_STREQ("F:", fs_windows_root.GetDirectory().GetCString());
  // EXPECT_STREQ("\\", fs_windows_root.GetFilename().GetCString()); // It
  // returns "/"

  FileSpec fs_posix_long("/foo/bar/baz", false, FileSpec::ePathSyntaxPosix);
  EXPECT_STREQ("/foo/bar/baz", fs_posix_long.GetCString());
  EXPECT_STREQ("/foo/bar", fs_posix_long.GetDirectory().GetCString());
  EXPECT_STREQ("baz", fs_posix_long.GetFilename().GetCString());

  FileSpec fs_windows_long("F:\\bar\\baz", false, FileSpec::ePathSyntaxWindows);
  EXPECT_STREQ("F:\\bar\\baz", fs_windows_long.GetCString());
  // EXPECT_STREQ("F:\\bar", fs_windows_long.GetDirectory().GetCString()); // It
  // returns "F:/bar"
  EXPECT_STREQ("baz", fs_windows_long.GetFilename().GetCString());

  FileSpec fs_posix_trailing_slash("/foo/bar/", false,
                                   FileSpec::ePathSyntaxPosix);
  EXPECT_STREQ("/foo/bar/.", fs_posix_trailing_slash.GetCString());
  EXPECT_STREQ("/foo/bar", fs_posix_trailing_slash.GetDirectory().GetCString());
  EXPECT_STREQ(".", fs_posix_trailing_slash.GetFilename().GetCString());

  FileSpec fs_windows_trailing_slash("F:\\bar\\", false,
                                     FileSpec::ePathSyntaxWindows);
  EXPECT_STREQ("F:\\bar\\.", fs_windows_trailing_slash.GetCString());
  // EXPECT_STREQ("F:\\bar",
  // fs_windows_trailing_slash.GetDirectory().GetCString()); // It returns
  // "F:/bar"
  EXPECT_STREQ(".", fs_windows_trailing_slash.GetFilename().GetCString());
}

TEST(FileSpecTest, AppendPathComponent) {
  FileSpec fs_posix("/foo", false, FileSpec::ePathSyntaxPosix);
  fs_posix.AppendPathComponent("bar");
  EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
  EXPECT_STREQ("/foo", fs_posix.GetDirectory().GetCString());
  EXPECT_STREQ("bar", fs_posix.GetFilename().GetCString());

  FileSpec fs_posix_2("/foo", false, FileSpec::ePathSyntaxPosix);
  fs_posix_2.AppendPathComponent("//bar/baz");
  EXPECT_STREQ("/foo/bar/baz", fs_posix_2.GetCString());
  EXPECT_STREQ("/foo/bar", fs_posix_2.GetDirectory().GetCString());
  EXPECT_STREQ("baz", fs_posix_2.GetFilename().GetCString());

  FileSpec fs_windows("F:\\bar", false, FileSpec::ePathSyntaxWindows);
  fs_windows.AppendPathComponent("baz");
  EXPECT_STREQ("F:\\bar\\baz", fs_windows.GetCString());
  // EXPECT_STREQ("F:\\bar", fs_windows.GetDirectory().GetCString()); // It
  // returns "F:/bar"
  EXPECT_STREQ("baz", fs_windows.GetFilename().GetCString());

  FileSpec fs_posix_root("/", false, FileSpec::ePathSyntaxPosix);
  fs_posix_root.AppendPathComponent("bar");
  EXPECT_STREQ("/bar", fs_posix_root.GetCString());
  EXPECT_STREQ("/", fs_posix_root.GetDirectory().GetCString());
  EXPECT_STREQ("bar", fs_posix_root.GetFilename().GetCString());

  FileSpec fs_windows_root("F:\\", false, FileSpec::ePathSyntaxWindows);
  fs_windows_root.AppendPathComponent("bar");
  EXPECT_STREQ("F:\\bar", fs_windows_root.GetCString());
  // EXPECT_STREQ("F:\\", fs_windows_root.GetDirectory().GetCString()); // It
  // returns "F:/"
  EXPECT_STREQ("bar", fs_windows_root.GetFilename().GetCString());
}

TEST(FileSpecTest, CopyByAppendingPathComponent) {
  FileSpec fs = FileSpec("/foo", false, FileSpec::ePathSyntaxPosix)
                    .CopyByAppendingPathComponent("bar");
  EXPECT_STREQ("/foo/bar", fs.GetCString());
  EXPECT_STREQ("/foo", fs.GetDirectory().GetCString());
  EXPECT_STREQ("bar", fs.GetFilename().GetCString());
}

static void Compare(const FileSpec &one, const FileSpec &two, bool full_match,
                    bool remove_backup_dots, bool result) {
  EXPECT_EQ(result, FileSpec::Equal(one, two, full_match, remove_backup_dots))
      << "File one: " << one.GetCString() << "\nFile two: " << two.GetCString()
      << "\nFull match: " << full_match
      << "\nRemove backup dots: " << remove_backup_dots;
}

TEST(FileSpecTest, EqualSeparator) {
  FileSpec backward("C:\\foo\\bar", false, FileSpec::ePathSyntaxWindows);
  FileSpec forward("C:/foo/bar", false, FileSpec::ePathSyntaxWindows);
  EXPECT_EQ(forward, backward);

  const bool full_match = true;
  const bool remove_backup_dots = true;
  const bool match = true;
  Compare(forward, backward, full_match, remove_backup_dots, match);
  Compare(forward, backward, full_match, !remove_backup_dots, match);
  Compare(forward, backward, !full_match, remove_backup_dots, match);
  Compare(forward, backward, !full_match, !remove_backup_dots, match);
}

#if 0
TEST(FileSpecTest, EqualDotsWindows) {
  const bool full_match = true;
  const bool remove_backup_dots = true;
  const bool match = true;
  std::pair<const char *, const char *> tests[] = {
      {R"(C:\foo\bar\baz)", R"(C:\foo\foo\..\bar\baz)"},
      {R"(C:\bar\baz)", R"(C:\foo\..\bar\baz)"},
      {R"(C:\bar\baz)", R"(C:/foo/../bar/baz)"},
      {R"(C:/bar/baz)", R"(C:\foo\..\bar\baz)"},
      {R"(C:\bar)", R"(C:\foo\..\bar)"},
  };

  for(const auto &test: tests) {
    FileSpec one(test.first, false, FileSpec::ePathSyntaxWindows);
    FileSpec two(test.second, false, FileSpec::ePathSyntaxWindows);
    EXPECT_NE(one, two);
    Compare(one, two, full_match, remove_backup_dots, match);
    Compare(one, two, full_match, !remove_backup_dots, !match);
    Compare(one, two, !full_match, remove_backup_dots, match);
    Compare(one, two, !full_match, !remove_backup_dots, match);
  }

}
#endif

TEST(FileSpecTest, EqualDotsPosix) {
  const bool full_match = true;
  const bool remove_backup_dots = true;
  const bool match = true;
  std::pair<const char *, const char *> tests[] = {
      {R"(/foo/bar/baz)", R"(/foo/foo/../bar/baz)"},
      {R"(/bar/baz)", R"(/foo/../bar/baz)"},
//      {R"(/bar)", R"(/foo/../bar)"},
  };

  for(const auto &test: tests) {
    FileSpec one(test.first, false, FileSpec::ePathSyntaxPosix);
    FileSpec two(test.second, false, FileSpec::ePathSyntaxPosix);
    EXPECT_NE(one, two);
    Compare(one, two, full_match, remove_backup_dots, match);
    Compare(one, two, full_match, !remove_backup_dots, !match);
    Compare(one, two, !full_match, remove_backup_dots, match);
//    Compare(one, two, !full_match, !remove_backup_dots, match);
  }

}

#if 0
TEST(FileSpecTest, EqualDotsPosixRoot) {
  const bool full_match = true;
  const bool remove_backup_dots = true;
  const bool match = true;
  std::pair<const char *, const char *> tests[] = {
      {R"(/)", R"(/..)"},
      {R"(/)", R"(/foo/..)"},
  };

  for(const auto &test: tests) {
    FileSpec one(test.first, false, FileSpec::ePathSyntaxPosix);
    FileSpec two(test.second, false, FileSpec::ePathSyntaxPosix);
    EXPECT_NE(one, two);
    Compare(one, two, full_match, remove_backup_dots, match);
    Compare(one, two, !full_match, remove_backup_dots, match);
  }
}
#endif
OpenPOWER on IntegriCloud