#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 "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/VirtualFileSystem.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;
RawComment Comment(SourceMgr, CommentRange, EmptyOpts, true);
DiagnosticsEngine Diags(new DiagnosticIDs, new DiagnosticOptions);
return Comment.getFormattedText(SourceMgr, Diags);
}
};
TEST_F(CommentTextTest, FormattedText) {
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.)";
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);
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);
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);
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);
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);
}
TEST_F(CommentTextTest, KeepsDoxygenControlSeqs) {
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);
}
TEST_F(CommentTextTest, EmptyFormattedText) {
const char *ExpectedOutput = "";
auto Formatted = formatComment("//!<");
EXPECT_EQ(ExpectedOutput, Formatted);
}
}