2015-09-23 16:30:47 +08:00
|
|
|
//===- unittest/Format/SortIncludesTest.cpp - Include sort unit tests -----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "FormatTestUtils.h"
|
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "format-test"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SortIncludesTest : public ::testing::Test {
|
|
|
|
protected:
|
2015-09-29 15:53:08 +08:00
|
|
|
std::string sort(llvm::StringRef Code, StringRef FileName = "input.cpp") {
|
2015-09-23 16:30:47 +08:00
|
|
|
std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
|
2015-09-29 15:53:08 +08:00
|
|
|
std::string Sorted =
|
|
|
|
applyAllReplacements(Code, sortIncludes(Style, Code, Ranges, FileName));
|
|
|
|
return applyAllReplacements(Sorted,
|
|
|
|
reformat(Style, Sorted, Ranges, FileName));
|
2015-09-23 16:30:47 +08:00
|
|
|
}
|
2015-09-29 15:53:08 +08:00
|
|
|
|
2015-11-23 16:36:35 +08:00
|
|
|
unsigned newCursor(llvm::StringRef Code, unsigned Cursor) {
|
|
|
|
std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
|
|
|
|
sortIncludes(Style, Code, Ranges, "input.cpp", &Cursor);
|
|
|
|
return Cursor;
|
|
|
|
}
|
|
|
|
|
2015-09-29 15:53:08 +08:00
|
|
|
FormatStyle Style = getLLVMStyle();
|
2015-11-23 16:36:35 +08:00
|
|
|
|
2015-09-23 16:30:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(SortIncludesTest, BasicSorting) {
|
|
|
|
EXPECT_EQ("#include \"a.h\"\n"
|
|
|
|
"#include \"b.h\"\n"
|
|
|
|
"#include \"c.h\"\n",
|
|
|
|
sort("#include \"a.h\"\n"
|
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"#include \"b.h\"\n"));
|
|
|
|
}
|
|
|
|
|
2015-11-21 17:17:08 +08:00
|
|
|
TEST_F(SortIncludesTest, SupportClangFormatOff) {
|
|
|
|
EXPECT_EQ("#include <a>\n"
|
|
|
|
"#include <b>\n"
|
|
|
|
"#include <c>\n"
|
|
|
|
"// clang-format off\n"
|
|
|
|
"#include <b>\n"
|
|
|
|
"#include <a>\n"
|
|
|
|
"#include <c>\n"
|
|
|
|
"// clang-format on\n",
|
|
|
|
sort("#include <b>\n"
|
|
|
|
"#include <a>\n"
|
|
|
|
"#include <c>\n"
|
|
|
|
"// clang-format off\n"
|
|
|
|
"#include <b>\n"
|
|
|
|
"#include <a>\n"
|
|
|
|
"#include <c>\n"
|
|
|
|
"// clang-format on\n"));
|
|
|
|
}
|
|
|
|
|
2015-11-16 20:38:56 +08:00
|
|
|
TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
|
|
|
|
Style.SortIncludes = false;
|
|
|
|
EXPECT_EQ("#include \"a.h\"\n"
|
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"#include \"b.h\"\n",
|
|
|
|
sort("#include \"a.h\"\n"
|
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"#include \"b.h\"\n"));
|
|
|
|
}
|
|
|
|
|
2015-10-22 01:13:45 +08:00
|
|
|
TEST_F(SortIncludesTest, MixIncludeAndImport) {
|
|
|
|
EXPECT_EQ("#include \"a.h\"\n"
|
|
|
|
"#import \"b.h\"\n"
|
|
|
|
"#include \"c.h\"\n",
|
|
|
|
sort("#include \"a.h\"\n"
|
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"#import \"b.h\"\n"));
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:30:47 +08:00
|
|
|
TEST_F(SortIncludesTest, FixTrailingComments) {
|
|
|
|
EXPECT_EQ("#include \"a.h\" // comment\n"
|
|
|
|
"#include \"bb.h\" // comment\n"
|
|
|
|
"#include \"ccc.h\"\n",
|
|
|
|
sort("#include \"a.h\" // comment\n"
|
|
|
|
"#include \"ccc.h\"\n"
|
|
|
|
"#include \"bb.h\" // comment\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SortIncludesTest, LeadingWhitespace) {
|
|
|
|
EXPECT_EQ("#include \"a.h\"\n"
|
|
|
|
"#include \"b.h\"\n"
|
|
|
|
"#include \"c.h\"\n",
|
|
|
|
sort(" #include \"a.h\"\n"
|
|
|
|
" #include \"c.h\"\n"
|
|
|
|
" #include \"b.h\"\n"));
|
|
|
|
EXPECT_EQ("#include \"a.h\"\n"
|
|
|
|
"#include \"b.h\"\n"
|
|
|
|
"#include \"c.h\"\n",
|
|
|
|
sort("# include \"a.h\"\n"
|
|
|
|
"# include \"c.h\"\n"
|
|
|
|
"# include \"b.h\"\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SortIncludesTest, GreaterInComment) {
|
|
|
|
EXPECT_EQ("#include \"a.h\"\n"
|
|
|
|
"#include \"b.h\" // >\n"
|
|
|
|
"#include \"c.h\"\n",
|
|
|
|
sort("#include \"a.h\"\n"
|
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"#include \"b.h\" // >\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SortIncludesTest, SortsLocallyInEachBlock) {
|
|
|
|
EXPECT_EQ("#include \"a.h\"\n"
|
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"\n"
|
|
|
|
"#include \"b.h\"\n",
|
2015-09-29 15:53:08 +08:00
|
|
|
sort("#include \"a.h\"\n"
|
|
|
|
"#include \"c.h\"\n"
|
2015-09-23 16:30:47 +08:00
|
|
|
"\n"
|
|
|
|
"#include \"b.h\"\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
|
2015-09-29 15:53:08 +08:00
|
|
|
EXPECT_EQ("#include \"a.h\"\n"
|
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"#include <b.h>\n"
|
|
|
|
"#include <d.h>\n",
|
|
|
|
sort("#include <d.h>\n"
|
|
|
|
"#include <b.h>\n"
|
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"#include \"a.h\"\n"));
|
|
|
|
|
|
|
|
Style = getGoogleStyle(FormatStyle::LK_Cpp);
|
2015-09-23 16:30:47 +08:00
|
|
|
EXPECT_EQ("#include <b.h>\n"
|
|
|
|
"#include <d.h>\n"
|
|
|
|
"#include \"a.h\"\n"
|
|
|
|
"#include \"c.h\"\n",
|
|
|
|
sort("#include <d.h>\n"
|
|
|
|
"#include <b.h>\n"
|
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"#include \"a.h\"\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SortIncludesTest, HandlesMultilineIncludes) {
|
|
|
|
EXPECT_EQ("#include \"a.h\"\n"
|
|
|
|
"#include \"b.h\"\n"
|
|
|
|
"#include \"c.h\"\n",
|
|
|
|
sort("#include \"a.h\"\n"
|
|
|
|
"#include \\\n"
|
|
|
|
"\"c.h\"\n"
|
|
|
|
"#include \"b.h\"\n"));
|
|
|
|
}
|
|
|
|
|
2015-09-29 15:53:08 +08:00
|
|
|
TEST_F(SortIncludesTest, LeavesMainHeaderFirst) {
|
|
|
|
EXPECT_EQ("#include \"llvm/a.h\"\n"
|
|
|
|
"#include \"b.h\"\n"
|
|
|
|
"#include \"c.h\"\n",
|
|
|
|
sort("#include \"llvm/a.h\"\n"
|
|
|
|
"#include \"c.h\"\n"
|
2015-12-21 20:14:17 +08:00
|
|
|
"#include \"b.h\"\n",
|
|
|
|
"a.cc"));
|
2015-10-19 09:36:09 +08:00
|
|
|
EXPECT_EQ("#include \"llvm/a.h\"\n"
|
|
|
|
"#include \"b.h\"\n"
|
|
|
|
"#include \"c.h\"\n",
|
|
|
|
sort("#include \"llvm/a.h\"\n"
|
2015-12-21 20:14:17 +08:00
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"#include \"b.h\"\n",
|
|
|
|
"a_main.cc"));
|
|
|
|
EXPECT_EQ("#include \"llvm/input.h\"\n"
|
|
|
|
"#include \"b.h\"\n"
|
|
|
|
"#include \"c.h\"\n",
|
|
|
|
sort("#include \"llvm/input.h\"\n"
|
2015-10-19 09:36:09 +08:00
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"#include \"b.h\"\n",
|
|
|
|
"input.mm"));
|
2015-09-29 15:53:08 +08:00
|
|
|
|
|
|
|
// Don't do this in headers.
|
|
|
|
EXPECT_EQ("#include \"b.h\"\n"
|
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"#include \"llvm/a.h\"\n",
|
|
|
|
sort("#include \"llvm/a.h\"\n"
|
|
|
|
"#include \"c.h\"\n"
|
|
|
|
"#include \"b.h\"\n",
|
2015-12-21 20:14:17 +08:00
|
|
|
"a.h"));
|
2015-09-29 15:53:08 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 18:10:16 +08:00
|
|
|
TEST_F(SortIncludesTest, NegativePriorities) {
|
|
|
|
Style.IncludeCategories = {{".*important_os_header.*", -1}, {".*", 1}};
|
|
|
|
EXPECT_EQ("#include \"important_os_header.h\"\n"
|
2015-12-21 20:14:17 +08:00
|
|
|
"#include \"c_main.h\"\n"
|
|
|
|
"#include \"a_other.h\"\n",
|
|
|
|
sort("#include \"c_main.h\"\n"
|
|
|
|
"#include \"a_other.h\"\n"
|
|
|
|
"#include \"important_os_header.h\"\n",
|
|
|
|
"c_main.cc"));
|
|
|
|
|
|
|
|
// check stable when re-run
|
|
|
|
EXPECT_EQ("#include \"important_os_header.h\"\n"
|
|
|
|
"#include \"c_main.h\"\n"
|
|
|
|
"#include \"a_other.h\"\n",
|
|
|
|
sort("#include \"important_os_header.h\"\n"
|
|
|
|
"#include \"c_main.h\"\n"
|
|
|
|
"#include \"a_other.h\"\n",
|
|
|
|
"c_main.cc"));
|
2015-12-16 18:10:16 +08:00
|
|
|
}
|
|
|
|
|
2015-11-23 16:36:35 +08:00
|
|
|
TEST_F(SortIncludesTest, CalculatesCorrectCursorPosition) {
|
|
|
|
std::string Code = "#include <ccc>\n" // Start of line: 0
|
|
|
|
"#include <bbbbbb>\n" // Start of line: 15
|
|
|
|
"#include <a>\n"; // Start of line: 33
|
|
|
|
EXPECT_EQ(31u, newCursor(Code, 0));
|
|
|
|
EXPECT_EQ(13u, newCursor(Code, 15));
|
|
|
|
EXPECT_EQ(0u, newCursor(Code, 33));
|
|
|
|
|
|
|
|
EXPECT_EQ(41u, newCursor(Code, 10));
|
|
|
|
EXPECT_EQ(23u, newCursor(Code, 25));
|
|
|
|
EXPECT_EQ(10u, newCursor(Code, 43));
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:30:47 +08:00
|
|
|
} // end namespace
|
|
|
|
} // end namespace format
|
|
|
|
} // end namespace clang
|