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

JUnits for resolve prefix.

This commit is contained in:
Doug Schaefer 2005-04-18 17:07:03 +00:00
parent ec7b7d4076
commit 241c3fcffe
4 changed files with 160 additions and 0 deletions

View file

@ -15,6 +15,7 @@ import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.parser.tests.ParserTestSuite;
import org.eclipse.cdt.core.parser.tests.prefix.CompletionTestSuite;
/**
* @author jcamelon
@ -42,6 +43,7 @@ public class DOMParserTestSuite extends TestCase {
suite.addTestSuite( AST2CPPSpecFailingTest.class );
suite.addTestSuite( AST2CSpecTest.class );
suite.addTestSuite( AST2CSpecFailingTest.class );
suite.addTest( CompletionTestSuite.suite() );
return suite;
}

View file

@ -0,0 +1,49 @@
/**********************************************************************
* Copyright (c) 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
**********************************************************************/
package org.eclipse.cdt.core.parser.tests.prefix;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IVariable;
public class BasicCompletionTest extends CompletionTestBase {
public void testVar() throws Exception {
StringBuffer code = new StringBuffer();
code.append("int blah = 4;");
code.append("int two = bl");
ASTCompletionNode node = getGPPCompletionNode(code.toString());
IASTName[] names = node.getNames();
assertEquals(1, names.length);
IBinding[] bindings = names[0].resolvePrefix();
assertEquals(1, bindings.length);
IVariable var = (IVariable)bindings[0];
assertEquals("blah", var.getName());
}
public void testFunction() throws Exception {
StringBuffer code = new StringBuffer();
code.append("void func(int x);");
code.append("void func2() { fu");
ASTCompletionNode node = getGPPCompletionNode(code.toString());
IASTName[] names = node.getNames();
// There are two names, one as an expression, one as a declaration
assertEquals(2, names.length);
// The declaration name is not hooked up to the TU due to backtrack
assertNull(names[1].getTranslationUnit());
// The points to our functions
IBinding[] bindings = names[0].resolvePrefix();
// There should be two since they both start with fu
assertEquals(2, bindings.length);
assertEquals("func", ((IFunction)bindings[0]).getName());
assertEquals("func2", ((IFunction)bindings[1]).getName());
}
}

View file

@ -0,0 +1,88 @@
/**********************************************************************
* Copyright (c) 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
**********************************************************************/
package org.eclipse.cdt.core.parser.tests.prefix;
import junit.framework.TestCase;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.internal.core.dom.parser.ISourceCodeParser;
import org.eclipse.cdt.internal.core.dom.parser.c.ANSICParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.dom.parser.c.GCCParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.dom.parser.c.GNUCSourceParser;
import org.eclipse.cdt.internal.core.dom.parser.c.ICParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ANSICPPParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.GNUCPPSourceParser;
import org.eclipse.cdt.internal.core.dom.parser.cpp.GPPParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.parser.ParserException;
import org.eclipse.cdt.internal.core.parser.scanner2.DOMScanner;
import org.eclipse.cdt.internal.core.parser.scanner2.FileCodeReaderFactory;
import org.eclipse.cdt.internal.core.parser.scanner2.GCCScannerExtensionConfiguration;
import org.eclipse.cdt.internal.core.parser.scanner2.GPPScannerExtensionConfiguration;
import org.eclipse.cdt.internal.core.parser.scanner2.IScannerExtensionConfiguration;
public class CompletionTestBase extends TestCase {
private static final IParserLogService NULL_LOG = new NullLogService();
protected ASTCompletionNode getCompletionNode(String code, ParserLanguage lang, boolean useGNUExtensions) throws ParserException {
CodeReader codeReader = new CodeReader(code.toCharArray());
ScannerInfo scannerInfo = new ScannerInfo();
IScannerExtensionConfiguration configuration = null;
if( lang == ParserLanguage.C )
configuration = new GCCScannerExtensionConfiguration();
else
configuration = new GPPScannerExtensionConfiguration();
IScanner scanner = new DOMScanner( codeReader, scannerInfo, ParserMode.COMPLETE_PARSE, lang, NULL_LOG, configuration, FileCodeReaderFactory.getInstance() );
ISourceCodeParser parser = null;
if( lang == ParserLanguage.CPP )
{
ICPPParserExtensionConfiguration config = null;
if (useGNUExtensions)
config = new GPPParserExtensionConfiguration();
else
config = new ANSICPPParserExtensionConfiguration();
parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG,
config );
}
else
{
ICParserExtensionConfiguration config = null;
if (useGNUExtensions)
config = new GCCParserExtensionConfiguration();
else
config = new ANSICParserExtensionConfiguration();
parser = new GNUCSourceParser( scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG, config );
}
scanner.setContentAssistMode(code.length());
parser.parse();
return parser.getCompletionNode();
}
protected ASTCompletionNode getGPPCompletionNode(String code) throws ParserException {
return getCompletionNode(code, ParserLanguage.CPP, true);
}
protected ASTCompletionNode getGCCCompletionNode(String code) throws ParserException {
return getCompletionNode(code, ParserLanguage.C, true);
}
}

View file

@ -0,0 +1,21 @@
/**********************************************************************
* Copyright (c) 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
**********************************************************************/
package org.eclipse.cdt.core.parser.tests.prefix;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class CompletionTestSuite extends TestCase {
public static Test suite() {
TestSuite suite= new TestSuite(CompletionTestSuite.class.getName());
suite.addTestSuite(BasicCompletionTest.class);
return suite;
}
}