Java, typeres: add vararg tests for method resolution

This commit is contained in:
Bendegúz Nagy
2017-07-08 19:31:05 +02:00
parent 65be8c6afb
commit 176ccb4533
2 changed files with 50 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import java.util.StringTokenizer;
import net.sourceforge.pmd.typeresolution.testdata.MethodFirstPhase;
import net.sourceforge.pmd.typeresolution.testdata.MethodMostSpecific;
import net.sourceforge.pmd.typeresolution.testdata.MethodThirdPhase;
import net.sourceforge.pmd.typeresolution.testdata.MethodSecondPhase;
import org.apache.commons.io.IOUtils;
import org.jaxen.JaxenException;
@ -1302,6 +1303,34 @@ public class ClassTypeResolverTest {
assertEquals("All expressions not tested", index, expressions.size());
}
@Test
public void testMethodThirdPhase() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(MethodThirdPhase.class);
List<AbstractJavaTypeNode> expressions = convertList(
acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"),
AbstractJavaTypeNode.class);
int index = 0;
// Exception a = vararg(10, (Number) null, (Number) null);
assertEquals(Exception.class, expressions.get(index).getType());
assertEquals(Exception.class, getChildType(expressions.get(index), 0));
assertEquals(Exception.class, getChildType(expressions.get(index++), 1));
// Exception b = vararg(10);
assertEquals(Exception.class, expressions.get(index).getType());
assertEquals(Exception.class, getChildType(expressions.get(index), 0));
assertEquals(Exception.class, getChildType(expressions.get(index++), 1));
// int c = vararg(10, "", "", "");
assertEquals(int.class, expressions.get(index).getType());
assertEquals(int.class, getChildType(expressions.get(index), 0));
assertEquals(int.class, getChildType(expressions.get(index++), 1));
// Make sure we got them all
assertEquals("All expressions not tested", index, expressions.size());
}
private Class<?> getChildType(Node node, int childIndex) {

View File

@ -0,0 +1,21 @@
package net.sourceforge.pmd.typeresolution.testdata;
public class MethodThirdPhase {
void test() {
// more args than parameters
Exception a = vararg(10, (Number) null, (Number) null);
// less args than parameters
Exception b = vararg(10);
// component type determined properly
int c = vararg(10, "", "", "");
// TODO: add most specific tests among vararg conversion
}
Exception vararg(int a, Number... b) { return null; }
int vararg(int a, String c, String... b) {return 0; }
}