Index: src/org/eclipse/jdt/text/tests/JdtTextTestSuite.java =================================================================== RCS file: /home/eclipse/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/JdtTextTestSuite.java,v retrieving revision 1.15 diff -u -r1.15 JdtTextTestSuite.java --- src/org/eclipse/jdt/text/tests/JdtTextTestSuite.java 25 Feb 2005 15:13:08 -0000 1.15 +++ src/org/eclipse/jdt/text/tests/JdtTextTestSuite.java 25 Aug 2005 04:51:35 -0000 @@ -50,6 +50,7 @@ suite.addTest(AutoboxingSemanticHighlightingTest.suite()); suite.addTest(NewForLoopJavaContextTest.suite()); suite.addTest(JavaDoubleClickSelectorTest.suite()); + suite.addTest(BreakContinueTargetFinderTest.suite()); //$JUnit-END$ return suite; Index: src/org/eclipse/jdt/text/tests/BreakContinueTargetFinderTest.java =================================================================== RCS file: src/org/eclipse/jdt/text/tests/BreakContinueTargetFinderTest.java diff -N src/org/eclipse/jdt/text/tests/BreakContinueTargetFinderTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/jdt/text/tests/BreakContinueTargetFinderTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,276 @@ +package org.eclipse.jdt.text.tests; + +import java.util.List; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTParser; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.internal.corext.dom.ASTNodes; +import org.eclipse.jdt.internal.ui.search.BreakContinueTargetFinder; + +/** + * Tests for the BreakContinueTargerFinder class. + */ +public class BreakContinueTargetFinderTest extends TestCase{ + + public static Test suite() { + return new TestSuite(BreakContinueTargetFinderTest.class); + } + + private ASTParser fParser; + private BreakContinueTargetFinder fFinder; + protected void setUp() throws Exception { + fParser = ASTParser.newParser(AST.JLS3); + fFinder= new BreakContinueTargetFinder(); + } + + private ASTNode getFoundTarget(StringBuffer source, int offset, int length) { + fParser.setSource(source.toString().toCharArray()); + CompilationUnit root = (CompilationUnit) fParser.createAST(null); + String string = fFinder.initialize(root, offset, length, source.toString()); + assertNull(string, string); + List selections = fFinder.perform(); + assertEquals("number of selections", 1, selections.size()); + return (ASTNode) selections.get(0); + } + + private void checkSelection(StringBuffer s, int offset, int length, String expected) { + ASTNode selected = getFoundTarget(s, offset, length); + String selectedSource = s.substring(selected.getStartPosition(), ASTNodes.getExclusiveEnd(selected)); + assertEquals(expected, selectedSource); + } + + public void testBreakFor() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(int[] xs){\n"); + s.append(" for (int i = 0; i < xs.length; i++) {\n"); + s.append(" break;"); + s.append(" }\n"); + s.append(" }\n"); + s.append("}\n"); + int offset= 1 + s.indexOf("break");//middle of word + int length= 0; + checkSelection(s, offset, length, "for"); + } + + public void testBreakForeach() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(int[] xs){\n"); + s.append(" for (int i : xs){\n"); + s.append(" break;"); + s.append(" }\n"); + s.append(" }\n"); + s.append("}\n"); + int offset= 1 + s.indexOf("break");//middle of word + int length= 0; + checkSelection(s, offset, length, "for"); + } + + public void testBreakWhile() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(boolean b){\n"); + s.append(" while (b) {\n"); + s.append(" System.err.println(b);\n"); + s.append(" break;\n"); + s.append(" }\n"); + s.append(" }\n"); + s.append("}"); + int offset= 1 + s.indexOf("break");//middle of word + int length= 0; + checkSelection(s, offset, length, "while"); + } + + public void testBreakDo() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(boolean b){\n"); + s.append(" do {\n"); + s.append(" System.err.println(b);\n"); + s.append(" break;\n"); + s.append(" } while(b);\n"); + s.append(" }\n"); + s.append("}"); + int offset= 1 + s.indexOf("break");//middle of word + int length= 0; + checkSelection(s, offset, length, "do"); + } + + public void testBreakSwitch() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(int i){\n"); + s.append(" switch (i){\n"); + s.append(" case 1: System.err.println(i); break;\n"); + s.append(" default:System.out.println(i);\n"); + s.append(" }\n"); + s.append(" }\n"); + s.append("}\n"); + int offset= 1 + s.indexOf("break");//middle of word + int length= 2; + checkSelection(s, offset, length, "switch"); + } + + public void testLabeledBreakFor() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(int[] xs){\n"); + s.append(" bar: for (int i = 0; i < xs.length; i++) {\n"); + s.append(" do{\n"); + s.append(" break bar;"); + s.append(" }while (xs != null);\n"); + s.append(" }\n"); + s.append(" }\n"); + s.append("}\n"); + int offset= 1 + s.indexOf("break");//middle of word + int length= 0; + checkSelection(s, offset, length, "bar"); + } + + public void testLabeledBreakFor1() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(int[] xs){\n"); + s.append(" bar: for (int i = 0; i < xs.length; i++) {\n"); + s.append(" baz: do{\n"); + s.append(" break bar;"); + s.append(" }while (xs != null);\n"); + s.append(" }\n"); + s.append(" }\n"); + s.append("}\n"); + int offset= 1 + s.indexOf("break");//middle of word + int length= 0; + checkSelection(s, offset, length, "bar"); + } + + public void testContinueFor() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(int[] xs){\n"); + s.append(" for (int i = 0; i < xs.length; i++) {\n"); + s.append(" continue;"); + s.append(" }\n"); + s.append(" }\n"); + s.append("}\n"); + int offset= 1 + s.indexOf("continue");//middle of word + int length= 0; + checkSelection(s, offset, length, "for"); + } + + public void testContinueForeach() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(int[] xs){\n"); + s.append(" for (int i : xs){\n"); + s.append(" continue;"); + s.append(" }\n"); + s.append(" }\n"); + s.append("}\n"); + int offset= 1 + s.indexOf("continue");//middle of word + int length= 0; + checkSelection(s, offset, length, "for"); + } + + public void testContinueWhile() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(boolean b){\n"); + s.append(" while (b) {\n"); + s.append(" System.err.println(b);\n"); + s.append(" continue;\n"); + s.append(" }\n"); + s.append(" }\n"); + s.append("}"); + int offset= 1 + s.indexOf("continue");//middle of word + int length= 0; + checkSelection(s, offset, length, "while"); + } + + public void testContinueDo() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(boolean b){\n"); + s.append(" do {\n"); + s.append(" System.err.println(b);\n"); + s.append(" continue;\n"); + s.append(" } while(b);\n"); + s.append(" }\n"); + s.append("}"); + int offset= 1 + s.indexOf("continue");//middle of word + int length= 0; + checkSelection(s, offset, length, "do"); + } + + //continue skips over switches + public void testContinueSwitch() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(int i){\n"); + s.append(" do{\n"); + s.append(" switch (i){\n"); + s.append(" case 1: System.err.println(i); continue;\n"); + s.append(" default:System.out.println(i);\n"); + s.append(" }\n"); + s.append(" }while(i != 9);\n"); + s.append(" }\n"); + s.append("}\n"); + int offset= 1 + s.indexOf("continue");//middle of word + int length= 2; + checkSelection(s, offset, length, "do"); + } + + public void testLabeledContinueFor() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(int[] xs){\n"); + s.append(" bar: for (int i = 0; i < xs.length; i++) {\n"); + s.append(" do{\n"); + s.append(" continue bar;"); + s.append(" }while (xs != null);\n"); + s.append(" }\n"); + s.append(" }\n"); + s.append("}\n"); + int offset= 1 + s.indexOf("continue");//middle of word + int length= 0; + checkSelection(s, offset, length, "bar"); + } + + public void testLabeledContinueFor1() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(int[] xs){\n"); + s.append(" bar: for (int i = 0; i < xs.length; i++) {\n"); + s.append(" baz: do{\n"); + s.append(" continue bar;"); + s.append(" }while (xs != null);\n"); + s.append(" }\n"); + s.append(" }\n"); + s.append("}\n"); + int offset= 1 + s.indexOf("continue");//middle of word + int length= 0; + checkSelection(s, offset, length, "bar"); + } + + public void testLabeledContinueFor2() throws Exception { + StringBuffer s= new StringBuffer(); + s.append("class A{\n"); + s.append(" void foo(int[] xs){\n"); + s.append(" bar: for (int i = 0; i < xs.length; i++) {\n"); + s.append(" baz: do{\n"); + s.append(" continue bar;"); + s.append(" }while (xs != null);\n"); + s.append(" }\n"); + s.append(" }\n"); + s.append("}\n"); + int offset= s.indexOf("continue bar;") + 1+ "continue ".length();//middle of label reference + int length= 0; + checkSelection(s, offset, length, "bar"); + } +}