1 #include "clang/Format/Format.h"
2 #include "gtest/gtest.h"
4 #define DEBUG_TYPE "format-test"
10 class SortImportsTestJava
: public ::testing::Test
{
12 std::vector
<tooling::Range
> GetCodeRange(StringRef Code
) {
13 return std::vector
<tooling::Range
>(1, tooling::Range(0, Code
.size()));
16 std::string
sort(StringRef Code
, std::vector
<tooling::Range
> Ranges
) {
17 auto Replaces
= sortIncludes(FmtStyle
, Code
, Ranges
, "input.java");
18 Ranges
= tooling::calculateRangesAfterReplacements(Replaces
, Ranges
);
19 auto Sorted
= applyAllReplacements(Code
, Replaces
);
20 EXPECT_TRUE(static_cast<bool>(Sorted
));
21 auto Result
= applyAllReplacements(
22 *Sorted
, reformat(FmtStyle
, *Sorted
, Ranges
, "input.java"));
23 EXPECT_TRUE(static_cast<bool>(Result
));
27 std::string
sort(StringRef Code
) { return sort(Code
, GetCodeRange(Code
)); }
32 SortImportsTestJava() {
33 FmtStyle
= getGoogleStyle(FormatStyle::LK_Java
);
34 FmtStyle
.JavaImportGroups
= {"com.test", "org", "com"};
35 FmtStyle
.SortIncludes
= FormatStyle::SI_CaseInsensitive
;
39 TEST_F(SortImportsTestJava
, StaticSplitFromNormal
) {
40 EXPECT_EQ("import static org.b;\n"
43 sort("import org.a;\n"
44 "import static org.b;\n"));
47 TEST_F(SortImportsTestJava
, CapitalBeforeLowercase
) {
48 EXPECT_EQ("import org.Test;\n"
49 "import org.a.Test;\n"
51 sort("import org.a.Test;\n"
56 TEST_F(SortImportsTestJava
, KeepSplitGroupsWithOneNewImport
) {
57 EXPECT_EQ("import static com.test.a;\n"
59 "import static org.a;\n"
61 "import static com.a;\n"
63 "import com.test.b;\n"
64 "import com.test.c;\n"
69 sort("import static com.test.a;\n"
71 "import static org.a;\n"
73 "import static com.a;\n"
75 "import com.test.b;\n"
80 "import com.test.c;\n"));
83 TEST_F(SortImportsTestJava
, SplitGroupsWithNewline
) {
84 EXPECT_EQ("import static com.test.a;\n"
86 "import static org.a;\n"
88 "import static com.a;\n"
90 "import com.test.b;\n"
95 sort("import static com.test.a;\n"
96 "import static org.a;\n"
97 "import static com.a;\n"
98 "import com.test.b;\n"
103 TEST_F(SortImportsTestJava
, UnspecifiedGroupAfterAllGroups
) {
104 EXPECT_EQ("import com.test.a;\n"
112 sort("import com.test.a;\n"
119 TEST_F(SortImportsTestJava
, NoSortOutsideRange
) {
120 std::vector
<tooling::Range
> Ranges
= {tooling::Range(27, 15)};
121 EXPECT_EQ("import org.b;\n"
126 sort("import org.b;\n"
134 TEST_F(SortImportsTestJava
, SortWhenRangeContainsOneLine
) {
135 std::vector
<tooling::Range
> Ranges
= {tooling::Range(27, 20)};
136 EXPECT_EQ("import org.a;\n"
143 sort("import org.b;\n"
152 TEST_F(SortImportsTestJava
, SortLexicographically
) {
153 EXPECT_EQ("import org.a.*;\n"
157 sort("import org.aa;\n"
160 "import org.aA;\n"));
163 TEST_F(SortImportsTestJava
, StaticInCommentHasNoEffect
) {
164 EXPECT_EQ("import org.a; // static\n"
166 "import org.c; // static\n",
167 sort("import org.a; // static\n"
168 "import org.c; // static\n"
172 TEST_F(SortImportsTestJava
, CommentsWithAffectedImports
) {
173 EXPECT_EQ("import org.a;\n"
180 sort("import org.a;\n"
189 TEST_F(SortImportsTestJava
, CommentWithUnaffectedImports
) {
190 EXPECT_EQ("import org.a;\n"
193 sort("import org.a;\n"
198 TEST_F(SortImportsTestJava
, CommentAfterAffectedImports
) {
199 EXPECT_EQ("import org.a;\n"
202 sort("import org.b;\n"
207 TEST_F(SortImportsTestJava
, CommentBeforeAffectedImports
) {
208 EXPECT_EQ("// comment\n"
216 TEST_F(SortImportsTestJava
, FormatTotallyOff
) {
217 EXPECT_EQ("// clang-format off\n"
220 "// clang-format on\n",
221 sort("// clang-format off\n"
224 "// clang-format on\n"));
227 TEST_F(SortImportsTestJava
, FormatTotallyOn
) {
228 EXPECT_EQ("// clang-format off\n"
229 "// clang-format on\n"
232 sort("// clang-format off\n"
233 "// clang-format on\n"
238 TEST_F(SortImportsTestJava
, FormatPariallyOnShouldNotReorder
) {
239 EXPECT_EQ("// clang-format off\n"
242 "// clang-format on\n"
245 sort("// clang-format off\n"
248 "// clang-format on\n"
253 TEST_F(SortImportsTestJava
, SortJavaStaticImport
) {
254 FmtStyle
.SortJavaStaticImport
= FormatStyle::SJSIO_Before
;
255 EXPECT_EQ("import static com.test.a;\n"
257 "import static org.a;\n"
259 "import static com.a;\n"
261 "import com.test.b;\n"
266 sort("import static com.test.a;\n"
267 "import static org.a;\n"
268 "import static com.a;\n"
269 "import com.test.b;\n"
273 FmtStyle
.SortJavaStaticImport
= FormatStyle::SJSIO_After
;
274 EXPECT_EQ("import com.test.b;\n"
275 "import com.test.c;\n"
281 "import static com.test.a;\n"
283 "import static org.a;\n"
285 "import static com.a;\n",
286 sort("import static com.test.a;\n"
287 "import static org.a;\n"
288 "import static com.a;\n"
289 "import com.test.b;\n"
292 "import com.test.c;\n"));
295 TEST_F(SortImportsTestJava
, SortJavaStaticImportAsGroup
) {
296 FmtStyle
.SortJavaStaticImport
= FormatStyle::SJSIO_After
;
298 EXPECT_EQ("import com.test.a;\n"
299 "import com.test.b;\n"
301 "import static org.a;\n"
302 "import static org.b;\n",
303 sort("import com.test.a;\n"
304 "import static org.a;\n"
305 "import com.test.b;\n"
306 "import static org.b;\n"));
309 TEST_F(SortImportsTestJava
, DeduplicateImports
) {
310 EXPECT_EQ("import org.a;\n", sort("import org.a;\n"
314 TEST_F(SortImportsTestJava
, NoNewlineAtEnd
) {
315 EXPECT_EQ("import org.a;\n"
317 sort("import org.b;\n"
321 TEST_F(SortImportsTestJava
, ImportNamedFunction
) {
322 EXPECT_EQ("import X;\n"
336 TEST_F(SortImportsTestJava
, NoReplacementsForValidImports
) {
337 // Identical #includes have led to a failure with an unstable sort.
338 std::string Code
= "import org.a;\n"
341 sortIncludes(FmtStyle
, Code
, GetCodeRange(Code
), "input.java").empty());
344 TEST_F(SortImportsTestJava
, NoReplacementsForValidImportsWindows
) {
345 std::string Code
= "import org.a;\r\n"
348 sortIncludes(FmtStyle
, Code
, GetCodeRange(Code
), "input.java").empty());
352 } // end namespace format
353 } // end namespace clang