From 060f7fe3e3c52bb04d771c2577a3712123574fff Mon Sep 17 00:00:00 2001 From: Ilya Biryukov Date: Tue, 29 Jan 2019 14:31:19 +0000 Subject: [PATCH] [clangd] Unit test for sourceLocationInMainFile. This should have been part of r352494, which added the corresponding function. The unit test ended up as a separate commit accidentally. llvm-svn: 352501 --- .../unittests/clangd/SourceCodeTests.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/clang-tools-extra/unittests/clangd/SourceCodeTests.cpp b/clang-tools-extra/unittests/clangd/SourceCodeTests.cpp index c17ce46c776c..316a0d5288d4 100644 --- a/clang-tools-extra/unittests/clangd/SourceCodeTests.cpp +++ b/clang-tools-extra/unittests/clangd/SourceCodeTests.cpp @@ -5,6 +5,7 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// +#include "Annotations.h" #include "SourceCode.h" #include "llvm/Support/Error.h" #include "llvm/Support/raw_os_ostream.h" @@ -16,6 +17,9 @@ namespace clang { namespace clangd { namespace { +using llvm::Failed; +using llvm::HasValue; + MATCHER_P2(Pos, Line, Col, "") { return arg.line == Line && arg.character == Col; } @@ -140,6 +144,38 @@ TEST(SourceCodeTests, IsRangeConsecutive) { isRangeConsecutive(range({2, 2}, {2, 3}), range({2, 4}, {2, 5}))); } +TEST(SourceCodeTests, SourceLocationInMainFile) { + Annotations Source(R"cpp( + ^in^t ^foo + ^bar + ^baz ^() {} {} {} {} { }^ +)cpp"); + + SourceManagerForFile Owner("foo.cpp", Source.code()); + SourceManager &SM = Owner.get(); + + SourceLocation StartOfFile = SM.getLocForStartOfFile(SM.getMainFileID()); + EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 0)), + HasValue(StartOfFile)); + // End of file. + EXPECT_THAT_EXPECTED( + sourceLocationInMainFile(SM, position(4, 0)), + HasValue(StartOfFile.getLocWithOffset(Source.code().size()))); + // Column number is too large. + EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 1)), Failed()); + EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 100)), + Failed()); + EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(4, 1)), Failed()); + // Line number is too large. + EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(5, 0)), Failed()); + // Check all positions mentioned in the test return valid results. + for (auto P : Source.points()) { + size_t Offset = llvm::cantFail(positionToOffset(Source.code(), P)); + EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, P), + HasValue(StartOfFile.getLocWithOffset(Offset))); + } +} + } // namespace } // namespace clangd } // namespace clang