brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.0 KiB · 675173c Raw
136 lines · cpp
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-exception6//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:30  std::string formatComment(llvm::StringRef CommentText) {31    SourceManagerForFile FileSourceMgr("comment-test.cpp", CommentText);32    SourceManager& SourceMgr = FileSourceMgr.get();33 34    auto CommentStartOffset = CommentText.find("/");35    assert(CommentStartOffset != llvm::StringRef::npos);36    FileID File = SourceMgr.getMainFileID();37 38    SourceRange CommentRange(39        SourceMgr.getLocForStartOfFile(File).getLocWithOffset(40            CommentStartOffset),41        SourceMgr.getLocForEndOfFile(File));42    CommentOptions EmptyOpts;43    // FIXME: technically, merged that we set here is incorrect, but that44    // shouldn't matter.45    RawComment Comment(SourceMgr, CommentRange, EmptyOpts, /*Merged=*/true);46    DiagnosticOptions DiagOpts;47    DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts);48    return Comment.getFormattedText(SourceMgr, Diags);49  }50};51 52TEST_F(CommentTextTest, FormattedText) {53  // clang-format off54  auto ExpectedOutput =55R"(This function does this and that.56For example,57   Runnning it in that case will give you58   this result.59That's about it.)";60  // Two-slash comments.61  auto Formatted = formatComment(62R"cpp(63// This function does this and that.64// For example,65//    Runnning it in that case will give you66//    this result.67// That's about it.)cpp");68  EXPECT_EQ(ExpectedOutput, Formatted);69 70  // Three-slash comments.71  Formatted = formatComment(72R"cpp(73/// This function does this and that.74/// For example,75///    Runnning it in that case will give you76///    this result.77/// That's about it.)cpp");78  EXPECT_EQ(ExpectedOutput, Formatted);79 80  // Block comments.81  Formatted = formatComment(82R"cpp(83/* This function does this and that.84 * For example,85 *    Runnning it in that case will give you86 *    this result.87 * That's about it.*/)cpp");88  EXPECT_EQ(ExpectedOutput, Formatted);89 90  // Doxygen-style block comments.91  Formatted = formatComment(92R"cpp(93/** This function does this and that.94  * For example,95  *    Runnning it in that case will give you96  *    this result.97  * That's about it.*/)cpp");98  EXPECT_EQ(ExpectedOutput, Formatted);99 100  // Weird indentation.101  Formatted = formatComment(102R"cpp(103       // This function does this and that.104  //      For example,105  //         Runnning it in that case will give you106        //   this result.107       // That's about it.)cpp");108  EXPECT_EQ(ExpectedOutput, Formatted);109  // clang-format on110}111 112TEST_F(CommentTextTest, KeepsDoxygenControlSeqs) {113  // clang-format off114  auto ExpectedOutput =115R"(\brief This is the brief part of the comment.116\param a something about a.117@param b something about b.)";118 119  auto Formatted = formatComment(120R"cpp(121/// \brief This is the brief part of the comment.122/// \param a something about a.123/// @param b something about b.)cpp");124  EXPECT_EQ(ExpectedOutput, Formatted);125  // clang-format on126}127 128TEST_F(CommentTextTest, EmptyFormattedText) {129  // Test that empty formatted text doesn't cause crash.130  const char *ExpectedOutput = "";131  auto Formatted = formatComment("//!<");132  EXPECT_EQ(ExpectedOutput, Formatted);133}134 135} // namespace clang136