Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2011, 2012 IBM Corporation and others. |
2 |
* Copyright (c) 2011, 2013 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 545-548
Link Here
|
545 |
assertExpectedExistInProposals(proposals, new String[] {expected1}); |
545 |
assertExpectedExistInProposals(proposals, new String[] {expected1}); |
546 |
} |
546 |
} |
547 |
|
547 |
|
|
|
548 |
public void testConvertToIfReturn1() throws Exception { |
549 |
// positive cases |
550 |
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); |
551 |
StringBuffer buf= new StringBuffer(); |
552 |
buf.append("package test1;\n"); |
553 |
buf.append("public class E {\n"); |
554 |
buf.append(" public void foo1() {\n"); |
555 |
buf.append(" if (a) {\n"); |
556 |
buf.append(" System.out.println(\"1\");\n"); |
557 |
buf.append(" System.out.println(\"11\");\n"); |
558 |
buf.append(" }\n"); |
559 |
buf.append(" }\n\n"); |
560 |
buf.append(" public void foo2() {\n"); |
561 |
buf.append(" bar();\n"); |
562 |
buf.append(" if (b) {\n"); |
563 |
buf.append(" System.out.println(\"2\");\n"); |
564 |
buf.append(" System.out.println(\"22\");\n"); |
565 |
buf.append(" }\n"); |
566 |
buf.append(" }\n\n"); |
567 |
buf.append(" public void foo3() {\n"); |
568 |
buf.append(" if (c) {\n"); |
569 |
buf.append(" if (d) {\n"); |
570 |
buf.append(" System.out.println(\"3\");\n"); |
571 |
buf.append(" System.out.println(\"33\");\n"); |
572 |
buf.append(" }\n"); |
573 |
buf.append(" }\n"); |
574 |
buf.append(" }\n"); |
575 |
buf.append("}\n"); |
576 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
577 |
|
578 |
String str= "if (a)"; |
579 |
AssistContext context= getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0); |
580 |
List proposals= collectAssists(context, false); |
581 |
assertCorrectLabels(proposals); |
582 |
StringBuffer buf1= new StringBuffer(); |
583 |
buf1.append("package test1;\n"); |
584 |
buf1.append("public class E {\n"); |
585 |
buf1.append(" public void foo1() {\n"); |
586 |
buf1.append(" if (!a)\n"); |
587 |
buf1.append(" return;\n"); |
588 |
buf1.append(" System.out.println(\"1\");\n"); |
589 |
buf1.append(" System.out.println(\"11\");\n"); |
590 |
buf1.append(" }\n\n"); |
591 |
buf1.append(" public void foo2() {\n"); |
592 |
buf1.append(" bar();\n"); |
593 |
buf1.append(" if (b) {\n"); |
594 |
buf1.append(" System.out.println(\"2\");\n"); |
595 |
buf1.append(" System.out.println(\"22\");\n"); |
596 |
buf1.append(" }\n"); |
597 |
buf1.append(" }\n\n"); |
598 |
buf1.append(" public void foo3() {\n"); |
599 |
buf1.append(" if (c) {\n"); |
600 |
buf1.append(" if (d) {\n"); |
601 |
buf1.append(" System.out.println(\"3\");\n"); |
602 |
buf1.append(" System.out.println(\"33\");\n"); |
603 |
buf1.append(" }\n"); |
604 |
buf1.append(" }\n"); |
605 |
buf1.append(" }\n"); |
606 |
buf1.append("}\n"); |
607 |
String expected1= buf1.toString(); |
608 |
assertExpectedExistInProposals(proposals, new String[] { expected1 }); |
609 |
|
610 |
str= "if (b)"; |
611 |
context= getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0); |
612 |
proposals= collectAssists(context, false); |
613 |
assertCorrectLabels(proposals); |
614 |
buf1= new StringBuffer(); |
615 |
buf1.append("package test1;\n"); |
616 |
buf1.append("public class E {\n"); |
617 |
buf1.append(" public void foo1() {\n"); |
618 |
buf1.append(" if (a) {\n"); |
619 |
buf1.append(" System.out.println(\"1\");\n"); |
620 |
buf1.append(" System.out.println(\"11\");\n"); |
621 |
buf1.append(" }\n"); |
622 |
buf1.append(" }\n\n"); |
623 |
buf1.append(" public void foo2() {\n"); |
624 |
buf1.append(" bar();\n"); |
625 |
buf1.append(" if (!b)\n"); |
626 |
buf1.append(" return;\n"); |
627 |
buf1.append(" System.out.println(\"2\");\n"); |
628 |
buf1.append(" System.out.println(\"22\");\n"); |
629 |
buf1.append(" }\n\n"); |
630 |
buf1.append(" public void foo3() {\n"); |
631 |
buf1.append(" if (c) {\n"); |
632 |
buf1.append(" if (d) {\n"); |
633 |
buf1.append(" System.out.println(\"3\");\n"); |
634 |
buf1.append(" System.out.println(\"33\");\n"); |
635 |
buf1.append(" }\n"); |
636 |
buf1.append(" }\n"); |
637 |
buf1.append(" }\n"); |
638 |
buf1.append("}\n"); |
639 |
String expected2= buf1.toString(); |
640 |
assertExpectedExistInProposals(proposals, new String[] { expected2 }); |
641 |
|
642 |
str= "if (d)"; |
643 |
context= getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0); |
644 |
proposals= collectAssists(context, false); |
645 |
assertCorrectLabels(proposals); |
646 |
buf1= new StringBuffer(); |
647 |
buf1.append("package test1;\n"); |
648 |
buf1.append("public class E {\n"); |
649 |
buf1.append(" public void foo1() {\n"); |
650 |
buf1.append(" if (a) {\n"); |
651 |
buf1.append(" System.out.println(\"1\");\n"); |
652 |
buf1.append(" System.out.println(\"11\");\n"); |
653 |
buf1.append(" }\n"); |
654 |
buf1.append(" }\n\n"); |
655 |
buf1.append(" public void foo2() {\n"); |
656 |
buf1.append(" bar();\n"); |
657 |
buf1.append(" if (b) {\n"); |
658 |
buf1.append(" System.out.println(\"2\");\n"); |
659 |
buf1.append(" System.out.println(\"22\");\n"); |
660 |
buf1.append(" }\n"); |
661 |
buf1.append(" }\n\n"); |
662 |
buf1.append(" public void foo3() {\n"); |
663 |
buf1.append(" if (c) {\n"); |
664 |
buf1.append(" if (!d)\n"); |
665 |
buf1.append(" return;\n"); |
666 |
buf1.append(" System.out.println(\"3\");\n"); |
667 |
buf1.append(" System.out.println(\"33\");\n"); |
668 |
buf1.append(" }\n"); |
669 |
buf1.append(" }\n"); |
670 |
buf1.append("}\n"); |
671 |
String expected3= buf1.toString(); |
672 |
assertExpectedExistInProposals(proposals, new String[] { expected3 }); |
673 |
} |
674 |
|
675 |
public void testConvertToIfReturn2() throws Exception { |
676 |
// negative cases |
677 |
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); |
678 |
StringBuffer buf= new StringBuffer(); |
679 |
buf.append("package test1;\n"); |
680 |
buf.append("public class E {\n"); |
681 |
buf.append(" public void foo1() {\n"); |
682 |
buf.append(" if (true) {\n"); |
683 |
buf.append(" System.out.println(\"1\");\n"); |
684 |
buf.append(" System.out.println(\"2\");\n"); |
685 |
buf.append(" }\n"); |
686 |
buf.append(" bar();"); |
687 |
buf.append(" }\n\n"); |
688 |
buf.append(" public void foo2() {\n"); |
689 |
buf.append(" if (a) \n"); |
690 |
buf.append(" if (b) {\n"); |
691 |
buf.append(" System.out.println(\"1\");\n"); |
692 |
buf.append(" System.out.println(\"2\");\n"); |
693 |
buf.append(" }\n"); |
694 |
buf.append(" }\n\n"); |
695 |
buf.append(" public void foo3() {\n"); |
696 |
buf.append(" if (c) {\n"); |
697 |
buf.append(" return;\n"); |
698 |
buf.append(" }\n"); |
699 |
buf.append(" }\n"); |
700 |
buf.append("}\n"); |
701 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
702 |
|
703 |
String str= "if (true)"; // not the last executable statement in the method |
704 |
AssistContext context= getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0); |
705 |
List proposals= collectAssists(context, false); |
706 |
assertCorrectLabels(proposals); |
707 |
assertProposalDoesNotExist(proposals, CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn); |
708 |
|
709 |
str= "if (b)"; // not present in a block |
710 |
context= getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0); |
711 |
proposals= collectAssists(context, false); |
712 |
assertCorrectLabels(proposals); |
713 |
assertProposalDoesNotExist(proposals, CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn); |
714 |
|
715 |
str= "if (c)"; // no other statement in 'then' part other than 'return' |
716 |
context= getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0); |
717 |
proposals= collectAssists(context, false); |
718 |
assertCorrectLabels(proposals); |
719 |
assertProposalDoesNotExist(proposals, CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn); |
720 |
} |
721 |
|
722 |
public void testConvertToIfReturn3() throws Exception { |
723 |
// 'if' should be in a 'method' returning 'void' |
724 |
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); |
725 |
StringBuffer buf= new StringBuffer(); |
726 |
buf.append("package test1;\n"); |
727 |
buf.append("public class E {\n"); |
728 |
buf.append(" static {\n"); |
729 |
buf.append(" if (a) {\n"); |
730 |
buf.append(" System.out.println(\"1\");\n"); |
731 |
buf.append(" }\n"); |
732 |
buf.append(" }\n"); |
733 |
buf.append(" public String foo1() {\n"); |
734 |
buf.append(" if (b) {\n"); |
735 |
buf.append(" System.out.println(\"1\");\n"); |
736 |
buf.append(" return \"foo\"\n"); |
737 |
buf.append(" }\n"); |
738 |
buf.append(" }\n\n"); |
739 |
|
740 |
buf.append("}\n"); |
741 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
742 |
|
743 |
String str= "if (a)"; // not in a method |
744 |
AssistContext context= getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0); |
745 |
List proposals= collectAssists(context, false); |
746 |
assertCorrectLabels(proposals); |
747 |
assertProposalDoesNotExist(proposals, CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn); |
748 |
|
749 |
str= "if (b)"; // method does not return 'void' |
750 |
context= getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0); |
751 |
proposals= collectAssists(context, false); |
752 |
assertCorrectLabels(proposals); |
753 |
assertProposalDoesNotExist(proposals, CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn); |
754 |
} |
755 |
|
756 |
public void testConvertToIfReturn4() throws Exception { |
757 |
// 'if' should not be in a loop |
758 |
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); |
759 |
StringBuffer buf= new StringBuffer(); |
760 |
buf.append("package test1;\n"); |
761 |
buf.append("public class E {\n"); |
762 |
buf.append(" public void foo1() {\n"); |
763 |
buf.append(" for (int i; i < 3; i++) {\n"); |
764 |
buf.append(" if (a) {\n"); |
765 |
buf.append(" System.out.println(\"1\");\n"); |
766 |
buf.append(" }\n"); |
767 |
buf.append(" }\n"); |
768 |
buf.append(" }\n\n"); |
769 |
buf.append(" public void foo2() {\n"); |
770 |
buf.append(" List<String> strs= new ArrayList<String>;\n"); |
771 |
buf.append(" for (String s : strs) {\n"); |
772 |
buf.append(" if (b) {\n"); |
773 |
buf.append(" System.out.println(\"2\");\n"); |
774 |
buf.append(" }\n"); |
775 |
buf.append(" }\n"); |
776 |
buf.append(" }\n\n"); |
777 |
buf.append(" public void foo3() {\n"); |
778 |
buf.append(" do {\n"); |
779 |
buf.append(" if (c) {\n"); |
780 |
buf.append(" System.out.println(\"3\");\n"); |
781 |
buf.append(" }\n"); |
782 |
buf.append(" } while (true)\n"); |
783 |
buf.append(" }\n\n"); |
784 |
buf.append(" public void foo4() {\n"); |
785 |
buf.append(" while (true) {\n"); |
786 |
buf.append(" if (d) {\n"); |
787 |
buf.append(" System.out.println(\"4\");\n"); |
788 |
buf.append(" }\n"); |
789 |
buf.append(" }\n"); |
790 |
buf.append(" }\n"); |
791 |
buf.append("}\n"); |
792 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
793 |
|
794 |
String str= "if (a)"; |
795 |
AssistContext context= getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0); |
796 |
List proposals= collectAssists(context, false); |
797 |
assertCorrectLabels(proposals); |
798 |
assertProposalDoesNotExist(proposals, CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn); |
799 |
|
800 |
str= "if (b)"; |
801 |
context= getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0); |
802 |
proposals= collectAssists(context, false); |
803 |
assertCorrectLabels(proposals); |
804 |
assertProposalDoesNotExist(proposals, CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn); |
805 |
|
806 |
str= "if (c)"; |
807 |
context= getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0); |
808 |
proposals= collectAssists(context, false); |
809 |
assertCorrectLabels(proposals); |
810 |
assertProposalDoesNotExist(proposals, CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn); |
811 |
|
812 |
str= "if (d)"; |
813 |
context= getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0); |
814 |
proposals= collectAssists(context, false); |
815 |
assertCorrectLabels(proposals); |
816 |
assertProposalDoesNotExist(proposals, CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn); |
817 |
} |
548 |
} |
818 |
} |