180814287SRaphael Isemann //===-- FileSpecTest.cpp --------------------------------------------------===//
20e0906c2SPavel Labath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60e0906c2SPavel Labath //
70e0906c2SPavel Labath //===----------------------------------------------------------------------===//
80e0906c2SPavel Labath 
90e0906c2SPavel Labath #include "gtest/gtest.h"
100e0906c2SPavel Labath 
110e0906c2SPavel Labath #include "lldb/Utility/FileSpec.h"
120e0906c2SPavel Labath 
130e0906c2SPavel Labath using namespace lldb_private;
140e0906c2SPavel Labath 
PosixSpec(llvm::StringRef path)15d1a561d4SPavel Labath static FileSpec PosixSpec(llvm::StringRef path) {
16d1a561d4SPavel Labath   return FileSpec(path, FileSpec::Style::posix);
17d1a561d4SPavel Labath }
18d1a561d4SPavel Labath 
WindowsSpec(llvm::StringRef path)19d1a561d4SPavel Labath static FileSpec WindowsSpec(llvm::StringRef path) {
20d1a561d4SPavel Labath   return FileSpec(path, FileSpec::Style::windows);
21d1a561d4SPavel Labath }
22d1a561d4SPavel Labath 
TEST(FileSpecTest,FileAndDirectoryComponents)230e0906c2SPavel Labath TEST(FileSpecTest, FileAndDirectoryComponents) {
248f3be7a3SJonas Devlieghere   FileSpec fs_posix("/foo/bar", FileSpec::Style::posix);
25*1b4b12a3SNico Weber   EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
260e0906c2SPavel Labath   EXPECT_STREQ("/foo", fs_posix.GetDirectory().GetCString());
270e0906c2SPavel Labath   EXPECT_STREQ("bar", fs_posix.GetFilename().GetCString());
280e0906c2SPavel Labath 
298f3be7a3SJonas Devlieghere   FileSpec fs_windows("F:\\bar", FileSpec::Style::windows);
30*1b4b12a3SNico Weber   EXPECT_STREQ("F:\\bar", fs_windows.GetCString());
31*1b4b12a3SNico Weber   // EXPECT_STREQ("F:\\", fs_windows.GetDirectory().GetCString()); // It returns
320e0906c2SPavel Labath   // "F:/"
330e0906c2SPavel Labath   EXPECT_STREQ("bar", fs_windows.GetFilename().GetCString());
340e0906c2SPavel Labath 
358f3be7a3SJonas Devlieghere   FileSpec fs_posix_root("/", FileSpec::Style::posix);
36*1b4b12a3SNico Weber   EXPECT_STREQ("/", fs_posix_root.GetCString());
370e0906c2SPavel Labath   EXPECT_EQ(nullptr, fs_posix_root.GetDirectory().GetCString());
380e0906c2SPavel Labath   EXPECT_STREQ("/", fs_posix_root.GetFilename().GetCString());
390e0906c2SPavel Labath 
408f3be7a3SJonas Devlieghere   FileSpec fs_net_drive("//net", FileSpec::Style::posix);
41*1b4b12a3SNico Weber   EXPECT_STREQ("//net", fs_net_drive.GetCString());
42ad8d48f9SJonas Devlieghere   EXPECT_EQ(nullptr, fs_net_drive.GetDirectory().GetCString());
43ad8d48f9SJonas Devlieghere   EXPECT_STREQ("//net", fs_net_drive.GetFilename().GetCString());
44ad8d48f9SJonas Devlieghere 
458f3be7a3SJonas Devlieghere   FileSpec fs_net_root("//net/", FileSpec::Style::posix);
46*1b4b12a3SNico Weber   EXPECT_STREQ("//net/", fs_net_root.GetCString());
47ad8d48f9SJonas Devlieghere   EXPECT_STREQ("//net", fs_net_root.GetDirectory().GetCString());
48ad8d48f9SJonas Devlieghere   EXPECT_STREQ("/", fs_net_root.GetFilename().GetCString());
49ad8d48f9SJonas Devlieghere 
508f3be7a3SJonas Devlieghere   FileSpec fs_windows_drive("F:", FileSpec::Style::windows);
51*1b4b12a3SNico Weber   EXPECT_STREQ("F:", fs_windows_drive.GetCString());
520e0906c2SPavel Labath   EXPECT_EQ(nullptr, fs_windows_drive.GetDirectory().GetCString());
530e0906c2SPavel Labath   EXPECT_STREQ("F:", fs_windows_drive.GetFilename().GetCString());
540e0906c2SPavel Labath 
558f3be7a3SJonas Devlieghere   FileSpec fs_windows_root("F:\\", FileSpec::Style::windows);
56*1b4b12a3SNico Weber   EXPECT_STREQ("F:\\", fs_windows_root.GetCString());
570e0906c2SPavel Labath   EXPECT_STREQ("F:", fs_windows_root.GetDirectory().GetCString());
580e0906c2SPavel Labath   // EXPECT_STREQ("\\", fs_windows_root.GetFilename().GetCString()); // It
590e0906c2SPavel Labath   // returns "/"
600e0906c2SPavel Labath 
618f3be7a3SJonas Devlieghere   FileSpec fs_posix_long("/foo/bar/baz", FileSpec::Style::posix);
62*1b4b12a3SNico Weber   EXPECT_STREQ("/foo/bar/baz", fs_posix_long.GetCString());
630e0906c2SPavel Labath   EXPECT_STREQ("/foo/bar", fs_posix_long.GetDirectory().GetCString());
640e0906c2SPavel Labath   EXPECT_STREQ("baz", fs_posix_long.GetFilename().GetCString());
650e0906c2SPavel Labath 
668f3be7a3SJonas Devlieghere   FileSpec fs_windows_long("F:\\bar\\baz", FileSpec::Style::windows);
67*1b4b12a3SNico Weber   EXPECT_STREQ("F:\\bar\\baz", fs_windows_long.GetCString());
680e0906c2SPavel Labath   // EXPECT_STREQ("F:\\bar", fs_windows_long.GetDirectory().GetCString()); // It
690e0906c2SPavel Labath   // returns "F:/bar"
700e0906c2SPavel Labath   EXPECT_STREQ("baz", fs_windows_long.GetFilename().GetCString());
710e0906c2SPavel Labath 
728f3be7a3SJonas Devlieghere   FileSpec fs_posix_trailing_slash("/foo/bar/", FileSpec::Style::posix);
73*1b4b12a3SNico Weber   EXPECT_STREQ("/foo/bar", fs_posix_trailing_slash.GetCString());
74776cd7adSGreg Clayton   EXPECT_STREQ("/foo", fs_posix_trailing_slash.GetDirectory().GetCString());
75776cd7adSGreg Clayton   EXPECT_STREQ("bar", fs_posix_trailing_slash.GetFilename().GetCString());
760e0906c2SPavel Labath 
778f3be7a3SJonas Devlieghere   FileSpec fs_windows_trailing_slash("F:\\bar\\", FileSpec::Style::windows);
78*1b4b12a3SNico Weber   EXPECT_STREQ("F:\\bar", fs_windows_trailing_slash.GetCString());
79776cd7adSGreg Clayton   EXPECT_STREQ("bar", fs_windows_trailing_slash.GetFilename().GetCString());
800e0906c2SPavel Labath }
810e0906c2SPavel Labath 
TEST(FileSpecTest,AppendPathComponent)820e0906c2SPavel Labath TEST(FileSpecTest, AppendPathComponent) {
838f3be7a3SJonas Devlieghere   FileSpec fs_posix("/foo", FileSpec::Style::posix);
840e0906c2SPavel Labath   fs_posix.AppendPathComponent("bar");
85*1b4b12a3SNico Weber   EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
860e0906c2SPavel Labath   EXPECT_STREQ("/foo", fs_posix.GetDirectory().GetCString());
870e0906c2SPavel Labath   EXPECT_STREQ("bar", fs_posix.GetFilename().GetCString());
880e0906c2SPavel Labath 
898f3be7a3SJonas Devlieghere   FileSpec fs_posix_2("/foo", FileSpec::Style::posix);
900e0906c2SPavel Labath   fs_posix_2.AppendPathComponent("//bar/baz");
91*1b4b12a3SNico Weber   EXPECT_STREQ("/foo/bar/baz", fs_posix_2.GetCString());
920e0906c2SPavel Labath   EXPECT_STREQ("/foo/bar", fs_posix_2.GetDirectory().GetCString());
930e0906c2SPavel Labath   EXPECT_STREQ("baz", fs_posix_2.GetFilename().GetCString());
940e0906c2SPavel Labath 
958f3be7a3SJonas Devlieghere   FileSpec fs_windows("F:\\bar", FileSpec::Style::windows);
960e0906c2SPavel Labath   fs_windows.AppendPathComponent("baz");
97*1b4b12a3SNico Weber   EXPECT_STREQ("F:\\bar\\baz", fs_windows.GetCString());
980e0906c2SPavel Labath   // EXPECT_STREQ("F:\\bar", fs_windows.GetDirectory().GetCString()); // It
990e0906c2SPavel Labath   // returns "F:/bar"
1000e0906c2SPavel Labath   EXPECT_STREQ("baz", fs_windows.GetFilename().GetCString());
1010e0906c2SPavel Labath 
1028f3be7a3SJonas Devlieghere   FileSpec fs_posix_root("/", FileSpec::Style::posix);
1030e0906c2SPavel Labath   fs_posix_root.AppendPathComponent("bar");
104*1b4b12a3SNico Weber   EXPECT_STREQ("/bar", fs_posix_root.GetCString());
1050e0906c2SPavel Labath   EXPECT_STREQ("/", fs_posix_root.GetDirectory().GetCString());
1060e0906c2SPavel Labath   EXPECT_STREQ("bar", fs_posix_root.GetFilename().GetCString());
1070e0906c2SPavel Labath 
1088f3be7a3SJonas Devlieghere   FileSpec fs_windows_root("F:\\", FileSpec::Style::windows);
1090e0906c2SPavel Labath   fs_windows_root.AppendPathComponent("bar");
110*1b4b12a3SNico Weber   EXPECT_STREQ("F:\\bar", fs_windows_root.GetCString());
1110e0906c2SPavel Labath   // EXPECT_STREQ("F:\\", fs_windows_root.GetDirectory().GetCString()); // It
1120e0906c2SPavel Labath   // returns "F:/"
1130e0906c2SPavel Labath   EXPECT_STREQ("bar", fs_windows_root.GetFilename().GetCString());
1140e0906c2SPavel Labath }
1150e0906c2SPavel Labath 
TEST(FileSpecTest,CopyByAppendingPathComponent)1160e0906c2SPavel Labath TEST(FileSpecTest, CopyByAppendingPathComponent) {
117d1a561d4SPavel Labath   FileSpec fs = PosixSpec("/foo").CopyByAppendingPathComponent("bar");
118*1b4b12a3SNico Weber   EXPECT_STREQ("/foo/bar", fs.GetCString());
1190e0906c2SPavel Labath   EXPECT_STREQ("/foo", fs.GetDirectory().GetCString());
1200e0906c2SPavel Labath   EXPECT_STREQ("bar", fs.GetFilename().GetCString());
1210e0906c2SPavel Labath }
1220e0906c2SPavel Labath 
TEST(FileSpecTest,PrependPathComponent)1230e0906c2SPavel Labath TEST(FileSpecTest, PrependPathComponent) {
1248f3be7a3SJonas Devlieghere   FileSpec fs_posix("foo", FileSpec::Style::posix);
1250e0906c2SPavel Labath   fs_posix.PrependPathComponent("/bar");
126*1b4b12a3SNico Weber   EXPECT_STREQ("/bar/foo", fs_posix.GetCString());
1270e0906c2SPavel Labath 
1288f3be7a3SJonas Devlieghere   FileSpec fs_posix_2("foo/bar", FileSpec::Style::posix);
1290e0906c2SPavel Labath   fs_posix_2.PrependPathComponent("/baz");
130*1b4b12a3SNico Weber   EXPECT_STREQ("/baz/foo/bar", fs_posix_2.GetCString());
1310e0906c2SPavel Labath 
1328f3be7a3SJonas Devlieghere   FileSpec fs_windows("baz", FileSpec::Style::windows);
1330e0906c2SPavel Labath   fs_windows.PrependPathComponent("F:\\bar");
134*1b4b12a3SNico Weber   EXPECT_STREQ("F:\\bar\\baz", fs_windows.GetCString());
1350e0906c2SPavel Labath 
1368f3be7a3SJonas Devlieghere   FileSpec fs_posix_root("bar", FileSpec::Style::posix);
1370e0906c2SPavel Labath   fs_posix_root.PrependPathComponent("/");
138*1b4b12a3SNico Weber   EXPECT_STREQ("/bar", fs_posix_root.GetCString());
1390e0906c2SPavel Labath 
1408f3be7a3SJonas Devlieghere   FileSpec fs_windows_root("bar", FileSpec::Style::windows);
1410e0906c2SPavel Labath   fs_windows_root.PrependPathComponent("F:\\");
142*1b4b12a3SNico Weber   EXPECT_STREQ("F:\\bar", fs_windows_root.GetCString());
1430e0906c2SPavel Labath }
1440e0906c2SPavel Labath 
TEST(FileSpecTest,EqualSeparator)1450e0906c2SPavel Labath TEST(FileSpecTest, EqualSeparator) {
146d1a561d4SPavel Labath   EXPECT_EQ(WindowsSpec("C:\\foo\\bar"), WindowsSpec("C:/foo/bar"));
1470e0906c2SPavel Labath }
1480e0906c2SPavel Labath 
TEST(FileSpecTest,EqualDotsWindows)1490e0906c2SPavel Labath TEST(FileSpecTest, EqualDotsWindows) {
1500e0906c2SPavel Labath   std::pair<const char *, const char *> tests[] = {
1510e0906c2SPavel Labath       {R"(C:\foo\bar\baz)", R"(C:\foo\foo\..\bar\baz)"},
1520e0906c2SPavel Labath       {R"(C:\bar\baz)", R"(C:\foo\..\bar\baz)"},
1530e0906c2SPavel Labath       {R"(C:\bar\baz)", R"(C:/foo/../bar/baz)"},
1540e0906c2SPavel Labath       {R"(C:/bar/baz)", R"(C:\foo\..\bar\baz)"},
1550e0906c2SPavel Labath       {R"(C:\bar)", R"(C:\foo\..\bar)"},
1560e0906c2SPavel Labath       {R"(C:\foo\bar)", R"(C:\foo\.\bar)"},
1570e0906c2SPavel Labath       {R"(C:\foo\bar)", R"(C:\foo\bar\.)"},
1580e0906c2SPavel Labath   };
1590e0906c2SPavel Labath 
1600e0906c2SPavel Labath   for (const auto &test : tests) {
161d1a561d4SPavel Labath     SCOPED_TRACE(llvm::Twine(test.first) + " <=> " + test.second);
162d1a561d4SPavel Labath     EXPECT_EQ(WindowsSpec(test.first), WindowsSpec(test.second));
1630e0906c2SPavel Labath   }
1640e0906c2SPavel Labath }
1650e0906c2SPavel Labath 
TEST(FileSpecTest,EqualDotsPosix)1660e0906c2SPavel Labath TEST(FileSpecTest, EqualDotsPosix) {
1670e0906c2SPavel Labath   std::pair<const char *, const char *> tests[] = {
1680e0906c2SPavel Labath       {R"(/foo/bar/baz)", R"(/foo/foo/../bar/baz)"},
1690e0906c2SPavel Labath       {R"(/bar/baz)", R"(/foo/../bar/baz)"},
1700e0906c2SPavel Labath       {R"(/bar)", R"(/foo/../bar)"},
1710e0906c2SPavel Labath       {R"(/foo/bar)", R"(/foo/./bar)"},
1720e0906c2SPavel Labath       {R"(/foo/bar)", R"(/foo/bar/.)"},
1730e0906c2SPavel Labath   };
1740e0906c2SPavel Labath 
1750e0906c2SPavel Labath   for (const auto &test : tests) {
176d1a561d4SPavel Labath     SCOPED_TRACE(llvm::Twine(test.first) + " <=> " + test.second);
177d1a561d4SPavel Labath     EXPECT_EQ(PosixSpec(test.first), PosixSpec(test.second));
1780e0906c2SPavel Labath   }
1790e0906c2SPavel Labath }
1800e0906c2SPavel Labath 
TEST(FileSpecTest,EqualDotsPosixRoot)1810e0906c2SPavel Labath TEST(FileSpecTest, EqualDotsPosixRoot) {
1820e0906c2SPavel Labath   std::pair<const char *, const char *> tests[] = {
1830e0906c2SPavel Labath       {R"(/)", R"(/..)"},
1840e0906c2SPavel Labath       {R"(/)", R"(/.)"},
1850e0906c2SPavel Labath       {R"(/)", R"(/foo/..)"},
1860e0906c2SPavel Labath   };
1870e0906c2SPavel Labath 
1880e0906c2SPavel Labath   for (const auto &test : tests) {
189d1a561d4SPavel Labath     SCOPED_TRACE(llvm::Twine(test.first) + " <=> " + test.second);
190d1a561d4SPavel Labath     EXPECT_EQ(PosixSpec(test.first), PosixSpec(test.second));
1910e0906c2SPavel Labath   }
1920e0906c2SPavel Labath }
1930e0906c2SPavel Labath 
TEST(FileSpecTest,GuessPathStyle)194841bea93SPavel Labath TEST(FileSpecTest, GuessPathStyle) {
195841bea93SPavel Labath   EXPECT_EQ(FileSpec::Style::posix, FileSpec::GuessPathStyle("/foo/bar.txt"));
196841bea93SPavel Labath   EXPECT_EQ(FileSpec::Style::posix, FileSpec::GuessPathStyle("//net/bar.txt"));
197841bea93SPavel Labath   EXPECT_EQ(FileSpec::Style::windows,
198841bea93SPavel Labath             FileSpec::GuessPathStyle(R"(C:\foo.txt)"));
199841bea93SPavel Labath   EXPECT_EQ(FileSpec::Style::windows,
200b548f584SMartin Storsjö             FileSpec::GuessPathStyle(R"(C:/foo.txt)"));
201b548f584SMartin Storsjö   EXPECT_EQ(FileSpec::Style::windows,
202841bea93SPavel Labath             FileSpec::GuessPathStyle(R"(\\net\foo.txt)"));
203f72ae5cbSJaroslav Sevcik   EXPECT_EQ(FileSpec::Style::windows, FileSpec::GuessPathStyle(R"(Z:\)"));
204b548f584SMartin Storsjö   EXPECT_EQ(FileSpec::Style::windows, FileSpec::GuessPathStyle(R"(Z:/)"));
205841bea93SPavel Labath   EXPECT_EQ(llvm::None, FileSpec::GuessPathStyle("foo.txt"));
206841bea93SPavel Labath   EXPECT_EQ(llvm::None, FileSpec::GuessPathStyle("foo/bar.txt"));
207f72ae5cbSJaroslav Sevcik   EXPECT_EQ(llvm::None, FileSpec::GuessPathStyle("Z:"));
208841bea93SPavel Labath }
209841bea93SPavel Labath 
TEST(FileSpecTest,GetPath)210d1a561d4SPavel Labath TEST(FileSpecTest, GetPath) {
2110e0906c2SPavel Labath   std::pair<const char *, const char *> posix_tests[] = {
2120e0906c2SPavel Labath       {"/foo/.././bar", "/bar"},
2130e0906c2SPavel Labath       {"/foo/./../bar", "/bar"},
2140e0906c2SPavel Labath       {"/foo/../bar", "/bar"},
2150e0906c2SPavel Labath       {"/foo/./bar", "/foo/bar"},
2160e0906c2SPavel Labath       {"/foo/..", "/"},
2170e0906c2SPavel Labath       {"/foo/.", "/foo"},
2180e0906c2SPavel Labath       {"/foo//bar", "/foo/bar"},
2190e0906c2SPavel Labath       {"/foo//bar/baz", "/foo/bar/baz"},
2200e0906c2SPavel Labath       {"/foo//bar/./baz", "/foo/bar/baz"},
2210e0906c2SPavel Labath       {"/./foo", "/foo"},
2220e0906c2SPavel Labath       {"/", "/"},
223e7306b10SPavel Labath       {"//", "/"},
2240e0906c2SPavel Labath       {"//net", "//net"},
2250e0906c2SPavel Labath       {"/..", "/"},
2260e0906c2SPavel Labath       {"/.", "/"},
2270e0906c2SPavel Labath       {"..", ".."},
22839d50b72SGreg Clayton       {".", "."},
2290e0906c2SPavel Labath       {"../..", "../.."},
23039d50b72SGreg Clayton       {"foo/..", "."},
2310e0906c2SPavel Labath       {"foo/../bar", "bar"},
2320e0906c2SPavel Labath       {"../foo/..", ".."},
2330e0906c2SPavel Labath       {"./foo", "foo"},
234776cd7adSGreg Clayton       {"././foo", "foo"},
235776cd7adSGreg Clayton       {"../foo", "../foo"},
236776cd7adSGreg Clayton       {"../../foo", "../../foo"},
2370e0906c2SPavel Labath   };
2380e0906c2SPavel Labath   for (auto test : posix_tests) {
239e7306b10SPavel Labath     SCOPED_TRACE(llvm::Twine("test.first = ") + test.first);
240d1a561d4SPavel Labath     EXPECT_EQ(test.second, PosixSpec(test.first).GetPath());
2410e0906c2SPavel Labath   }
2420e0906c2SPavel Labath 
2430e0906c2SPavel Labath   std::pair<const char *, const char *> windows_tests[] = {
2440e0906c2SPavel Labath       {R"(c:\bar\..\bar)", R"(c:\bar)"},
2450e0906c2SPavel Labath       {R"(c:\bar\.\bar)", R"(c:\bar\bar)"},
2460e0906c2SPavel Labath       {R"(c:\bar\..)", R"(c:\)"},
2470e0906c2SPavel Labath       {R"(c:\bar\.)", R"(c:\bar)"},
2480e0906c2SPavel Labath       {R"(c:\.\bar)", R"(c:\bar)"},
2490e0906c2SPavel Labath       {R"(\)", R"(\)"},
250e7306b10SPavel Labath       {R"(\\)", R"(\)"},
251e7306b10SPavel Labath       {R"(\\net)", R"(\\net)"},
2520e0906c2SPavel Labath       {R"(c:\..)", R"(c:\)"},
2530e0906c2SPavel Labath       {R"(c:\.)", R"(c:\)"},
25458c7bf24SReid Kleckner       {R"(\..)", R"(\)"},
2550e0906c2SPavel Labath       //      {R"(c:..)", R"(c:..)"},
2560e0906c2SPavel Labath       {R"(..)", R"(..)"},
25739d50b72SGreg Clayton       {R"(.)", R"(.)"},
25858c7bf24SReid Kleckner       {R"(c:..\..)", R"(c:)"},
2590e0906c2SPavel Labath       {R"(..\..)", R"(..\..)"},
26039d50b72SGreg Clayton       {R"(foo\..)", R"(.)"},
2610e0906c2SPavel Labath       {R"(foo\..\bar)", R"(bar)"},
2620e0906c2SPavel Labath       {R"(..\foo\..)", R"(..)"},
2630e0906c2SPavel Labath       {R"(.\foo)", R"(foo)"},
264776cd7adSGreg Clayton       {R"(.\.\foo)", R"(foo)"},
265776cd7adSGreg Clayton       {R"(..\foo)", R"(..\foo)"},
266776cd7adSGreg Clayton       {R"(..\..\foo)", R"(..\..\foo)"},
2670e0906c2SPavel Labath   };
2680e0906c2SPavel Labath   for (auto test : windows_tests) {
269d1a561d4SPavel Labath     SCOPED_TRACE(llvm::Twine("test.first = ") + test.first);
270d1a561d4SPavel Labath     EXPECT_EQ(test.second, WindowsSpec(test.first).GetPath());
2710e0906c2SPavel Labath   }
2720e0906c2SPavel Labath }
2730e0906c2SPavel Labath 
TEST(FileSpecTest,FormatFileSpec)2740e0906c2SPavel Labath TEST(FileSpecTest, FormatFileSpec) {
2752cb7cf8eSPavel Labath   auto win = FileSpec::Style::windows;
2760e0906c2SPavel Labath 
2770e0906c2SPavel Labath   FileSpec F;
2780e0906c2SPavel Labath   EXPECT_EQ("(empty)", llvm::formatv("{0}", F).str());
2790e0906c2SPavel Labath   EXPECT_EQ("(empty)", llvm::formatv("{0:D}", F).str());
2800e0906c2SPavel Labath   EXPECT_EQ("(empty)", llvm::formatv("{0:F}", F).str());
2810e0906c2SPavel Labath 
2828f3be7a3SJonas Devlieghere   F = FileSpec("C:\\foo\\bar.txt", win);
2830e0906c2SPavel Labath   EXPECT_EQ("C:\\foo\\bar.txt", llvm::formatv("{0}", F).str());
2840e0906c2SPavel Labath   EXPECT_EQ("C:\\foo\\", llvm::formatv("{0:D}", F).str());
2850e0906c2SPavel Labath   EXPECT_EQ("bar.txt", llvm::formatv("{0:F}", F).str());
2860e0906c2SPavel Labath 
2878f3be7a3SJonas Devlieghere   F = FileSpec("foo\\bar.txt", win);
2880e0906c2SPavel Labath   EXPECT_EQ("foo\\bar.txt", llvm::formatv("{0}", F).str());
2890e0906c2SPavel Labath   EXPECT_EQ("foo\\", llvm::formatv("{0:D}", F).str());
2900e0906c2SPavel Labath   EXPECT_EQ("bar.txt", llvm::formatv("{0:F}", F).str());
2910e0906c2SPavel Labath 
2928f3be7a3SJonas Devlieghere   F = FileSpec("foo", win);
2930e0906c2SPavel Labath   EXPECT_EQ("foo", llvm::formatv("{0}", F).str());
2940e0906c2SPavel Labath   EXPECT_EQ("foo", llvm::formatv("{0:F}", F).str());
2950e0906c2SPavel Labath   EXPECT_EQ("(empty)", llvm::formatv("{0:D}", F).str());
2960e0906c2SPavel Labath }
297776cd7adSGreg Clayton 
TEST(FileSpecTest,IsRelative)29886188d8aSGreg Clayton TEST(FileSpecTest, IsRelative) {
29986188d8aSGreg Clayton   llvm::StringRef not_relative[] = {
30086188d8aSGreg Clayton     "/",
30186188d8aSGreg Clayton     "/a",
30286188d8aSGreg Clayton     "/a/",
30386188d8aSGreg Clayton     "/a/b",
30486188d8aSGreg Clayton     "/a/b/",
30586188d8aSGreg Clayton     "//",
30686188d8aSGreg Clayton     "//a/",
30786188d8aSGreg Clayton     "//a/b",
30886188d8aSGreg Clayton     "//a/b/",
30986188d8aSGreg Clayton     "~",
31086188d8aSGreg Clayton     "~/",
31186188d8aSGreg Clayton     "~/a",
31286188d8aSGreg Clayton     "~/a/",
3130f3ed7a4SRaphael Isemann     "~/a/b",
31486188d8aSGreg Clayton     "~/a/b/",
31586188d8aSGreg Clayton     "/foo/.",
31686188d8aSGreg Clayton     "/foo/..",
31786188d8aSGreg Clayton     "/foo/../",
31886188d8aSGreg Clayton     "/foo/../.",
31986188d8aSGreg Clayton   };
32086188d8aSGreg Clayton   for (const auto &path: not_relative) {
321d1a561d4SPavel Labath     SCOPED_TRACE(path);
322d1a561d4SPavel Labath     EXPECT_FALSE(PosixSpec(path).IsRelative());
32386188d8aSGreg Clayton   }
32486188d8aSGreg Clayton   llvm::StringRef is_relative[] = {
32586188d8aSGreg Clayton     ".",
32686188d8aSGreg Clayton     "./",
32786188d8aSGreg Clayton     ".///",
32886188d8aSGreg Clayton     "a",
32986188d8aSGreg Clayton     "./a",
33086188d8aSGreg Clayton     "./a/",
33186188d8aSGreg Clayton     "./a/",
33286188d8aSGreg Clayton     "./a/b",
33386188d8aSGreg Clayton     "./a/b/",
33486188d8aSGreg Clayton     "../foo",
33586188d8aSGreg Clayton     "foo/bar.c",
33686188d8aSGreg Clayton     "./foo/bar.c"
33786188d8aSGreg Clayton   };
33886188d8aSGreg Clayton   for (const auto &path: is_relative) {
339d1a561d4SPavel Labath     SCOPED_TRACE(path);
340d1a561d4SPavel Labath     EXPECT_TRUE(PosixSpec(path).IsRelative());
34186188d8aSGreg Clayton   }
34286188d8aSGreg Clayton }
34386188d8aSGreg Clayton 
TEST(FileSpecTest,RemoveLastPathComponent)344df8e291eSJonas Devlieghere TEST(FileSpecTest, RemoveLastPathComponent) {
3458f3be7a3SJonas Devlieghere   FileSpec fs_posix("/foo/bar/baz", FileSpec::Style::posix);
346*1b4b12a3SNico Weber   EXPECT_STREQ("/foo/bar/baz", fs_posix.GetCString());
347df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
348*1b4b12a3SNico Weber   EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
349df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
350*1b4b12a3SNico Weber   EXPECT_STREQ("/foo", fs_posix.GetCString());
351df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
352*1b4b12a3SNico Weber   EXPECT_STREQ("/", fs_posix.GetCString());
353df8e291eSJonas Devlieghere   EXPECT_FALSE(fs_posix.RemoveLastPathComponent());
354*1b4b12a3SNico Weber   EXPECT_STREQ("/", fs_posix.GetCString());
355df8e291eSJonas Devlieghere 
3568f3be7a3SJonas Devlieghere   FileSpec fs_posix_relative("./foo/bar/baz", FileSpec::Style::posix);
357*1b4b12a3SNico Weber   EXPECT_STREQ("foo/bar/baz", fs_posix_relative.GetCString());
358df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_posix_relative.RemoveLastPathComponent());
359*1b4b12a3SNico Weber   EXPECT_STREQ("foo/bar", fs_posix_relative.GetCString());
360df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_posix_relative.RemoveLastPathComponent());
361*1b4b12a3SNico Weber   EXPECT_STREQ("foo", fs_posix_relative.GetCString());
362df8e291eSJonas Devlieghere   EXPECT_FALSE(fs_posix_relative.RemoveLastPathComponent());
363*1b4b12a3SNico Weber   EXPECT_STREQ("foo", fs_posix_relative.GetCString());
364df8e291eSJonas Devlieghere 
3658f3be7a3SJonas Devlieghere   FileSpec fs_posix_relative2("./", FileSpec::Style::posix);
366*1b4b12a3SNico Weber   EXPECT_STREQ(".", fs_posix_relative2.GetCString());
367df8e291eSJonas Devlieghere   EXPECT_FALSE(fs_posix_relative2.RemoveLastPathComponent());
368*1b4b12a3SNico Weber   EXPECT_STREQ(".", fs_posix_relative2.GetCString());
369df8e291eSJonas Devlieghere   EXPECT_FALSE(fs_posix_relative.RemoveLastPathComponent());
370*1b4b12a3SNico Weber   EXPECT_STREQ(".", fs_posix_relative2.GetCString());
371df8e291eSJonas Devlieghere 
3728f3be7a3SJonas Devlieghere   FileSpec fs_windows("C:\\foo\\bar\\baz", FileSpec::Style::windows);
373*1b4b12a3SNico Weber   EXPECT_STREQ("C:\\foo\\bar\\baz", fs_windows.GetCString());
374df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
375*1b4b12a3SNico Weber   EXPECT_STREQ("C:\\foo\\bar", fs_windows.GetCString());
376df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
377*1b4b12a3SNico Weber   EXPECT_STREQ("C:\\foo", fs_windows.GetCString());
378df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
379*1b4b12a3SNico Weber   EXPECT_STREQ("C:\\", fs_windows.GetCString());
380df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
381*1b4b12a3SNico Weber   EXPECT_STREQ("C:", fs_windows.GetCString());
382df8e291eSJonas Devlieghere   EXPECT_FALSE(fs_windows.RemoveLastPathComponent());
383*1b4b12a3SNico Weber   EXPECT_STREQ("C:", fs_windows.GetCString());
384df8e291eSJonas Devlieghere }
385bf716eb8SPavel Labath 
TEST(FileSpecTest,Equal)386bf716eb8SPavel Labath TEST(FileSpecTest, Equal) {
387bf716eb8SPavel Labath   auto Eq = [](const char *a, const char *b, bool full) {
388bf716eb8SPavel Labath     return FileSpec::Equal(PosixSpec(a), PosixSpec(b), full);
389bf716eb8SPavel Labath   };
390bf716eb8SPavel Labath   EXPECT_TRUE(Eq("/foo/bar", "/foo/bar", true));
391bf716eb8SPavel Labath   EXPECT_TRUE(Eq("/foo/bar", "/foo/bar", false));
392bf716eb8SPavel Labath 
393bf716eb8SPavel Labath   EXPECT_FALSE(Eq("/foo/bar", "/foo/baz", true));
394bf716eb8SPavel Labath   EXPECT_FALSE(Eq("/foo/bar", "/foo/baz", false));
395bf716eb8SPavel Labath 
396bf716eb8SPavel Labath   EXPECT_FALSE(Eq("/bar/foo", "/baz/foo", true));
397bf716eb8SPavel Labath   EXPECT_FALSE(Eq("/bar/foo", "/baz/foo", false));
398bf716eb8SPavel Labath 
399bf716eb8SPavel Labath   EXPECT_FALSE(Eq("/bar/foo", "foo", true));
400bf716eb8SPavel Labath   EXPECT_TRUE(Eq("/bar/foo", "foo", false));
401bf716eb8SPavel Labath 
402bf716eb8SPavel Labath   EXPECT_FALSE(Eq("foo", "/bar/foo", true));
403bf716eb8SPavel Labath   EXPECT_TRUE(Eq("foo", "/bar/foo", false));
404bf716eb8SPavel Labath }
405532290e6SPavel Labath 
TEST(FileSpecTest,Match)406532290e6SPavel Labath TEST(FileSpecTest, Match) {
407532290e6SPavel Labath   auto Match = [](const char *pattern, const char *file) {
408532290e6SPavel Labath     return FileSpec::Match(PosixSpec(pattern), PosixSpec(file));
409532290e6SPavel Labath   };
410532290e6SPavel Labath   EXPECT_TRUE(Match("/foo/bar", "/foo/bar"));
411532290e6SPavel Labath   EXPECT_FALSE(Match("/foo/bar", "/oof/bar"));
412532290e6SPavel Labath   EXPECT_FALSE(Match("/foo/bar", "/foo/baz"));
413532290e6SPavel Labath   EXPECT_FALSE(Match("/foo/bar", "bar"));
414532290e6SPavel Labath   EXPECT_FALSE(Match("/foo/bar", ""));
415532290e6SPavel Labath 
416532290e6SPavel Labath   EXPECT_TRUE(Match("bar", "/foo/bar"));
417532290e6SPavel Labath   EXPECT_FALSE(Match("bar", "/foo/baz"));
418532290e6SPavel Labath   EXPECT_TRUE(Match("bar", "bar"));
419532290e6SPavel Labath   EXPECT_FALSE(Match("bar", "baz"));
420532290e6SPavel Labath   EXPECT_FALSE(Match("bar", ""));
421532290e6SPavel Labath 
422532290e6SPavel Labath   EXPECT_TRUE(Match("", "/foo/bar"));
423532290e6SPavel Labath   EXPECT_TRUE(Match("", ""));
424532290e6SPavel Labath 
425532290e6SPavel Labath }
426bc9b6b33SJonas Devlieghere 
TEST(FileSpecTest,Yaml)427bc9b6b33SJonas Devlieghere TEST(FileSpecTest, Yaml) {
428bc9b6b33SJonas Devlieghere   std::string buffer;
429bc9b6b33SJonas Devlieghere   llvm::raw_string_ostream os(buffer);
430bc9b6b33SJonas Devlieghere 
431bc9b6b33SJonas Devlieghere   // Serialize.
432bc9b6b33SJonas Devlieghere   FileSpec fs_windows("F:\\bar", FileSpec::Style::windows);
433bc9b6b33SJonas Devlieghere   llvm::yaml::Output yout(os);
434bc9b6b33SJonas Devlieghere   yout << fs_windows;
435bc9b6b33SJonas Devlieghere   os.flush();
436bc9b6b33SJonas Devlieghere 
437bc9b6b33SJonas Devlieghere   // Deserialize.
438bc9b6b33SJonas Devlieghere   FileSpec deserialized;
439bc9b6b33SJonas Devlieghere   llvm::yaml::Input yin(buffer);
440bc9b6b33SJonas Devlieghere   yin >> deserialized;
441bc9b6b33SJonas Devlieghere 
442bc9b6b33SJonas Devlieghere   EXPECT_EQ(deserialized.GetPathStyle(), fs_windows.GetPathStyle());
443bc9b6b33SJonas Devlieghere   EXPECT_EQ(deserialized.GetFilename(), fs_windows.GetFilename());
444bc9b6b33SJonas Devlieghere   EXPECT_EQ(deserialized.GetDirectory(), fs_windows.GetDirectory());
445bc9b6b33SJonas Devlieghere   EXPECT_EQ(deserialized, fs_windows);
446bc9b6b33SJonas Devlieghere }
44714970669SJonas Devlieghere 
TEST(FileSpecTest,OperatorBool)44814970669SJonas Devlieghere TEST(FileSpecTest, OperatorBool) {
44914970669SJonas Devlieghere   EXPECT_FALSE(FileSpec());
45014970669SJonas Devlieghere   EXPECT_FALSE(FileSpec(""));
45114970669SJonas Devlieghere   EXPECT_TRUE(FileSpec("/foo/bar"));
45214970669SJonas Devlieghere }
453