diff --git a/core/org.eclipse.cdt.ui.tests/resources/folding/FoldingTest.cpp b/core/org.eclipse.cdt.ui.tests/resources/folding/FoldingTest.cpp new file mode 100644 index 00000000000..642e31f16f5 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/folding/FoldingTest.cpp @@ -0,0 +1,68 @@ +/* + * header comment + */ + +#define MULTI_LINE_MACRO(x) \ + if (DBG) { \ + printf(x); \ + } + +#if 0 +# if 1 + // +# endif +#elif X +// X +#else +# if 1 +# if 0 +# if 1 + // +# endif +# else +# if 1 + // +# endif +# endif +# endif +#endif + +/* + * comment + */ +int y; + +#if 1 +int func() { +#else +int func2() { +#endif + return 0; +} + +// multiple single line comments +// multiple single line comments +// multiple single line comments +// multiple single line comments +// multiple single line comments + +class Class { +public: + int pubField; + static int staticPubMethod(int arg) { + return 0; + } + int pubMethod(); +}; + +int Class::pubMethod() { + return pubField; +} + +struct CppStruct { + int structField; +}; + +union CppUnion { + int unionField; +}; diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/AbstractSemanticHighlightingTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/AbstractSemanticHighlightingTest.java index 0bdb6580298..36eaf41a621 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/AbstractSemanticHighlightingTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/AbstractSemanticHighlightingTest.java @@ -61,13 +61,14 @@ public class AbstractSemanticHighlightingTest extends TestCase { } protected String getTestFilename() { - return "/SHTest/src/SHTest.cpp"; + return fTestFilename; } protected void tearDown () throws Exception { EditorTestHelper.closeEditor(fEditor); IPreferenceStore store= CUIPlugin.getDefault().getPreferenceStore(); + store.setToDefault(PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_ENABLED); SemanticHighlighting[] semanticHighlightings= SemanticHighlightings.getSemanticHighlightings(); for (int i= 0, n= semanticHighlightings.length; i < n; i++) { diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java index 3211888b111..4d1d9036c9d 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java @@ -306,9 +306,8 @@ public class CAutoIndentTest extends TestCase { assertEquals(" */", tester.getLine()); //$NON-NLS-1$ tester.type('\n'); assertEquals(3, tester.getCaretLine()); - // TODO: indent is one space - should be no indent -// assertEquals("", tester.getLine()); //$NON-NLS-1$ -// assertEquals(0, tester.getCaretColumn()); + assertEquals("", tester.getLine()); //$NON-NLS-1$ + assertEquals(0, tester.getCaretColumn()); } } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CWordIteratorTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CWordIteratorTest.java index ba5e1123a7b..56a706abd88 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CWordIteratorTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CWordIteratorTest.java @@ -20,7 +20,7 @@ import org.eclipse.cdt.internal.ui.text.CWordIterator; public class CWordIteratorTest extends BreakIteratorTest { public static Test suite() { - return new TestSuite(CBreakIteratorTest.class); + return new TestSuite(CWordIteratorTest.class); } /* diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FoldingTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FoldingTest.java new file mode 100644 index 00000000000..8aa930f315d --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FoldingTest.java @@ -0,0 +1,167 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Anton Leherbauer (Wind River Systems) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.text; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.Position; +import org.eclipse.jface.text.source.Annotation; +import org.eclipse.jface.text.source.SourceViewer; +import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel; + +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.testplugin.CProjectHelper; +import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.ui.PreferenceConstants; + +import org.eclipse.cdt.internal.ui.editor.CEditor; + +/** + * Code folding tests. + */ +public class FoldingTest extends TestCase { + + private static final String LINKED_FOLDER= "resources/folding"; + private static final String PROJECT= "FoldingTest"; + + private ICProject fCProject; + private final String fTestFilename= "/FoldingTest/src/FoldingTest.cpp"; + + private static CEditor fEditor; + + private static SourceViewer fSourceViewer; + + public static Test suite() { + return new TestSuite(FoldingTest.class); + } + + public FoldingTest(String name) { + super(name); + } + + protected void setUp() throws Exception { + super.setUp(); + fCProject= EditorTestHelper.createCProject(PROJECT, LINKED_FOLDER); + + IPreferenceStore store= CUIPlugin.getDefault().getPreferenceStore(); + store.setValue(PreferenceConstants.EDITOR_FOLDING_ENABLED, true); + store.setValue(PreferenceConstants.EDITOR_FOLDING_COMMENTS_ENABLED, true); + store.setValue(PreferenceConstants.EDITOR_FOLDING_PREPROCESSOR_BRANCHES_ENABLED, true); + + fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(fTestFilename), true); + fSourceViewer= EditorTestHelper.getSourceViewer(fEditor); + assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100)); + } + + protected void tearDown () throws Exception { + EditorTestHelper.closeEditor(fEditor); + + if (fCProject != null) + CProjectHelper.delete(fCProject); + + IPreferenceStore store= CUIPlugin.getDefault().getPreferenceStore(); + store.setToDefault(PreferenceConstants.EDITOR_FOLDING_ENABLED); + store.setToDefault(PreferenceConstants.EDITOR_FOLDING_COMMENTS_ENABLED); + store.setToDefault(PreferenceConstants.EDITOR_FOLDING_PREPROCESSOR_BRANCHES_ENABLED); + + super.tearDown(); + } + + protected void assertEqualPositions(Position[] expected, Position[] actual) { + assertEquals(expected.length, actual.length); + for (int i= 0, n= expected.length; i < n; i++) { + assertEquals(expected[i].isDeleted(), actual[i].isDeleted()); + assertEquals(expected[i].getOffset(), actual[i].getOffset()); + assertEquals(expected[i].getLength(), actual[i].getLength()); + } + } + + protected Position createPosition(int startLine, int endLine) throws BadLocationException { + IDocument document= fSourceViewer.getDocument(); + int startOffset= document.getLineOffset(startLine); + int endOffset= document.getLineOffset(endLine) + document.getLineLength(endLine) - document.getLineDelimiter(endLine).length(); + return new Position(startOffset, endOffset - startOffset); + } + + String toString(Position[] positions) throws BadLocationException { + StringBuffer buf= new StringBuffer(); + IDocument document= fSourceViewer.getDocument(); + buf.append("Position[] expected= new Position[] {\n"); + for (int i= 0, n= positions.length; i < n; i++) { + Position position= positions[i]; + int startLine= document.getLineOfOffset(position.getOffset()); + int endLine= document.getLineOfOffset(position.getOffset()+position.getLength()-1); + buf.append("\tcreatePosition(" + startLine + ", " + endLine + "),\n"); + } + buf.append("};\n"); + return buf.toString(); + } + + protected Position[] getFoldingPositions() { + List positions= new ArrayList(); + ProjectionAnnotationModel model= (ProjectionAnnotationModel)fEditor.getAdapter(ProjectionAnnotationModel.class); + assertNotNull(model); + for (Iterator iter= model.getAnnotationIterator(); iter.hasNext(); ) { + Annotation ann= (Annotation)iter.next(); + Position pos= model.getPosition(ann); + positions.add(pos); + } + Collections.sort(positions, new Comparator() { + public int compare(Object arg0, Object arg1) { + Position p0= (Position)arg0; + Position p1= (Position)arg1; + return p0.offset - p1.offset; + }}); + return (Position[]) positions.toArray(new Position[positions.size()]); + } + + public void testFoldingPositions() throws BadLocationException { + Position[] actual= getFoldingPositions(); + Position[] expected= new Position[] { + createPosition(0, 2), + createPosition(4, 7), + createPosition(9, 13), + createPosition(10, 12), + createPosition(13, 15), + createPosition(15, 27), + createPosition(16, 26), + createPosition(17, 21), + createPosition(18, 20), + createPosition(21, 25), + createPosition(22, 24), + createPosition(29, 31), + createPosition(34, 36), + createPosition(35, 40), + createPosition(36, 38), + createPosition(42, 46), + createPosition(48, 55), + createPosition(51, 53), + createPosition(51, 53), + createPosition(57, 59), + createPosition(61, 63), + createPosition(65, 67), + }; + if (true) System.out.println(toString(actual)); + assertEqualPositions(expected, actual); + } + +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java index b98e432d47b..5202dcc6195 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java @@ -36,5 +36,7 @@ public class TextTestSuite extends TestSuite { addTest(SemanticHighlightingTest.suite()); addTest(InactiveCodeHighlightingTest.suite()); + // folding tests + addTest(FoldingTest.suite()); } }