blob: 89092c4604d30684b1535b3f4a2de5acc4b88044 [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 Labath144119b2016-04-04 14:39:12 +000025 EXPECT_STREQ("F:\\", fs_windows.GetDirectory().GetCString());
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());
41 EXPECT_STREQ("\\", fs_windows_root.GetFilename().GetCString());
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());
50 // We get "F:/bar" instead.
Pavel Labath144119b2016-04-04 14:39:12 +000051 EXPECT_STREQ("F:\\bar", fs_windows_long.GetDirectory().GetCString());
Pavel Labathcc0e87c2016-03-11 08:44:44 +000052 EXPECT_STREQ("baz", fs_windows_long.GetFilename().GetCString());
53
54 FileSpec fs_posix_trailing_slash("/foo/bar/", false, FileSpec::ePathSyntaxPosix);
55 EXPECT_STREQ("/foo/bar/.", fs_posix_trailing_slash.GetCString());
56 EXPECT_STREQ("/foo/bar", fs_posix_trailing_slash.GetDirectory().GetCString());
57 EXPECT_STREQ(".", fs_posix_trailing_slash.GetFilename().GetCString());
58
59 FileSpec fs_windows_trailing_slash("F:\\bar\\", false, FileSpec::ePathSyntaxWindows);
60 EXPECT_STREQ("F:\\bar\\.", fs_windows_trailing_slash.GetCString());
61 // We get "F:/bar" instead.
Pavel Labath144119b2016-04-04 14:39:12 +000062 EXPECT_STREQ("F:\\bar", fs_windows_trailing_slash.GetDirectory().GetCString());
Pavel Labathcc0e87c2016-03-11 08:44:44 +000063 EXPECT_STREQ(".", fs_windows_trailing_slash.GetFilename().GetCString());
64}
65
66TEST(FileSpecTest, AppendPathComponent)
67{
68 FileSpec fs_posix("/foo", false, FileSpec::ePathSyntaxPosix);
69 fs_posix.AppendPathComponent("bar");
70 EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
71 EXPECT_STREQ("/foo", fs_posix.GetDirectory().GetCString());
72 EXPECT_STREQ("bar", fs_posix.GetFilename().GetCString());
73
Pavel Labath144119b2016-04-04 14:39:12 +000074 FileSpec fs_windows("F:\\bar", false, FileSpec::ePathSyntaxWindows);
75 fs_windows.AppendPathComponent("baz");
76 EXPECT_STREQ("F:\\bar\\baz", fs_windows.GetCString());
77 EXPECT_STREQ("F:\\bar", fs_windows.GetDirectory().GetCString());
78 EXPECT_STREQ("baz", fs_windows.GetFilename().GetCString());
Pavel Labathcc0e87c2016-03-11 08:44:44 +000079
80 FileSpec fs_posix_root("/", false, FileSpec::ePathSyntaxPosix);
81 fs_posix_root.AppendPathComponent("bar");
82 EXPECT_STREQ("/bar", fs_posix_root.GetCString());
83 EXPECT_STREQ("/", fs_posix_root.GetDirectory().GetCString());
84 EXPECT_STREQ("bar", fs_posix_root.GetFilename().GetCString());
85
Pavel Labath144119b2016-04-04 14:39:12 +000086 FileSpec fs_windows_root("F:\\", false, FileSpec::ePathSyntaxWindows);
Pavel Labathcc0e87c2016-03-11 08:44:44 +000087 fs_windows_root.AppendPathComponent("bar");
88 EXPECT_STREQ("F:\\bar", fs_windows_root.GetCString());
Pavel Labath144119b2016-04-04 14:39:12 +000089 EXPECT_STREQ("F:\\", fs_windows_root.GetDirectory().GetCString());
Pavel Labathcc0e87c2016-03-11 08:44:44 +000090 EXPECT_STREQ("bar", fs_windows_root.GetFilename().GetCString());
91}
92
93TEST(FileSpecTest, CopyByAppendingPathComponent)
94{
95 FileSpec fs = FileSpec("/foo", false, FileSpec::ePathSyntaxPosix).CopyByAppendingPathComponent("bar");
96 EXPECT_STREQ("/foo/bar", fs.GetCString());
97 EXPECT_STREQ("/foo", fs.GetDirectory().GetCString());
98 EXPECT_STREQ("bar", fs.GetFilename().GetCString());
99}