1 33 34 package edu.rice.cs.drjava.model.definitions; 35 36 import junit.framework.*; 37 import javax.swing.text.BadLocationException ; 38 43 import edu.rice.cs.drjava.DrJava; 44 import edu.rice.cs.drjava.DrJavaTestCase; 45 import edu.rice.cs.drjava.model.DJDocument; 46 import edu.rice.cs.drjava.model.definitions.reducedmodel.*; 47 import edu.rice.cs.drjava.config.*; 48 import edu.rice.cs.drjava.model.definitions.indent.*; 49 import edu.rice.cs.drjava.model.GlobalEventNotifier; 50 import edu.rice.cs.util.swing.Utilities; 52 53 57 public final class IndentTest extends DrJavaTestCase { 58 protected DefinitionsDocument doc; 59 60 static String noBrace = IndentInfo.noBrace; 61 static String openSquiggly = IndentInfo.openSquiggly; 62 static String openParen = IndentInfo.openParen; 63 static String openBracket = IndentInfo.openBracket; 64 private Integer indentLevel = new Integer (2); 65 private GlobalEventNotifier _notifier; 66 67 71 public IndentTest(String name) { super(name); } 72 73 74 public void setUp() throws Exception { 75 super.setUp(); 76 DrJava.getConfig().resetToDefaults(); 77 _notifier = new GlobalEventNotifier(); 78 doc = new DefinitionsDocument(_notifier); 79 DrJava.getConfig().setSetting(OptionConstants.INDENT_LEVEL,indentLevel); 80 } 81 82 85 public static Test suite() { return new TestSuite(IndentTest.class); } 86 87 88 public void testIndentComments() throws BadLocationException { 89 String text = 90 " foo();\n" + 91 " // foo\n" + 92 "/**\n" + 93 "\n" + 94 "* Comment\n" + 95 " * More comment\n" + 96 "code;\n" + 97 "* More comment\n" + 98 "\n" + 99 "*/\n" + 100 "\n"; 101 102 String indented = 103 " foo();\n" + " // foo\n" + " /**\n" + " * \n" + " * Comment\n" + " * More comment\n" + " code;\n" + " * More comment\n" + " * \n" + " */\n" + " \n"; 115 doc.insertString(0, text, null); 116 _assertContents(text, doc); 117 doc.indentLines(9, doc.getLength()); 118 _assertContents(indented, doc); 119 } 120 121 122 public void testMultiLineStarInsertFirstLine() throws BadLocationException { 123 String text = 124 "/**\n" + 125 "comments here blah blah\n" + 126 " */"; 127 128 String noStarAdded = 129 "/**\n" + 130 " comments here blah blah\n" + 131 " */"; 132 133 String starAdded = 134 "/**\n" + 135 " * comments here blah blah\n" + 136 " */"; 137 138 doc.insertString(0, text, null); 139 _assertContents(text, doc); 140 doc.gotoLine(2); 141 142 doc._indentLine(Indenter.OTHER); 143 _assertContents(noStarAdded, doc); 144 145 doc._indentLine(Indenter.ENTER_KEY_PRESS); 146 _assertContents(starAdded, doc); 147 } 148 149 150 public void testMultiLineStarInsertLaterLine() throws BadLocationException { 151 152 String text = 153 "/**\n" + 154 " * other comments\n" + 155 "comments here blah blah\n" + 156 " */"; 157 158 String noStarAdded = 159 "/**\n" + 160 " * other comments\n" + 161 " comments here blah blah\n" + 162 " */"; 163 164 String starAdded = 165 "/**\n" + 166 " * other comments\n" + 167 " * comments here blah blah\n" + 168 " */"; 169 170 doc.insertString(0, text, null); 171 _assertContents(text, doc); 172 doc.gotoLine(3); 173 174 doc._indentLine(Indenter.OTHER); 175 _assertContents(noStarAdded, doc); 176 177 doc._indentLine(Indenter.ENTER_KEY_PRESS); 178 _assertContents(starAdded, doc); 179 } 180 181 184 public void testIndentParenPhrases() throws BadLocationException { 185 String text = 186 "foo(i,\n" + 187 "j.\n" + 188 "bar().\n" + 189 "// foo();\n" + 190 "baz(),\n" + 191 "cond1 ||\n" + 192 "cond2);\n" + 193 "i = myArray[x *\n" + 194 "y.\n" + 195 "foo() +\n" + 196 "z\n" + 197 "];\n"; 198 199 String indented = 200 "foo(i,\n" + 201 " j.\n" + " bar().\n" + "// foo();\n" + " baz(),\n" + " cond1 ||\n" + " cond2);\n" + "i = myArray[x *\n" + " y.\n" + " foo() +\n" + " z\n" + " ];\n"; 213 doc.insertString(0, text, null); 214 _assertContents(text, doc); 215 doc.indentLines(0, doc.getLength()); 216 _assertContents(indented, doc); 217 } 218 219 222 public void testIndentBraces() throws BadLocationException { 223 String text = 224 "{\n" + 225 "class Foo\n" + 226 "extends F {\n" + 227 "int i; \n" + 228 "void foo() {\n" + 229 "if (true) {\n" + 230 "bar();\n" + 231 "}\n" + 232 "}\n" + 233 "/* comment */ }\n" + 234 "class Bar {\n" + 235 "/* comment\n" + 236 "*/ }\n" + 237 "int i;\n" + 238 "}\n"; 239 240 String indented = 241 "{\n" + 242 " class Foo\n" + " extends F {\n" + " int i; \n" + " void foo() {\n" + " if (true) {\n" + " bar();\n" + " }\n" + " }\n" + " /* comment */ }\n" + " class Bar {\n" + " /* comment\n" + " */ }\n" + " int i;\n" + "}\n"; 256 257 258 doc.insertString(0, text, null); 259 _assertContents(text, doc); 260 doc.indentLines(0, doc.getLength()); 261 _assertContents(indented, doc); 262 } 263 264 267 public void testIndentArray() throws BadLocationException { 268 String text = 269 "int[2][] a ={\n" + 270 "{\n" + 271 "1,\n" + 272 "2,\n" + 273 "3},\n" + 274 "{\n" + 275 "4,\n" + 276 "5}\n" + 277 "};\n"; 278 279 String indented = 280 "int[2][] a ={\n" + 281 " {\n" + 282 " 1,\n" + 283 " 2,\n" + 284 " 3},\n" + 285 " {\n" + 286 " 4,\n" + 287 " 5}\n" + 288 "};\n"; 289 290 291 292 293 doc.insertString(0, text, null); 294 _assertContents(text, doc); 295 doc.indentLines(0, doc.getLength()); 296 _assertContents(indented, doc); 297 } 298 299 302 public void testIndentCommonCases() throws BadLocationException { 303 String text = 304 "int x;\n" + 305 " int y;\n" + 306 " class Foo\n" + 307 " extends F\n" + 308 " {\n" + 309 " }"; 310 311 String indented = 312 "int x;\n" + 313 "int y;\n" + 314 "class Foo\n" + 315 " extends F\n" + 316 "{\n" + 317 "}"; 318 319 doc.insertString(0, text, null); 320 _assertContents(text, doc); 321 doc.indentLines(0, doc.getLength()); 322 _assertContents(indented, doc); 323 } 324 325 328 public void testIndentSwitch() throws BadLocationException { 329 String text = 330 "switch (x) {\n" + 331 "case 1:\n" + 332 "foo();\n" + 333 "break;\n" + 334 "case 2: case 3:\n" + 335 "case 4: case 5:\n" + 336 "bar();\n" + 337 "break;\n" + 338 "}\n"; 339 340 String indented = 341 "switch (x) {\n" + 342 " case 1:\n" + " foo();\n" + " break;\n" + " case 2: case 3:\n" + " case 4: case 5:\n" + " bar();\n" + " break;\n" + "}\n"; 351 352 doc.insertString(0, text, null); 353 _assertContents(text, doc); 354 doc.indentLines(0, doc.getLength()); 355 _assertContents(indented, doc); 356 } 357 358 361 public void testIndentTernary() throws BadLocationException { 362 String text = 363 "test1 = x ? y : z;\n" + 364 "test2 = x ? y :\n" + 365 "z;\n" + 366 "foo();\n" + 367 "test3 =\n" + 368 "x ?\n" + 369 "y :\n" + 370 "z;\n" + 371 "bar();\n" + 372 "test4 = (x ?\n" + 373 "y :\n" + 374 "z);\n"; 375 376 String indented = 377 "test1 = x ? y : z;\n" + "test2 = x ? y :\n" + " z;\n" + "foo();\n" + "test3 =\n" + " x ?\n" + " y :\n" + " z;\n" + "bar();\n" + "test4 = (x ?\n" + " y :\n" + " z);\n"; 390 391 doc.insertString(0, text, null); 392 _assertContents(text, doc); 393 doc.indentLines(0, doc.getLength()); 394 _assertContents(indented, doc); 395 } 396 397 401 public void testIndentInfoSquiggly() throws BadLocationException { 402 BraceReduction _reduced = doc.getReduced(); 404 IndentInfo ii = _reduced.getIndentInformation(); 405 _assertIndentInfo(ii, noBrace, -1, -1, -1); 406 doc.insertString(0, "\n", null); 408 _assertContents("\n", doc); 409 ii = _reduced.getIndentInformation(); 410 _assertIndentInfo(ii, noBrace, -1, -1, 0); 411 doc.insertString(0, "{\n\n", null); 413 _assertContents("{\n\n\n", doc); 415 ii = _reduced.getIndentInformation(); 416 _assertIndentInfo(ii, openSquiggly, -1, 3, 0); 417 doc.insertString(3, "{\n\n", null); 419 _assertContents("{\n\n{\n\n\n", doc); 421 ii = _reduced.getIndentInformation(); 422 _assertIndentInfo(ii, openSquiggly, 3, 3, 0); 423 doc.insertString(6, " {\n\n", null); 425 _assertContents("{\n\n{\n\n {\n\n\n", doc); 427 ii = _reduced.getIndentInformation(); 428 _assertIndentInfo(ii, openSquiggly, 5, 3, 0); 429 } 430 431 435 public void testIndentInfoParen() throws BadLocationException { 436 BraceReduction _reduced = doc.getReduced(); 438 doc.insertString(0, "\n(\n", null); 439 IndentInfo ii = _reduced.getIndentInformation(); 440 _assertIndentInfo(ii, openParen, 2, 2, 0); 441 doc.insertString(1, " helo ", null); 443 doc.move(2); 444 _assertContents("\n helo (\n", doc); 446 ii = _reduced.getIndentInformation(); 447 _assertIndentInfo(ii, openParen, 9, 2, 0); 448 doc.move(-1); 450 doc.insertString(9, " (", null); 451 doc.move(1); 452 _assertContents("\n helo ( (\n", doc); 454 ii = _reduced.getIndentInformation(); 455 _assertIndentInfo(ii, openParen, 11, 2, 0); 456 } 457 458 462 public void testIndentInfoBracket() throws BadLocationException { 463 BraceReduction _reduced = doc.getReduced(); 465 doc.insertString(0, "\n[\n", null); 466 IndentInfo ii = _reduced.getIndentInformation(); 467 _assertIndentInfo(ii, openBracket, 2, 2, 0); 468 doc.insertString(1, " helo ", null); 470 doc.move(2); 471 _assertContents("\n helo [\n", doc); 473 ii = _reduced.getIndentInformation(); 474 _assertIndentInfo(ii, openBracket, 9, 2, 0); 475 doc.move(-1); 477 doc.insertString(9, " [", null); 478 doc.move(1); 479 _assertContents("\n helo [ [\n", doc); 481 ii = _reduced.getIndentInformation(); 482 _assertIndentInfo(ii, openBracket, 11, 2, 0); 483 } 484 485 489 public void testIndentInfoPrevNewline () throws BadLocationException { 490 BraceReduction _reduced = doc.getReduced(); 491 doc.insertString(0, "{\n {\nhello", null); 492 IndentInfo ii = _reduced.getIndentInformation(); 494 _assertIndentInfo(ii, openSquiggly, 9, 7, 5); 495 } 496 497 501 public void testEndOfBlockComment () throws BadLocationException { 502 doc.insertString(0, "\n{\n hello;\n /*\n hello\n */", null); 503 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 504 _assertContents("\n{\n hello;\n /*\n hello\n */", doc); 505 } 506 507 511 public void testAfterBlockComment () throws BadLocationException { 512 doc.insertString(0, "\n{\n hello;\n /*\n hello\n */\nhello", null); 513 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 514 _assertContents("\n{\n hello;\n /*\n hello\n */\n hello", doc); 515 } 516 517 521 public void testAfterBlockComment3 () throws BadLocationException { 522 doc.insertString(0, "\n{\n hello;\n /*\n hello\n grr*/\nhello", null); 523 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 524 _assertContents("\n{\n hello;\n /*\n hello\n grr*/\n hello", doc); 525 } 526 527 531 public void testAfterBlockComment4 () throws BadLocationException { 532 doc.insertString(0, "\n{\n hello;\n /*\n hello\n */ hello", null); 533 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 534 _assertContents("\n{\n hello;\n /*\n hello\n */ hello", doc); 535 } 536 537 541 public void testAfterBlockComment2 () throws BadLocationException { 542 doc.insertString(0, "\n{\n hello;\n /*\n hello\n */ (\nhello", null); 543 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 544 _assertContents("\n{\n hello;\n /*\n hello\n */ (\n hello", doc); 545 } 546 547 551 public void testIndentInfoBlockComments () throws BadLocationException { 552 BraceReduction _reduced = doc.getReduced(); 553 doc.insertString(0, "(\n /*\n*\n", null); 554 _reduced.move(-1); 556 IndentInfo ii = _reduced.getIndentInformation(); 557 _assertIndentInfo(ii, openParen, -1, 7, 1); 558 } 559 560 564 public void testIndentInfoBlockComments2 () throws BadLocationException { 565 BraceReduction _reduced = doc.getReduced(); 566 doc.insertString(0, "\n(\n /*\n*\n", null); 567 _reduced.move(-1); 569 IndentInfo ii = _reduced.getIndentInformation(); 570 _assertIndentInfo(ii, openParen, 7, 7, 1); 571 } 572 573 577 public void testIndentInfoBlockComments3 () throws BadLocationException { 578 BraceReduction _reduced = doc.getReduced(); 579 doc.insertString(0, "{\n /*\n*\n", null); 580 _reduced.move(-1); 582 IndentInfo ii = _reduced.getIndentInformation(); 583 _assertIndentInfo(ii, openSquiggly, -1, 8, 1); 584 } 585 586 590 public void testIndentInfoBlockComments4 () throws BadLocationException { 591 BraceReduction _reduced = doc.getReduced(); 592 doc.insertString(0, "\n{\n /*\n*\n", null); 593 _reduced.move(-1); 595 IndentInfo ii = _reduced.getIndentInformation(); 596 _assertIndentInfo(ii, openSquiggly, 8, 8, 1); 597 } 598 599 603 public void testSkippingBraces () throws BadLocationException { 604 BraceReduction _reduced = doc.getReduced(); 605 doc.insertString(0, "\n{\n { ()}\n}", null); 606 IndentInfo ii = _reduced.getIndentInformation(); 607 _assertIndentInfo(ii, openSquiggly, 12, 12, 1); 608 } 609 610 614 public void testSkippingComments () throws BadLocationException { 615 BraceReduction _reduced = doc.getReduced(); 617 doc.insertString(0, "\n{\n //{ ()\n}", null); 618 IndentInfo ii = _reduced.getIndentInformation(); 619 _assertIndentInfo(ii, openSquiggly, 13, 13, 1); 620 } 621 622 626 public void testSkippingCommentsBraceAtBeginning () throws BadLocationException { 627 BraceReduction _reduced = doc.getReduced(); 629 doc.insertString(0, "{\n //{ ()}{", null); 630 IndentInfo ii = _reduced.getIndentInformation(); 631 _assertIndentInfo(ii, openSquiggly, -1, 13, 11); 632 } 633 634 638 public void testNothingToIndentOn () throws BadLocationException { 639 BraceReduction _reduced = doc.getReduced(); 641 doc.insertString(0, " //{ ()}{", null); 642 IndentInfo ii = _reduced.getIndentInformation(); 643 _assertIndentInfo(ii, noBrace, -1, -1, -1); 644 } 645 646 650 public void testStartSimple () throws BadLocationException { 651 doc.insertString(0, "abcde", null); 653 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 654 _assertContents("abcde", doc); 655 } 656 657 661 public void testStartSpaceIndent () throws BadLocationException { 662 doc.insertString(0, " abcde", null); 664 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 665 _assertContents("abcde", doc); 666 } 667 668 672 public void testStartBrace () throws BadLocationException { 673 doc.insertString(0, "public class temp \n {", null); 675 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 676 _assertContents("public class temp \n{", doc); 677 } 678 679 683 public void testEndBrace () throws BadLocationException { 684 doc.insertString(0, "public class temp \n{ \n }", null); 686 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 687 _assertContents("public class temp \n{ \n}", doc); 688 } 689 690 694 public void testInsideClass () throws BadLocationException { 695 doc.insertString(0, "public class temp \n{ \ntext here", null); 697 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 698 _assertContents("public class temp \n{ \n text here", doc); 699 } 700 701 705 public void testInsideClassWithBraceSets () throws BadLocationException { 706 doc.insertString(0, "public class temp \n{ ()\ntext here", null); 708 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 709 _assertContents("public class temp \n{ ()\n text here", doc); 710 } 711 712 716 public void testIgnoreBraceOnSameLine () throws BadLocationException { 717 doc.insertString(0, "public class temp \n{ ()\n{text here", null); 719 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 720 _assertContents("public class temp \n{ ()\n {text here", doc); 721 } 722 723 733 734 738 public void testWeird () throws BadLocationException { 739 doc.insertString(0, "hello\n", null); 741 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 742 _assertContents("hello\n ", doc); 743 } 744 745 749 public void testWierd2 () throws BadLocationException { 750 doc.insertString(0, "hello", null); 752 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 753 _assertContents("hello", doc); 754 } 755 756 760 public void testMotion () throws BadLocationException { 761 doc.insertString(0, "hes{\n{abcde", null); 763 doc.insertString(11, "\n{", null); 764 doc.move(-8); 766 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 768 _assertContents("hes{\n {abcde\n{", doc); 770 } 771 772 776 public void testNextCharIsNewline () throws BadLocationException { 777 doc.insertString(0, "hes{\n{abcde", null); 779 doc.insertString(11, "\n{", null); 780 doc.move(-2); 782 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 784 _assertContents("hes{\n {abcde\n{", doc); 786 } 787 788 792 public void testFor () throws BadLocationException { 793 doc.insertString(0, "for(;;)\n", null); 795 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 796 _assertContents("for(;;)\n ", doc); 797 } 798 799 803 public void testFor2 () throws BadLocationException { 804 doc.insertString(0, "{\n for(;;)\n", null); 806 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 807 _assertContents("{\n for(;;)\n ", doc); 808 } 809 810 814 public void testOpenParen () throws BadLocationException { 815 doc.insertString(0, "hello(\n", null); 817 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 818 _assertContents("hello(\n ", doc); 819 } 820 821 825 public void testPrintString () throws BadLocationException { 826 doc.insertString(0, "Sys.out(\"hello\"\n", null); 828 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 829 _assertContents("Sys.out(\"hello\"\n ", doc); 830 } 831 832 836 public void testOpenBracket () throws BadLocationException { 837 doc.insertString(0, "hello[\n", null); 839 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 840 _assertContents("hello[\n ", doc); 841 } 842 843 847 public void testSquigglyAlignment () throws BadLocationException { 848 doc.insertString(0, "{\n }", null); 850 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 851 _assertContents("{\n}", doc); 852 } 853 854 858 public void testSpaceBrace () throws BadLocationException { 859 doc.insertString(0, " {\n", null); 861 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 862 _assertContents(" {\n ", doc); 863 } 864 865 875 876 886 887 891 public void testEnter () throws BadLocationException { 892 doc.insertString(0, "\n\n", null); 894 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 895 _assertContents("\n\n", doc); 896 } 897 898 902 public void testEnter2 () throws BadLocationException { 903 doc.insertString(0, "\n", null); 905 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 906 _assertContents("\n", doc); 907 } 908 909 913 public void testNotRecognizeComments () throws BadLocationException { 914 doc.insertString(0, "\nhello //bal;\n", null); 916 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 917 _assertContents("\nhello //bal;\n ", doc); 918 } 919 920 924 public void testNotRecognizeComments2 () throws BadLocationException { 925 doc.insertString(0, "\nhello; /*bal*/\n ", null); 927 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 928 _assertContents("\nhello; /*bal*/\n", doc); 929 } 930 931 935 public void testBlockIndent () throws BadLocationException { 936 doc.insertString(0, "hello\n{\n{\n {", null); 938 doc.indentLines(8, 13); 939 _assertContents("hello\n{\n {\n {", doc); 940 } 941 942 948 public void testBlockIndent2 () throws BadLocationException { 949 doc.insertString(0, " x;\n y;\n", null); 950 doc.indentLines(0, doc.getLength()); 951 _assertContents("x;\ny;\n", doc); 952 } 953 954 958 public void testIndentInsideCommentBlock () throws BadLocationException { 959 doc.insertString(0, "hello\n{\n/*{\n{\n*/\nhehe", null); 960 doc.indentLines(0, 21); 961 _assertContents("hello\n{\n /*{\n {\n */\n hehe", doc); 962 } 963 964 968 public void testSecondLineProblem () throws BadLocationException { 969 doc.insertString(0, "\n", null); 971 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 972 _assertContents("\n", doc); 973 } 974 975 979 public void testSecondLineProblem2 () throws BadLocationException { 980 doc.insertString(0, "a\n", null); 982 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 983 _assertContents("a\n ", doc); 984 } 985 986 990 public void testSmallFileProblem () throws BadLocationException { 991 doc.insertString(0, "\n\n", null); 993 doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation()); 994 _assertContents("\n\n", doc); 995 } 996 997 1000 public void testAnonymousInnerClass() throws BadLocationException { 1001 String text = 1002 "addWindowListener(new WindowAdapter() {\n" + 1003 "public void windowClosing(WindowEvent e) {\n" + 1004 "dispose();\n" + 1005 "}\n" + 1006 "void x() {\n" + 1007 "\n" + 1008 "}\n" + 1009 "\n" + 1010 "}\n" + 1011 ");\n"; 1012 String indented = 1013 "addWindowListener(new WindowAdapter() {\n" + 1014 " public void windowClosing(WindowEvent e) {\n" + 1015 " dispose();\n" + 1016 " }\n" + 1017 " void x() {\n" + 1018 " \n" + 1019 " }\n" + 1020 " \n" + 1021 "}\n" + 1022 ");\n"; 1023 1024 1025 doc.insertString(0, text, null); 1026 _assertContents(text, doc); 1027 doc.indentLines(0, doc.getLength()); 1028 _assertContents(indented, doc); 1029 } 1030 1031 1032 1112 public void testLiveUpdateOfIndentLevel() throws BadLocationException { 1113 1114 String text = 1115 "int[2][] a ={\n" + 1116 "{\n" + 1117 "1,\n" + 1118 "2,\n" + 1119 "3},\n" + 1120 "{\n" + 1121 "4,\n" + 1122 "5}\n" + 1123 "};\n"; 1124 1125 String indentedBefore = 1126 "int[2][] a ={\n" + 1127 " {\n" + 1128 " 1,\n" + 1129 " 2,\n" + 1130 " 3},\n" + 1131 " {\n" + 1132 " 4,\n" + 1133 " 5}\n" + 1134 "};\n"; 1135 1136 String indentedAfter = 1137 "int[2][] a ={\n" + 1138 " {\n" + 1139 " 1,\n" + 1140 " 2,\n" + 1141 " 3},\n" + 1142 " {\n" + 1143 " 4,\n" + 1144 " 5}\n" + 1145 "};\n"; 1146 1147 doc.insertString(0, text, null); 1148 1149 _assertContents(text, doc); 1150 doc.indentLines(0, doc.getLength()); 1151 _assertContents(indentedBefore, doc); 1152 DrJava.getConfig().setSetting(OptionConstants.INDENT_LEVEL, new Integer (8)); 1153 1154 Utilities.clearEventQueue(); 1155 doc.indentLines(0, doc.getLength()); 1156 _assertContents(indentedAfter, doc); 1157 } 1158 1159 1163 public void testNestedIfInSwitch() throws BadLocationException { 1164 String text = 1165 "switch(cond) {\n" + 1166 "case 1:\n" + 1167 "object.doStuff();\n" + 1168 "if(object.hasDoneStuff()) {\n" + 1169 "thingy.doOtherStuff();\n" + 1170 "lion.roar(\"raaargh\");\n" + 1171 "}\n" + 1172 "break;\n" + 1173 "}\n"; 1174 1175 String indented = 1176 "switch(cond) {\n" + 1177 " case 1:\n" + 1178 " object.doStuff();\n" + 1179 " if(object.hasDoneStuff()) {\n" + 1180 " thingy.doOtherStuff();\n" + 1181 " lion.roar(\"raaargh\");\n" + 1182 " }\n" + 1183 " break;\n" + 1184 "}\n"; 1185 1186 doc.insertString(0, text, null); 1187 _assertContents(text, doc); 1188 doc.indentLines(0, doc.getLength()); 1189 _assertContents(indented, doc); 1190 } 1191 1192 1221 public void testIndentingCorrectLine() throws BadLocationException { 1222 String test1 = 1223 "class A {\n" + 1224 " int a = 5;\n" + 1225 " }"; 1226 1227 String test1Correct = 1228 "class A {\n" + 1229 " int a = 5;\n" + 1230 "}"; 1231 1232 String test2 = 1233 " {\n" + 1234 " int a = 5;\n" + 1235 " }\n"; 1236 1237 String test2Correct = 1238 "{\n" + 1239 " int a = 5;\n" + 1240 " }\n"; 1241 1242 doc.insertString(0, test1, null); 1243 _assertContents(test1, doc); 1244 doc.setCurrentLocation(20); 1245 doc.indentLines(20,20); 1246 _assertContents(test1, doc); 1249 1250 doc = new DefinitionsDocument(_notifier); 1251 1252 doc.insertString(0, test1, null); 1253 _assertContents(test1, doc); 1254 doc.indentLines(28,28); 1255 _assertContents(test1Correct, doc); 1256 1257 doc = new DefinitionsDocument(_notifier); 1258 1259 doc.insertString(0, test2, null); 1260 _assertContents(test2, doc); 1261 doc.setCurrentLocation(5); 1262 doc.indentLines(5,5); 1263 _assertContents(test2Correct, doc); 1264 } 1265 1266 1272 1301 private void _assertContents(String expected, DJDocument document) throws BadLocationException { 1302 assertEquals("document contents", expected, document.getText()); 1303 } 1304 1305 private void _assertIndentInfo(IndentInfo ii, String braceType, int distToNewline, int distToBrace, int distToPrevNewline) { 1306 assertEquals("indent info: brace type", braceType, ii.braceType); 1307 assertEquals("indent info: dist to new line", distToNewline, ii.distToNewline); 1308 assertEquals("indent info: dist to brace", distToBrace, ii.distToBrace); 1309 assertEquals("indent info: dist to prev new line", distToPrevNewline, ii.distToPrevNewline); 1310 } 1311 1312 1320 1345 1372 1397 1422 1447 1472 1497} 1498 | Popular Tags |