2016-04-25 23:09:22 +08:00
|
|
|
//===- unittest/Format/CleanupTest.cpp - Code cleanup unit tests ----------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-04-25 23:09:22 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
|
2016-08-01 18:16:37 +08:00
|
|
|
#include "../Tooling/ReplacementTest.h"
|
2016-05-27 16:20:02 +08:00
|
|
|
#include "../Tooling/RewriterTestContext.h"
|
2016-04-25 23:09:22 +08:00
|
|
|
#include "clang/Tooling/Core/Replacement.h"
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2016-08-01 18:16:37 +08:00
|
|
|
using clang::tooling::ReplacementTest;
|
|
|
|
using clang::tooling::toReplacements;
|
|
|
|
|
2016-04-25 23:09:22 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CleanupTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
std::string cleanup(llvm::StringRef Code,
|
|
|
|
const std::vector<tooling::Range> &Ranges,
|
|
|
|
const FormatStyle &Style = getLLVMStyle()) {
|
|
|
|
tooling::Replacements Replaces = format::cleanup(Style, Code, Ranges);
|
|
|
|
|
2016-07-11 21:53:12 +08:00
|
|
|
auto Result = applyAllReplacements(Code, Replaces);
|
|
|
|
EXPECT_TRUE(static_cast<bool>(Result));
|
|
|
|
return *Result;
|
2016-04-25 23:09:22 +08:00
|
|
|
}
|
2016-09-13 23:02:43 +08:00
|
|
|
|
|
|
|
// Returns code after cleanup around \p Offsets.
|
|
|
|
std::string cleanupAroundOffsets(llvm::ArrayRef<unsigned> Offsets,
|
2017-05-29 16:41:11 +08:00
|
|
|
llvm::StringRef Code,
|
|
|
|
const FormatStyle &Style = getLLVMStyle()) {
|
2016-09-13 23:02:43 +08:00
|
|
|
std::vector<tooling::Range> Ranges;
|
|
|
|
for (auto Offset : Offsets)
|
|
|
|
Ranges.push_back(tooling::Range(Offset, 0));
|
2017-05-29 16:41:11 +08:00
|
|
|
return cleanup(Code, Ranges, Style);
|
2016-09-13 23:02:43 +08:00
|
|
|
}
|
2016-04-25 23:09:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(CleanupTest, DeleteEmptyNamespaces) {
|
|
|
|
std::string Code = "namespace A {\n"
|
|
|
|
"namespace B {\n"
|
|
|
|
"} // namespace B\n"
|
|
|
|
"} // namespace A\n\n"
|
|
|
|
"namespace C {\n"
|
|
|
|
"namespace D { int i; }\n"
|
|
|
|
"inline namespace E { namespace { } }\n"
|
|
|
|
"}";
|
|
|
|
std::string Expected = "\n\n\n\n\nnamespace C {\n"
|
|
|
|
"namespace D { int i; }\n \n"
|
|
|
|
"}";
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({28, 91, 132}, Code));
|
2016-04-25 23:09:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CleanupTest, NamespaceWithSyntaxError) {
|
|
|
|
std::string Code = "namespace A {\n"
|
|
|
|
"namespace B {\n" // missing r_brace
|
|
|
|
"} // namespace A\n\n"
|
|
|
|
"namespace C {\n"
|
|
|
|
"namespace D int i; }\n"
|
|
|
|
"inline namespace E { namespace { } }\n"
|
|
|
|
"}";
|
|
|
|
std::string Expected = "namespace A {\n"
|
|
|
|
"\n\n\nnamespace C {\n"
|
|
|
|
"namespace D int i; }\n \n"
|
|
|
|
"}";
|
|
|
|
std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanup(Code, Ranges));
|
2016-04-25 23:09:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CleanupTest, EmptyNamespaceNotAffected) {
|
|
|
|
std::string Code = "namespace A {\n\n"
|
|
|
|
"namespace {\n\n}}";
|
|
|
|
// Even though the namespaces are empty, but the inner most empty namespace
|
|
|
|
// block is not affected by the changed ranges.
|
|
|
|
std::string Expected = "namespace A {\n\n"
|
|
|
|
"namespace {\n\n}}";
|
|
|
|
// Set the changed range to be the second "\n".
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({14}, Code));
|
2016-04-25 23:09:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CleanupTest, EmptyNamespaceWithCommentsNoBreakBeforeBrace) {
|
|
|
|
std::string Code = "namespace A {\n"
|
|
|
|
"namespace B {\n"
|
|
|
|
"// Yo\n"
|
|
|
|
"} // namespace B\n"
|
|
|
|
"} // namespace A\n"
|
|
|
|
"namespace C { // Yo\n"
|
|
|
|
"}";
|
|
|
|
std::string Expected = "\n\n\n\n\n\n";
|
|
|
|
std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
|
|
|
|
std::string Result = cleanup(Code, Ranges);
|
|
|
|
EXPECT_EQ(Expected, Result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CleanupTest, EmptyNamespaceWithCommentsBreakBeforeBrace) {
|
|
|
|
std::string Code = "namespace A\n"
|
|
|
|
"/* Yo */ {\n"
|
|
|
|
"namespace B\n"
|
|
|
|
"{\n"
|
|
|
|
"// Yo\n"
|
|
|
|
"} // namespace B\n"
|
|
|
|
"} // namespace A\n"
|
|
|
|
"namespace C\n"
|
|
|
|
"{ // Yo\n"
|
|
|
|
"}\n";
|
|
|
|
std::string Expected = "\n\n\n\n\n\n\n\n\n\n";
|
|
|
|
std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
|
|
|
|
FormatStyle Style = getLLVMStyle();
|
|
|
|
Style.BraceWrapping.AfterNamespace = true;
|
|
|
|
std::string Result = cleanup(Code, Ranges, Style);
|
|
|
|
EXPECT_EQ(Expected, Result);
|
|
|
|
}
|
|
|
|
|
2016-10-05 23:49:01 +08:00
|
|
|
TEST_F(CleanupTest, EmptyNamespaceAroundConditionalCompilation) {
|
|
|
|
std::string Code = "#ifdef A\n"
|
|
|
|
"int a;\n"
|
|
|
|
"int b;\n"
|
|
|
|
"#else\n"
|
|
|
|
"#endif\n"
|
|
|
|
"namespace {}";
|
|
|
|
std::string Expected = "#ifdef A\n"
|
|
|
|
"int a;\n"
|
|
|
|
"int b;\n"
|
|
|
|
"#else\n"
|
|
|
|
"#endif\n";
|
|
|
|
std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
|
|
|
|
FormatStyle Style = getLLVMStyle();
|
|
|
|
std::string Result = cleanup(Code, Ranges, Style);
|
|
|
|
EXPECT_EQ(Expected, Result);
|
|
|
|
}
|
|
|
|
|
2016-05-18 16:02:56 +08:00
|
|
|
TEST_F(CleanupTest, CtorInitializationSimpleRedundantComma) {
|
|
|
|
std::string Code = "class A {\nA() : , {} };";
|
|
|
|
std::string Expected = "class A {\nA() {} };";
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({17, 19}, Code));
|
2016-05-18 16:02:56 +08:00
|
|
|
|
|
|
|
Code = "class A {\nA() : x(1), {} };";
|
|
|
|
Expected = "class A {\nA() : x(1) {} };";
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({23}, Code));
|
2016-05-18 16:02:56 +08:00
|
|
|
|
|
|
|
Code = "class A {\nA() :,,,,{} };";
|
|
|
|
Expected = "class A {\nA() {} };";
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));
|
2016-05-18 16:02:56 +08:00
|
|
|
}
|
|
|
|
|
2016-10-20 22:58:45 +08:00
|
|
|
TEST_F(CleanupTest, CtorInitializationSimpleRedundantColon) {
|
|
|
|
std::string Code = "class A {\nA() : =default; };";
|
|
|
|
std::string Expected = "class A {\nA() =default; };";
|
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));
|
|
|
|
|
|
|
|
Code = "class A {\nA() : , =default; };";
|
|
|
|
Expected = "class A {\nA() =default; };";
|
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));
|
|
|
|
}
|
|
|
|
|
2016-09-13 23:02:43 +08:00
|
|
|
TEST_F(CleanupTest, ListRedundantComma) {
|
2016-05-18 16:02:56 +08:00
|
|
|
std::string Code = "void f() { std::vector<int> v = {1,2,,,3,{4,5}}; }";
|
|
|
|
std::string Expected = "void f() { std::vector<int> v = {1,2,3,{4,5}}; }";
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({40}, Code));
|
2016-05-18 16:02:56 +08:00
|
|
|
|
|
|
|
Code = "int main() { f(1,,2,3,,4);}";
|
|
|
|
Expected = "int main() { f(1,2,3,4);}";
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({17, 22}, Code));
|
|
|
|
}
|
|
|
|
|
2017-05-29 16:41:11 +08:00
|
|
|
TEST_F(CleanupTest, NoCleanupsForJavaScript) {
|
|
|
|
std::string Code = "function f() { var x = [a, b, , c]; }";
|
|
|
|
std::string Expected = "function f() { var x = [a, b, , c]; }";
|
|
|
|
const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript);
|
|
|
|
|
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code, Style));
|
|
|
|
}
|
|
|
|
|
2016-09-13 23:02:43 +08:00
|
|
|
TEST_F(CleanupTest, TrailingCommaInParens) {
|
|
|
|
std::string Code = "int main() { f(,1,,2,3,f(1,2,),4,,);}";
|
|
|
|
std::string Expected = "int main() { f(1,2,3,f(1,2),4);}";
|
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({15, 18, 29, 33}, Code));
|
2019-09-17 23:10:39 +08:00
|
|
|
|
|
|
|
// Lambda contents are also checked for trailing commas.
|
|
|
|
Code = "int main() { [](){f(,1,,2,3,f(1,2,),4,,);}();}";
|
|
|
|
Expected = "int main() { [](){f(1,2,3,f(1,2),4);}();}";
|
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({20, 23, 34, 38}, Code));
|
2016-09-13 23:02:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CleanupTest, TrailingCommaInBraces) {
|
2019-09-17 23:10:39 +08:00
|
|
|
// Trailing comma is allowed in brace list.
|
2016-09-13 23:02:43 +08:00
|
|
|
// If there was trailing comma in the original code, then trailing comma is
|
|
|
|
// preserved. In this example, element between the last two commas is deleted
|
|
|
|
// causing the second-last comma to be redundant.
|
|
|
|
std::string Code = "void f() { std::vector<int> v = {1,2,3,,}; }";
|
|
|
|
std::string Expected = "void f() { std::vector<int> v = {1,2,3,}; }";
|
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({39}, Code));
|
|
|
|
|
2019-09-17 23:10:39 +08:00
|
|
|
// If there was no trailing comma in the original code, then trailing comma
|
2016-09-13 23:02:43 +08:00
|
|
|
// introduced by replacements should be cleaned up. In this example, the
|
|
|
|
// element after the last comma is deleted causing the last comma to be
|
|
|
|
// redundant.
|
|
|
|
Code = "void f() { std::vector<int> v = {1,2,3,}; }";
|
|
|
|
// FIXME: redundant trailing comma should be removed.
|
|
|
|
Expected = "void f() { std::vector<int> v = {1,2,3,}; }";
|
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({39}, Code));
|
|
|
|
|
|
|
|
// Still no trailing comma in the original code, but two elements are deleted,
|
|
|
|
// which makes it seems like there was trailing comma.
|
|
|
|
Code = "void f() { std::vector<int> v = {1, 2, 3, , }; }";
|
|
|
|
// FIXME: redundant trailing comma should also be removed.
|
|
|
|
Expected = "void f() { std::vector<int> v = {1, 2, 3, }; }";
|
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({42, 44}, Code));
|
2016-05-18 16:02:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CleanupTest, CtorInitializationBracesInParens) {
|
|
|
|
std::string Code = "class A {\nA() : x({1}),, {} };";
|
|
|
|
std::string Expected = "class A {\nA() : x({1}) {} };";
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({24, 26}, Code));
|
2016-05-18 16:02:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CleanupTest, RedundantCommaNotInAffectedRanges) {
|
|
|
|
std::string Code =
|
|
|
|
"class A {\nA() : x({1}), /* comment */, { int x = 0; } };";
|
|
|
|
std::string Expected =
|
|
|
|
"class A {\nA() : x({1}), /* comment */, { int x = 0; } };";
|
|
|
|
// Set the affected range to be "int x = 0", which does not intercept the
|
|
|
|
// constructor initialization list.
|
|
|
|
std::vector<tooling::Range> Ranges(1, tooling::Range(42, 9));
|
|
|
|
std::string Result = cleanup(Code, Ranges);
|
|
|
|
EXPECT_EQ(Expected, Result);
|
|
|
|
|
|
|
|
Code = "class A {\nA() : x(1), {} };";
|
|
|
|
Expected = "class A {\nA() : x(1), {} };";
|
|
|
|
// No range. Fixer should do nothing.
|
|
|
|
Ranges.clear();
|
|
|
|
Result = cleanup(Code, Ranges);
|
|
|
|
EXPECT_EQ(Expected, Result);
|
|
|
|
}
|
|
|
|
|
2016-09-10 01:50:49 +08:00
|
|
|
TEST_F(CleanupTest, RemoveCommentsAroundDeleteCode) {
|
|
|
|
std::string Code =
|
|
|
|
"class A {\nA() : x({1}), /* comment */, /* comment */ {} };";
|
|
|
|
std::string Expected = "class A {\nA() : x({1}) {} };";
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({25, 40}, Code));
|
2016-05-18 16:02:56 +08:00
|
|
|
|
2016-09-10 01:50:49 +08:00
|
|
|
Code = "class A {\nA() : x({1}), // comment\n {} };";
|
|
|
|
Expected = "class A {\nA() : x({1})\n {} };";
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({25}, Code));
|
2016-05-18 16:02:56 +08:00
|
|
|
|
|
|
|
Code = "class A {\nA() : x({1}), // comment\n , y(1),{} };";
|
2016-09-10 01:50:49 +08:00
|
|
|
Expected = "class A {\nA() : x({1}), y(1){} };";
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({38}, Code));
|
2016-05-18 16:02:56 +08:00
|
|
|
|
|
|
|
Code = "class A {\nA() : x({1}), \n/* comment */, y(1),{} };";
|
2016-09-10 01:50:49 +08:00
|
|
|
Expected = "class A {\nA() : x({1}), \n y(1){} };";
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({40}, Code));
|
2016-05-18 16:02:56 +08:00
|
|
|
|
|
|
|
Code = "class A {\nA() : , // comment\n y(1),{} };";
|
|
|
|
Expected = "class A {\nA() : // comment\n y(1){} };";
|
2016-09-13 23:02:43 +08:00
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({17}, Code));
|
2016-10-20 22:58:45 +08:00
|
|
|
|
|
|
|
Code = "class A {\nA() // comment\n : ,,{} };";
|
|
|
|
Expected = "class A {\nA() // comment\n {} };";
|
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code));
|
|
|
|
|
|
|
|
Code = "class A {\nA() // comment\n : ,,=default; };";
|
|
|
|
Expected = "class A {\nA() // comment\n =default; };";
|
|
|
|
EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code));
|
2016-05-18 16:02:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CleanupTest, CtorInitializerInNamespace) {
|
|
|
|
std::string Code = "namespace A {\n"
|
|
|
|
"namespace B {\n" // missing r_brace
|
|
|
|
"} // namespace A\n\n"
|
|
|
|
"namespace C {\n"
|
|
|
|
"class A { A() : x(0),, {} };\n"
|
|
|
|
"inline namespace E { namespace { } }\n"
|
|
|
|
"}";
|
|
|
|
std::string Expected = "namespace A {\n"
|
|
|
|
"\n\n\nnamespace C {\n"
|
|
|
|
"class A { A() : x(0) {} };\n \n"
|
|
|
|
"}";
|
|
|
|
std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
|
|
|
|
std::string Result = cleanup(Code, Ranges);
|
|
|
|
EXPECT_EQ(Expected, Result);
|
|
|
|
}
|
|
|
|
|
2016-08-01 18:16:37 +08:00
|
|
|
class CleanUpReplacementsTest : public ReplacementTest {
|
2016-05-27 16:20:02 +08:00
|
|
|
protected:
|
2016-05-31 21:34:20 +08:00
|
|
|
tooling::Replacement createReplacement(unsigned Offset, unsigned Length,
|
|
|
|
StringRef Text) {
|
|
|
|
return tooling::Replacement(FileName, Offset, Length, Text);
|
2016-05-27 16:20:02 +08:00
|
|
|
}
|
|
|
|
|
2016-09-23 23:10:56 +08:00
|
|
|
tooling::Replacement createInsertion(StringRef IncludeDirective) {
|
|
|
|
return createReplacement(UINT_MAX, 0, IncludeDirective);
|
|
|
|
}
|
|
|
|
|
|
|
|
tooling::Replacement createDeletion(StringRef HeaderName) {
|
|
|
|
return createReplacement(UINT_MAX, 1, HeaderName);
|
2016-05-31 21:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string apply(StringRef Code,
|
2017-04-21 22:21:21 +08:00
|
|
|
const tooling::Replacements &Replaces) {
|
2016-07-11 21:53:12 +08:00
|
|
|
auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style);
|
|
|
|
EXPECT_TRUE(static_cast<bool>(CleanReplaces))
|
|
|
|
<< llvm::toString(CleanReplaces.takeError()) << "\n";
|
|
|
|
auto Result = applyAllReplacements(Code, *CleanReplaces);
|
|
|
|
EXPECT_TRUE(static_cast<bool>(Result));
|
|
|
|
return *Result;
|
2016-05-31 21:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string formatAndApply(StringRef Code,
|
2017-04-21 22:21:21 +08:00
|
|
|
const tooling::Replacements &Replaces) {
|
2016-07-11 21:53:12 +08:00
|
|
|
auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style);
|
|
|
|
EXPECT_TRUE(static_cast<bool>(CleanReplaces))
|
|
|
|
<< llvm::toString(CleanReplaces.takeError()) << "\n";
|
|
|
|
auto FormattedReplaces = formatReplacements(Code, *CleanReplaces, Style);
|
|
|
|
EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
|
|
|
|
<< llvm::toString(FormattedReplaces.takeError()) << "\n";
|
|
|
|
auto Result = applyAllReplacements(Code, *FormattedReplaces);
|
|
|
|
EXPECT_TRUE(static_cast<bool>(Result));
|
|
|
|
return *Result;
|
2016-05-31 21:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int getOffset(StringRef Code, int Line, int Column) {
|
|
|
|
RewriterTestContext Context;
|
|
|
|
FileID ID = Context.createInMemoryFile(FileName, Code);
|
|
|
|
auto DecomposedLocation =
|
|
|
|
Context.Sources.getDecomposedLoc(Context.getLocation(ID, Line, Column));
|
|
|
|
return DecomposedLocation.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string FileName = "fix.cpp";
|
|
|
|
FormatStyle Style = getLLVMStyle();
|
2016-05-27 16:20:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(CleanUpReplacementsTest, FixOnlyAffectedCodeAfterReplacements) {
|
|
|
|
std::string Code = "namespace A {\n"
|
|
|
|
"namespace B {\n"
|
|
|
|
" int x;\n"
|
|
|
|
"} // namespace B\n"
|
|
|
|
"} // namespace A\n"
|
|
|
|
"\n"
|
|
|
|
"namespace C {\n"
|
|
|
|
"namespace D { int i; }\n"
|
|
|
|
"inline namespace E { namespace { int y; } }\n"
|
|
|
|
"int x= 0;"
|
|
|
|
"}";
|
|
|
|
std::string Expected = "\n\nnamespace C {\n"
|
|
|
|
"namespace D { int i; }\n\n"
|
|
|
|
"int x= 0;"
|
|
|
|
"}";
|
2016-08-01 18:16:37 +08:00
|
|
|
tooling::Replacements Replaces =
|
|
|
|
toReplacements({createReplacement(getOffset(Code, 3, 3), 6, ""),
|
|
|
|
createReplacement(getOffset(Code, 9, 34), 6, "")});
|
2016-05-31 21:34:20 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesLLVMStyle) {
|
|
|
|
std::string Code = "#include \"x/fix.h\"\n"
|
|
|
|
"#include \"a.h\"\n"
|
|
|
|
"#include \"b.h\"\n"
|
2018-05-05 01:55:13 +08:00
|
|
|
"#include \"z.h\"\n"
|
2016-05-31 21:34:20 +08:00
|
|
|
"#include \"clang/Format/Format.h\"\n"
|
|
|
|
"#include <memory>\n";
|
|
|
|
std::string Expected = "#include \"x/fix.h\"\n"
|
|
|
|
"#include \"a.h\"\n"
|
|
|
|
"#include \"b.h\"\n"
|
|
|
|
"#include \"new/new.h\"\n"
|
2018-05-05 01:55:13 +08:00
|
|
|
"#include \"z.h\"\n"
|
2016-05-31 21:34:20 +08:00
|
|
|
"#include \"clang/Format/Format.h\"\n"
|
2018-05-05 01:55:13 +08:00
|
|
|
"#include <list>\n"
|
|
|
|
"#include <memory>\n";
|
2016-08-01 18:16:37 +08:00
|
|
|
tooling::Replacements Replaces =
|
|
|
|
toReplacements({createInsertion("#include <list>"),
|
|
|
|
createInsertion("#include \"new/new.h\"")});
|
2016-05-31 21:34:20 +08:00
|
|
|
EXPECT_EQ(Expected, apply(Code, Replaces));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesGoogleStyle) {
|
|
|
|
std::string Code = "#include \"x/fix.h\"\n"
|
|
|
|
"\n"
|
|
|
|
"#include <vector>\n"
|
|
|
|
"\n"
|
|
|
|
"#include \"y/a.h\"\n"
|
|
|
|
"#include \"z/b.h\"\n";
|
|
|
|
std::string Expected = "#include \"x/fix.h\"\n"
|
|
|
|
"\n"
|
|
|
|
"#include <list>\n"
|
2018-05-05 01:55:13 +08:00
|
|
|
"#include <vector>\n"
|
2016-05-31 21:34:20 +08:00
|
|
|
"\n"
|
2018-05-05 01:55:13 +08:00
|
|
|
"#include \"x/x.h\"\n"
|
2016-05-31 21:34:20 +08:00
|
|
|
"#include \"y/a.h\"\n"
|
2018-05-05 01:55:13 +08:00
|
|
|
"#include \"z/b.h\"\n";
|
2016-08-01 18:16:37 +08:00
|
|
|
tooling::Replacements Replaces =
|
|
|
|
toReplacements({createInsertion("#include <list>"),
|
|
|
|
createInsertion("#include \"x/x.h\"")});
|
2016-05-31 21:34:20 +08:00
|
|
|
Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
|
|
|
|
EXPECT_EQ(Expected, apply(Code, Replaces));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortLLVM) {
|
|
|
|
std::string Code = "\nint x;";
|
2016-06-03 20:52:59 +08:00
|
|
|
std::string Expected = "\n#include \"fix.h\"\n"
|
2016-05-31 21:34:20 +08:00
|
|
|
"#include \"a.h\"\n"
|
|
|
|
"#include \"b.h\"\n"
|
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"#include <list>\n"
|
|
|
|
"#include <vector>\n"
|
2016-06-03 20:52:59 +08:00
|
|
|
"int x;";
|
2016-08-01 18:16:37 +08:00
|
|
|
tooling::Replacements Replaces = toReplacements(
|
|
|
|
{createInsertion("#include \"a.h\""), createInsertion("#include \"c.h\""),
|
|
|
|
createInsertion("#include \"b.h\""),
|
|
|
|
createInsertion("#include <vector>"), createInsertion("#include <list>"),
|
|
|
|
createInsertion("#include \"fix.h\"")});
|
2016-05-31 21:34:20 +08:00
|
|
|
EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortGoogle) {
|
|
|
|
std::string Code = "\nint x;";
|
2016-06-03 20:52:59 +08:00
|
|
|
std::string Expected = "\n#include \"fix.h\"\n"
|
2019-04-03 17:25:16 +08:00
|
|
|
"\n"
|
2016-05-31 21:34:20 +08:00
|
|
|
"#include <list>\n"
|
|
|
|
"#include <vector>\n"
|
2019-04-03 17:25:16 +08:00
|
|
|
"\n"
|
2016-05-31 21:34:20 +08:00
|
|
|
"#include \"a.h\"\n"
|
|
|
|
"#include \"b.h\"\n"
|
|
|
|
"#include \"c.h\"\n"
|
2016-06-03 20:52:59 +08:00
|
|
|
"int x;";
|
2016-08-01 18:16:37 +08:00
|
|
|
tooling::Replacements Replaces = toReplacements(
|
|
|
|
{createInsertion("#include \"a.h\""), createInsertion("#include \"c.h\""),
|
|
|
|
createInsertion("#include \"b.h\""),
|
|
|
|
createInsertion("#include <vector>"), createInsertion("#include <list>"),
|
|
|
|
createInsertion("#include \"fix.h\"")});
|
2016-05-31 21:34:20 +08:00
|
|
|
Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
|
|
|
|
EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
|
|
|
|
}
|
|
|
|
|
2018-05-15 04:17:53 +08:00
|
|
|
TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCodeMultipleInsertions) {
|
|
|
|
std::string Code = "#include <map>";
|
|
|
|
// FIXME: a better behavior is to only append on newline to Code, but this
|
|
|
|
// case should be rare in practice.
|
|
|
|
std::string Expected =
|
|
|
|
"#include <map>\n#include <string>\n\n#include <vector>\n";
|
|
|
|
tooling::Replacements Replaces =
|
|
|
|
toReplacements({createInsertion("#include <string>"),
|
|
|
|
createInsertion("#include <vector>")});
|
|
|
|
EXPECT_EQ(Expected, apply(Code, Replaces));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-31 21:34:20 +08:00
|
|
|
TEST_F(CleanUpReplacementsTest, FormatCorrectLineWhenHeadersAreInserted) {
|
|
|
|
std::string Code = "\n"
|
2016-06-03 20:52:59 +08:00
|
|
|
"int x;\n"
|
2016-05-31 21:34:20 +08:00
|
|
|
"int a;\n"
|
|
|
|
"int a;\n"
|
|
|
|
"int a;";
|
|
|
|
|
2016-06-03 20:52:59 +08:00
|
|
|
std::string Expected = "\n#include \"x.h\"\n"
|
2016-05-31 21:34:20 +08:00
|
|
|
"#include \"y.h\"\n"
|
|
|
|
"#include \"clang/x/x.h\"\n"
|
|
|
|
"#include <list>\n"
|
|
|
|
"#include <vector>\n"
|
2016-06-03 20:52:59 +08:00
|
|
|
"int x;\n"
|
2016-05-31 21:34:20 +08:00
|
|
|
"int a;\n"
|
|
|
|
"int b;\n"
|
|
|
|
"int a;";
|
2016-08-01 18:16:37 +08:00
|
|
|
tooling::Replacements Replaces = toReplacements(
|
|
|
|
{createReplacement(getOffset(Code, 4, 8), 1, "b"),
|
|
|
|
createInsertion("#include <vector>"), createInsertion("#include <list>"),
|
|
|
|
createInsertion("#include \"clang/x/x.h\""),
|
|
|
|
createInsertion("#include \"y.h\""),
|
|
|
|
createInsertion("#include \"x.h\"")});
|
2016-05-31 21:34:20 +08:00
|
|
|
EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
|
|
|
|
}
|
|
|
|
|
2016-09-23 23:10:56 +08:00
|
|
|
TEST_F(CleanUpReplacementsTest, SimpleDeleteIncludes) {
|
|
|
|
std::string Code = "#include \"abc.h\"\n"
|
|
|
|
"#include \"xyz.h\" // comment\n"
|
|
|
|
"#include \"xyz\"\n"
|
|
|
|
"int x;\n";
|
|
|
|
std::string Expected = "#include \"xyz\"\n"
|
|
|
|
"int x;\n";
|
|
|
|
tooling::Replacements Replaces =
|
|
|
|
toReplacements({createDeletion("abc.h"), createDeletion("xyz.h")});
|
|
|
|
EXPECT_EQ(Expected, apply(Code, Replaces));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CleanUpReplacementsTest, InsertionAndDeleteHeader) {
|
|
|
|
std::string Code = "#include \"a.h\"\n"
|
|
|
|
"\n"
|
|
|
|
"#include <vector>\n";
|
|
|
|
std::string Expected = "#include \"a.h\"\n"
|
|
|
|
"\n"
|
|
|
|
"#include <map>\n";
|
|
|
|
tooling::Replacements Replaces = toReplacements(
|
|
|
|
{createDeletion("<vector>"), createInsertion("#include <map>")});
|
|
|
|
EXPECT_EQ(Expected, apply(Code, Replaces));
|
|
|
|
}
|
|
|
|
|
2016-04-25 23:09:22 +08:00
|
|
|
} // end namespace
|
|
|
|
} // end namespace format
|
|
|
|
} // end namespace clang
|