1 | //===- UsingDeclarationsSorterTest.cpp - Formatting unit tests ------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "clang/Format/Format.h" |
10 | |
11 | #include "llvm/Support/Debug.h" |
12 | #include "gtest/gtest.h" |
13 | |
14 | #define DEBUG_TYPE "using-declarations-sorter-test" |
15 | |
16 | namespace clang { |
17 | namespace format { |
18 | namespace { |
19 | |
20 | class UsingDeclarationsSorterTest : public ::testing::Test { |
21 | protected: |
22 | std::string sortUsingDeclarations(llvm::StringRef Code, |
23 | const std::vector<tooling::Range> &Ranges, |
24 | const FormatStyle &Style = getLLVMStyle()) { |
25 | LLVM_DEBUG(llvm::errs() << "---\n" ); |
26 | LLVM_DEBUG(llvm::errs() << Code << "\n\n" ); |
27 | tooling::Replacements Replaces = |
28 | clang::format::sortUsingDeclarations(Style, Code, Ranges, FileName: "<stdin>" ); |
29 | auto Result = applyAllReplacements(Code, Replaces); |
30 | EXPECT_TRUE(static_cast<bool>(Result)); |
31 | LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n" ); |
32 | return *Result; |
33 | } |
34 | |
35 | std::string sortUsingDeclarations(llvm::StringRef Code, |
36 | const FormatStyle &Style = getLLVMStyle()) { |
37 | return sortUsingDeclarations(Code, |
38 | /*Ranges=*/{1, tooling::Range(0, Code.size())}, |
39 | Style); |
40 | } |
41 | }; |
42 | |
43 | TEST_F(UsingDeclarationsSorterTest, SwapsTwoConsecutiveUsingDeclarations) { |
44 | FormatStyle Style = getLLVMStyle(); |
45 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
46 | EXPECT_EQ("using a;\n" |
47 | "using b;" , |
48 | sortUsingDeclarations("using a;\n" |
49 | "using b;" , |
50 | Style)); |
51 | EXPECT_EQ("using a;\n" |
52 | "using aa;" , |
53 | sortUsingDeclarations("using aa;\n" |
54 | "using a;" , |
55 | Style)); |
56 | EXPECT_EQ("using a;\n" |
57 | "using ::a;" , |
58 | sortUsingDeclarations("using a;\n" |
59 | "using ::a;" , |
60 | Style)); |
61 | |
62 | EXPECT_EQ("using a::bcd;\n" |
63 | "using a::cd;" , |
64 | sortUsingDeclarations("using a::cd;\n" |
65 | "using a::bcd;" , |
66 | Style)); |
67 | |
68 | EXPECT_EQ("using a;\n" |
69 | "using a::a;" , |
70 | sortUsingDeclarations("using a::a;\n" |
71 | "using a;" , |
72 | Style)); |
73 | |
74 | EXPECT_EQ("using a::ba::aa;\n" |
75 | "using a::bb::ccc;" , |
76 | sortUsingDeclarations("using a::bb::ccc;\n" |
77 | "using a::ba::aa;" , |
78 | Style)); |
79 | |
80 | EXPECT_EQ("using a;\n" |
81 | "using typename a;" , |
82 | sortUsingDeclarations("using typename a;\n" |
83 | "using a;" , |
84 | Style)); |
85 | |
86 | EXPECT_EQ("using typename z;\n" |
87 | "using typenamea;" , |
88 | sortUsingDeclarations("using typenamea;\n" |
89 | "using typename z;" , |
90 | Style)); |
91 | |
92 | EXPECT_EQ("using a, b;\n" |
93 | "using aa;" , |
94 | sortUsingDeclarations("using aa;\n" |
95 | "using a, b;" , |
96 | Style)); |
97 | |
98 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
99 | EXPECT_EQ("using a;\n" |
100 | "using b;" , |
101 | sortUsingDeclarations("using a;\n" |
102 | "using b;" , |
103 | Style)); |
104 | EXPECT_EQ("using a;\n" |
105 | "using aa;" , |
106 | sortUsingDeclarations("using aa;\n" |
107 | "using a;" , |
108 | Style)); |
109 | EXPECT_EQ("using a;\n" |
110 | "using ::a;" , |
111 | sortUsingDeclarations("using a;\n" |
112 | "using ::a;" , |
113 | Style)); |
114 | |
115 | EXPECT_EQ("using a::bcd;\n" |
116 | "using a::cd;" , |
117 | sortUsingDeclarations("using a::cd;\n" |
118 | "using a::bcd;" , |
119 | Style)); |
120 | |
121 | EXPECT_EQ("using a;\n" |
122 | "using a::a;" , |
123 | sortUsingDeclarations("using a::a;\n" |
124 | "using a;" , |
125 | Style)); |
126 | |
127 | EXPECT_EQ("using a::ba::aa;\n" |
128 | "using a::bb::ccc;" , |
129 | sortUsingDeclarations("using a::bb::ccc;\n" |
130 | "using a::ba::aa;" , |
131 | Style)); |
132 | |
133 | EXPECT_EQ("using a;\n" |
134 | "using typename a;" , |
135 | sortUsingDeclarations("using typename a;\n" |
136 | "using a;" , |
137 | Style)); |
138 | |
139 | EXPECT_EQ("using typename z;\n" |
140 | "using typenamea;" , |
141 | sortUsingDeclarations("using typenamea;\n" |
142 | "using typename z;" , |
143 | Style)); |
144 | |
145 | EXPECT_EQ("using a, b;\n" |
146 | "using aa;" , |
147 | sortUsingDeclarations("using aa;\n" |
148 | "using a, b;" , |
149 | Style)); |
150 | } |
151 | |
152 | TEST_F(UsingDeclarationsSorterTest, UsingDeclarationOrder) { |
153 | FormatStyle Style = getLLVMStyle(); |
154 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
155 | EXPECT_EQ("using A;\n" |
156 | "using a;" , |
157 | sortUsingDeclarations("using A;\n" |
158 | "using a;" , |
159 | Style)); |
160 | EXPECT_EQ("using a;\n" |
161 | "using A;" , |
162 | sortUsingDeclarations("using a;\n" |
163 | "using A;" , |
164 | Style)); |
165 | EXPECT_EQ("using a;\n" |
166 | "using B;" , |
167 | sortUsingDeclarations("using B;\n" |
168 | "using a;" , |
169 | Style)); |
170 | |
171 | // Ignores leading '::'. |
172 | EXPECT_EQ("using ::a;\n" |
173 | "using A;" , |
174 | sortUsingDeclarations("using ::a;\n" |
175 | "using A;" , |
176 | Style)); |
177 | |
178 | EXPECT_EQ("using ::A;\n" |
179 | "using a;" , |
180 | sortUsingDeclarations("using ::A;\n" |
181 | "using a;" , |
182 | Style)); |
183 | |
184 | // Sorts '_' before 'a' and 'A'. |
185 | EXPECT_EQ("using _;\n" |
186 | "using A;" , |
187 | sortUsingDeclarations("using A;\n" |
188 | "using _;" , |
189 | Style)); |
190 | EXPECT_EQ("using _;\n" |
191 | "using a;" , |
192 | sortUsingDeclarations("using a;\n" |
193 | "using _;" , |
194 | Style)); |
195 | EXPECT_EQ("using a::_;\n" |
196 | "using a::a;" , |
197 | sortUsingDeclarations("using a::a;\n" |
198 | "using a::_;" , |
199 | Style)); |
200 | |
201 | // Sorts non-namespace names before namespace names at the same level. |
202 | EXPECT_EQ("using ::testing::_;\n" |
203 | "using ::testing::Aardvark;\n" |
204 | "using ::testing::kMax;\n" |
205 | "using ::testing::Xylophone;\n" |
206 | "using ::testing::apple::Honeycrisp;\n" |
207 | "using ::testing::zebra::Stripes;" , |
208 | sortUsingDeclarations("using ::testing::Aardvark;\n" |
209 | "using ::testing::Xylophone;\n" |
210 | "using ::testing::kMax;\n" |
211 | "using ::testing::_;\n" |
212 | "using ::testing::apple::Honeycrisp;\n" |
213 | "using ::testing::zebra::Stripes;" , |
214 | Style)); |
215 | |
216 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
217 | EXPECT_EQ("using A;\n" |
218 | "using a;" , |
219 | sortUsingDeclarations("using A;\n" |
220 | "using a;" , |
221 | Style)); |
222 | EXPECT_EQ("using a;\n" |
223 | "using A;" , |
224 | sortUsingDeclarations("using a;\n" |
225 | "using A;" , |
226 | Style)); |
227 | EXPECT_EQ("using a;\n" |
228 | "using B;" , |
229 | sortUsingDeclarations("using B;\n" |
230 | "using a;" , |
231 | Style)); |
232 | |
233 | // Ignores leading '::'. |
234 | EXPECT_EQ("using ::a;\n" |
235 | "using A;" , |
236 | sortUsingDeclarations("using ::a;\n" |
237 | "using A;" , |
238 | Style)); |
239 | |
240 | EXPECT_EQ("using ::A;\n" |
241 | "using a;" , |
242 | sortUsingDeclarations("using ::A;\n" |
243 | "using a;" , |
244 | Style)); |
245 | |
246 | // Sorts '_' before 'a' and 'A'. |
247 | EXPECT_EQ("using _;\n" |
248 | "using A;" , |
249 | sortUsingDeclarations("using A;\n" |
250 | "using _;" , |
251 | Style)); |
252 | EXPECT_EQ("using _;\n" |
253 | "using a;" , |
254 | sortUsingDeclarations("using a;\n" |
255 | "using _;" , |
256 | Style)); |
257 | EXPECT_EQ("using a::_;\n" |
258 | "using a::a;" , |
259 | sortUsingDeclarations("using a::a;\n" |
260 | "using a::_;" , |
261 | Style)); |
262 | |
263 | // Sorts non-namespace names before namespace names at the same level. |
264 | EXPECT_EQ("using ::testing::_;\n" |
265 | "using ::testing::Aardvark;\n" |
266 | "using ::testing::apple::Honeycrisp;\n" |
267 | "using ::testing::kMax;\n" |
268 | "using ::testing::Xylophone;\n" |
269 | "using ::testing::zebra::Stripes;" , |
270 | sortUsingDeclarations("using ::testing::Aardvark;\n" |
271 | "using ::testing::Xylophone;\n" |
272 | "using ::testing::kMax;\n" |
273 | "using ::testing::_;\n" |
274 | "using ::testing::apple::Honeycrisp;\n" |
275 | "using ::testing::zebra::Stripes;" , |
276 | Style)); |
277 | } |
278 | |
279 | TEST_F(UsingDeclarationsSorterTest, SortsStably) { |
280 | FormatStyle Style = getLLVMStyle(); |
281 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
282 | EXPECT_EQ("using a;\n" |
283 | "using A;\n" |
284 | "using a;\n" |
285 | "using A;\n" |
286 | "using a;\n" |
287 | "using A;\n" |
288 | "using a;\n" |
289 | "using B;\n" |
290 | "using b;\n" |
291 | "using B;\n" |
292 | "using b;\n" |
293 | "using B;\n" |
294 | "using b;" , |
295 | sortUsingDeclarations("using a;\n" |
296 | "using B;\n" |
297 | "using a;\n" |
298 | "using b;\n" |
299 | "using A;\n" |
300 | "using a;\n" |
301 | "using b;\n" |
302 | "using B;\n" |
303 | "using b;\n" |
304 | "using A;\n" |
305 | "using a;\n" |
306 | "using b;\n" |
307 | "using b;\n" |
308 | "using B;\n" |
309 | "using b;\n" |
310 | "using A;\n" |
311 | "using a;" , |
312 | Style)); |
313 | |
314 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
315 | EXPECT_EQ("using a;\n" |
316 | "using A;\n" |
317 | "using a;\n" |
318 | "using A;\n" |
319 | "using a;\n" |
320 | "using A;\n" |
321 | "using a;\n" |
322 | "using B;\n" |
323 | "using b;\n" |
324 | "using B;\n" |
325 | "using b;\n" |
326 | "using B;\n" |
327 | "using b;" , |
328 | sortUsingDeclarations("using a;\n" |
329 | "using B;\n" |
330 | "using a;\n" |
331 | "using b;\n" |
332 | "using A;\n" |
333 | "using a;\n" |
334 | "using b;\n" |
335 | "using B;\n" |
336 | "using b;\n" |
337 | "using A;\n" |
338 | "using a;\n" |
339 | "using b;\n" |
340 | "using b;\n" |
341 | "using B;\n" |
342 | "using b;\n" |
343 | "using A;\n" |
344 | "using a;" , |
345 | Style)); |
346 | } |
347 | |
348 | TEST_F(UsingDeclarationsSorterTest, SortsMultipleTopLevelDeclarations) { |
349 | FormatStyle Style = getLLVMStyle(); |
350 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
351 | EXPECT_EQ("using a;\n" |
352 | "using b;\n" |
353 | "using c;\n" |
354 | "using d;\n" |
355 | "using e;" , |
356 | sortUsingDeclarations("using d;\n" |
357 | "using b;\n" |
358 | "using e;\n" |
359 | "using a;\n" |
360 | "using c;" , |
361 | Style)); |
362 | |
363 | EXPECT_EQ("#include <iostream>\n" |
364 | "using std::cin;\n" |
365 | "using std::cout;\n" |
366 | "using ::std::endl;\n" |
367 | "int main();" , |
368 | sortUsingDeclarations("#include <iostream>\n" |
369 | "using std::cout;\n" |
370 | "using ::std::endl;\n" |
371 | "using std::cin;\n" |
372 | "int main();" , |
373 | Style)); |
374 | |
375 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
376 | EXPECT_EQ("using a;\n" |
377 | "using b;\n" |
378 | "using c;\n" |
379 | "using d;\n" |
380 | "using e;" , |
381 | sortUsingDeclarations("using d;\n" |
382 | "using b;\n" |
383 | "using e;\n" |
384 | "using a;\n" |
385 | "using c;" , |
386 | Style)); |
387 | |
388 | EXPECT_EQ("#include <iostream>\n" |
389 | "using std::cin;\n" |
390 | "using std::cout;\n" |
391 | "using ::std::endl;\n" |
392 | "int main();" , |
393 | sortUsingDeclarations("#include <iostream>\n" |
394 | "using std::cout;\n" |
395 | "using ::std::endl;\n" |
396 | "using std::cin;\n" |
397 | "int main();" , |
398 | Style)); |
399 | } |
400 | |
401 | TEST_F(UsingDeclarationsSorterTest, BreaksOnEmptyLines) { |
402 | FormatStyle Style = getLLVMStyle(); |
403 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
404 | EXPECT_EQ("using b;\n" |
405 | "using c;\n" |
406 | "\n" |
407 | "using a;\n" |
408 | "using d;" , |
409 | sortUsingDeclarations("using c;\n" |
410 | "using b;\n" |
411 | "\n" |
412 | "using d;\n" |
413 | "using a;" , |
414 | Style)); |
415 | |
416 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
417 | EXPECT_EQ("using b;\n" |
418 | "using c;\n" |
419 | "\n" |
420 | "using a;\n" |
421 | "using d;" , |
422 | sortUsingDeclarations("using c;\n" |
423 | "using b;\n" |
424 | "\n" |
425 | "using d;\n" |
426 | "using a;" , |
427 | Style)); |
428 | } |
429 | |
430 | TEST_F(UsingDeclarationsSorterTest, BreaksOnUsingNamespace) { |
431 | FormatStyle Style = getLLVMStyle(); |
432 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
433 | EXPECT_EQ("using b;\n" |
434 | "using namespace std;\n" |
435 | "using a;" , |
436 | sortUsingDeclarations("using b;\n" |
437 | "using namespace std;\n" |
438 | "using a;" , |
439 | Style)); |
440 | |
441 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
442 | EXPECT_EQ("using b;\n" |
443 | "using namespace std;\n" |
444 | "using a;" , |
445 | sortUsingDeclarations("using b;\n" |
446 | "using namespace std;\n" |
447 | "using a;" , |
448 | Style)); |
449 | } |
450 | |
451 | TEST_F(UsingDeclarationsSorterTest, KeepsUsingDeclarationsInPPDirectives) { |
452 | FormatStyle Style = getLLVMStyle(); |
453 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
454 | EXPECT_EQ("#define A \\\n" |
455 | "using b;\\\n" |
456 | "using a;" , |
457 | sortUsingDeclarations("#define A \\\n" |
458 | "using b;\\\n" |
459 | "using a;" , |
460 | Style)); |
461 | |
462 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
463 | EXPECT_EQ("#define A \\\n" |
464 | "using b;\\\n" |
465 | "using a;" , |
466 | sortUsingDeclarations("#define A \\\n" |
467 | "using b;\\\n" |
468 | "using a;" , |
469 | Style)); |
470 | } |
471 | |
472 | TEST_F(UsingDeclarationsSorterTest, KeepsTypeAliases) { |
473 | auto Code = "struct C { struct B { struct A; }; };\n" |
474 | "using B = C::B;\n" |
475 | "using A = B::A;" ; |
476 | |
477 | FormatStyle Style = getLLVMStyle(); |
478 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
479 | EXPECT_EQ(Code, sortUsingDeclarations(Code, Style)); |
480 | |
481 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
482 | EXPECT_EQ(Code, sortUsingDeclarations(Code, Style)); |
483 | } |
484 | |
485 | TEST_F(UsingDeclarationsSorterTest, MovesTrailingCommentsWithDeclarations) { |
486 | FormatStyle Style = getLLVMStyle(); |
487 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
488 | EXPECT_EQ("using a; // line a1\n" |
489 | "using b; /* line b1\n" |
490 | " * line b2\n" |
491 | " * line b3 */\n" |
492 | "using c; // line c1\n" |
493 | " // line c2" , |
494 | sortUsingDeclarations("using c; // line c1\n" |
495 | " // line c2\n" |
496 | "using b; /* line b1\n" |
497 | " * line b2\n" |
498 | " * line b3 */\n" |
499 | "using a; // line a1" , |
500 | Style)); |
501 | |
502 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
503 | EXPECT_EQ("using a; // line a1\n" |
504 | "using b; /* line b1\n" |
505 | " * line b2\n" |
506 | " * line b3 */\n" |
507 | "using c; // line c1\n" |
508 | " // line c2" , |
509 | sortUsingDeclarations("using c; // line c1\n" |
510 | " // line c2\n" |
511 | "using b; /* line b1\n" |
512 | " * line b2\n" |
513 | " * line b3 */\n" |
514 | "using a; // line a1" , |
515 | Style)); |
516 | } |
517 | |
518 | TEST_F(UsingDeclarationsSorterTest, SortsInStructScope) { |
519 | FormatStyle Style = getLLVMStyle(); |
520 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
521 | EXPECT_EQ("struct pt3 : pt2 {\n" |
522 | " using pt2::x;\n" |
523 | " using pt2::y;\n" |
524 | " float z;\n" |
525 | "};" , |
526 | sortUsingDeclarations("struct pt3 : pt2 {\n" |
527 | " using pt2::y;\n" |
528 | " using pt2::x;\n" |
529 | " float z;\n" |
530 | "};" , |
531 | Style)); |
532 | |
533 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
534 | EXPECT_EQ("struct pt3 : pt2 {\n" |
535 | " using pt2::x;\n" |
536 | " using pt2::y;\n" |
537 | " float z;\n" |
538 | "};" , |
539 | sortUsingDeclarations("struct pt3 : pt2 {\n" |
540 | " using pt2::y;\n" |
541 | " using pt2::x;\n" |
542 | " float z;\n" |
543 | "};" , |
544 | Style)); |
545 | } |
546 | |
547 | TEST_F(UsingDeclarationsSorterTest, KeepsOperators) { |
548 | FormatStyle Style = getLLVMStyle(); |
549 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
550 | EXPECT_EQ("using a::operator();\n" |
551 | "using a::operator-;\n" |
552 | "using a::operator+;" , |
553 | sortUsingDeclarations("using a::operator();\n" |
554 | "using a::operator-;\n" |
555 | "using a::operator+;" , |
556 | Style)); |
557 | |
558 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
559 | EXPECT_EQ("using a::operator();\n" |
560 | "using a::operator-;\n" |
561 | "using a::operator+;" , |
562 | sortUsingDeclarations("using a::operator();\n" |
563 | "using a::operator-;\n" |
564 | "using a::operator+;" , |
565 | Style)); |
566 | } |
567 | |
568 | TEST_F(UsingDeclarationsSorterTest, SortsUsingDeclarationsInsideNamespaces) { |
569 | FormatStyle Style = getLLVMStyle(); |
570 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
571 | EXPECT_EQ("namespace A {\n" |
572 | "struct B;\n" |
573 | "struct C;\n" |
574 | "}\n" |
575 | "namespace X {\n" |
576 | "using A::B;\n" |
577 | "using A::C;\n" |
578 | "}" , |
579 | sortUsingDeclarations("namespace A {\n" |
580 | "struct B;\n" |
581 | "struct C;\n" |
582 | "}\n" |
583 | "namespace X {\n" |
584 | "using A::C;\n" |
585 | "using A::B;\n" |
586 | "}" , |
587 | Style)); |
588 | |
589 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
590 | EXPECT_EQ("namespace A {\n" |
591 | "struct B;\n" |
592 | "struct C;\n" |
593 | "}\n" |
594 | "namespace X {\n" |
595 | "using A::B;\n" |
596 | "using A::C;\n" |
597 | "}" , |
598 | sortUsingDeclarations("namespace A {\n" |
599 | "struct B;\n" |
600 | "struct C;\n" |
601 | "}\n" |
602 | "namespace X {\n" |
603 | "using A::C;\n" |
604 | "using A::B;\n" |
605 | "}" , |
606 | Style)); |
607 | } |
608 | |
609 | TEST_F(UsingDeclarationsSorterTest, SupportsClangFormatOff) { |
610 | FormatStyle Style = getLLVMStyle(); |
611 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
612 | EXPECT_EQ("// clang-format off\n" |
613 | "using b;\n" |
614 | "using a;\n" |
615 | "// clang-format on\n" |
616 | "using c;\n" |
617 | "using d;" , |
618 | sortUsingDeclarations("// clang-format off\n" |
619 | "using b;\n" |
620 | "using a;\n" |
621 | "// clang-format on\n" |
622 | "using d;\n" |
623 | "using c;" , |
624 | Style)); |
625 | |
626 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
627 | EXPECT_EQ("// clang-format off\n" |
628 | "using b;\n" |
629 | "using a;\n" |
630 | "// clang-format on\n" |
631 | "using c;\n" |
632 | "using d;" , |
633 | sortUsingDeclarations("// clang-format off\n" |
634 | "using b;\n" |
635 | "using a;\n" |
636 | "// clang-format on\n" |
637 | "using d;\n" |
638 | "using c;" , |
639 | Style)); |
640 | } |
641 | |
642 | TEST_F(UsingDeclarationsSorterTest, SortsPartialRangeOfUsingDeclarations) { |
643 | FormatStyle Style = getLLVMStyle(); |
644 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
645 | // Sorts the whole block of using declarations surrounding the range. |
646 | EXPECT_EQ("using a;\n" |
647 | "using b;\n" |
648 | "using c;" , |
649 | sortUsingDeclarations("using b;\n" |
650 | "using c;\n" // starts at offset 10 |
651 | "using a;" , |
652 | {tooling::Range(10, 15)}, Style)); |
653 | EXPECT_EQ("using a;\n" |
654 | "using b;\n" |
655 | "using c;\n" |
656 | "using A = b;" , |
657 | sortUsingDeclarations("using b;\n" |
658 | "using c;\n" // starts at offset 10 |
659 | "using a;\n" |
660 | "using A = b;" , |
661 | {tooling::Range(10, 15)}, Style)); |
662 | |
663 | EXPECT_EQ("using d;\n" |
664 | "using c;\n" |
665 | "\n" |
666 | "using a;\n" |
667 | "using b;\n" |
668 | "\n" |
669 | "using f;\n" |
670 | "using e;" , |
671 | sortUsingDeclarations("using d;\n" |
672 | "using c;\n" |
673 | "\n" |
674 | "using b;\n" // starts at offset 19 |
675 | "using a;\n" |
676 | "\n" |
677 | "using f;\n" |
678 | "using e;" , |
679 | {tooling::Range(19, 1)}, Style)); |
680 | |
681 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
682 | // Sorts the whole block of using declarations surrounding the range. |
683 | EXPECT_EQ("using a;\n" |
684 | "using b;\n" |
685 | "using c;" , |
686 | sortUsingDeclarations("using b;\n" |
687 | "using c;\n" // starts at offset 10 |
688 | "using a;" , |
689 | {tooling::Range(10, 15)}, Style)); |
690 | EXPECT_EQ("using a;\n" |
691 | "using b;\n" |
692 | "using c;\n" |
693 | "using A = b;" , |
694 | sortUsingDeclarations("using b;\n" |
695 | "using c;\n" // starts at offset 10 |
696 | "using a;\n" |
697 | "using A = b;" , |
698 | {tooling::Range(10, 15)}, Style)); |
699 | |
700 | EXPECT_EQ("using d;\n" |
701 | "using c;\n" |
702 | "\n" |
703 | "using a;\n" |
704 | "using b;\n" |
705 | "\n" |
706 | "using f;\n" |
707 | "using e;" , |
708 | sortUsingDeclarations("using d;\n" |
709 | "using c;\n" |
710 | "\n" |
711 | "using b;\n" // starts at offset 19 |
712 | "using a;\n" |
713 | "\n" |
714 | "using f;\n" |
715 | "using e;" , |
716 | {tooling::Range(19, 1)}, Style)); |
717 | } |
718 | |
719 | TEST_F(UsingDeclarationsSorterTest, |
720 | SortsUsingDeclarationsWithLeadingkComments) { |
721 | FormatStyle Style = getLLVMStyle(); |
722 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
723 | EXPECT_EQ("/* comment */ using a;\n" |
724 | "/* comment */ using b;" , |
725 | sortUsingDeclarations("/* comment */ using b;\n" |
726 | "/* comment */ using a;" , |
727 | Style)); |
728 | |
729 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
730 | EXPECT_EQ("/* comment */ using a;\n" |
731 | "/* comment */ using b;" , |
732 | sortUsingDeclarations("/* comment */ using b;\n" |
733 | "/* comment */ using a;" , |
734 | Style)); |
735 | } |
736 | |
737 | TEST_F(UsingDeclarationsSorterTest, DeduplicatesUsingDeclarations) { |
738 | FormatStyle Style = getLLVMStyle(); |
739 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
740 | EXPECT_EQ("using a;\n" |
741 | "using b;\n" |
742 | "using c;\n" |
743 | "\n" |
744 | "using a;\n" |
745 | "using e;" , |
746 | sortUsingDeclarations("using c;\n" |
747 | "using a;\n" |
748 | "using b;\n" |
749 | "using a;\n" |
750 | "using b;\n" |
751 | "\n" |
752 | "using e;\n" |
753 | "using a;\n" |
754 | "using e;" , |
755 | Style)); |
756 | |
757 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
758 | EXPECT_EQ("using a;\n" |
759 | "using b;\n" |
760 | "using c;\n" |
761 | "\n" |
762 | "using a;\n" |
763 | "using e;" , |
764 | sortUsingDeclarations("using c;\n" |
765 | "using a;\n" |
766 | "using b;\n" |
767 | "using a;\n" |
768 | "using b;\n" |
769 | "\n" |
770 | "using e;\n" |
771 | "using a;\n" |
772 | "using e;" , |
773 | Style)); |
774 | } |
775 | |
776 | TEST_F(UsingDeclarationsSorterTest, |
777 | SortsUsingDeclarationsWithDifferentCountsOfScopes) { |
778 | FormatStyle Style = getLLVMStyle(); |
779 | EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric, Style.SortUsingDeclarations); |
780 | EXPECT_EQ("using boost::regex;\n" |
781 | "using boost::regex_constants::icase;\n" |
782 | "using std::move;\n" |
783 | "using std::string;\n" |
784 | "using std::chrono::duration_cast;\n" |
785 | "using std::chrono::microseconds;\n" |
786 | "using std::chrono::seconds;\n" |
787 | "using std::chrono::steady_clock;" , |
788 | sortUsingDeclarations("using boost::regex;\n" |
789 | "using boost::regex_constants::icase;\n" |
790 | "using std::chrono::duration_cast;\n" |
791 | "using std::chrono::microseconds;\n" |
792 | "using std::chrono::seconds;\n" |
793 | "using std::chrono::steady_clock;\n" |
794 | "using std::move;\n" |
795 | "using std::string;" , |
796 | Style)); |
797 | |
798 | Style.SortUsingDeclarations = FormatStyle::SUD_Lexicographic; |
799 | EXPECT_EQ("using boost::regex;\n" |
800 | "using boost::regex_constants::icase;\n" |
801 | "using std::chrono::duration_cast;\n" |
802 | "using std::chrono::microseconds;\n" |
803 | "using std::chrono::seconds;\n" |
804 | "using std::chrono::steady_clock;\n" |
805 | "using std::move;\n" |
806 | "using std::string;" , |
807 | sortUsingDeclarations("using boost::regex;\n" |
808 | "using boost::regex_constants::icase;\n" |
809 | "using std::move;\n" |
810 | "using std::string;\n" |
811 | "using std::chrono::duration_cast;\n" |
812 | "using std::chrono::microseconds;\n" |
813 | "using std::chrono::seconds;\n" |
814 | "using std::chrono::steady_clock;" , |
815 | Style)); |
816 | } |
817 | |
818 | } // end namespace |
819 | } // end namespace format |
820 | } // end namespace clang |
821 | |