1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Content Assist Work: More JUnit tests

This commit is contained in:
Hoda Amer 2004-01-16 20:19:05 +00:00
parent 9960da3ec4
commit bcdd0ab7c3
28 changed files with 928 additions and 88 deletions

View file

@ -1,3 +1,7 @@
2004-01-16 Hoda Amer
Modified CModelElementsTest to test for enumerator constant expression
Bug#47552
2004-01-15 Andrew Niefer
Moved testBug48307_FriendFunction_1 & testBug48307_FriendFunction_2 to ContextualParseTest
Updated ContextualParseTest now that the order of prefix lookup results is predictable.

View file

@ -273,10 +273,11 @@ public class CModelElementsTests extends TestCase {
checkElementOffset((CElement)enum);
checkLineNumbers((CElement)enum, 57, 61);
// enum ---> enumerator: first
// enum ---> enumerator: first = 1
ArrayList enumEnumerators = enum.getChildrenOfType(ICElement.C_ENUMERATOR);
IEnumerator first = (IEnumerator) enumEnumerators.get(0);
assertEquals(first.getElementName(), new String("first"));
assertEquals("1", first.getConstantExpression());
checkElementOffset((CElement)first);
// enum ---> enumerator: second
IEnumerator second = (IEnumerator) enumEnumerators.get(1);

View file

@ -55,7 +55,7 @@ namespace MyPackage
// check enums
// enum without name
enum {
first,
first = 1,
second,
third
}

View file

@ -1,3 +1,6 @@
2004-01-16 Hoda Amer
Solution to bug#47552: IEnumerator#getConstantExpression is always empty
2004-01-15 Hoda Amer
Moved Content Assist log to the UI plugin

View file

@ -43,6 +43,7 @@ import org.eclipse.cdt.core.parser.ast.IASTDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTEnumerator;
import org.eclipse.cdt.core.parser.ast.IASTExpression;
import org.eclipse.cdt.core.parser.ast.IASTField;
import org.eclipse.cdt.core.parser.ast.IASTFunction;
import org.eclipse.cdt.core.parser.ast.IASTInclusion;
@ -380,6 +381,13 @@ public class CModelBuilder {
private Enumerator createEnumerator(Parent enum, IASTEnumerator enumDef){
Enumerator element = new Enumerator (enum, enumDef.getName().toString());
IASTExpression initialValue = enumDef.getInitialValue();
if(initialValue != null){
if(initialValue.getLiteralString().length() > 0)
element.setConstantExpression(initialValue.getLiteralString());
else
element.setConstantExpression(initialValue.getIdExpression());
}
// add to parent
enum.addChild(element);
// set enumerator position

View file

@ -34,11 +34,10 @@ public interface IASTNode {
public static final LookupKind METHODS = new LookupKind( 9 );
public static final LookupKind FIELDS = new LookupKind( 10 );
public static final LookupKind CONSTRUCTORS = new LookupKind (11);
public static final LookupKind NAMESPACES = new LookupKind( 12 );
public static final LookupKind MACROS = new LookupKind( 13 );
public static final LookupKind ENUMERATIONS = new LookupKind( 14 );
public static final LookupKind ENUMERATORS = new LookupKind( 15 );
public static final LookupKind THIS = new LookupKind(16);
public static final LookupKind NAMESPACES = new LookupKind( 12 );
public static final LookupKind ENUMERATIONS = new LookupKind( 13 );
public static final LookupKind ENUMERATORS = new LookupKind( 14 );
public static final LookupKind THIS = new LookupKind(15);
/**
* @param enumValue

View file

@ -103,10 +103,13 @@ public class ASTUtil {
if(clause != null){
IASTExpression expression = clause.getAssigmentExpression();
if(expression != null){
String literal = expression.getLiteralString();
if(literal.length() > 0)
String literal = (expression.getLiteralString().length() > 0
? expression.getLiteralString()
: expression.getIdExpression() );
if(literal.length() > 0){
initializer.append("=");
initializer.append(literal);
}
}
}
}

View file

@ -1,3 +1,7 @@
2004-01-16 Hoda Amer
Added More success JUnit tests.
Added two failed JUnit tests.
2004-01-15 Hoda Amer
Moved Content Assist testing to the UI.tests plugin
Started a new framework of JUnit tests for content assist.

View file

@ -21,10 +21,15 @@ class aClass {
public:
int aField;
int aMethod();
};
}
class anotherClass {
public:
int anotherField;
void anotherMethod();
}
namespace aNamespace {
void aNamespaceFunction(){
}
};

View file

@ -0,0 +1,5 @@
#include "CompletionTestStart.h"
class aThirdClass {
a
}

View file

@ -0,0 +1,6 @@
#include "CompletionTestStart.h"
void anotherClass::anotherMethod()
{
}

View file

@ -0,0 +1,7 @@
#include "CompletionTestStart.h"
void anotherClass::anotherMethod()
{
aClass* c = new aClass();
c->a
}

View file

@ -0,0 +1,10 @@
#include "CompletionTestStart.h"
aClass* foo(){
return new aClass();
}
void anotherClass::anotherMethod()
{
foo()->a
}

View file

@ -0,0 +1,6 @@
#include "CompletionTestStart.h"
void anotherClass::anotherMethod()
{
aNamespace::
}

View file

@ -0,0 +1,6 @@
#include "CompletionTestStart.h"
void anotherClass::anotherMethod()
{
aNamespace::a
}

View file

@ -9,9 +9,8 @@ import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.cdt.ui.tests.text.PartitionTokenScannerTest;
import org.eclipse.cdt.ui.tests.text.contentassist.CompletionProposalsTest1;
import org.eclipse.cdt.ui.tests.text.contentassist.CompletionProposalsTest2;
import org.eclipse.cdt.ui.tests.text.contentassist.CompletionProposalsTest3;
import org.eclipse.cdt.ui.tests.text.contentassist.*;
import org.eclipse.cdt.ui.tests.text.contentassist.failedtests.*;
import org.eclipse.cdt.ui.tests.textmanipulation.TextBufferTest;
@ -33,11 +32,22 @@ public class AutomatedSuite extends TestSuite {
* Construct the test suite.
*/
public AutomatedSuite() {
// Success Tests
addTest(PartitionTokenScannerTest.suite());
addTest(TextBufferTest.suite());
addTest(CompletionProposalsTest1.suite());
addTest(CompletionProposalsTest2.suite());
addTest(CompletionProposalsTest3.suite());
addTest(CompletionProposalsTest4.suite());
addTest(CompletionProposalsTest5.suite());
addTest(CompletionProposalsTest6.suite());
addTest(CompletionProposalsTest7.suite());
// Failed Tests
addTest(CompletionProposalsFailedTest1.suite());
addTest(CompletionProposalsFailedTest2.suite());
}
}

View file

@ -43,7 +43,7 @@ import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
public abstract class CompletionProposalsTest extends TestCase{
public abstract class CompletionProposalsBaseTest extends TestCase{
private final String pluginName = "org.eclipse.cdt.ui.tests";
private final String projectName = "TestProject1";
private final String projectType = "bin";
@ -56,7 +56,7 @@ public abstract class CompletionProposalsTest extends TestCase{
private Document document = null;
public CompletionProposalsTest(String name) {
public CompletionProposalsBaseTest(String name) {
super(name);
}
@ -84,9 +84,9 @@ public abstract class CompletionProposalsTest extends TestCase{
fCFile = fCProject.getProject().getFile(fileName);
if ( (!fCFile.exists()) &&( !fHeaderFile.exists() )) {
try{
String fileFullPath = pluginRoot+ getFileFullPath();
FileInputStream headerFileIn = new FileInputStream(pluginRoot+ getHeaderFileFullPath());
fHeaderFile.create(headerFileIn,false, monitor);
String fileFullPath = pluginRoot+ getFileFullPath();
FileInputStream bodyFileIn = new FileInputStream(fileFullPath);
fCFile.create(bodyFileIn,false, monitor);
} catch (CoreException e) {
@ -164,9 +164,9 @@ public abstract class CompletionProposalsTest extends TestCase{
assertEquals(prefix, getExpectedPrefix());
String[] expected = getExpectedResultsValues();
assertEquals(results.length, expected.length);
assertTrue(results.length >= expected.length);
for (int i = 0; i<results.length; i++){
for (int i = 0; i<expected.length; i++){
ICompletionProposal proposal = results[i];
String displayString = proposal.getDisplayString();
assertEquals(displayString, expected[i]);

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2001 Rational Software Corp. and others.
* Copyright (c) 2004 Rational Software Corp. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
@ -16,9 +16,11 @@ import org.eclipse.cdt.core.parser.ast.IASTCompletionNode.CompletionKind;
/**
* @author hamer
*
* Testing Function/Method scope, statement start, with a prefix
*
*/
public class CompletionProposalsTest1 extends CompletionProposalsTest{
public class CompletionProposalsTest1 extends CompletionProposalsBaseTest{
private final String fileName = "CompletionTestStart1.cpp";
private final String fileFullPath ="resources/contentassist/" + fileName;
@ -36,11 +38,10 @@ public class CompletionProposalsTest1 extends CompletionProposalsTest{
"anotherFunction() void",
"aClass",
"anotherClass",
"aNamespace",
"anEnumeration",
"AStruct",
// "AMacro",
"asm",
"auto"
"AMacro"
};
public CompletionProposalsTest1(String name) {

View file

@ -16,9 +16,12 @@ import org.eclipse.cdt.core.parser.ast.IASTCompletionNode.CompletionKind;
/**
* @author hamer
*
* Testing Function/Method scope, a class context, with a prefix
* After a .
*
*/
public class CompletionProposalsTest2 extends CompletionProposalsTest{
public class CompletionProposalsTest2 extends CompletionProposalsBaseTest{
private final String fileName = "CompletionTestStart2.cpp";
private final String fileFullPath ="resources/contentassist/" + fileName;
private final String headerFileName = "CompletionTestStart.h";

View file

@ -16,9 +16,11 @@ import org.eclipse.cdt.core.parser.ast.IASTCompletionNode.CompletionKind;
/**
* @author hamer
*
* Testing Global scope, declaration start, with a prefix
*
*/
public class CompletionProposalsTest3 extends CompletionProposalsTest{
public class CompletionProposalsTest3 extends CompletionProposalsBaseTest{
private final String fileName = "CompletionTestStart3.cpp";
private final String fileFullPath ="resources/contentassist/" + fileName;
private final String headerFileName = "CompletionTestStart.h";
@ -32,9 +34,7 @@ public class CompletionProposalsTest3 extends CompletionProposalsTest{
"anotherClass",
"anEnumeration",
"AStruct",
// "AMacro",
"asm",
"auto"
"AMacro"
};
public CompletionProposalsTest3(String name) {

View file

@ -0,0 +1,119 @@
/**********************************************************************
* Copyright (c) 2004 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.ui.tests.text.contentassist;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.parser.ast.IASTCompletionNode.CompletionKind;
/**
* @author hamer
*
* Testing Class scope, declaration start, with a prefix
*
*/
public class CompletionProposalsTest4 extends CompletionProposalsBaseTest{
private final String fileName = "CompletionTestStart4.cpp";
private final String fileFullPath ="resources/contentassist/" + fileName;
private final String headerFileName = "CompletionTestStart.h";
private final String headerFileFullPath ="resources/contentassist/" + headerFileName;
private final String expectedScopeName = "ASTClassSpecifier";
private final String expectedContextName = "null";
private final CompletionKind expectedKind = CompletionKind.FIELD_TYPE;
private final String expectedPrefix = "a";
private final String[] expectedResults = {
"aClass",
"anotherClass",
"aThirdClass",
"aNamespace",
"anEnumeration",
"AStruct"
};
public CompletionProposalsTest4(String name) {
super(name);
}
public static Test suite() {
TestSuite suite= new TestSuite(CompletionProposalsTest4.class.getName());
suite.addTest(new CompletionProposalsTest4("testCompletionProposals"));
return suite;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getCompletionPosition()
*/
protected int getCompletionPosition() {
return getBuffer().indexOf(" a ") + 2;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedScope()
*/
protected String getExpectedScopeClassName() {
return expectedScopeName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedContext()
*/
protected String getExpectedContextClassName() {
return expectedContextName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedKind()
*/
protected CompletionKind getExpectedKind() {
return expectedKind;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedPrefix()
*/
protected String getExpectedPrefix() {
return expectedPrefix;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedResultsValues()
*/
protected String[] getExpectedResultsValues() {
return expectedResults;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileName()
*/
protected String getFileName() {
return fileName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileFullPath()
*/
protected String getFileFullPath() {
return fileFullPath;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileFullPath()
*/
protected String getHeaderFileFullPath() {
return headerFileFullPath;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileName()
*/
protected String getHeaderFileName() {
return headerFileName;
}
}

View file

@ -0,0 +1,116 @@
/**********************************************************************
* Copyright (c) 2004 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.ui.tests.text.contentassist;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.parser.ast.IASTCompletionNode.CompletionKind;
/**
* @author hamer
*
* Testing Function/Method scope, statement start, with no prefix
* Lookup.THIS
*
*/
public class CompletionProposalsTest5 extends CompletionProposalsBaseTest{
private final String fileName = "CompletionTestStart5.cpp";
private final String fileFullPath ="resources/contentassist/" + fileName;
private final String headerFileName = "CompletionTestStart.h";
private final String headerFileFullPath ="resources/contentassist/" + headerFileName;
private final String expectedScopeName = "ASTMethod";
private final String expectedContextName = "null";
private final CompletionKind expectedKind = CompletionKind.STATEMENT_START;
private final String expectedPrefix = "";
private final String[] expectedResults = {
"anotherField : int",
"anotherMethod() void"
};
public CompletionProposalsTest5(String name) {
super(name);
}
public static Test suite() {
TestSuite suite= new TestSuite(CompletionProposalsTest5.class.getName());
suite.addTest(new CompletionProposalsTest5("testCompletionProposals"));
return suite;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getCompletionPosition()
*/
protected int getCompletionPosition() {
return getBuffer().indexOf(" ") + 2;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedScope()
*/
protected String getExpectedScopeClassName() {
return expectedScopeName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedContext()
*/
protected String getExpectedContextClassName() {
return expectedContextName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedKind()
*/
protected CompletionKind getExpectedKind() {
return expectedKind;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedPrefix()
*/
protected String getExpectedPrefix() {
return expectedPrefix;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedResultsValues()
*/
protected String[] getExpectedResultsValues() {
return expectedResults;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileName()
*/
protected String getFileName() {
return fileName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileFullPath()
*/
protected String getFileFullPath() {
return fileFullPath;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileFullPath()
*/
protected String getHeaderFileFullPath() {
return headerFileFullPath;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileName()
*/
protected String getHeaderFileName() {
return headerFileName;
}
}

View file

@ -0,0 +1,116 @@
/**********************************************************************
* Copyright (c) 2004 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.ui.tests.text.contentassist;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.parser.ast.IASTCompletionNode.CompletionKind;
/**
* @author hamer
*
* Testing Function/Method scope, a class context, with a prefix
* After an ->
*
*/
public class CompletionProposalsTest6 extends CompletionProposalsBaseTest{
private final String fileName = "CompletionTestStart6.cpp";
private final String fileFullPath ="resources/contentassist/" + fileName;
private final String headerFileName = "CompletionTestStart.h";
private final String headerFileFullPath ="resources/contentassist/" + headerFileName;
private final String expectedScopeName = "ASTMethod";
private final String expectedContextName = "ASTClassSpecifier";
private final CompletionKind expectedKind = CompletionKind.MEMBER_REFERENCE;
private final String expectedPrefix = "a";
private final String[] expectedResults = {
"aField : int",
"aMethod() int"
};
public CompletionProposalsTest6(String name) {
super(name);
}
public static Test suite() {
TestSuite suite= new TestSuite(CompletionProposalsTest6.class.getName());
suite.addTest(new CompletionProposalsTest6("testCompletionProposals"));
return suite;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getCompletionPosition()
*/
protected int getCompletionPosition() {
return getBuffer().indexOf(" c->a ") + 5;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedScope()
*/
protected String getExpectedScopeClassName() {
return expectedScopeName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedContext()
*/
protected String getExpectedContextClassName() {
return expectedContextName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedKind()
*/
protected CompletionKind getExpectedKind() {
return expectedKind;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedPrefix()
*/
protected String getExpectedPrefix() {
return expectedPrefix;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedResultsValues()
*/
protected String[] getExpectedResultsValues() {
return expectedResults;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileName()
*/
protected String getFileName() {
return fileName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileFullPath()
*/
protected String getFileFullPath() {
return fileFullPath;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileFullPath()
*/
protected String getHeaderFileFullPath() {
return headerFileFullPath;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileName()
*/
protected String getHeaderFileName() {
return headerFileName;
}
}

View file

@ -0,0 +1,116 @@
/**********************************************************************
* Copyright (c) 2004 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.ui.tests.text.contentassist;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.parser.ast.IASTCompletionNode.CompletionKind;
/**
* @author hamer
*
* Testing Function/Method scope, a class context, with a prefix
* Complex Context: Function return value: foo()->a(CTRL+SPACE)
*
*/
public class CompletionProposalsTest7 extends CompletionProposalsBaseTest{
private final String fileName = "CompletionTestStart7.cpp";
private final String fileFullPath ="resources/contentassist/" + fileName;
private final String headerFileName = "CompletionTestStart.h";
private final String headerFileFullPath ="resources/contentassist/" + headerFileName;
private final String expectedScopeName = "ASTMethod";
private final String expectedContextName = "ASTClassSpecifier";
private final CompletionKind expectedKind = CompletionKind.MEMBER_REFERENCE;
private final String expectedPrefix = "a";
private final String[] expectedResults = {
"aField : int",
"aMethod() int"
};
public CompletionProposalsTest7(String name) {
super(name);
}
public static Test suite() {
TestSuite suite= new TestSuite(CompletionProposalsTest7.class.getName());
suite.addTest(new CompletionProposalsTest7("testCompletionProposals"));
return suite;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getCompletionPosition()
*/
protected int getCompletionPosition() {
return getBuffer().indexOf("->a ") + 3;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedScope()
*/
protected String getExpectedScopeClassName() {
return expectedScopeName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedContext()
*/
protected String getExpectedContextClassName() {
return expectedContextName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedKind()
*/
protected CompletionKind getExpectedKind() {
return expectedKind;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedPrefix()
*/
protected String getExpectedPrefix() {
return expectedPrefix;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedResultsValues()
*/
protected String[] getExpectedResultsValues() {
return expectedResults;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileName()
*/
protected String getFileName() {
return fileName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileFullPath()
*/
protected String getFileFullPath() {
return fileFullPath;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileFullPath()
*/
protected String getHeaderFileFullPath() {
return headerFileFullPath;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileName()
*/
protected String getHeaderFileName() {
return headerFileName;
}
}

View file

@ -0,0 +1,118 @@
/**********************************************************************
* Copyright (c) 2004 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.ui.tests.text.contentassist.failedtests;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.parser.ast.IASTCompletionNode.CompletionKind;
import org.eclipse.cdt.ui.tests.text.contentassist.CompletionProposalsBaseTest;
/**
* @author hamer
*
* Testing Function/Method scope, Namespace context, with no prefix
* Bug#50152: Wrong context sent after a "::"
*
*/
public class CompletionProposalsFailedTest1 extends CompletionProposalsBaseTest{
private final String fileName = "CompletionFailedTestStart1.cpp";
private final String fileFullPath ="resources/contentassist/failedtests/" + fileName;
private final String headerFileName = "CompletionTestStart.h";
private final String headerFileFullPath ="resources/contentassist/" + headerFileName;
private final String expectedScopeName = "ASTMethod";
private final String expectedContextName = "null"; // should be "ASTNamespaceDefinition";
private final CompletionKind expectedKind = CompletionKind.SINGLE_NAME_REFERENCE; // should be CompletionKind.SCOPED_REFERENCE;
private final String expectedPrefix = "::"; // should be "";
private final String[] expectedResults = {
// shoud be "aNamespaceFunction() void"
};
public CompletionProposalsFailedTest1(String name) {
super(name);
}
public static Test suite() {
TestSuite suite= new TestSuite(CompletionProposalsFailedTest1.class.getName());
suite.addTest(new CompletionProposalsFailedTest1("testCompletionProposals"));
return suite;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getCompletionPosition()
*/
protected int getCompletionPosition() {
return getBuffer().indexOf(":: ") + 2;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedScope()
*/
protected String getExpectedScopeClassName() {
return expectedScopeName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedContext()
*/
protected String getExpectedContextClassName() {
return expectedContextName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedKind()
*/
protected CompletionKind getExpectedKind() {
return expectedKind;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedPrefix()
*/
protected String getExpectedPrefix() {
return expectedPrefix;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedResultsValues()
*/
protected String[] getExpectedResultsValues() {
return expectedResults;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileName()
*/
protected String getFileName() {
return fileName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileFullPath()
*/
protected String getFileFullPath() {
return fileFullPath;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileFullPath()
*/
protected String getHeaderFileFullPath() {
return headerFileFullPath;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileName()
*/
protected String getHeaderFileName() {
return headerFileName;
}
}

View file

@ -0,0 +1,118 @@
/**********************************************************************
* Copyright (c) 2004 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.ui.tests.text.contentassist.failedtests;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.parser.ast.IASTCompletionNode.CompletionKind;
import org.eclipse.cdt.ui.tests.text.contentassist.CompletionProposalsBaseTest;
/**
* @author hamer
*
* Testing Function/Method scope, Namespace context, with a prefix
* Bug#50152: Wrong context sent after a "::"
*
*/
public class CompletionProposalsFailedTest2 extends CompletionProposalsBaseTest{
private final String fileName = "CompletionFailedTestStart2.cpp";
private final String fileFullPath ="resources/contentassist/failedtests/" + fileName;
private final String headerFileName = "CompletionTestStart.h";
private final String headerFileFullPath ="resources/contentassist/" + headerFileName;
private final String expectedScopeName = "ASTMethod";
private final String expectedContextName = "null"; // should be "ASTNamespaceDefinition";
private final CompletionKind expectedKind = CompletionKind.SINGLE_NAME_REFERENCE; // should be CompletionKind.SCOPED_REFERENCE;
private final String expectedPrefix = "a";
private final String[] expectedResults = {
// shoud be "aNamespaceFunction() void"
};
public CompletionProposalsFailedTest2(String name) {
super(name);
}
public static Test suite() {
TestSuite suite= new TestSuite(CompletionProposalsFailedTest2.class.getName());
suite.addTest(new CompletionProposalsFailedTest2("testCompletionProposals"));
return suite;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getCompletionPosition()
*/
protected int getCompletionPosition() {
return getBuffer().indexOf("::a ") + 3;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedScope()
*/
protected String getExpectedScopeClassName() {
return expectedScopeName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedContext()
*/
protected String getExpectedContextClassName() {
return expectedContextName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedKind()
*/
protected CompletionKind getExpectedKind() {
return expectedKind;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedPrefix()
*/
protected String getExpectedPrefix() {
return expectedPrefix;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedResultsValues()
*/
protected String[] getExpectedResultsValues() {
return expectedResults;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileName()
*/
protected String getFileName() {
return fileName;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileFullPath()
*/
protected String getFileFullPath() {
return fileFullPath;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileFullPath()
*/
protected String getHeaderFileFullPath() {
return headerFileFullPath;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileName()
*/
protected String getHeaderFileName() {
return headerFileName;
}
}

View file

@ -1,3 +1,6 @@
2004-01-16 Hoda Amer
Added lookupMacros to the CompletionEngine
2004-01-16 John Camelon
IASTNode.LookupException references changed to IASTNode.LookupError.

View file

@ -13,8 +13,12 @@ package org.eclipse.cdt.internal.ui.text.contentassist;
import java.io.CharArrayReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CoreModel;
@ -41,7 +45,6 @@ import org.eclipse.cdt.core.parser.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTEnumerator;
import org.eclipse.cdt.core.parser.ast.IASTField;
import org.eclipse.cdt.core.parser.ast.IASTFunction;
import org.eclipse.cdt.core.parser.ast.IASTMacro;
import org.eclipse.cdt.core.parser.ast.IASTMethod;
import org.eclipse.cdt.core.parser.ast.IASTNamespaceDefinition;
import org.eclipse.cdt.core.parser.ast.IASTNode;
@ -49,8 +52,8 @@ import org.eclipse.cdt.core.parser.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTScope;
import org.eclipse.cdt.core.parser.ast.IASTVariable;
import org.eclipse.cdt.core.parser.ast.IASTCompletionNode.CompletionKind;
import org.eclipse.cdt.core.parser.ast.IASTNode.LookupKind;
import org.eclipse.cdt.core.parser.ast.IASTNode.ILookupResult;
import org.eclipse.cdt.core.parser.ast.IASTNode.LookupKind;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.parser.util.ASTUtil;
import org.eclipse.cdt.internal.ui.util.IDebugLogConstants;
@ -92,6 +95,7 @@ public class CompletionEngine implements RelevanceConstants{
"template",
};
private static final String exceptionKeyword = "...";
private Map macroMap = new HashMap();
// scope relevance element counters
private int numFields = 0;
private int numVariables = 0;
@ -104,7 +108,6 @@ public class CompletionEngine implements RelevanceConstants{
private int numEnumerations = 0;
private int numEnumerators = 0;
private int numNamespaces = 0;
private int numMacros = 0;
public CompletionEngine(ICompletionRequestor completionRequestor){
requestor = completionRequestor;
@ -183,9 +186,11 @@ public class CompletionEngine implements RelevanceConstants{
ParserLanguage language = CoreModel.getDefault().hasCCNature(project) ? ParserLanguage.CPP : ParserLanguage.C;
IParser parser = null;
IScanner scanner = null;
macroMap.clear();
try
{
IScanner scanner = ParserFactory.createScanner( reader, realPath.toOSString(), scanInfo, ParserMode.COMPLETION_PARSE, language, requestor, ParserUtil.getParserLogService() );
scanner = ParserFactory.createScanner( reader, realPath.toOSString(), scanInfo, ParserMode.COMPLETION_PARSE, language, requestor, ParserUtil.getParserLogService() );
parser = ParserFactory.createParser( scanner, requestor, ParserMode.COMPLETION_PARSE, language, ParserUtil.getParserLogService() );
}
catch( ParserFactoryError pfe )
@ -196,6 +201,7 @@ public class CompletionEngine implements RelevanceConstants{
IASTCompletionNode result = null;
try {
result = parser.parse(completionOffset);
macroMap = scanner.getDefinitions();
} catch (ParseError e ) {
//TODO - this can be more than just a Not Implemented exception
}
@ -300,14 +306,6 @@ public class CompletionEngine implements RelevanceConstants{
completionStart, completionLength, relevance);
}
}
else if(node instanceof IASTMacro){
numMacros++;
IASTMacro macro = (IASTMacro)node;
int relevance = computeRelevance(ICElement.C_MACRO, prefix, macro.getName());
relevance += totalNumberOfResults - numMacros;
requestor.acceptMacro(macro.getName(), completionStart, completionLength, relevance);
}
else if(node instanceof IASTNamespaceDefinition){
numNamespaces++;
IASTNamespaceDefinition namespace = (IASTNamespaceDefinition)node;
@ -349,6 +347,22 @@ public class CompletionEngine implements RelevanceConstants{
log("No of Keywords = " + numOfKeywords);
}
private void addMacroToCompletions (String macroName){
int relevance = MACRO_TYPE_RELEVANCE;
requestor.acceptMacro(macroName, completionStart, completionLength, relevance);
}
private void addMacrosToCompletions(Iterator macros){
int numOfMacros = 0;
while (macros.hasNext()){
String macro = (String) macros.next();
addMacroToCompletions(macro);
numOfMacros++;
}
log("No of Macros = " + numOfMacros);
}
private void resetElementNumbers(){
numFields = 0;
numVariables = 0;
@ -361,11 +375,12 @@ public class CompletionEngine implements RelevanceConstants{
numEnumerations = 0;
numEnumerators = 0;
numNamespaces = 0;
numMacros = 0;
}
private void addToCompletions (ILookupResult result){
if(result == null)
if(result == null){
log("Lookup Results = null ................. !!! No Lookup Results found !!! ");
return;
}
Iterator nodes = result.getNodes();
int numberOfElements = result.getResultsSize();
@ -379,7 +394,7 @@ public class CompletionEngine implements RelevanceConstants{
return ;
}
private List lookupKeyword(String prefix, int lookupType){
private List lookupKeywords(String prefix, int lookupType){
List result = new ArrayList();
switch (lookupType){
case BASIC_TYPES_KEYWORDS:
@ -408,6 +423,30 @@ public class CompletionEngine implements RelevanceConstants{
return null;
}
}
private List lookupMacros(String prefix){
Set keySet = new TreeSet(macroMap.keySet());
Iterator i = keySet.iterator();
List resultSet = new ArrayList();
while( i.hasNext() )
{
String key = (String) i.next();
String value = "";
if(key.length() > prefix.length()) {
value = key.substring(0, prefix.length());
}else {
value = key;
}
if( value.equalsIgnoreCase( prefix ) ) {
resultSet.add( key );
}
else if( value.compareToIgnoreCase( prefix ) > 0 )
break;
}
return resultSet;
}
private void completionOnMemberReference(IASTCompletionNode completionNode){
// Completing after a dot
// 1. Get the search scope node
@ -428,26 +467,41 @@ public class CompletionEngine implements RelevanceConstants{
IASTScope searchNode = completionNode.getCompletionScope();
ILookupResult result = null;
// lookup fields and methods with the right visibility
IASTNode.LookupKind[] kinds = new IASTNode.LookupKind[8];
kinds[0] = IASTNode.LookupKind.FIELDS;
kinds[1] = IASTNode.LookupKind.METHODS;
kinds[2] = IASTNode.LookupKind.VARIABLES;
kinds[3] = IASTNode.LookupKind.STRUCTURES;
kinds[4] = IASTNode.LookupKind.ENUMERATIONS;
kinds[5] = IASTNode.LookupKind.NAMESPACES;
kinds[6] = IASTNode.LookupKind.FUNCTIONS;
kinds[7] = IASTNode.LookupKind.LOCAL_VARIABLES;
if (completionNode.getCompletionPrefix().length() > 0){
// lookup fields and methods with the right visibility
IASTNode.LookupKind[] kinds = new IASTNode.LookupKind[8];
kinds[0] = IASTNode.LookupKind.FIELDS;
kinds[1] = IASTNode.LookupKind.METHODS;
kinds[2] = IASTNode.LookupKind.VARIABLES;
kinds[3] = IASTNode.LookupKind.STRUCTURES;
kinds[4] = IASTNode.LookupKind.ENUMERATIONS;
kinds[5] = IASTNode.LookupKind.NAMESPACES;
kinds[6] = IASTNode.LookupKind.FUNCTIONS;
kinds[7] = IASTNode.LookupKind.LOCAL_VARIABLES;
result = lookup (searchNode, completionNode.getCompletionPrefix(), kinds, completionNode.getCompletionContext());
addToCompletions (result);
List macros = lookupMacros(completionNode.getCompletionPrefix());
addMacrosToCompletions(macros.iterator());
}
else // prefix is empty
{
// instead of only fields and methods
IASTNode.LookupKind[] kinds = new IASTNode.LookupKind[1];
kinds[0] = IASTNode.LookupKind.THIS;
result = lookup(searchNode, completionNode.getCompletionPrefix(), kinds, completionNode.getCompletionContext());
addToCompletions(result);
}
result = lookup (searchNode, completionNode.getCompletionPrefix(), kinds, completionNode.getCompletionContext());
addToCompletions (result);
}
private void completionOnScopedReference(IASTCompletionNode completionNode){
// 1. Get the search scope node
// the search node is the name before the qualification
IASTScope searchNode = completionNode.getCompletionScope();
// here we have to look for anything that could be referenced within this scope
// 1. lookup local variables, global variables, functions, methods, structures, enums, macros, and namespaces
// 1. lookup local variables, global variables, functions, methods, structures, enums, and namespaces
IASTNode.LookupKind[] kinds = new IASTNode.LookupKind[4];
kinds[0] = IASTNode.LookupKind.VARIABLES;
kinds[1] = IASTNode.LookupKind.STRUCTURES;
@ -482,6 +536,12 @@ public class CompletionEngine implements RelevanceConstants{
completionOnTypeReference(completionNode);
// 2. Get the search scope node
IASTScope searchNode = completionNode.getCompletionScope();
IASTNode.LookupKind[] kinds = new IASTNode.LookupKind[1];
kinds[0] = IASTNode.LookupKind.NAMESPACES;
ILookupResult result = lookup(searchNode, completionNode.getCompletionPrefix(), kinds, completionNode.getCompletionContext());
addToCompletions(result);
// TODO
// 3. provide a template for constructor/ destructor
// 4. lookup methods
@ -497,36 +557,33 @@ public class CompletionEngine implements RelevanceConstants{
private void completionOnVariableType(IASTCompletionNode completionNode){
// 1. basic completion on all types
completionOnTypeReference(completionNode);
IASTScope searchNode = completionNode.getCompletionScope();
IASTNode.LookupKind[] kinds = new IASTNode.LookupKind[1];
kinds[0] = IASTNode.LookupKind.NAMESPACES;
ILookupResult result = lookup(searchNode, completionNode.getCompletionPrefix(), kinds, completionNode.getCompletionContext());
addToCompletions(result);
}
private void completionOnSingleNameReference(IASTCompletionNode completionNode){
// 1. Get the search scope node
// the search node is the code scope inwhich completion is requested
IASTScope searchNode = completionNode.getCompletionScope();
// if prefix is not empty
if (completionNode.getCompletionPrefix().length() > 0){
// here we have to look for any names that could be referenced within this scope
// 1. lookup local variables, global variables, functions, methods, structures, enums, macros, and namespaces
IASTNode.LookupKind[] kinds = new IASTNode.LookupKind[7];
kinds[0] = IASTNode.LookupKind.LOCAL_VARIABLES;
kinds[1] = IASTNode.LookupKind.FIELDS;
kinds[2] = IASTNode.LookupKind.VARIABLES;
kinds[3] = IASTNode.LookupKind.METHODS;
kinds[4] = IASTNode.LookupKind.FUNCTIONS;
kinds[5] = IASTNode.LookupKind.NAMESPACES;
kinds[6] = IASTNode.LookupKind.ENUMERATORS;
ILookupResult result = lookup(searchNode, completionNode.getCompletionPrefix(), kinds, completionNode.getCompletionContext());
addToCompletions(result);
} else // prefix is empty
{
// instead of only fields and methods
// kinds[0] = IASTNode.LookupKind.THIS
IASTNode.LookupKind[] kinds = new IASTNode.LookupKind[3];
kinds[0] = IASTNode.LookupKind.LOCAL_VARIABLES;
kinds[1] = IASTNode.LookupKind.FIELDS;
kinds[2] = IASTNode.LookupKind.METHODS;
ILookupResult result = lookup(searchNode, completionNode.getCompletionPrefix(), kinds, completionNode.getCompletionContext());
addToCompletions(result);
}
// here we have to look for any names that could be referenced within this scope
// 1. lookup local variables, global variables, functions, methods, structures, enums, macros, and namespaces
IASTNode.LookupKind[] kinds = new IASTNode.LookupKind[7];
kinds[0] = IASTNode.LookupKind.LOCAL_VARIABLES;
kinds[1] = IASTNode.LookupKind.FIELDS;
kinds[2] = IASTNode.LookupKind.VARIABLES;
kinds[3] = IASTNode.LookupKind.METHODS;
kinds[4] = IASTNode.LookupKind.FUNCTIONS;
kinds[5] = IASTNode.LookupKind.NAMESPACES;
kinds[6] = IASTNode.LookupKind.ENUMERATORS;
ILookupResult result = lookup(searchNode, completionNode.getCompletionPrefix(), kinds, completionNode.getCompletionContext());
addToCompletions(result);
List macros = lookupMacros(completionNode.getCompletionPrefix());
addMacrosToCompletions(macros.iterator());
}
private void completionOnClassReference(IASTCompletionNode completionNode){
@ -559,10 +616,8 @@ public class CompletionEngine implements RelevanceConstants{
// 1. Get the search scope node
IASTScope searchNode = completionNode.getCompletionScope();
// only look for macros
IASTNode.LookupKind[] kinds = new IASTNode.LookupKind[1];
kinds[0] = IASTNode.LookupKind.MACROS;
ILookupResult result = lookup(searchNode, completionNode.getCompletionPrefix(), kinds, completionNode.getCompletionContext());
addToCompletions(result);
List result = lookupMacros(completionNode.getCompletionPrefix());
addMacrosToCompletions(result.iterator());
}
private void completionOnFunctionReference(IASTCompletionNode completionNode){
// TODO: complete the lookups
@ -579,7 +634,7 @@ public class CompletionEngine implements RelevanceConstants{
private void completionOnKeyword(IASTCompletionNode completionNode){
// lookup every type of keywords
// 1. basic types keword list
List result = lookupKeyword(completionNode.getCompletionPrefix(), BASIC_TYPES_KEYWORDS);
List result = lookupKeywords(completionNode.getCompletionPrefix(), BASIC_TYPES_KEYWORDS);
addKeywordsToCompletions(result.iterator());
}
@ -800,8 +855,6 @@ public class CompletionEngine implements RelevanceConstants{
kindName.append("CONSTRUCTORS");
else if(kind == IASTNode.LookupKind.NAMESPACES)
kindName.append("NAMESPACES");
else if(kind == IASTNode.LookupKind.MACROS)
kindName.append("MACROS");
else if(kind == IASTNode.LookupKind.ENUMERATIONS)
kindName.append("ENUMERATIONS");
else if(kind == IASTNode.LookupKind.ENUMERATORS)