[NFC][Coroutines] Use structured binding with llvm::enumerate in CoroSplit (#116879)
[llvm-project.git] / lldb / unittests / Target / PathMappingListTest.cpp
blob29d83782e08fbbe22a468bae7440fd8f40e5ccc2
1 //===-- PathMappingListTest.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Target/PathMappingList.h"
10 #include "lldb/Utility/FileSpec.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "gtest/gtest.h"
13 #include <utility>
15 using namespace lldb_private;
17 namespace {
18 struct Matches {
19 FileSpec original;
20 FileSpec remapped;
21 Matches(const char *o, const char *r) : original(o), remapped(r) {}
22 Matches(const char *o, llvm::sys::path::Style style, const char *r)
23 : original(o, style), remapped(r) {}
25 } // namespace
27 static void TestPathMappings(const PathMappingList &map,
28 llvm::ArrayRef<Matches> matches,
29 llvm::ArrayRef<ConstString> fails) {
30 ConstString actual_remapped;
31 for (const auto &fail : fails) {
32 SCOPED_TRACE(fail.GetCString());
33 EXPECT_FALSE(map.RemapPath(fail, actual_remapped))
34 << "actual_remapped: " << actual_remapped.GetCString();
36 for (const auto &match : matches) {
37 SCOPED_TRACE(match.original.GetPath() + " -> " + match.remapped.GetPath());
38 std::string orig_normalized = match.original.GetPath();
39 EXPECT_TRUE(
40 map.RemapPath(ConstString(match.original.GetPath()), actual_remapped));
41 EXPECT_EQ(FileSpec(actual_remapped.GetStringRef()), match.remapped);
42 FileSpec unmapped_spec;
43 EXPECT_TRUE(
44 map.ReverseRemapPath(match.remapped, unmapped_spec).has_value());
45 std::string unmapped_path = unmapped_spec.GetPath();
46 EXPECT_EQ(unmapped_path, orig_normalized);
50 TEST(PathMappingListTest, RelativeTests) {
51 Matches matches[] = {
52 {".", "/tmp"},
53 {"./", "/tmp"},
54 {"./////", "/tmp"},
55 {"./foo.c", "/tmp/foo.c"},
56 {"foo.c", "/tmp/foo.c"},
57 {"./bar/foo.c", "/tmp/bar/foo.c"},
58 {"bar/foo.c", "/tmp/bar/foo.c"},
60 ConstString fails[] = {
61 #ifdef _WIN32
62 ConstString("C:\\"),
63 ConstString("C:\\a"),
64 #else
65 ConstString("/a"),
66 ConstString("/"),
67 #endif
69 PathMappingList map;
70 map.Append(".", "/tmp", false);
71 TestPathMappings(map, matches, fails);
72 PathMappingList map2;
73 map2.Append("", "/tmp", false);
74 TestPathMappings(map, matches, fails);
77 TEST(PathMappingListTest, AbsoluteTests) {
78 PathMappingList map;
79 map.Append("/old", "/new", false);
80 Matches matches[] = {
81 {"/old", "/new"},
82 {"/old/", "/new"},
83 {"/old/foo/.", "/new/foo"},
84 {"/old/foo.c", "/new/foo.c"},
85 {"/old/foo.c/.", "/new/foo.c"},
86 {"/old/./foo.c", "/new/foo.c"},
88 ConstString fails[] = {
89 ConstString("/foo"),
90 ConstString("/"),
91 ConstString("foo.c"),
92 ConstString("./foo.c"),
93 ConstString("../foo.c"),
94 ConstString("../bar/foo.c"),
96 TestPathMappings(map, matches, fails);
99 TEST(PathMappingListTest, RemapRoot) {
100 PathMappingList map;
101 map.Append("/", "/new", false);
102 Matches matches[] = {
103 {"/old", "/new/old"},
104 {"/old/", "/new/old"},
105 {"/old/foo/.", "/new/old/foo"},
106 {"/old/foo.c", "/new/old/foo.c"},
107 {"/old/foo.c/.", "/new/old/foo.c"},
108 {"/old/./foo.c", "/new/old/foo.c"},
110 ConstString fails[] = {
111 ConstString("foo.c"),
112 ConstString("./foo.c"),
113 ConstString("../foo.c"),
114 ConstString("../bar/foo.c"),
116 TestPathMappings(map, matches, fails);
119 #ifndef _WIN32
120 TEST(PathMappingListTest, CrossPlatformTests) {
121 PathMappingList map;
122 map.Append(R"(C:\old)", "/new", false);
123 Matches matches[] = {
124 {R"(C:\old)", llvm::sys::path::Style::windows, "/new"},
125 {R"(C:\old\)", llvm::sys::path::Style::windows, "/new"},
126 {R"(C:\old\foo\.)", llvm::sys::path::Style::windows, "/new/foo"},
127 {R"(C:\old\foo.c)", llvm::sys::path::Style::windows, "/new/foo.c"},
128 {R"(C:\old\foo.c\.)", llvm::sys::path::Style::windows, "/new/foo.c"},
129 {R"(C:\old\.\foo.c)", llvm::sys::path::Style::windows, "/new/foo.c"},
131 ConstString fails[] = {
132 ConstString("/foo"),
133 ConstString("/"),
134 ConstString("foo.c"),
135 ConstString("./foo.c"),
136 ConstString("../foo.c"),
137 ConstString("../bar/foo.c"),
139 TestPathMappings(map, matches, fails);
141 #endif