Pavel Labath | cc0e87c | 2016-03-11 08:44:44 +0000 | [diff] [blame] | 1 | //===-- 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 | |
| 14 | using namespace lldb_private; |
| 15 | |
| 16 | TEST(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 Labath | a212c58 | 2016-04-14 09:38:06 +0000 | [diff] [blame] | 25 | // EXPECT_STREQ("F:\\", fs_windows.GetDirectory().GetCString()); // It returns "F:/" |
Pavel Labath | cc0e87c | 2016-03-11 08:44:44 +0000 | [diff] [blame] | 26 | 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 Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 33 | 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 Labath | a212c58 | 2016-04-14 09:38:06 +0000 | [diff] [blame] | 41 | // EXPECT_STREQ("\\", fs_windows_root.GetFilename().GetCString()); // It returns "/" |
Pavel Labath | cc0e87c | 2016-03-11 08:44:44 +0000 | [diff] [blame] | 42 | |
| 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 Labath | a212c58 | 2016-04-14 09:38:06 +0000 | [diff] [blame] | 50 | // EXPECT_STREQ("F:\\bar", fs_windows_long.GetDirectory().GetCString()); // It returns "F:/bar" |
Pavel Labath | cc0e87c | 2016-03-11 08:44:44 +0000 | [diff] [blame] | 51 | 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 Labath | a212c58 | 2016-04-14 09:38:06 +0000 | [diff] [blame] | 60 | // EXPECT_STREQ("F:\\bar", fs_windows_trailing_slash.GetDirectory().GetCString()); // It returns "F:/bar" |
Pavel Labath | cc0e87c | 2016-03-11 08:44:44 +0000 | [diff] [blame] | 61 | EXPECT_STREQ(".", fs_windows_trailing_slash.GetFilename().GetCString()); |
| 62 | } |
| 63 | |
| 64 | TEST(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 Ingham | 291fd35 | 2016-08-23 17:13:33 +0000 | [diff] [blame^] | 72 | 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 Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 78 | FileSpec fs_windows("F:\\bar", false, FileSpec::ePathSyntaxWindows); |
| 79 | fs_windows.AppendPathComponent("baz"); |
| 80 | EXPECT_STREQ("F:\\bar\\baz", fs_windows.GetCString()); |
Pavel Labath | a212c58 | 2016-04-14 09:38:06 +0000 | [diff] [blame] | 81 | // EXPECT_STREQ("F:\\bar", fs_windows.GetDirectory().GetCString()); // It returns "F:/bar" |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 82 | EXPECT_STREQ("baz", fs_windows.GetFilename().GetCString()); |
Pavel Labath | cc0e87c | 2016-03-11 08:44:44 +0000 | [diff] [blame] | 83 | |
| 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 Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 90 | FileSpec fs_windows_root("F:\\", false, FileSpec::ePathSyntaxWindows); |
Pavel Labath | cc0e87c | 2016-03-11 08:44:44 +0000 | [diff] [blame] | 91 | fs_windows_root.AppendPathComponent("bar"); |
| 92 | EXPECT_STREQ("F:\\bar", fs_windows_root.GetCString()); |
Pavel Labath | a212c58 | 2016-04-14 09:38:06 +0000 | [diff] [blame] | 93 | // EXPECT_STREQ("F:\\", fs_windows_root.GetDirectory().GetCString()); // It returns "F:/" |
Pavel Labath | cc0e87c | 2016-03-11 08:44:44 +0000 | [diff] [blame] | 94 | EXPECT_STREQ("bar", fs_windows_root.GetFilename().GetCString()); |
| 95 | } |
| 96 | |
| 97 | TEST(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 Labath | a212c58 | 2016-04-14 09:38:06 +0000 | [diff] [blame] | 104 | |
| 105 | TEST(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 | } |