Add additional tests

* Parse problem with switch fallthrough
* Verify type of switch expression
This commit is contained in:
Andreas Dangel
2019-03-16 09:22:51 +01:00
parent 2793c3e029
commit ca0975b68f
4 changed files with 22 additions and 1 deletions

View File

@ -7,7 +7,7 @@
package net.sourceforge.pmd.lang.java.ast;
public class ASTSwitchExpression extends AbstractJavaNode {
public class ASTSwitchExpression extends AbstractJavaTypeNode {
ASTSwitchExpression(int id) {
super(id);
}

View File

@ -85,6 +85,8 @@ public class Java12Test {
Assert.assertEquals(6, switchExpression.jjtGetNumChildren());
Assert.assertTrue(switchExpression.jjtGetChild(0) instanceof ASTExpression);
Assert.assertEquals(5, switchExpression.findChildrenOfType(ASTSwitchLabeledRule.class).size());
Assert.assertEquals(Integer.TYPE, switchExpression.getType());
}
}

View File

@ -274,6 +274,14 @@ public class ParserCornersTest {
Assert.assertTrue("Test setup wrong - variable 'someVarNameSameAsMethodReference' not found anymore!", foundVariable);
}
@Test
public void testSwitchWithFallthrough() throws Exception {
ASTCompilationUnit compilationUnit = ParserTstUtil.parseAndTypeResolveJava("11", readAsString("SwitchWithFallthrough.java"));
Assert.assertNotNull(compilationUnit);
ASTSwitchStatement switchStatement = compilationUnit.getFirstDescendantOfType(ASTSwitchStatement.class);
Assert.assertEquals(2, switchStatement.findChildrenOfType(ASTSwitchLabel.class).size());
}
private String readAsString(String resource) {
try (InputStream in = ParserCornersTest.class.getResourceAsStream(resource)) {
return IOUtils.toString(in, StandardCharsets.UTF_8);

View File

@ -0,0 +1,11 @@
public class SwitchWithFallthrough {
// only fall through, no block statements.
public void myMethod() {
int a = 1;
switch (a) {
case 1:
default:
}
}
}