CommentTextTest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //===- unittest/AST/CommentTextTest.cpp - Comment text extraction test ----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Tests for user-friendly output formatting of comments, i.e.
  11. // RawComment::getFormattedText().
  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. namespace clang {
  26. class CommentTextTest : public ::testing::Test {
  27. protected:
  28. std::string formatComment(llvm::StringRef CommentText) {
  29. SourceManagerForFile FileSourceMgr("comment-test.cpp", CommentText);
  30. SourceManager& SourceMgr = FileSourceMgr.get();
  31. auto CommentStartOffset = CommentText.find("/");
  32. assert(CommentStartOffset != llvm::StringRef::npos);
  33. FileID File = SourceMgr.getMainFileID();
  34. SourceRange CommentRange(
  35. SourceMgr.getLocForStartOfFile(File).getLocWithOffset(
  36. CommentStartOffset),
  37. SourceMgr.getLocForEndOfFile(File));
  38. CommentOptions EmptyOpts;
  39. // FIXME: technically, merged that we set here is incorrect, but that
  40. // shouldn't matter.
  41. RawComment Comment(SourceMgr, CommentRange, EmptyOpts, /*Merged=*/true);
  42. DiagnosticsEngine Diags(new DiagnosticIDs, new DiagnosticOptions);
  43. return Comment.getFormattedText(SourceMgr, Diags);
  44. }
  45. };
  46. TEST_F(CommentTextTest, FormattedText) {
  47. // clang-format off
  48. auto ExpectedOutput =
  49. R"(This function does this and that.
  50. For example,
  51. Runnning it in that case will give you
  52. this result.
  53. That's about it.)";
  54. // Two-slash comments.
  55. auto Formatted = formatComment(
  56. R"cpp(
  57. // This function does this and that.
  58. // For example,
  59. // Runnning it in that case will give you
  60. // this result.
  61. // That's about it.)cpp");
  62. EXPECT_EQ(ExpectedOutput, Formatted);
  63. // Three-slash comments.
  64. Formatted = formatComment(
  65. R"cpp(
  66. /// This function does this and that.
  67. /// For example,
  68. /// Runnning it in that case will give you
  69. /// this result.
  70. /// That's about it.)cpp");
  71. EXPECT_EQ(ExpectedOutput, Formatted);
  72. // Block comments.
  73. Formatted = formatComment(
  74. R"cpp(
  75. /* This function does this and that.
  76. * For example,
  77. * Runnning it in that case will give you
  78. * this result.
  79. * That's about it.*/)cpp");
  80. EXPECT_EQ(ExpectedOutput, Formatted);
  81. // Doxygen-style block comments.
  82. Formatted = formatComment(
  83. R"cpp(
  84. /** This function does this and that.
  85. * For example,
  86. * Runnning it in that case will give you
  87. * this result.
  88. * That's about it.*/)cpp");
  89. EXPECT_EQ(ExpectedOutput, Formatted);
  90. // Weird indentation.
  91. Formatted = formatComment(
  92. R"cpp(
  93. // This function does this and that.
  94. // For example,
  95. // Runnning it in that case will give you
  96. // this result.
  97. // That's about it.)cpp");
  98. EXPECT_EQ(ExpectedOutput, Formatted);
  99. // clang-format on
  100. }
  101. TEST_F(CommentTextTest, KeepsDoxygenControlSeqs) {
  102. // clang-format off
  103. auto ExpectedOutput =
  104. R"(\brief This is the brief part of the comment.
  105. \param a something about a.
  106. @param b something about b.)";
  107. auto Formatted = formatComment(
  108. R"cpp(
  109. /// \brief This is the brief part of the comment.
  110. /// \param a something about a.
  111. /// @param b something about b.)cpp");
  112. EXPECT_EQ(ExpectedOutput, Formatted);
  113. // clang-format on
  114. }
  115. } // namespace clang