123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- //===- 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:
- std::vector<tooling::Range> GetCodeRange(StringRef Code) {
- return std::vector<tooling::Range>(1, tooling::Range(0, Code.size()));
- }
- std::string sort(StringRef Code, StringRef FileName = "input.cpp") {
- auto Ranges = GetCodeRange(Code);
- auto Sorted =
- applyAllReplacements(Code, sortIncludes(Style, Code, Ranges, FileName));
- EXPECT_TRUE(static_cast<bool>(Sorted));
- auto Result = applyAllReplacements(
- *Sorted, reformat(Style, *Sorted, Ranges, FileName));
- EXPECT_TRUE(static_cast<bool>(Result));
- return *Result;
- }
- unsigned newCursor(llvm::StringRef Code, unsigned Cursor) {
- sortIncludes(Style, Code, GetCodeRange(Code), "input.cpp", &Cursor);
- return Cursor;
- }
- FormatStyle Style = getLLVMStyle();
- };
- 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"));
- }
- TEST_F(SortIncludesTest, NoReplacementsForValidIncludes) {
- // Identical #includes have led to a failure with an unstable sort.
- std::string Code = "#include <a>\n"
- "#include <b>\n"
- "#include <b>\n"
- "#include <b>\n"
- "#include <b>\n"
- "#include <c>\n";
- EXPECT_TRUE(sortIncludes(Style, Code, GetCodeRange(Code), "a.cc").empty());
- }
- 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"));
- }
- 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"));
- }
- 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"));
- }
- 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",
- sort("#include \"a.h\"\n"
- "#include \"c.h\"\n"
- "\n"
- "#include \"b.h\"\n"));
- }
- TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
- 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);
- 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"));
- }
- TEST_F(SortIncludesTest, LeavesMainHeaderFirst) {
- Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
- 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"
- "#include \"b.h\"\n",
- "a.cc"));
- 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"
- "#include \"b.h\"\n",
- "a_test.cc"));
- EXPECT_EQ("#include \"llvm/input.h\"\n"
- "#include \"b.h\"\n"
- "#include \"c.h\"\n",
- sort("#include \"llvm/input.h\"\n"
- "#include \"c.h\"\n"
- "#include \"b.h\"\n",
- "input.mm"));
- // Don't allow prefixes.
- EXPECT_EQ("#include \"b.h\"\n"
- "#include \"c.h\"\n"
- "#include \"llvm/not_a.h\"\n",
- sort("#include \"llvm/not_a.h\"\n"
- "#include \"c.h\"\n"
- "#include \"b.h\"\n",
- "a.cc"));
- // Don't do this for _main and other suffixes.
- 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",
- "a_main.cc"));
- // 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",
- "a.h"));
- // Only do this in the first #include block.
- EXPECT_EQ("#include <a>\n"
- "\n"
- "#include \"b.h\"\n"
- "#include \"c.h\"\n"
- "#include \"llvm/a.h\"\n",
- sort("#include <a>\n"
- "\n"
- "#include \"llvm/a.h\"\n"
- "#include \"c.h\"\n"
- "#include \"b.h\"\n",
- "a.cc"));
- // Only recognize the first #include with a matching basename as main include.
- EXPECT_EQ("#include \"a.h\"\n"
- "#include \"b.h\"\n"
- "#include \"c.h\"\n"
- "#include \"llvm/a.h\"\n",
- sort("#include \"b.h\"\n"
- "#include \"a.h\"\n"
- "#include \"c.h\"\n"
- "#include \"llvm/a.h\"\n",
- "a.cc"));
- }
- TEST_F(SortIncludesTest, NegativePriorities) {
- Style.IncludeCategories = {{".*important_os_header.*", -1}, {".*", 1}};
- EXPECT_EQ("#include \"important_os_header.h\"\n"
- "#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"));
- }
- 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));
- }
- } // end namespace
- } // end namespace format
- } // end namespace clang
|