SortIncludesTest.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //===- unittest/Format/SortIncludesTest.cpp - Include sort unit tests -----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "FormatTestUtils.h"
  10. #include "clang/Format/Format.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "gtest/gtest.h"
  13. #define DEBUG_TYPE "format-test"
  14. namespace clang {
  15. namespace format {
  16. namespace {
  17. class SortIncludesTest : public ::testing::Test {
  18. protected:
  19. std::string sort(llvm::StringRef Code, StringRef FileName = "input.cpp") {
  20. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  21. std::string Sorted =
  22. applyAllReplacements(Code, sortIncludes(Style, Code, Ranges, FileName));
  23. return applyAllReplacements(Sorted,
  24. reformat(Style, Sorted, Ranges, FileName));
  25. }
  26. FormatStyle Style = getLLVMStyle();
  27. };
  28. TEST_F(SortIncludesTest, BasicSorting) {
  29. EXPECT_EQ("#include \"a.h\"\n"
  30. "#include \"b.h\"\n"
  31. "#include \"c.h\"\n",
  32. sort("#include \"a.h\"\n"
  33. "#include \"c.h\"\n"
  34. "#include \"b.h\"\n"));
  35. }
  36. TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
  37. Style.SortIncludes = false;
  38. EXPECT_EQ("#include \"a.h\"\n"
  39. "#include \"c.h\"\n"
  40. "#include \"b.h\"\n",
  41. sort("#include \"a.h\"\n"
  42. "#include \"c.h\"\n"
  43. "#include \"b.h\"\n"));
  44. }
  45. TEST_F(SortIncludesTest, MixIncludeAndImport) {
  46. EXPECT_EQ("#include \"a.h\"\n"
  47. "#import \"b.h\"\n"
  48. "#include \"c.h\"\n",
  49. sort("#include \"a.h\"\n"
  50. "#include \"c.h\"\n"
  51. "#import \"b.h\"\n"));
  52. }
  53. TEST_F(SortIncludesTest, FixTrailingComments) {
  54. EXPECT_EQ("#include \"a.h\" // comment\n"
  55. "#include \"bb.h\" // comment\n"
  56. "#include \"ccc.h\"\n",
  57. sort("#include \"a.h\" // comment\n"
  58. "#include \"ccc.h\"\n"
  59. "#include \"bb.h\" // comment\n"));
  60. }
  61. TEST_F(SortIncludesTest, LeadingWhitespace) {
  62. EXPECT_EQ("#include \"a.h\"\n"
  63. "#include \"b.h\"\n"
  64. "#include \"c.h\"\n",
  65. sort(" #include \"a.h\"\n"
  66. " #include \"c.h\"\n"
  67. " #include \"b.h\"\n"));
  68. EXPECT_EQ("#include \"a.h\"\n"
  69. "#include \"b.h\"\n"
  70. "#include \"c.h\"\n",
  71. sort("# include \"a.h\"\n"
  72. "# include \"c.h\"\n"
  73. "# include \"b.h\"\n"));
  74. }
  75. TEST_F(SortIncludesTest, GreaterInComment) {
  76. EXPECT_EQ("#include \"a.h\"\n"
  77. "#include \"b.h\" // >\n"
  78. "#include \"c.h\"\n",
  79. sort("#include \"a.h\"\n"
  80. "#include \"c.h\"\n"
  81. "#include \"b.h\" // >\n"));
  82. }
  83. TEST_F(SortIncludesTest, SortsLocallyInEachBlock) {
  84. EXPECT_EQ("#include \"a.h\"\n"
  85. "#include \"c.h\"\n"
  86. "\n"
  87. "#include \"b.h\"\n",
  88. sort("#include \"a.h\"\n"
  89. "#include \"c.h\"\n"
  90. "\n"
  91. "#include \"b.h\"\n"));
  92. }
  93. TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
  94. EXPECT_EQ("#include \"a.h\"\n"
  95. "#include \"c.h\"\n"
  96. "#include <b.h>\n"
  97. "#include <d.h>\n",
  98. sort("#include <d.h>\n"
  99. "#include <b.h>\n"
  100. "#include \"c.h\"\n"
  101. "#include \"a.h\"\n"));
  102. Style = getGoogleStyle(FormatStyle::LK_Cpp);
  103. EXPECT_EQ("#include <b.h>\n"
  104. "#include <d.h>\n"
  105. "#include \"a.h\"\n"
  106. "#include \"c.h\"\n",
  107. sort("#include <d.h>\n"
  108. "#include <b.h>\n"
  109. "#include \"c.h\"\n"
  110. "#include \"a.h\"\n"));
  111. }
  112. TEST_F(SortIncludesTest, HandlesMultilineIncludes) {
  113. EXPECT_EQ("#include \"a.h\"\n"
  114. "#include \"b.h\"\n"
  115. "#include \"c.h\"\n",
  116. sort("#include \"a.h\"\n"
  117. "#include \\\n"
  118. "\"c.h\"\n"
  119. "#include \"b.h\"\n"));
  120. }
  121. TEST_F(SortIncludesTest, LeavesMainHeaderFirst) {
  122. EXPECT_EQ("#include \"llvm/a.h\"\n"
  123. "#include \"b.h\"\n"
  124. "#include \"c.h\"\n",
  125. sort("#include \"llvm/a.h\"\n"
  126. "#include \"c.h\"\n"
  127. "#include \"b.h\"\n"));
  128. EXPECT_EQ("#include \"llvm/a.h\"\n"
  129. "#include \"b.h\"\n"
  130. "#include \"c.h\"\n",
  131. sort("#include \"llvm/a.h\"\n"
  132. "#include \"c.h\"\n"
  133. "#include \"b.h\"\n",
  134. "input.mm"));
  135. // Don't do this in headers.
  136. EXPECT_EQ("#include \"b.h\"\n"
  137. "#include \"c.h\"\n"
  138. "#include \"llvm/a.h\"\n",
  139. sort("#include \"llvm/a.h\"\n"
  140. "#include \"c.h\"\n"
  141. "#include \"b.h\"\n",
  142. "some_header.h"));
  143. }
  144. } // end namespace
  145. } // end namespace format
  146. } // end namespace clang