1 //===-- TweakTests.cpp ------------------------------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
8
9 #include "TestFS.h"
10 #include "TestTU.h"
11 #include "TweakTesting.h"
12 #include "refactor/Tweak.h"
13 #include "clang/Basic/Diagnostic.h"
14 #include "clang/Basic/DiagnosticIDs.h"
15 #include "clang/Basic/DiagnosticOptions.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/FileSystemOptions.h"
18 #include "clang/Basic/LLVM.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "clang/Basic/SourceManager.h"
21 #include "clang/Rewrite/Core/Rewriter.h"
22 #include "clang/Tooling/Core/Replacement.h"
23 #include "llvm/ADT/IntrusiveRefCntPtr.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/StringMap.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/Support/Error.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/VirtualFileSystem.h"
30 #include "llvm/Testing/Support/Error.h"
31 #include "gmock/gmock-matchers.h"
32 #include "gmock/gmock.h"
33 #include "gtest/gtest.h"
34 #include <cassert>
35 #include <string>
36 #include <utility>
37 #include <vector>
38
39 namespace clang {
40 namespace clangd {
41 namespace {
42
TEST(FileEdits,AbsolutePath)43 TEST(FileEdits, AbsolutePath) {
44 auto RelPaths = {"a.h", "foo.cpp", "test/test.cpp"};
45
46 llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS(
47 new llvm::vfs::InMemoryFileSystem);
48 MemFS->setCurrentWorkingDirectory(testRoot());
49 for (const auto *Path : RelPaths)
50 MemFS->addFile(Path, 0, llvm::MemoryBuffer::getMemBuffer("", Path));
51 FileManager FM(FileSystemOptions(), MemFS);
52 DiagnosticsEngine DE(new DiagnosticIDs, new DiagnosticOptions);
53 SourceManager SM(DE, FM);
54
55 for (const auto *Path : RelPaths) {
56 auto FID = SM.createFileID(*FM.getFile(Path), SourceLocation(),
57 clang::SrcMgr::C_User);
58 auto Res = Tweak::Effect::fileEdit(SM, FID, tooling::Replacements());
59 ASSERT_THAT_EXPECTED(Res, llvm::Succeeded());
60 EXPECT_EQ(Res->first, testPath(Path));
61 }
62 }
63
64 } // namespace
65 } // namespace clangd
66 } // namespace clang
67