|
6 | 6 | //
|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 | 8 |
|
9 |
| -#include "FormatTestUtils.h" |
| 9 | +#include "FormatTestBase.h" |
10 | 10 | #include "clang/Format/Format.h"
|
11 | 11 | #include "llvm/ADT/StringRef.h"
|
12 | 12 | #include "llvm/Support/Debug.h"
|
13 | 13 | #include "gtest/gtest.h"
|
14 | 14 |
|
15 |
| -#define DEBUG_TYPE "format-test" |
| 15 | +#define DEBUG_TYPE "sort-includes-test" |
16 | 16 |
|
17 | 17 | namespace clang {
|
18 | 18 | namespace format {
|
19 | 19 | namespace {
|
20 | 20 |
|
21 |
| -class SortIncludesTest : public ::testing::Test { |
| 21 | +class SortIncludesTest : public test::FormatTestBase { |
22 | 22 | protected:
|
23 | 23 | std::vector<tooling::Range> GetCodeRange(StringRef Code) {
|
24 | 24 | return std::vector<tooling::Range>(1, tooling::Range(0, Code.size()));
|
@@ -821,6 +821,122 @@ TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionWithRegrouping) {
|
821 | 821 | EXPECT_EQ(27u, newCursor(Code, 28)); // Start of last line
|
822 | 822 | }
|
823 | 823 |
|
| 824 | +TEST_F(SortIncludesTest, |
| 825 | + CalculatesCorrectCursorPositionWhenNoReplacementsWithRegroupingAndCRLF) { |
| 826 | + Style.IncludeBlocks = Style.IBS_Regroup; |
| 827 | + FmtStyle.LineEnding = FormatStyle::LE_CRLF; |
| 828 | + Style.IncludeCategories = { |
| 829 | + {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}}; |
| 830 | + std::string Code = "#include \"a\"\r\n" // Start of line: 0 |
| 831 | + "\r\n" // Start of line: 14 |
| 832 | + "#include \"b\"\r\n" // Start of line: 16 |
| 833 | + "\r\n" // Start of line: 30 |
| 834 | + "#include \"c\"\r\n" // Start of line: 32 |
| 835 | + "\r\n" // Start of line: 46 |
| 836 | + "int i;"; // Start of line: 48 |
| 837 | + verifyNoChange(Code); |
| 838 | + EXPECT_EQ(0u, newCursor(Code, 0)); |
| 839 | + EXPECT_EQ(14u, newCursor(Code, 14)); |
| 840 | + EXPECT_EQ(16u, newCursor(Code, 16)); |
| 841 | + EXPECT_EQ(30u, newCursor(Code, 30)); |
| 842 | + EXPECT_EQ(32u, newCursor(Code, 32)); |
| 843 | + EXPECT_EQ(46u, newCursor(Code, 46)); |
| 844 | + EXPECT_EQ(48u, newCursor(Code, 48)); |
| 845 | +} |
| 846 | + |
| 847 | +TEST_F( |
| 848 | + SortIncludesTest, |
| 849 | + CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF) { |
| 850 | + Style.IncludeBlocks = Style.IBS_Regroup; |
| 851 | + FmtStyle.LineEnding = FormatStyle::LE_CRLF; |
| 852 | + Style.IncludeCategories = {{".*", 0, 0, false}}; |
| 853 | + std::string Code = "#include \"a\"\r\n" // Start of line: 0 |
| 854 | + "\r\n" // Start of line: 14 |
| 855 | + "#include \"b\"\r\n" // Start of line: 16 |
| 856 | + "\r\n" // Start of line: 30 |
| 857 | + "#include \"c\"\r\n" // Start of line: 32 |
| 858 | + "\r\n" // Start of line: 46 |
| 859 | + "int i;"; // Start of line: 48 |
| 860 | + std::string Expected = "#include \"a\"\r\n" // Start of line: 0 |
| 861 | + "#include \"b\"\r\n" // Start of line: 14 |
| 862 | + "#include \"c\"\r\n" // Start of line: 28 |
| 863 | + "\r\n" // Start of line: 42 |
| 864 | + "int i;"; // Start of line: 44 |
| 865 | + EXPECT_EQ(Expected, sort(Code)); |
| 866 | + EXPECT_EQ(0u, newCursor(Code, 0)); |
| 867 | + EXPECT_EQ( |
| 868 | + 14u, |
| 869 | + newCursor(Code, 14)); // cursor on empty line in include block is ignored |
| 870 | + EXPECT_EQ(14u, newCursor(Code, 16)); |
| 871 | + EXPECT_EQ( |
| 872 | + 30u, |
| 873 | + newCursor(Code, 30)); // cursor on empty line in include block is ignored |
| 874 | + EXPECT_EQ(28u, newCursor(Code, 32)); |
| 875 | + EXPECT_EQ(42u, newCursor(Code, 46)); |
| 876 | + EXPECT_EQ(44u, newCursor(Code, 48)); |
| 877 | +} |
| 878 | + |
| 879 | +// FIXME: the tests below should pass. |
| 880 | +#if 0 |
| 881 | +TEST_F( |
| 882 | + SortIncludesTest, |
| 883 | + CalculatesCorrectCursorPositionWhenNewLineReplacementsWithRegroupingAndCRLF) { |
| 884 | + Style.IncludeBlocks = Style.IBS_Regroup; |
| 885 | + FmtStyle.LineEnding = FormatStyle::LE_CRLF; |
| 886 | + Style.IncludeCategories = { |
| 887 | + {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}}; |
| 888 | + std::string Code = "#include \"a\"\r\n" // Start of line: 0 |
| 889 | + "#include \"b\"\r\n" // Start of line: 14 |
| 890 | + "#include \"c\"\r\n" // Start of line: 28 |
| 891 | + "\r\n" // Start of line: 42 |
| 892 | + "int i;"; // Start of line: 44 |
| 893 | + std::string Expected = "#include \"a\"\r\n" // Start of line: 0 |
| 894 | + "\r\n" // Start of line: 14 |
| 895 | + "#include \"b\"\r\n" // Start of line: 16 |
| 896 | + "\r\n" // Start of line: 30 |
| 897 | + "#include \"c\"\r\n" // Start of line: 32 |
| 898 | + "\r\n" // Start of line: 46 |
| 899 | + "int i;"; // Start of line: 48 |
| 900 | + EXPECT_EQ(Expected, sort(Code)); |
| 901 | + EXPECT_EQ(0u, newCursor(Code, 0)); |
| 902 | + EXPECT_EQ(15u, newCursor(Code, 16)); |
| 903 | + EXPECT_EQ(30u, newCursor(Code, 32)); |
| 904 | + EXPECT_EQ(44u, newCursor(Code, 46)); |
| 905 | + EXPECT_EQ(46u, newCursor(Code, 48)); |
| 906 | +} |
| 907 | + |
| 908 | +TEST_F( |
| 909 | + SortIncludesTest, |
| 910 | + CalculatesCorrectCursorPositionWhenNoNewLineReplacementsWithRegroupingAndCRLF) { |
| 911 | + Style.IncludeBlocks = Style.IBS_Regroup; |
| 912 | + FmtStyle.LineEnding = FormatStyle::LE_CRLF; |
| 913 | + Style.IncludeCategories = { |
| 914 | + {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}}; |
| 915 | + std::string Code = "#include \"a\"\r\n" // Start of line: 0 |
| 916 | + "\r\n" // Start of line: 14 |
| 917 | + "#include \"c\"\r\n" // Start of line: 16 |
| 918 | + "\r\n" // Start of line: 30 |
| 919 | + "#include \"b\"\r\n" // Start of line: 32 |
| 920 | + "\r\n" // Start of line: 46 |
| 921 | + "int i;"; // Start of line: 48 |
| 922 | + std::string Expected = "#include \"a\"\r\n" // Start of line: 0 |
| 923 | + "\r\n" // Start of line: 14 |
| 924 | + "#include \"b\"\r\n" // Start of line: 16 |
| 925 | + "\r\n" // Start of line: 30 |
| 926 | + "#include \"c\"\r\n" // Start of line: 32 |
| 927 | + "\r\n" // Start of line: 46 |
| 928 | + "int i;"; // Start of line: 48 |
| 929 | + EXPECT_EQ(Expected, sort(Code)); |
| 930 | + EXPECT_EQ(0u, newCursor(Code, 0)); |
| 931 | + EXPECT_EQ(14u, newCursor(Code, 14)); |
| 932 | + EXPECT_EQ(30u, newCursor(Code, 32)); |
| 933 | + EXPECT_EQ(30u, newCursor(Code, 30)); |
| 934 | + EXPECT_EQ(15u, newCursor(Code, 15)); |
| 935 | + EXPECT_EQ(44u, newCursor(Code, 46)); |
| 936 | + EXPECT_EQ(46u, newCursor(Code, 48)); |
| 937 | +} |
| 938 | +#endif |
| 939 | + |
824 | 940 | TEST_F(SortIncludesTest, DeduplicateIncludes) {
|
825 | 941 | EXPECT_EQ("#include <a>\n"
|
826 | 942 | "#include <b>\n"
|
|
0 commit comments