10#include "llvm/Support/JSON.h"
11#include "gmock/gmock.h"
12#include "gtest/gtest.h"
18using ::testing::ElementsAre;
19MATCHER_P2(Mapping, ClientPath, ServerPath,
"") {
20 return arg.ClientPath == ClientPath && arg.ServerPath == ServerPath;
23bool failedParse(llvm::StringRef RawMappings) {
26 consumeError(Mappings.takeError());
32TEST(ParsePathMappingTests, WindowsPath) {
34 EXPECT_TRUE(failedParse(R
"(C:a=/root)"));
35 EXPECT_TRUE(failedParse(R"(\C:a=/root)"));
37 EXPECT_TRUE(failedParse(R
"(\a=/root)"));
39 llvm::Expected<PathMappings> ParsedMappings =
41 ASSERT_TRUE(bool(ParsedMappings));
42 EXPECT_THAT(*ParsedMappings, ElementsAre(Mapping(
"/C:/a",
"/root")));
45 ASSERT_TRUE(bool(ParsedMappings));
46 EXPECT_THAT(*ParsedMappings, ElementsAre(Mapping(
"//Server/C$",
"/root")));
49TEST(ParsePathMappingTests, UnixPath) {
51 EXPECT_TRUE(failedParse(
"a/b=/root"));
54 ASSERT_TRUE(
bool(ParsedMappings));
55 EXPECT_THAT(*ParsedMappings, ElementsAre(Mapping(
"/A/b",
"/root")));
58 ASSERT_TRUE(bool(ParsedMappings));
59 EXPECT_THAT(*ParsedMappings, ElementsAre(Mapping(R
"(/a/b\\ar)", "/root")));
62TEST(ParsePathMappingTests, ImproperFormat) {
64 EXPECT_TRUE(failedParse(
"/home/myuser1="));
66 EXPECT_TRUE(failedParse(
"home/project=/workarea/project"));
68 EXPECT_TRUE(failedParse(
"/home==/workarea"));
70 EXPECT_TRUE(failedParse(
"/home"));
72 EXPECT_TRUE(failedParse(
"/home,/workarea"));
75TEST(ParsePathMappingTests, ParsesMultiple) {
76 std::string RawPathMappings =
77 "/home/project=/workarea/project,/home/project/.includes=/opt/include";
79 ASSERT_TRUE(
bool(Parsed));
81 ElementsAre(Mapping(
"/home/project",
"/workarea/project"),
82 Mapping(
"/home/project/.includes",
"/opt/include")));
85bool mapsProperly(llvm::StringRef Orig, llvm::StringRef
Expected,
91 std::string Actual = MappedPath ? *MappedPath : Orig.str();
92 EXPECT_STREQ(
Expected.str().c_str(), Actual.c_str());
96TEST(DoPathMappingTests, PreservesOriginal) {
98 EXPECT_TRUE(mapsProperly(
"file:///home",
"file:///home",
"",
102TEST(DoPathMappingTests, UsesFirstMatch) {
103 EXPECT_TRUE(mapsProperly(
"file:///home/foo.cpp",
"file:///workarea1/foo.cpp",
104 "/home=/workarea1,/home=/workarea2",
108TEST(DoPathMappingTests, IgnoresSubstrings) {
110 EXPECT_TRUE(mapsProperly(
"file://home/foo-bar.cpp",
"file://home/foo-bar.cpp",
111 "/home/foo=/home/bar",
115TEST(DoPathMappingTests, MapsOutgoingPaths) {
117 EXPECT_TRUE(mapsProperly(
"file:///workarea/foo.cpp",
"file:///home/foo.cpp",
122TEST(DoPathMappingTests, OnlyMapFileUris) {
123 EXPECT_TRUE(mapsProperly(
"test:///home/foo.cpp",
"test:///home/foo.cpp",
128TEST(DoPathMappingTests, RespectsCaseSensitivity) {
129 EXPECT_TRUE(mapsProperly(
"file:///HOME/foo.cpp",
"file:///HOME/foo.cpp",
134TEST(DoPathMappingTests, MapsWindowsPaths) {
136 EXPECT_TRUE(mapsProperly(
"file:///C:/home/foo.cpp",
137 "file:///C:/workarea/foo.cpp", R
"(C:\home=C:\workarea)",
141TEST(DoPathMappingTests, MapsWindowsUnixInterop) {
143 EXPECT_TRUE(mapsProperly(
144 "file:///C:/home/foo.cpp",
"file:///workarea/foo.cpp",
148TEST(ApplyPathMappingTests, PreservesOriginalParams) {
149 auto Params = llvm::json::parse(R
"({
150 "textDocument": {"uri": "file:///home/foo.cpp"},
151 "position": {"line": 0, "character": 0}
153 ASSERT_TRUE(bool(Params));
154 llvm::json::Value ExpectedParams = *Params;
157 EXPECT_EQ(*Params, ExpectedParams);
160TEST(ApplyPathMappingTests, MapsAllMatchingPaths) {
162 auto Params = llvm::json::parse(R
"({
163 "rootUri": {"uri": "file:///home/foo.cpp"},
164 "workspaceFolders": ["file:///home/src", "file:///tmp"]
166 auto ExpectedParams = llvm::json::parse(R
"({
167 "rootUri": {"uri": "file:///workarea/foo.cpp"},
168 "workspaceFolders": ["file:///workarea/src", "file:///tmp"]
171 ASSERT_TRUE(
bool(Params) &&
bool(ExpectedParams) &&
bool(Mappings));
173 EXPECT_EQ(*Params, *ExpectedParams);
176TEST(ApplyPathMappingTests, MapsOutbound) {
177 auto Params = llvm::json::parse(R
"({
180 {"uri": "file:///opt/include/foo.h"},
181 {"uri": "file:///workarea/src/foo.cpp"}]
183 auto ExpectedParams = llvm::json::parse(R
"({
186 {"uri": "file:///home/.includes/foo.h"},
187 {"uri": "file:///home/src/foo.cpp"}]
191 ASSERT_TRUE(
bool(Params) &&
bool(ExpectedParams) &&
bool(Mappings));
193 EXPECT_EQ(*Params, *ExpectedParams);
196TEST(ApplyPathMappingTests, MapsKeys) {
197 auto Params = llvm::json::parse(R
"({
199 "file:///home/foo.cpp": {"newText": "..."},
200 "file:///home/src/bar.cpp": {"newText": "..."}
203 auto ExpectedParams = llvm::json::parse(R
"({
205 "file:///workarea/foo.cpp": {"newText": "..."},
206 "file:///workarea/src/bar.cpp": {"newText": "..."}
210 ASSERT_TRUE(
bool(Params) &&
bool(ExpectedParams) &&
bool(Mappings));
212 EXPECT_EQ(*Params, *ExpectedParams);
enum clang::clangd::@1062::Bracket::Direction Dir
std::vector< const char * > Expected
MATCHER_P2(hasFlag, Flag, Path, "")
llvm::Expected< PathMappings > parsePathMappings(llvm::StringRef RawPathMappings)
Parse the command line RawPathMappings (e.g.
void applyPathMappings(llvm::json::Value &V, PathMapping::Direction Dir, const PathMappings &Mappings)
Applies the Mappings to all the file:// URIs in Params.
TEST(BackgroundQueueTest, Priority)
std::vector< PathMapping > PathMappings
std::optional< std::string > doPathMapping(llvm::StringRef S, PathMapping::Direction Dir, const PathMappings &Mappings)
Returns a modified S with the first matching path in Mappings substituted, if applicable.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//