blob: 461be60ff13c9228f7eaca4e505fdc891df75003 [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());
25 EXPECT_STREQ("F:", fs_windows.GetDirectory().GetCString());
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
33 FileSpec fs_windows_root("F:", false, FileSpec::ePathSyntaxWindows);
34 EXPECT_STREQ("F:", fs_windows_root.GetCString());
35 EXPECT_EQ(nullptr, fs_windows_root.GetDirectory().GetCString());
36 EXPECT_STREQ("F:", fs_windows_root.GetFilename().GetCString());
37
38 FileSpec fs_posix_long("/foo/bar/baz", false, FileSpec::ePathSyntaxPosix);
39 EXPECT_STREQ("/foo/bar/baz", fs_posix_long.GetCString());
40 EXPECT_STREQ("/foo/bar", fs_posix_long.GetDirectory().GetCString());
41 EXPECT_STREQ("baz", fs_posix_long.GetFilename().GetCString());
42
43 FileSpec fs_windows_long("F:\\bar\\baz", false, FileSpec::ePathSyntaxWindows);
44 EXPECT_STREQ("F:\\bar\\baz", fs_windows_long.GetCString());
45 // We get "F:/bar" instead.
46 // EXPECT_STREQ("F:\\bar", fs_windows_long.GetDirectory().GetCString());
47 EXPECT_STREQ("baz", fs_windows_long.GetFilename().GetCString());
48
49 FileSpec fs_posix_trailing_slash("/foo/bar/", false, FileSpec::ePathSyntaxPosix);
50 EXPECT_STREQ("/foo/bar/.", fs_posix_trailing_slash.GetCString());
51 EXPECT_STREQ("/foo/bar", fs_posix_trailing_slash.GetDirectory().GetCString());
52 EXPECT_STREQ(".", fs_posix_trailing_slash.GetFilename().GetCString());
53
54 FileSpec fs_windows_trailing_slash("F:\\bar\\", false, FileSpec::ePathSyntaxWindows);
55 EXPECT_STREQ("F:\\bar\\.", fs_windows_trailing_slash.GetCString());
56 // We get "F:/bar" instead.
57 // EXPECT_STREQ("F:\\bar", fs_windows_trailing_slash.GetDirectory().GetCString());
58 EXPECT_STREQ(".", fs_windows_trailing_slash.GetFilename().GetCString());
59}
60
61TEST(FileSpecTest, AppendPathComponent)
62{
63 FileSpec fs_posix("/foo", false, FileSpec::ePathSyntaxPosix);
64 fs_posix.AppendPathComponent("bar");
65 EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
66 EXPECT_STREQ("/foo", fs_posix.GetDirectory().GetCString());
67 EXPECT_STREQ("bar", fs_posix.GetFilename().GetCString());
68
69 FileSpec fs_windows("F:", false, FileSpec::ePathSyntaxWindows);
70 fs_windows.AppendPathComponent("bar");
71 EXPECT_STREQ("F:\\bar", fs_windows.GetCString());
72 EXPECT_STREQ("F:", fs_windows.GetDirectory().GetCString());
73 EXPECT_STREQ("bar", fs_windows.GetFilename().GetCString());
74
75 FileSpec fs_posix_root("/", false, FileSpec::ePathSyntaxPosix);
76 fs_posix_root.AppendPathComponent("bar");
77 EXPECT_STREQ("/bar", fs_posix_root.GetCString());
78 EXPECT_STREQ("/", fs_posix_root.GetDirectory().GetCString());
79 EXPECT_STREQ("bar", fs_posix_root.GetFilename().GetCString());
80
81 FileSpec fs_windows_root("F:", false, FileSpec::ePathSyntaxWindows);
82 fs_windows_root.AppendPathComponent("bar");
83 EXPECT_STREQ("F:\\bar", fs_windows_root.GetCString());
84 EXPECT_STREQ("F:", fs_windows_root.GetDirectory().GetCString());
85 EXPECT_STREQ("bar", fs_windows_root.GetFilename().GetCString());
86}
87
88TEST(FileSpecTest, CopyByAppendingPathComponent)
89{
90 FileSpec fs = FileSpec("/foo", false, FileSpec::ePathSyntaxPosix).CopyByAppendingPathComponent("bar");
91 EXPECT_STREQ("/foo/bar", fs.GetCString());
92 EXPECT_STREQ("/foo", fs.GetDirectory().GetCString());
93 EXPECT_STREQ("bar", fs.GetFilename().GetCString());
94}