Skip to content

Commit f8d10d5

Browse files
committed
[clang-format][NFC] Refactor formatting unit tests.
Pull out common base class for formatting unit tests, removing duplicate code that accumulated over the years. Pull out macro expansion test into its own test file.
1 parent b06e5ad commit f8d10d5

13 files changed

+544
-797
lines changed

clang/unittests/Format/BracesInserterTest.cpp

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,16 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "clang/Format/Format.h"
10-
11-
#include "../Tooling/ReplacementTest.h"
12-
#include "FormatTestUtils.h"
9+
#include "FormatTestBase.h"
1310

1411
#define DEBUG_TYPE "braces-inserter-test"
1512

1613
namespace clang {
1714
namespace format {
15+
namespace test {
1816
namespace {
1917

20-
class BracesInserterTest : public ::testing::Test {
21-
protected:
22-
std::string format(llvm::StringRef Code, const FormatStyle &Style,
23-
const std::vector<tooling::Range> &Ranges) {
24-
LLVM_DEBUG(llvm::errs() << "---\n");
25-
LLVM_DEBUG(llvm::errs() << Code << "\n\n");
26-
auto NonEmptyRanges = Ranges;
27-
if (Ranges.empty())
28-
NonEmptyRanges = {1, tooling::Range(0, Code.size())};
29-
FormattingAttemptStatus Status;
30-
tooling::Replacements Replaces =
31-
reformat(Style, Code, NonEmptyRanges, "<stdin>", &Status);
32-
EXPECT_EQ(true, Status.FormatComplete) << Code << "\n\n";
33-
ReplacementCount = Replaces.size();
34-
auto Result = applyAllReplacements(Code, Replaces);
35-
EXPECT_TRUE(static_cast<bool>(Result));
36-
LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
37-
return *Result;
38-
}
39-
40-
void _verifyFormat(const char *File, int Line, llvm::StringRef Expected,
41-
llvm::StringRef Code,
42-
const FormatStyle &Style = getLLVMStyle(),
43-
const std::vector<tooling::Range> &Ranges = {}) {
44-
testing::ScopedTrace t(File, Line, ::testing::Message() << Code.str());
45-
EXPECT_EQ(Expected.str(), format(Expected, Style, Ranges))
46-
<< "Expected code is not stable";
47-
EXPECT_EQ(Expected.str(), format(Code, Style, Ranges));
48-
if (Style.Language == FormatStyle::LK_Cpp && Ranges.empty()) {
49-
// Objective-C++ is a superset of C++, so everything checked for C++
50-
// needs to be checked for Objective-C++ as well.
51-
FormatStyle ObjCStyle = Style;
52-
ObjCStyle.Language = FormatStyle::LK_ObjC;
53-
EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle, Ranges));
54-
}
55-
}
56-
57-
void _verifyFormat(const char *File, int Line, llvm::StringRef Code,
58-
const FormatStyle &Style = getLLVMStyle(),
59-
const std::vector<tooling::Range> &Ranges = {}) {
60-
_verifyFormat(File, Line, Code, Code, Style, Ranges);
61-
}
62-
63-
int ReplacementCount;
64-
};
65-
66-
#define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
18+
class BracesInserterTest : public FormatTestBase {};
6719

6820
TEST_F(BracesInserterTest, InsertBraces) {
6921
FormatStyle Style = getLLVMStyle();
@@ -328,9 +280,10 @@ TEST_F(BracesInserterTest, InsertBracesRange) {
328280
" }",
329281
Code, Style, {tooling::Range(10, 8)}); // line 2
330282

331-
verifyFormat(Code, Style, {tooling::Range(19, 11)}); // line 3
283+
verifyFormat(Code, Code, Style, {tooling::Range(19, 11)}); // line 3
332284
}
333285

334286
} // namespace
287+
} // namespace test
335288
} // namespace format
336289
} // namespace clang

clang/unittests/Format/BracesRemoverTest.cpp

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,16 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "clang/Format/Format.h"
10-
11-
#include "../Tooling/ReplacementTest.h"
12-
#include "FormatTestUtils.h"
9+
#include "FormatTestBase.h"
1310

1411
#define DEBUG_TYPE "braces-remover-test"
1512

1613
namespace clang {
1714
namespace format {
15+
namespace test {
1816
namespace {
1917

20-
// TODO:
21-
// Refactor the class declaration, which is copied from BracesInserterTest.cpp.
22-
class BracesRemoverTest : public ::testing::Test {
23-
protected:
24-
std::string format(llvm::StringRef Code, const FormatStyle &Style,
25-
const std::vector<tooling::Range> &Ranges) {
26-
LLVM_DEBUG(llvm::errs() << "---\n");
27-
LLVM_DEBUG(llvm::errs() << Code << "\n\n");
28-
auto NonEmptyRanges = Ranges;
29-
if (Ranges.empty())
30-
NonEmptyRanges = {1, tooling::Range(0, Code.size())};
31-
FormattingAttemptStatus Status;
32-
tooling::Replacements Replaces =
33-
reformat(Style, Code, NonEmptyRanges, "<stdin>", &Status);
34-
EXPECT_EQ(true, Status.FormatComplete) << Code << "\n\n";
35-
ReplacementCount = Replaces.size();
36-
auto Result = applyAllReplacements(Code, Replaces);
37-
EXPECT_TRUE(static_cast<bool>(Result));
38-
LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
39-
return *Result;
40-
}
41-
42-
void _verifyFormat(const char *File, int Line, llvm::StringRef Expected,
43-
llvm::StringRef Code,
44-
const FormatStyle &Style = getLLVMStyle(),
45-
const std::vector<tooling::Range> &Ranges = {}) {
46-
testing::ScopedTrace t(File, Line, ::testing::Message() << Code.str());
47-
EXPECT_EQ(Expected.str(), format(Expected, Style, Ranges))
48-
<< "Expected code is not stable";
49-
EXPECT_EQ(Expected.str(), format(Code, Style, Ranges));
50-
if (Style.Language == FormatStyle::LK_Cpp && Ranges.empty()) {
51-
// Objective-C++ is a superset of C++, so everything checked for C++
52-
// needs to be checked for Objective-C++ as well.
53-
FormatStyle ObjCStyle = Style;
54-
ObjCStyle.Language = FormatStyle::LK_ObjC;
55-
EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle, Ranges));
56-
}
57-
}
58-
59-
void _verifyFormat(const char *File, int Line, llvm::StringRef Code,
60-
const FormatStyle &Style = getLLVMStyle(),
61-
const std::vector<tooling::Range> &Ranges = {}) {
62-
_verifyFormat(File, Line, Code, Code, Style, Ranges);
63-
}
64-
65-
int ReplacementCount;
66-
};
67-
68-
#define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
18+
class BracesRemoverTest : public FormatTestBase {};
6919

7020
TEST_F(BracesRemoverTest, RemoveBraces) {
7121
FormatStyle Style = getLLVMStyle();
@@ -982,5 +932,6 @@ TEST_F(BracesRemoverTest, RemoveBraces) {
982932
}
983933

984934
} // namespace
935+
} // namespace test
985936
} // namespace format
986937
} // namespace clang

clang/unittests/Format/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_clang_unittest(FormatTests
1414
FormatTestJS.cpp
1515
FormatTestJava.cpp
1616
FormatTestJson.cpp
17+
FormatTestMacroExpansion.cpp
1718
FormatTestObjC.cpp
1819
FormatTestProto.cpp
1920
FormatTestRawStrings.cpp
@@ -22,6 +23,7 @@ add_clang_unittest(FormatTests
2223
FormatTestTextProto.cpp
2324
FormatTestVerilog.cpp
2425
FormatTokenSourceTest.cpp
26+
FormatReplacementTest.cpp
2527
IntegerLiteralSeparatorTest.cpp
2628
MacroCallReconstructorTest.cpp
2729
MacroExpanderTest.cpp
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===- unittest/Format/FormatReplacementTest.cpp - Formatting unit test ---===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "../Tooling/ReplacementTest.h"
10+
#include "clang/Format/Format.h"
11+
12+
namespace clang {
13+
namespace tooling {
14+
namespace {
15+
16+
using format::FormatStyle;
17+
using format::getLLVMStyle;
18+
19+
TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
20+
// Column limit is 20.
21+
std::string Code = "Type *a =\n"
22+
" new Type();\n"
23+
"g(iiiii, 0, jjjjj,\n"
24+
" 0, kkkkk, 0, mm);\n"
25+
"int bad = format ;";
26+
std::string Expected = "auto a = new Type();\n"
27+
"g(iiiii, nullptr,\n"
28+
" jjjjj, nullptr,\n"
29+
" kkkkk, nullptr,\n"
30+
" mm);\n"
31+
"int bad = format ;";
32+
FileID ID = Context.createInMemoryFile("format.cpp", Code);
33+
tooling::Replacements Replaces = toReplacements(
34+
{tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
35+
"auto "),
36+
tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
37+
"nullptr"),
38+
tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
39+
"nullptr"),
40+
tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
41+
"nullptr")});
42+
43+
FormatStyle Style = getLLVMStyle();
44+
Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
45+
auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
46+
EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
47+
<< llvm::toString(FormattedReplaces.takeError()) << "\n";
48+
auto Result = applyAllReplacements(Code, *FormattedReplaces);
49+
EXPECT_TRUE(static_cast<bool>(Result));
50+
EXPECT_EQ(Expected, *Result);
51+
}
52+
53+
TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
54+
std::string Code = "#include \"a.h\"\n"
55+
"#include \"c.h\"\n"
56+
"\n"
57+
"int main() {\n"
58+
" return 0;\n"
59+
"}";
60+
std::string Expected = "#include \"a.h\"\n"
61+
"#include \"b.h\"\n"
62+
"#include \"c.h\"\n"
63+
"\n"
64+
"int main() {\n"
65+
" return 0;\n"
66+
"}";
67+
FileID ID = Context.createInMemoryFile("fix.cpp", Code);
68+
tooling::Replacements Replaces = toReplacements(
69+
{tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
70+
"#include \"b.h\"\n")});
71+
72+
FormatStyle Style = getLLVMStyle();
73+
Style.SortIncludes = FormatStyle::SI_CaseSensitive;
74+
auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
75+
EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
76+
<< llvm::toString(FormattedReplaces.takeError()) << "\n";
77+
auto Result = applyAllReplacements(Code, *FormattedReplaces);
78+
EXPECT_TRUE(static_cast<bool>(Result));
79+
EXPECT_EQ(Expected, *Result);
80+
}
81+
82+
} // namespace
83+
} // namespace tooling
84+
} // namespace clang

0 commit comments

Comments
 (0)