1 //===-- FormatTests.cpp - Automatic code formatting tests -----------------===//
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
7 //===----------------------------------------------------------------------===//
10 #include "Annotations.h"
11 #include "SourceCode.h"
12 #include "clang/Format/Format.h"
13 #include "clang/Tooling/Core/Replacement.h"
14 #include "llvm/Support/Error.h"
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
22 std::string
afterTyped(llvm::StringRef CodeWithCursor
, llvm::StringRef Typed
,
23 clang::format::FormatStyle Style
) {
24 Annotations
Code(CodeWithCursor
);
25 unsigned Cursor
= llvm::cantFail(positionToOffset(Code
.code(), Code
.point()));
26 auto Changes
= formatIncremental(Code
.code(), Cursor
, Typed
, Style
);
27 tooling::Replacements Merged
;
28 for (const auto& R
: Changes
)
29 if (llvm::Error E
= Merged
.add(R
))
30 ADD_FAILURE() << llvm::toString(std::move(E
));
31 auto NewCode
= tooling::applyAllReplacements(Code
.code(), Merged
);
32 EXPECT_TRUE(bool(NewCode
))
33 << "Bad replacements: " << llvm::toString(NewCode
.takeError());
34 NewCode
->insert(transformCursorPosition(Cursor
, Changes
), "^");
38 // We can't pass raw strings directly to EXPECT_EQ because of gcc bugs.
39 void expectAfterNewline(const char *Before
, const char *After
,
40 format::FormatStyle Style
= format::getGoogleStyle(
41 format::FormatStyle::LK_Cpp
)) {
42 EXPECT_EQ(After
, afterTyped(Before
, "\n", Style
)) << Before
;
44 void expectAfter(const char *Typed
, const char *Before
, const char *After
,
45 format::FormatStyle Style
=
46 format::getGoogleStyle(format::FormatStyle::LK_Cpp
)) {
47 EXPECT_EQ(After
, afterTyped(Before
, Typed
, Style
)) << Before
;
50 TEST(FormatIncremental
, SplitComment
) {
51 expectAfterNewline(R
"cpp(
60 expectAfterNewline(R
"cpp(
61 // trailing whitespace is not a split
65 // trailing whitespace is not a split
69 expectAfterNewline(R
"cpp(
80 expectAfterNewline(R
"cpp(
89 expectAfterNewline(R
"cpp(
98 expectAfterNewline(R
"cpp(
99 /// editor continuation
103 /// editor continuation
107 expectAfterNewline(R
"cpp(
117 expectAfterNewline(R
"cpp(
126 // Fixed bug: the second line of the aligned comment shouldn't be "attached"
127 // to the cursor and outdented.
128 expectAfterNewline(R
"cpp(
131 return; // All spelled tokens are accounted for.
132 // that takes two lines
139 return; // All spelled tokens are accounted for.
140 // that takes two lines
145 // Handle tab character in leading indentation
146 format::FormatStyle TabStyle
=
147 format::getGoogleStyle(format::FormatStyle::LK_Cpp
);
148 TabStyle
.UseTab
= format::FormatStyle::UT_Always
;
149 TabStyle
.TabWidth
= 4;
150 TabStyle
.IndentWidth
= 4;
151 // Do not use raw strings, otherwise '\t' will be interpreted literally.
152 expectAfterNewline("void foo() {\n\t// this comment was\n^split\n}\n",
153 "void foo() {\n\t// this comment was\n\t// ^split\n}\n",
157 TEST(FormatIncremental
, Indentation
) {
158 expectAfterNewline(R
"cpp(
169 expectAfterNewline(R
"cpp(
180 expectAfterNewline(R
"cpp(
190 expectAfterNewline(R
"cpp(
201 // Mismatched brackets (1)
202 expectAfterNewline(R
"cpp(
215 // Mismatched brackets (2)
216 expectAfterNewline(R
"cpp(
230 expectAfterNewline(R
"cpp(
245 TEST(FormatIncremental
, FormatPreviousLine
) {
246 expectAfterNewline(R
"cpp(
259 expectAfterNewline(R
"cpp(
261 auto L = []{return;return;};
274 TEST(FormatIncremental
, Annoyances
) {
275 // Don't remove newlines the user typed!
276 expectAfterNewline(R
"cpp(
290 // FIXME: we should not remove newlines here, either.
291 expectAfterNewline(R
"cpp(
306 TEST(FormatIncremental
, FormatBrace
) {
307 expectAfter("}", R
"cpp(
314 vector<int> x = {1, 2, 3}^
319 } // namespace clangd