1 //===- unittest/Format/BracesInserterTest.cpp -----------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "FormatTestBase.h"
11 #define DEBUG_TYPE "braces-inserter-test"
18 class BracesInserterTest
: public FormatTestBase
{};
20 TEST_F(BracesInserterTest
, InsertBraces
) {
21 FormatStyle Style
= getLLVMStyle();
22 Style
.InsertBraces
= true;
24 verifyFormat("// clang-format off\n"
27 "// clang-format on\n"
31 "// clang-format off\n"
34 "// clang-format on\n"
38 verifyFormat("if (a) {\n"
57 verifyFormat("for (auto node : nodes) {\n"
62 "for (auto node : nodes)\n"
67 verifyFormat("for (auto node : nodes) {\n"
70 "for (auto node : nodes)\n"
82 verifyFormat("if (i) {\n"
94 verifyFormat("void f() {\n"
119 verifyFormat("if (a) {\n"
134 verifyFormat("if (a) {\n"
145 verifyFormat("if (a) {\n"
156 verifyFormat("if (a) { //\n"
163 verifyFormat("if (a) { // comment\n"
167 "if (a) // comment\n"
172 verifyFormat("if (a) {\n"
179 verifyFormat("if (a) {\n"
190 verifyFormat("if (a)\n"
198 verifyFormat("#if 0\n"
214 verifyFormat("do {\n"
223 Style
.RemoveBracesLLVM
= true;
224 verifyFormat("if (a) //\n"
227 Style
.RemoveBracesLLVM
= false;
229 Style
.ColumnLimit
= 15;
231 verifyFormat("#define A \\\n"
236 verifyFormat("if (a + b >\n"
244 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
245 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
247 verifyFormat("if (a) //\n"
256 TEST_F(BracesInserterTest
, InsertBracesRange
) {
257 FormatStyle Style
= getLLVMStyle();
258 Style
.InsertBraces
= true;
260 const StringRef
Code("while (a)\n"
264 verifyFormat("while (a) {\n"
268 Code
, Style
, {tooling::Range(0, 9)}); // line 1
270 verifyFormat("while (a) {\n"
275 Code
, Style
, {tooling::Range(0, 18)}); // lines 1-2
277 verifyFormat("while (a)\n"
281 Code
, Style
, {tooling::Range(10, 8)}); // line 2
283 verifyFormat(Code
, Code
, Style
, {tooling::Range(19, 11)}); // line 3
288 } // namespace format