mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-02 05:45:58 +02:00
Bug 304482 - Add tests for template proposals
This commit is contained in:
parent
1e12841459
commit
b0d05b60ce
3 changed files with 249 additions and 5 deletions
|
@ -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
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -98,9 +98,8 @@ public abstract class AbstractContentAssistTest extends BaseUITestCase {
|
||||||
fCFile= null;
|
fCFile= null;
|
||||||
super.tearDown();
|
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()) {
|
if (CTestPlugin.getDefault().isDebugging()) {
|
||||||
System.out.println("\n\n\n\n\nTesting "+this.getClass().getName());
|
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();
|
ContentAssistant assistant = new ContentAssistant();
|
||||||
CContentAssistProcessor processor = new CContentAssistProcessor(fEditor, assistant, contentType);
|
CContentAssistProcessor processor = new CContentAssistProcessor(fEditor, assistant, contentType);
|
||||||
long startTime= System.currentTimeMillis();
|
long startTime= System.currentTimeMillis();
|
||||||
|
sourceViewer.setSelectedRange(offset, length);
|
||||||
Object[] results = isCompletion
|
Object[] results = isCompletion
|
||||||
? (Object[]) processor.computeCompletionProposals(sourceViewer, offset)
|
? (Object[]) processor.computeCompletionProposals(sourceViewer, offset)
|
||||||
: (Object[]) processor.computeContextInformation(sourceViewer, offset);
|
: (Object[]) processor.computeContextInformation(sourceViewer, offset);
|
||||||
long endTime= System.currentTimeMillis();
|
long endTime= System.currentTimeMillis();
|
||||||
assertTrue(results != null);
|
assertTrue(results != null);
|
||||||
|
|
||||||
results= filterResults(results, isCode);
|
if(isTemplate) {
|
||||||
|
results= filterResultsKeepTemplates(results);
|
||||||
|
} else {
|
||||||
|
results= filterResults(results, isCode);
|
||||||
|
}
|
||||||
String[] resultStrings= toStringArray(results, compareType);
|
String[] resultStrings= toStringArray(results, compareType);
|
||||||
Arrays.sort(expected);
|
Arrays.sort(expected);
|
||||||
Arrays.sort(resultStrings);
|
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.
|
* Filter out template and keyword proposals.
|
||||||
* @param results
|
* @param results
|
||||||
|
@ -193,6 +201,20 @@ public abstract class AbstractContentAssistTest extends BaseUITestCase {
|
||||||
return filtered.toArray();
|
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) {
|
private String[] toStringArray(Object[] results, int compareType) {
|
||||||
String[] strings= new String[results.length];
|
String[] strings= new String[results.length];
|
||||||
for(int i=0; i< results.length; i++){
|
for(int i=0; i< results.length; i++){
|
||||||
|
|
|
@ -73,5 +73,7 @@ public class ContentAssist2TestSuite extends TestSuite {
|
||||||
addTest(ParameterHintTests.suite());
|
addTest(ParameterHintTests.suite());
|
||||||
|
|
||||||
addTest(ShowCamelCasePreferenceTest.suite());
|
addTest(ShowCamelCasePreferenceTest.suite());
|
||||||
|
|
||||||
|
addTest(TemplateProposalTest.suite());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue