Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc-Andre Laperle2012-01-16 01:25:06 -0500
committerMarc-Andre Laperle2012-01-16 01:25:06 -0500
commitb0d05b60ceb79d577858de138683adf07fb13987 (patch)
tree6f67e8b3293927af63f542cbba63df1ecf819506
parent1e1284145943a748f676748cc5169d4da5bfafa8 (diff)
downloadorg.eclipse.cdt-b0d05b60ceb79d577858de138683adf07fb13987.tar.gz
org.eclipse.cdt-b0d05b60ceb79d577858de138683adf07fb13987.tar.xz
org.eclipse.cdt-b0d05b60ceb79d577858de138683adf07fb13987.zip
Bug 304482 - Add tests for template proposals
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractContentAssistTest.java32
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/ContentAssist2TestSuite.java2
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/TemplateProposalTest.java220
3 files changed, 249 insertions, 5 deletions
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractContentAssistTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractContentAssistTest.java
index 31be146d05..ffdf98e021 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractContentAssistTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractContentAssistTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2004, 2010 IBM Corporation and others.
+ * Copyright (c) 2004, 2012 IBM Corporation 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
@@ -98,9 +98,8 @@ public abstract class AbstractContentAssistTest extends BaseUITestCase {
fCFile= null;
super.tearDown();
}
-
- protected void assertContentAssistResults(int offset, String[] expected, boolean isCompletion, int compareType) throws Exception {
-
+
+ protected void assertContentAssistResults(int offset, int length, String[] expected, boolean isCompletion, boolean isTemplate, int compareType) throws Exception {
if (CTestPlugin.getDefault().isDebugging()) {
System.out.println("\n\n\n\n\nTesting "+this.getClass().getName());
}
@@ -112,13 +111,18 @@ public abstract class AbstractContentAssistTest extends BaseUITestCase {
ContentAssistant assistant = new ContentAssistant();
CContentAssistProcessor processor = new CContentAssistProcessor(fEditor, assistant, contentType);
long startTime= System.currentTimeMillis();
+ sourceViewer.setSelectedRange(offset, length);
Object[] results = isCompletion
? (Object[]) processor.computeCompletionProposals(sourceViewer, offset)
: (Object[]) processor.computeContextInformation(sourceViewer, offset);
long endTime= System.currentTimeMillis();
assertTrue(results != null);
- results= filterResults(results, isCode);
+ if(isTemplate) {
+ results= filterResultsKeepTemplates(results);
+ } else {
+ results= filterResults(results, isCode);
+ }
String[] resultStrings= toStringArray(results, compareType);
Arrays.sort(expected);
Arrays.sort(resultStrings);
@@ -161,6 +165,10 @@ public abstract class AbstractContentAssistTest extends BaseUITestCase {
}
+ protected void assertContentAssistResults(int offset, String[] expected, boolean isCompletion, int compareType) throws Exception {
+ assertContentAssistResults(offset, 0, expected, isCompletion, false, compareType);
+ }
+
/**
* Filter out template and keyword proposals.
* @param results
@@ -193,6 +201,20 @@ public abstract class AbstractContentAssistTest extends BaseUITestCase {
return filtered.toArray();
}
+ /**
+ * Filter out proposals, keep only templates
+ */
+ private Object[] filterResultsKeepTemplates(Object[] results) {
+ List<Object> filtered= new ArrayList<Object>();
+ for (int i = 0; i < results.length; i++) {
+ Object result = results[i];
+ if (result instanceof TemplateProposal) {
+ filtered.add(result);
+ }
+ }
+ return filtered.toArray();
+ }
+
private String[] toStringArray(Object[] results, int compareType) {
String[] strings= new String[results.length];
for(int i=0; i< results.length; i++){
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/ContentAssist2TestSuite.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/ContentAssist2TestSuite.java
index 70db213e24..88d0f7bbb9 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/ContentAssist2TestSuite.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/ContentAssist2TestSuite.java
@@ -73,5 +73,7 @@ public class ContentAssist2TestSuite extends TestSuite {
addTest(ParameterHintTests.suite());
addTest(ShowCamelCasePreferenceTest.suite());
+
+ addTest(TemplateProposalTest.suite());
}
}
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/TemplateProposalTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/TemplateProposalTest.java
new file mode 100644
index 0000000000..d9c080794b
--- /dev/null
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/TemplateProposalTest.java
@@ -0,0 +1,220 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Marc-Andre Laperle 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:
+ * Marc-Andre Laperle - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.ui.tests.text.contentassist2;
+
+import junit.framework.Test;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.jface.text.templates.Template;
+import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
+import org.eclipse.jface.text.templates.persistence.TemplateStore;
+
+import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
+import org.eclipse.cdt.ui.CUIPlugin;
+
+import org.eclipse.cdt.internal.corext.template.c.CContextType;
+
+
+public class TemplateProposalTest extends AbstractContentAssistTest {
+
+ public TemplateProposalTest(String name) {
+ super(name, true);
+ }
+
+ public TemplateProposalTest(String name, boolean isCpp) {
+ super(name, isCpp);
+ }
+
+ public static Test suite() {
+ return BaseTestCase.suite(TemplateProposalTest.class, "_");
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ // Remove all the default templates. Tests will add templates as necessary.
+ TemplateStore templateStore = CUIPlugin.getDefault().getTemplateStore();
+ TemplatePersistenceData[] templateData = templateStore.getTemplateData(false);
+ for (TemplatePersistenceData templatePersistenceData : templateData) {
+ templateStore.delete(templatePersistenceData);
+ }
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+
+ // Restore the default templates
+ CUIPlugin.getDefault().getTemplateStore().restoreDefaults();
+ }
+
+ private static final String HEADER_FILE_NAME = "CompletionTest.h";
+ private static final String SOURCE_FILE_NAME = "CompletionTest.cpp";
+ private static final String SELECTION_START_TAG = "/*sel-start*/";
+ private static final String SELECTION_END_TAG = "/*sel-end*/";
+
+ private static final String TEMPLATE_NAME_WORD_SELECTION = "word selection template";
+ private static final String TEMPLATE_NAME_WORD_SELECTION_DISP = TEMPLATE_NAME_WORD_SELECTION + " - ";
+ private static final String TEMPLATE_NAME_LINE_SELECTION = "line selection template";
+ private static final String TEMPLATE_NAME_LINE_SELECTION_DISP = TEMPLATE_NAME_LINE_SELECTION + " - ";
+
+ protected int fSelectionOffset;
+ protected int fSelectionLength;
+ private IProject fProject;
+
+ @Override
+ protected IFile setUpProjectContent(IProject project) throws Exception {
+ fProject= project;
+ StringBuilder sourceContent= getContentsForTest(1)[0];
+ fSelectionOffset= sourceContent.indexOf(SELECTION_START_TAG);
+ assertTrue("No selection start specified", fSelectionOffset >= 0);
+ sourceContent.delete(fSelectionOffset, fSelectionOffset + SELECTION_START_TAG.length());
+ int selEndOffset = sourceContent.indexOf(SELECTION_END_TAG);
+
+ if (selEndOffset >= 0) {
+ sourceContent.delete(selEndOffset, selEndOffset + SELECTION_END_TAG.length());
+ fSelectionLength = selEndOffset - fSelectionOffset;
+ } else {
+ fSelectionLength = 0;
+ }
+
+ return createFile(project, SOURCE_FILE_NAME, sourceContent.toString());
+ }
+
+ private void addWordSelectionTemplate() {
+ Template newTemplate = new Template(TEMPLATE_NAME_WORD_SELECTION, "", CContextType.ID, "cout << ${word_selection};", true);
+ TemplatePersistenceData data= new TemplatePersistenceData(newTemplate, true);
+ CUIPlugin.getDefault().getTemplateStore().add(data);
+ }
+
+ private void addLineSelectionTemplate() {
+ Template newTemplate = new Template(TEMPLATE_NAME_LINE_SELECTION, "", CContextType.ID, "cout << ${line_selection};", true);
+ TemplatePersistenceData data= new TemplatePersistenceData(newTemplate, true);
+ CUIPlugin.getDefault().getTemplateStore().add(data);
+ }
+
+
+
+
+ //void func() {
+ ///*sel-start*/test foo bar/*sel-end*/
+ //}
+ public void testFullLineSelection() throws Exception {
+ addLineSelectionTemplate();
+ addWordSelectionTemplate();
+ final String[] expected= {
+ TEMPLATE_NAME_LINE_SELECTION_DISP
+ };
+ assertContentAssistResults(fSelectionOffset, fSelectionLength, expected, true, true, AbstractContentAssistTest.COMPARE_ID_STRINGS);
+ }
+
+ //void func() {
+ ///*sel-start*/test foo bar
+ //test foo bar/*sel-end*/
+ //}
+ public void testMultiLineSelection() throws Exception {
+ addLineSelectionTemplate();
+ addWordSelectionTemplate();
+ final String[] expected= {
+ TEMPLATE_NAME_LINE_SELECTION_DISP
+ };
+ assertContentAssistResults(fSelectionOffset, fSelectionLength, expected, true, true, AbstractContentAssistTest.COMPARE_ID_STRINGS);
+ }
+
+ //void func() {
+ //foo /*sel-start*/test/*sel-end*/
+ //}
+ public void testWordSelection() throws Exception {
+ addLineSelectionTemplate();
+ addWordSelectionTemplate();
+ final String[] expected= {
+ TEMPLATE_NAME_WORD_SELECTION_DISP
+ };
+ assertContentAssistResults(fSelectionOffset, fSelectionLength, expected, true, true, AbstractContentAssistTest.COMPARE_ID_STRINGS);
+ }
+
+ //void func() {
+ //foo/*sel-start*/test/*sel-end*/
+ //}
+ public void testPartialLineWordSelection() throws Exception {
+ addLineSelectionTemplate();
+ addWordSelectionTemplate();
+ final String[] expected= {
+ TEMPLATE_NAME_WORD_SELECTION_DISP
+ };
+ assertContentAssistResults(fSelectionOffset, fSelectionLength, expected, true, true, AbstractContentAssistTest.COMPARE_ID_STRINGS);
+ }
+
+ //void func() {
+ //test foo/*sel-start*/bar
+ //test foo /*sel-end*/bar
+ //}
+ public void testWordSelectionOverMultiLine() throws Exception {
+ addLineSelectionTemplate();
+ addWordSelectionTemplate();
+ final String[] expected= {
+ TEMPLATE_NAME_WORD_SELECTION_DISP
+ };
+ assertContentAssistResults(fSelectionOffset, fSelectionLength, expected, true, true, AbstractContentAssistTest.COMPARE_ID_STRINGS);
+ }
+
+ //void func() {
+ // /*sel-start*/test/*sel-end*/
+ //}
+ public void testBug298554_lineSelectedWithoutWhitespaces() throws Exception {
+ addLineSelectionTemplate();
+ final String[] expected= {
+ TEMPLATE_NAME_LINE_SELECTION_DISP
+ };
+ assertContentAssistResults(fSelectionOffset, fSelectionLength, expected, true, true, AbstractContentAssistTest.COMPARE_ID_STRINGS);
+ }
+
+ //void func() {
+ // /*sel-start*/test foo bar
+ //test foo bar/*sel-end*/
+ //}
+ public void testBug298554_multiLineSelectedWithoutWhitespaces() throws Exception {
+ addWordSelectionTemplate();
+ addLineSelectionTemplate();
+ final String[] expected= {
+ TEMPLATE_NAME_LINE_SELECTION_DISP
+ };
+ assertContentAssistResults(fSelectionOffset, fSelectionLength, expected, true, true, AbstractContentAssistTest.COMPARE_ID_STRINGS);
+ }
+
+ //void func() {
+ // /*sel-start*/test/*sel-end*/
+ //}
+ public void testBug304482_onlyWordOnLine() throws Exception {
+ addLineSelectionTemplate();
+ addWordSelectionTemplate();
+ final String[] expected= {
+ TEMPLATE_NAME_LINE_SELECTION_DISP,
+ TEMPLATE_NAME_WORD_SELECTION_DISP
+ };
+ assertContentAssistResults(fSelectionOffset, fSelectionLength, expected, true, true, AbstractContentAssistTest.COMPARE_ID_STRINGS);
+ }
+
+ //void func() {
+ ///*sel-start*/test/*sel-end*/
+ //}
+ public void testBug304482_onlyWordOnLineStartOfLine() throws Exception {
+ addLineSelectionTemplate();
+ addWordSelectionTemplate();
+ final String[] expected= {
+ TEMPLATE_NAME_LINE_SELECTION_DISP,
+ TEMPLATE_NAME_WORD_SELECTION_DISP
+ };
+ assertContentAssistResults(fSelectionOffset, fSelectionLength, expected, true, true, AbstractContentAssistTest.COMPARE_ID_STRINGS);
+ }
+
+}

Back to the top