package bluej.parser;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import bluej.JavaFXThreadingRule;
import bluej.editor.moe.ScopeColors;
import bluej.debugger.gentype.FieldReflective;
import bluej.debugger.gentype.GenTypeClass;
import bluej.debugger.gentype.GenTypeSolid;
import bluej.debugger.gentype.JavaType;
import bluej.debugger.gentype.MethodReflective;
import bluej.debugger.gentype.Reflective;
import bluej.editor.moe.MoeSyntaxDocument;
import bluej.parser.entity.ClassLoaderResolver;
import bluej.parser.entity.EntityResolver;
import bluej.parser.entity.JavaEntity;
import bluej.parser.entity.PackageResolver;
import bluej.parser.entity.TypeEntity;
import bluej.parser.lexer.LocatableToken;
import bluej.parser.nodes.ParsedCUNode;
import bluej.pkgmgr.JavadocResolver;
import bluej.utility.JavaReflective;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import static org.junit.Assert.*;
public class CompletionTest
{
@Rule
public JavaFXThreadingRule javafxRule = new JavaFXThreadingRule();
@BeforeClass
public static void initConfig()
{
InitConfig.init();
}
private TestEntityResolver resolver;
@Before
public void setUp() throws Exception
{
resolver = new TestEntityResolver(new ClassLoaderResolver(this.getClass().getClassLoader()));
}
| Generate a compilation unit node based on some source code.
|
private ParsedCUNode cuForSource(String sourceCode, String pkg)
{
return documentForSource(sourceCode, pkg).getParser();
}
| Get a MoeSyntaxDocument (with parser enabled) for the given source code.
|
private MoeSyntaxDocument documentForSource(String sourceCode, String pkg)
{
EntityResolver presolver = new PackageResolver(resolver, pkg);
MoeSyntaxDocument document = new MoeSyntaxDocument(presolver, ScopeColors.dummy());
document.enableParser(true);
document.insertString(0, sourceCode);
return document;
}
| Basic field access test.
|
@Test
public void testFieldAccess()
{
String aClassSrc = "class A {" +
" public static int f = 0;" +
"}";
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
EntityResolver resolver = new PackageResolver(this.resolver, "");
JavaEntity aClassEnt = resolver.resolvePackageOrClass("A", null);
assertNotNull(aClassEnt);
TypeEntity typeEnt = aClassEnt.resolveAsType();
assertNotNull(typeEnt);
GenTypeClass gtc = typeEnt.getClassType();
assertNotNull(gtc);
assertEquals("A", gtc.toString());
FieldReflective fref = gtc.getReflective().getDeclaredFields().get("f");
assertNotNull(fref);
assertEquals("int", fref.getType().toString());
assertEquals(Modifier.PUBLIC | Modifier.STATIC, fref.getModifiers());
}
| Field access - array declarators after field name
|
@Test
public void testArrayFieldAccess()
{
String aClassSrc = "class A {" +
" public static int f[] = null;" +
"}";
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
EntityResolver resolver = new PackageResolver(this.resolver, "");
JavaEntity aClassEnt = resolver.resolvePackageOrClass("A", null);
Map<String,FieldReflective> fields = aClassEnt.resolveAsType().getClassType().getReflective().getDeclaredFields();
JavaType ftype = fields.get("f").getType();
assertEquals("int[]", ftype.toString());
}
@Test
public void testCompletionAfterArrayElement() throws Exception
{
String aClassSrc = "class A {\n" +
" public static String s[] = null;\n" +
" void m() {\n" +
" s[0].length();\n" +
" }\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(67, doc);
assertNotNull(suggests);
assertEquals("java.lang.String", suggests.getSuggestionType().toString());
}
| Test multiple field declarations in one statement.
|
@Test
public void testMultiFieldAccess()
{
String aClassSrc = "class A {" +
" public static int f, g[] = null;" +
"}";
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
EntityResolver resolver = new PackageResolver(this.resolver, "");
JavaEntity aClassEnt = resolver.resolvePackageOrClass("A", null);
Reader r = new StringReader("");
CompletionParser cp = new CompletionParser(resolver, r, aClassEnt);
cp.parseExpression();
Map<String,FieldReflective> fields = aClassEnt.resolveAsType().getClassType().getReflective().getDeclaredFields();
JavaType ftype = fields.get("f").getType();
assertEquals("int", ftype.toString());
ftype = fields.get("g").getType();
assertNotNull(ftype);
assertEquals("int[]", ftype.toString());
}
| Access of a static field from another class
|
@Test
public void test2()
{
String aClassSrc = "class A {" +
" public static int f = 0;" +
"}";
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
String bClassSrc = "class B { }";
ParsedCUNode bNode = cuForSource(bClassSrc, "");
resolver.addCompilationUnit("", bNode);
EntityResolver resolver = new PackageResolver(this.resolver, "");
JavaEntity bClassEnt = resolver.resolvePackageOrClass("B", null);
Reader r = new StringReader("A.");
CompletionParser cp = new CompletionParser(bNode, r, bClassEnt);
cp.parseExpression();
Map<String,FieldReflective> fields = cp.getSuggestionType().asClass().getReflective().getDeclaredFields();
JavaType ftype = fields.get("f").getType();
assertEquals("int", ftype.toString());
}
| Completion from an expression involving a local variable
|
@Test
public void test3() throws Exception
{
String aClassSrc = "class A {\n" +
"void someMethod() {\n" +
" Object b = new Object();\n" +
" int a = b.hashCode();\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(73, doc);
assertNotNull(suggests);
assertEquals("java.lang.Object", suggests.getSuggestionType().toString());
}
| Completion from an expression involving a local variable declared as "var"
|*/
@Test
public void test3var() throws Exception
{
String aClassSrc = "class A {\n" + // 10
"void someMethod() {\n" + // +20 = 30
" var b = new Object();\n" + // +26 = 56
" int a = b.hashCode();\n" + // int a = b. <-- 70
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(70, doc);
assertNotNull(suggests);
assertEquals("java.lang.Object", suggests.getSuggestionType().toString());
}
/**
* Completion from an expression involving a local variable declared as "var"
*/
@Test
public void test4var() throws Exception
{
String aClassSrc = "class A {\n" +
"void someMethod() {\n" +
" var b = \"hello\";\n" +
" int a = b.length();\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(aClassSrc.indexOf("length()"), doc);
assertNotNull(suggests);
assertEquals("java.lang.String", suggests.getSuggestionType().toString());
}
/**
* Completion from an expression involving a local variable declared as "var"
*/
@Test
public void test5var() throws Exception
{
String aClassSrc = "class A {\n" +
"void someMethod() {\n" +
" var b = java.util.List.of(\"hello\");\n" +
" int a = b.get(0).length();\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggestsList = aNode.getExpressionType(aClassSrc.indexOf("get(0)"), doc);
assertNotNull(suggestsList);
assertEquals("java.util.List<java.lang.String>", suggestsList.getSuggestionType().toString());
ExpressionTypeInfo suggests = aNode.getExpressionType(aClassSrc.indexOf("length()"), doc);
assertNotNull(suggests);
assertEquals("java.lang.String", suggests.getSuggestionType().toString());
}
/** Test that a for-loop initializer creates a recognized variable */
@Test
public void testForInitializer() throws Exception
|
|{
|
|String aClassSrc = "class A {\n" + // 10
"void someMethod() {\n" + // +20 = 30
" for (Object o = null ; ; ) {\n" + // +33 = 63
" o.wait();\n" + // o. <-- 73
" }" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(73, doc);
assertNotNull(suggests);
assertEquals("java.lang.Object", suggests.getSuggestionType().toString());
}
/**
* Test that a for-loop initializer, declared with "var", creates a recognised variable
*/
@Test
public void testForInitializerVar() throws Exception
{
String aClassSrc = "class A {\n" + // 10
"void someMethod() {\n" + // +20 = 30
" for (var o = new Object() ; ; ) {\n" + // +38 = 68
" o.wait();\n" + // o. <-- 78
" }" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(78, doc);
assertNotNull(suggests);
assertEquals("java.lang.Object", suggests.getSuggestionType().toString());
}
/**
* Test a variable reference.
*/
@Test
public void testVariableRef() throws Exception
{
String aClassSrc = "class A {\n" + // 10
"void someMethod() {\n" + // +20 = 30
" String s = \"hello\";\n" + // +24 = 54
" s.length();" + // s. <-- 60
" }" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(60, doc);
assertNotNull(suggests);
assertEquals("java.lang.String", suggests.getSuggestionType().toString());
}
/**
* Check that forward variable references aren't allowed
*/
@Test
public void testNoBwardVarRef() throws Exception
|
|{
|
|String aClassSrc = "class A {\n" + // 10
"void someMethod() {\n" + // +20 = 30
" int a = b.hashCode();\n" + // int a = b. <-- 44
" Object b = new Object();\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(44, doc);
assertNull(suggests);
}
/**
* Test an expression referencing the containing class, and make sure it resolves
| correctly.
|
@Test
public void testSelfRef() throws Exception
{
String aClassSrc = "package abc;\n" +
"class A {\n" +
"void someMethod() {\n" +
" new A().hashCode();\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("abc", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(44, doc);
assertNotNull(suggests);
assertEquals("abc.A", suggests.getSuggestionType().toString());
}
| Completion from an expression involving inner classes accessing variables
| within the inner class
|
@Test
public void testInnerClasses() throws Exception
{
String aClassSrc = "class A {\n" +
"String test=\"fg\";\n" +
"class B {\n" +
"Integer temp=new Integer(\"1\");\n" +
"void bluej() {\n" +
"temp.hashCode();\n" +
"}\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(89, doc);
assertNotNull(suggests);
assertEquals("java.lang.Integer", suggests.getSuggestionType().toString());
GenTypeClass accessType = suggests.getAccessType();
assertNotNull(accessType);
assertEquals("A$B", accessType.getReflective().getName());
}
| Completion from an expression involving inner classes accessing variables
| within the outer class
|
@Test
public void testInnerClasses2() throws Exception
{
String aClassSrc = "class A {\n" +
"Integer test=new Integer(\"1\");\n" +
"class B {\n" +
"String temp=\"fg\";\n" +
"void bluet() {\n" +
"test.hashCode();\n" +
"}\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(89, doc);
assertNotNull(suggests);
assertEquals("java.lang.Integer", suggests.getSuggestionType().toString());
GenTypeClass accessType = suggests.getAccessType();
assertNotNull(accessType);
assertEquals("A$B", accessType.getReflective().getName());
}
@Test
public void testInnerClasses3() throws Exception
{
String aClassSrc =
"class A {\n" +
" Runnable r = new Runnable() {\n" +
" String x = \"\";\n" +
" public void run() {\n" +
" x.length();\n" +
" }\n" +
" };\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(93, doc);
assertNotNull(suggests);
assertEquals("java.lang.String", suggests.getSuggestionType().toString());
GenTypeClass accessType = suggests.getAccessType();
assertNotNull(accessType);
Reflective outer = accessType.getReflective().getOuterClass();
assertNotNull(outer);
assertEquals("A", outer.getName());
}
@Test
public void testInnerClasses4() throws Exception
{
String aClassSrc =
"class A {\n" +
" Runnable r = new Thread(new String(\"xxxx\")) {\n" +
" String x = \"\";\n" +
" public void run() {\n" +
" this.run();\n" +
" }\n" +
" };\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(109, doc);
assertNotNull(suggests);
GenTypeClass suggestsType = suggests.getSuggestionType().asClass();
assertNotNull(suggestsType);
List<GenTypeClass> supers = suggestsType.getReflective().getSuperTypes();
assertEquals(1, supers.size());
assertEquals("java.lang.Thread", supers.get(0).toString());
}
@Test
public void testPartial() throws Exception
{
String aClassSrc = "class A {\n" +
"String s = \"\";\n" +
"public void m() {\n" +
" s.c\n" +
"abcd()\n" +
"}}";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(48, doc);
assertNotNull(suggests);
assertEquals("java.lang.String", suggests.getSuggestionType().toString());
assertNotNull(suggests.getSuggestionToken());
assertEquals("c", suggests.getSuggestionToken().getText());
}
| This is a regression test. Attempting code completion just after a semicolon
| could cause an exception.
|
@Test
public void testAfterStatement() throws Exception
{
String aClassSrc = "class A {\n" +
"public void m() {\n" +
" this(one,two,three);\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(49, doc);
assertNotNull(suggests);
assertEquals("A", suggests.getSuggestionType().toString());
}
@Test
public void testThisDot() throws Exception
{
String aClassSrc = "class A {\n" +
"public void m() {\n" +
" this.\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(35, doc);
assertNotNull(suggests);
assertEquals("A", suggests.getSuggestionType().toString());
assertFalse(suggests.isStatic());
}
@Test
public void testTparCompletion() throws Exception
{
String aClassSrc = "class A<T extends String & Runnable> {\n" +
"public void m(T t) {\n" +
" (t+4).\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(68, doc);
assertNotNull(suggests);
assertEquals("java.lang.String", suggests.getSuggestionType().toString());
assertFalse(suggests.isStatic());
}
@Test
public void testTparCompletion2() throws Exception
{
String aClassSrc = "class A<T extends String & Runnable> {\n" +
"public void m(T t) {\n" +
" (t+4).\n" +
"}\n" +
"}\n";
MoeSyntaxDocument doc = documentForSource(aClassSrc, "");
ParsedCUNode aNode = doc.getParser();
resolver.addCompilationUnit("", aNode);
doc.remove(8, 1);
doc.insertString(8, "U");
doc.getParser();
doc.remove(53, 1);
doc.insertString(53, "U");
aNode = doc.getParser();
ExpressionTypeInfo suggests = aNode.getExpressionType(68, doc);
assertNotNull(suggests);
assertEquals("java.lang.String", suggests.getSuggestionType().toString());
assertFalse(suggests.isStatic());
}
@Test
public void testTparCompletion3() throws Exception
{
String aClassSrc = "class A<T extends String & Runnable> {\n" +
"public void m(T t) {\n" +
" (t+4).\n" +
"}\n" +
"}\n";
MoeSyntaxDocument doc = documentForSource(aClassSrc, "");
ParsedCUNode aNode = doc.getParser();
resolver.addCompilationUnit("", aNode);
doc.remove(53, 1);
doc.insertString(53, "U");
doc.getParser();
doc.remove(8, 1);
doc.insertString(8, "U");
aNode = doc.getParser();
ExpressionTypeInfo suggests = aNode.getExpressionType(68, doc);
assertNotNull(suggests);
assertEquals("java.lang.String", suggests.getSuggestionType().toString());
assertFalse(suggests.isStatic());
}
@Test
public void testTparCompletion4() throws Exception
{
String aClassSrc = "class A<T extends String, U extends T> {\n" +
"public void m(U u) {\n" +
" u.\n" +
"}\n" +
"}\n";
MoeSyntaxDocument doc = documentForSource(aClassSrc, "");
ParsedCUNode aNode = doc.getParser();
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(66, doc);
assertNotNull(suggests);
GenTypeSolid stsolid = suggests.getSuggestionType().asSolid();
assertNotNull(stsolid);
GenTypeClass [] stypes = stsolid.getReferenceSupertypes();
assertEquals(1, stypes.length);
assertEquals("java.lang.String", stypes[0].toString());
assertFalse(suggests.isStatic());
}
@Test
public void testTparCompletion5() throws Exception
{
String aClassSrc = "class A {\n" +
"public <T extends String, U extends T> void m(U u) {\n" +
" u.\n" +
"}\n" +
"}\n";
MoeSyntaxDocument doc = documentForSource(aClassSrc, "");
ParsedCUNode aNode = doc.getParser();
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(67, doc);
assertNotNull(suggests);
GenTypeSolid stsolid = suggests.getSuggestionType().asSolid();
assertNotNull(stsolid);
GenTypeClass [] stypes = stsolid.getReferenceSupertypes();
assertEquals(1, stypes.length);
assertEquals("java.lang.String", stypes[0].toString());
assertFalse(suggests.isStatic());
}
@Test
public void testCompletionOnKeyword1() throws Exception
{
String aClassSrc = "class A {\n" +
"public void m() {\n" +
" this.for\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(38, doc);
assertNotNull(suggests);
assertEquals("A", suggests.getSuggestionType().toString());
assertFalse(suggests.isStatic());
LocatableToken stoken = suggests.getSuggestionToken();
assertNotNull(stoken);
assertEquals("for", stoken.getText());
}
@Test
public void testCompletionOnKeyword2() throws Exception
{
String aClassSrc = "class A {\n" +
"public void m() {\n" +
" this.new\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(38, doc);
assertNotNull(suggests);
assertEquals("A", suggests.getSuggestionType().toString());
assertFalse(suggests.isStatic());
LocatableToken stoken = suggests.getSuggestionToken();
assertNotNull(stoken);
assertEquals("new", stoken.getText());
}
@Test
public void testCompletionOnKeyword3() throws Exception
{
String aClassSrc = "class A {\n" +
"public void m() {\n" +
" new\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(33, doc);
assertNotNull(suggests);
assertEquals("A", suggests.getSuggestionType().toString());
assertFalse(suggests.isStatic());
LocatableToken stoken = suggests.getSuggestionToken();
assertNotNull(stoken);
assertEquals("new", stoken.getText());
}
@Test
public void testCompletionOnKeyword4() throws Exception
{
String aClassSrc = "class A {\n" +
"public void m() {\n" +
" for\n" +
"}\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(33, doc);
assertNotNull(suggests);
assertEquals("A", suggests.getSuggestionType().toString());
assertFalse(suggests.isStatic());
LocatableToken stoken = suggests.getSuggestionToken();
assertNotNull(stoken);
assertEquals("for", stoken.getText());
}
@Test
public void testCompletionResolution() throws Exception
{
String canvasSrc = "class Canvas { }\n";
String aClassSrc =
"import java.awt.*;\n" +
"class A {\n" +
" Canvas canvas;\n" +
" public void m() {\n" +
" canvas.\n" +
" }\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode canvasNode = cuForSource(canvasSrc, "");
resolver.addCompilationUnit("", canvasNode);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(77, doc);
assertNotNull(suggests);
assertEquals("Canvas", suggests.getSuggestionType().toString());
assertFalse(suggests.isStatic());
LocatableToken stoken = suggests.getSuggestionToken();
assertNull(stoken);
}
@Test
public void testCompletionResolution2() throws Exception
{
String aClassSrc =
"import static javax.swing.text.DefaultEditorKit.*;\n" +
"class A {\n" +
" BeepAction ba;\n" +
" public void m() {\n" +
" ba.\n" +
" }\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(105, doc);
assertNotNull(suggests);
assertEquals("javax.swing.text.DefaultEditorKit.BeepAction", suggests.getSuggestionType().toString());
assertFalse(suggests.isStatic());
LocatableToken stoken = suggests.getSuggestionToken();
assertNull(stoken);
}
@Test
public void testCompletionInArrayElement() throws Exception
{
String aClassSrc =
"class A {\n" +
" public void m() {\n" +
" short[] array = new short[s\n" +
" }\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(61, doc);
assertNotNull(suggests);
assertEquals("A", suggests.getSuggestionType().toString());
assertEquals("s", suggests.getSuggestionToken().getText());
}
@Test
public void testCompletionOnGenericType() throws Exception
{
String aClassSrc =
"class A {\n" +
" public void m() {\n" +
" java.util.List<?> l = new java.util.LinkedList<Object>();\n" +
" l.size();\n" +
" }\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(98, doc);
assertNotNull(suggests);
assertEquals("java.util.List<?>", suggests.getSuggestionType().toString());
AssistContent [] assists = ParseUtils.getPossibleCompletions(suggests, new JavadocResolver() {
public String getJavadoc(String moduleName, String name)
{
throw new IllegalStateException();
}
public void getJavadoc(Reflective declType, Collection<? extends ConstructorOrMethodReflective> methods)
{
for (ConstructorOrMethodReflective method : methods) {
assertNotNull(((MethodReflective)method).getReturnType().getErasedType());
}
}
@Override
public boolean getJavadocAsync(ConstructorOrMethodReflective method,
AsyncCallback callback, Executor executor)
{
throw new RuntimeException("Not implemented in test stub.");
}
}, null);
for (AssistContent assist : assists) {
assist.getJavadoc();
}
}
| Regression test for bug #288
|
@Test
public void testInterNewCompletion() throws Exception
{
String aClassSrc =
"class A {\n" +
" public void m() {\n" +
" callMe(new Runnable() { });\n" +
" }\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(53, doc);
assertNotNull(suggests);
}
@Test
public void testPartialExpressionCompletion() throws Exception
{
String aClassSrc =
"class A {\n" +
" public void g() {\n" +
" String s = \"\";\n" +
" s.l\n" +
" s.length();\n" +
" }\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(56, doc);
assertNotNull(suggests);
}
@Test
public void testRegression312() throws Exception
{
String aClassSrc =
"class A extends javax.swing.JFrame {\n" +
" public void g() {\n" +
" }\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(57, doc);
assertNotNull(suggests);
AssistContent[] acontent = ParseUtils.getPossibleCompletions(suggests, new JavadocResolver() {
@Override
public void getJavadoc(Reflective declType, Collection<? extends ConstructorOrMethodReflective> method)
{
}
@Override
public String getJavadoc(String moduleName, String typeName)
{
throw new RuntimeException("Not implemented in test stub.");
}
@Override
public boolean getJavadocAsync(ConstructorOrMethodReflective method,
AsyncCallback callback, Executor executor)
{
throw new RuntimeException("Not implemented in test stub.");
}
}, null);
assertNotNull(acontent);
}
@Test
public void testRegression340() throws Exception
{
String aClassSrc =
"class A {\n" +
" public void g() {\n" +
" someMethod(new int[] {new String().length, 45});\n" +
" }\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(80, doc);
assertNotNull(suggests);
}
@Test
public void testCompletionAfterAnonClass() throws Exception
{
String aClassSrc =
"class A {\n" +
" public void g() {\n" +
" new Thread() {\n" +
" public void run() {\n" +
" int x = 5 + 6;\n" +
" }\n" +
" }.start();\n" +
" }\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(112, doc);
assertNotNull(suggests);
assertTrue(new GenTypeClass(new JavaReflective(Thread.class)).isAssignableFrom(suggests.getSuggestionType()));
}
@Test
public void testAfterArrayInitList() throws Exception
{
String aClassSrc =
"class A {\n" +
" public void g() {\n" +
" int l = new String[]{\"one\",\"two\"}.length;\n" +
" }\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(68, doc);
assertNotNull(suggests);
assertEquals("java.lang.String[]", suggests.getSuggestionType().toString());
}
@Test
public void test360() throws Exception
{
String cSrc = "package tpkg;\n" +
"class C {\n" +
" public static void doNothing() {}\n" +
"}\n";
ParsedCUNode cNode = documentForSource(cSrc, "tpkg").getParser();
resolver.addCompilationUnit("tpkg", cNode);
String aSrc = "import tpkgxx.*;\n" +
"class A {\n" +
" public A() {\n" +
" class B {\n" +
" public B() {\n" +
" C.\n" +
" }\n" +
" }\n" +
"}\n";
MoeSyntaxDocument aDoc = documentForSource(aSrc, "");
ParsedCUNode aNode = aDoc.getParser();
ExpressionTypeInfo suggests = aNode.getExpressionType(85, aDoc);
assertEquals(".", aDoc.getText(84, 1));
assertNull(suggests);
aDoc.remove(11, 2);
aNode = aDoc.getParser();
suggests = aNode.getExpressionType(83, aDoc);
assertNotNull(suggests);
assertEquals("tpkg.C", suggests.getSuggestionType().toString());
}
@Test
public void testVarargsParam() throws Exception
{
String aClassSrc =
"class A {\n" +
" public void g(String ... s) {\n" +
" System.out.print(s.length);\n" +
" }\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(66, doc);
assertNotNull(suggests);
assertEquals("java.lang.String[]", suggests.getSuggestionType().toString());
}
@Test
public void testMultiFieldDecWithInner() throws Exception
{
String aClassSrc =
"class A {\n" +
" Runnable r, b = new Runnable() {\n" +
" public void run() {\n" +
" c.run();\n" +
" }\n" +
" }, c = new Thread() {;\n" +
" public void run() {\n" +
" b.run();\n" +
" }\n" +
" };\n" +
"}\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
ExpressionTypeInfo suggests = aNode.getExpressionType(77, doc);
assertNotNull(suggests);
assertEquals("java.lang.Runnable", suggests.getSuggestionType().toString());
suggests = aNode.getExpressionType(148, doc);
assertNotNull(suggests);
assertEquals("java.lang.Runnable", suggests.getSuggestionType().toString());
}
@Test
public void testRegression571()
{
String aClassSrc =
"static int count = 0;\n";
PlainDocument doc = new PlainDocument();
doc.insertString(0, aClassSrc);
ParsedCUNode aNode = cuForSource(aClassSrc, "");
resolver.addCompilationUnit("", aNode);
aNode.getExpressionType(13, doc);
}
class PlainDocument
extends MoeSyntaxDocument
{
public PlainDocument()
{
super(ScopeColors.dummy());
}
}
}
top,
use,
map,
class CompletionTest
. initConfig
. setUp
. cuForSource
. documentForSource
. testFieldAccess
. testArrayFieldAccess
. testCompletionAfterArrayElement
. testMultiFieldAccess
. test2
. test3
. testSelfRef
. testInnerClasses
. testInnerClasses2
. testInnerClasses3
. testInnerClasses4
. testPartial
. testAfterStatement
. testThisDot
. testTparCompletion
. testTparCompletion2
. testTparCompletion3
. testTparCompletion4
. testTparCompletion5
. testCompletionOnKeyword1
. testCompletionOnKeyword2
. testCompletionOnKeyword3
. testCompletionOnKeyword4
. testCompletionResolution
. testCompletionResolution2
. testCompletionInArrayElement
. testCompletionOnGenericType
. getJavadoc
. getJavadoc
. getJavadocAsync
. testInterNewCompletion
. testPartialExpressionCompletion
. testRegression312
. getJavadoc
. getJavadoc
. getJavadocAsync
. testRegression340
. testCompletionAfterAnonClass
. testAfterArrayInitList
. test360
. testVarargsParam
. testMultiFieldDecWithInner
. testRegression571
top,
use,
map,
class PlainDocument
. PlainDocument
1203 neLoCode
+ 20 LoComm