xref: /llvm-project/clang-tools-extra/clangd/unittests/tweaks/TweakTests.cpp (revision 4d006520b8c0cc3a52913b4665bf741c737e5592)
15934a791SAdam Czachorowski //===-- TweakTests.cpp ------------------------------------------*- C++ -*-===//
25934a791SAdam Czachorowski //
35934a791SAdam Czachorowski // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45934a791SAdam Czachorowski // See https://llvm.org/LICENSE.txt for license information.
55934a791SAdam Czachorowski // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65934a791SAdam Czachorowski //
75934a791SAdam Czachorowski //===----------------------------------------------------------------------===//
85934a791SAdam Czachorowski 
95934a791SAdam Czachorowski #include "TestFS.h"
105934a791SAdam Czachorowski #include "refactor/Tweak.h"
115934a791SAdam Czachorowski #include "clang/Basic/Diagnostic.h"
125934a791SAdam Czachorowski #include "clang/Basic/DiagnosticIDs.h"
135934a791SAdam Czachorowski #include "clang/Basic/DiagnosticOptions.h"
145934a791SAdam Czachorowski #include "clang/Basic/FileManager.h"
155934a791SAdam Czachorowski #include "clang/Basic/FileSystemOptions.h"
165934a791SAdam Czachorowski #include "clang/Basic/LLVM.h"
175934a791SAdam Czachorowski #include "clang/Basic/SourceLocation.h"
185934a791SAdam Czachorowski #include "clang/Basic/SourceManager.h"
195934a791SAdam Czachorowski #include "clang/Tooling/Core/Replacement.h"
205934a791SAdam Czachorowski #include "llvm/ADT/IntrusiveRefCntPtr.h"
215934a791SAdam Czachorowski #include "llvm/ADT/StringRef.h"
225934a791SAdam Czachorowski #include "llvm/Support/Error.h"
235934a791SAdam Czachorowski #include "llvm/Support/MemoryBuffer.h"
245934a791SAdam Czachorowski #include "llvm/Support/VirtualFileSystem.h"
255934a791SAdam Czachorowski #include "llvm/Testing/Support/Error.h"
265934a791SAdam Czachorowski #include "gmock/gmock-matchers.h"
275934a791SAdam Czachorowski #include "gmock/gmock.h"
285934a791SAdam Czachorowski #include "gtest/gtest.h"
295934a791SAdam Czachorowski #include <cassert>
305934a791SAdam Czachorowski #include <string>
315934a791SAdam Czachorowski #include <utility>
325934a791SAdam Czachorowski #include <vector>
335934a791SAdam Czachorowski 
345934a791SAdam Czachorowski namespace clang {
355934a791SAdam Czachorowski namespace clangd {
365934a791SAdam Czachorowski namespace {
375934a791SAdam Czachorowski 
TEST(FileEdits,AbsolutePath)385934a791SAdam Czachorowski TEST(FileEdits, AbsolutePath) {
395934a791SAdam Czachorowski   auto RelPaths = {"a.h", "foo.cpp", "test/test.cpp"};
405934a791SAdam Czachorowski 
415934a791SAdam Czachorowski   llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS(
425934a791SAdam Czachorowski       new llvm::vfs::InMemoryFileSystem);
435934a791SAdam Czachorowski   MemFS->setCurrentWorkingDirectory(testRoot());
445934a791SAdam Czachorowski   for (const auto *Path : RelPaths)
455934a791SAdam Czachorowski     MemFS->addFile(Path, 0, llvm::MemoryBuffer::getMemBuffer("", Path));
465934a791SAdam Czachorowski   FileManager FM(FileSystemOptions(), MemFS);
475934a791SAdam Czachorowski   DiagnosticsEngine DE(new DiagnosticIDs, new DiagnosticOptions);
485934a791SAdam Czachorowski   SourceManager SM(DE, FM);
495934a791SAdam Czachorowski 
505934a791SAdam Czachorowski   for (const auto *Path : RelPaths) {
51*028e55d2SDuncan P. N. Exon Smith     auto FID = SM.createFileID(*FM.getOptionalFileRef(Path), SourceLocation(),
525934a791SAdam Czachorowski                                clang::SrcMgr::C_User);
535934a791SAdam Czachorowski     auto Res = Tweak::Effect::fileEdit(SM, FID, tooling::Replacements());
545934a791SAdam Czachorowski     ASSERT_THAT_EXPECTED(Res, llvm::Succeeded());
555934a791SAdam Czachorowski     EXPECT_EQ(Res->first, testPath(Path));
565934a791SAdam Czachorowski   }
575934a791SAdam Czachorowski }
585934a791SAdam Czachorowski 
595934a791SAdam Czachorowski } // namespace
605934a791SAdam Czachorowski } // namespace clangd
615934a791SAdam Czachorowski } // namespace clang
62