diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2018-07-28 11:06:01 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2018-07-28 11:06:01 +0000 |
commit | 486754660bb926339aefcf012a3f848592babb8b (patch) | |
tree | ecdbc446c9876f4f120f701c243373cd3cb43db3 /unittests/AST/CommentTextTest.cpp | |
parent | 55e6d896ad333f07bb3b1ba487df214fc268a4ab (diff) |
Notes
Diffstat (limited to 'unittests/AST/CommentTextTest.cpp')
-rw-r--r-- | unittests/AST/CommentTextTest.cpp | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/unittests/AST/CommentTextTest.cpp b/unittests/AST/CommentTextTest.cpp new file mode 100644 index 0000000000000..04475f1c10616 --- /dev/null +++ b/unittests/AST/CommentTextTest.cpp @@ -0,0 +1,128 @@ +//===- unittest/AST/CommentTextTest.cpp - Comment text extraction test ----===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Tests for user-friendly output formatting of comments, i.e. +// RawComment::getFormattedText(). +// +//===----------------------------------------------------------------------===// + +#include "clang/AST/RawCommentList.h" +#include "clang/Basic/CommentOptions.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/DiagnosticIDs.h" +#include "clang/Basic/FileManager.h" +#include "clang/Basic/FileSystemOptions.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Basic/VirtualFileSystem.h" +#include "llvm/Support/MemoryBuffer.h" +#include <gtest/gtest.h> + +namespace clang { + +class CommentTextTest : public ::testing::Test { +protected: + std::string formatComment(llvm::StringRef CommentText) { + SourceManagerForFile FileSourceMgr("comment-test.cpp", CommentText); + SourceManager& SourceMgr = FileSourceMgr.get(); + + auto CommentStartOffset = CommentText.find("/"); + assert(CommentStartOffset != llvm::StringRef::npos); + FileID File = SourceMgr.getMainFileID(); + + SourceRange CommentRange( + SourceMgr.getLocForStartOfFile(File).getLocWithOffset( + CommentStartOffset), + SourceMgr.getLocForEndOfFile(File)); + CommentOptions EmptyOpts; + // FIXME: technically, merged that we set here is incorrect, but that + // shouldn't matter. + RawComment Comment(SourceMgr, CommentRange, EmptyOpts, /*Merged=*/true); + DiagnosticsEngine Diags(new DiagnosticIDs, new DiagnosticOptions); + return Comment.getFormattedText(SourceMgr, Diags); + } +}; + +TEST_F(CommentTextTest, FormattedText) { + // clang-format off + auto ExpectedOutput = +R"(This function does this and that. +For example, + Runnning it in that case will give you + this result. +That's about it.)"; + // Two-slash comments. + auto Formatted = formatComment( +R"cpp( +// This function does this and that. +// For example, +// Runnning it in that case will give you +// this result. +// That's about it.)cpp"); + EXPECT_EQ(ExpectedOutput, Formatted); + + // Three-slash comments. + Formatted = formatComment( +R"cpp( +/// This function does this and that. +/// For example, +/// Runnning it in that case will give you +/// this result. +/// That's about it.)cpp"); + EXPECT_EQ(ExpectedOutput, Formatted); + + // Block comments. + Formatted = formatComment( +R"cpp( +/* This function does this and that. + * For example, + * Runnning it in that case will give you + * this result. + * That's about it.*/)cpp"); + EXPECT_EQ(ExpectedOutput, Formatted); + + // Doxygen-style block comments. + Formatted = formatComment( +R"cpp( +/** This function does this and that. + * For example, + * Runnning it in that case will give you + * this result. + * That's about it.*/)cpp"); + EXPECT_EQ(ExpectedOutput, Formatted); + + // Weird indentation. + Formatted = formatComment( +R"cpp( + // This function does this and that. + // For example, + // Runnning it in that case will give you + // this result. + // That's about it.)cpp"); + EXPECT_EQ(ExpectedOutput, Formatted); + // clang-format on +} + +TEST_F(CommentTextTest, KeepsDoxygenControlSeqs) { + // clang-format off + auto ExpectedOutput = +R"(\brief This is the brief part of the comment. +\param a something about a. +@param b something about b.)"; + + auto Formatted = formatComment( +R"cpp( +/// \brief This is the brief part of the comment. +/// \param a something about a. +/// @param b something about b.)cpp"); + EXPECT_EQ(ExpectedOutput, Formatted); + // clang-format on +} + +} // namespace clang |