llvm-project
134 строки · 4.0 Кб
1//===- unittest/AST/CommentTextTest.cpp - Comment text extraction test ----===//
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// Tests for user-friendly output formatting of comments, i.e.
10// RawComment::getFormattedText().
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/RawCommentList.h"15#include "clang/Basic/CommentOptions.h"16#include "clang/Basic/Diagnostic.h"17#include "clang/Basic/DiagnosticIDs.h"18#include "clang/Basic/FileManager.h"19#include "clang/Basic/FileSystemOptions.h"20#include "clang/Basic/SourceLocation.h"21#include "clang/Basic/SourceManager.h"22#include "llvm/Support/MemoryBuffer.h"23#include "llvm/Support/VirtualFileSystem.h"24#include <gtest/gtest.h>25
26namespace clang {27
28class CommentTextTest : public ::testing::Test {29protected:30std::string formatComment(llvm::StringRef CommentText) {31SourceManagerForFile FileSourceMgr("comment-test.cpp", CommentText);32SourceManager& SourceMgr = FileSourceMgr.get();33
34auto CommentStartOffset = CommentText.find("/");35assert(CommentStartOffset != llvm::StringRef::npos);36FileID File = SourceMgr.getMainFileID();37
38SourceRange CommentRange(39SourceMgr.getLocForStartOfFile(File).getLocWithOffset(40CommentStartOffset),41SourceMgr.getLocForEndOfFile(File));42CommentOptions EmptyOpts;43// FIXME: technically, merged that we set here is incorrect, but that44// shouldn't matter.45RawComment Comment(SourceMgr, CommentRange, EmptyOpts, /*Merged=*/true);46DiagnosticsEngine Diags(new DiagnosticIDs, new DiagnosticOptions);47return Comment.getFormattedText(SourceMgr, Diags);48}49};50
51TEST_F(CommentTextTest, FormattedText) {52// clang-format off53auto ExpectedOutput =54R"(This function does this and that.
55For example,
56Runnning it in that case will give you
57this result.
58That's about it.)";59// Two-slash comments.60auto Formatted = formatComment(61R"cpp(
62// This function does this and that.
63// For example,
64// Runnning it in that case will give you
65// this result.
66// That's about it.)cpp");67EXPECT_EQ(ExpectedOutput, Formatted);68
69// Three-slash comments.70Formatted = formatComment(71R"cpp(
72/// This function does this and that.
73/// For example,
74/// Runnning it in that case will give you
75/// this result.
76/// That's about it.)cpp");77EXPECT_EQ(ExpectedOutput, Formatted);78
79// Block comments.80Formatted = formatComment(81R"cpp(
82/* This function does this and that.
83* For example,
84* Runnning it in that case will give you
85* this result.
86* That's about it.*/)cpp");87EXPECT_EQ(ExpectedOutput, Formatted);88
89// Doxygen-style block comments.90Formatted = formatComment(91R"cpp(
92/** This function does this and that.
93* For example,
94* Runnning it in that case will give you
95* this result.
96* That's about it.*/)cpp");97EXPECT_EQ(ExpectedOutput, Formatted);98
99// Weird indentation.100Formatted = formatComment(101R"cpp(
102// This function does this and that.
103// For example,
104// Runnning it in that case will give you
105// this result.
106// That's about it.)cpp");107EXPECT_EQ(ExpectedOutput, Formatted);108// clang-format on109}
110
111TEST_F(CommentTextTest, KeepsDoxygenControlSeqs) {112// clang-format off113auto ExpectedOutput =114R"(\brief This is the brief part of the comment.
115\param a something about a.
116@param b something about b.)";117
118auto Formatted = formatComment(119R"cpp(
120/// \brief This is the brief part of the comment.
121/// \param a something about a.
122/// @param b something about b.)cpp");123EXPECT_EQ(ExpectedOutput, Formatted);124// clang-format on125}
126
127TEST_F(CommentTextTest, EmptyFormattedText) {128// Test that empty formatted text doesn't cause crash.129const char *ExpectedOutput = "";130auto Formatted = formatComment("//!<");131EXPECT_EQ(ExpectedOutput, Formatted);132}
133
134} // namespace clang135