Search in sources :

Example 26 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class CommentParsingSteps method thenBlockStatementInMethodInClassHasTotalContainedComments.

@Then("block statement in method $methodPosition in class $classPosition has $expectedCount total contained comments")
public void thenBlockStatementInMethodInClassHasTotalContainedComments(int methodPosition, int classPosition, int expectedCount) {
    TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1);
    MethodDeclaration methodUnderTest = getMemberByTypeAndPosition(classUnderTest, methodPosition - 1, MethodDeclaration.class);
    BlockStmt blockStmtUnderTest = methodUnderTest.getBody().orElse(null);
    assertThat(blockStmtUnderTest.getAllContainedComments().size(), is(expectedCount));
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Then(org.jbehave.core.annotations.Then)

Example 27 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class CommentParsingSteps method thenBlockStatementInMethodInClassIs.

@Then("block statement in method $methodPosition in class $classPosition orphan comment $commentPosition is \"$expectedContent\"")
public void thenBlockStatementInMethodInClassIs(int methodPosition, int classPosition, int commentPosition, String expectedContent) {
    TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1);
    MethodDeclaration methodUnderTest = getMemberByTypeAndPosition(classUnderTest, methodPosition - 1, MethodDeclaration.class);
    BlockStmt blockStmtUnderTest = methodUnderTest.getBody().orElse(null);
    Comment commentUnderTest = blockStmtUnderTest.getOrphanComments().get(commentPosition - 1);
    assertThat(commentUnderTest.getContent(), is(equalToIgnoringWhiteSpace(expectedContent)));
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Then(org.jbehave.core.annotations.Then)

Example 28 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class ManipulationSteps method whenABlockStmtIsAddedToMethodInClass.

@When("a BlockStmt is added to method $methodPosition in class $classPosition")
public void whenABlockStmtIsAddedToMethodInClass(int methodPosition, int classPosition) {
    CompilationUnit compilationUnit = (CompilationUnit) state.get("cu1");
    MethodDeclaration method = getMethodByPositionAndClassPosition(compilationUnit, methodPosition, classPosition);
    method.setBody(new BlockStmt());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) When(org.jbehave.core.annotations.When)

Example 29 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class ParsingSteps method thenLambdaInStatementInMethodInClassBlockStatement.

@Then("lambda in statement $statementPosition in method $methodPosition in class $classPosition block statement is \"$expectedBody\"")
public void thenLambdaInStatementInMethodInClassBlockStatement(int statementPosition, int methodPosition, int classPosition, String expectedBody) {
    LambdaExpr lambdaExpr = getLambdaExprInStatementInMethodInClass(statementPosition, methodPosition, classPosition);
    BlockStmt blockStmt = lambdaExpr.getBody().asBlockStmt();
    Statement lambdaStmt = blockStmt.getStatement(0);
    assertThat(lambdaStmt.toString(), is(expectedBody));
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Then(org.jbehave.core.annotations.Then)

Example 30 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class NodeRemovalTest method testRemoveStatementFromMethodBody.

@Test
public void testRemoveStatementFromMethodBody() {
    ClassOrInterfaceDeclaration testClass = cu.addClass("testC");
    MethodDeclaration addMethod = testClass.addMethod("testM");
    BlockStmt methodBody = addMethod.createBody();
    Statement addStatement = methodBody.addAndGetStatement("test");
    assertEquals(1, methodBody.getStatements().size());
    boolean remove = addStatement.remove();
    assertEquals(true, remove);
    assertEquals(0, methodBody.getStatements().size());
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Test(org.junit.Test)

Aggregations

BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)39 PropertyMetaModel (com.github.javaparser.metamodel.PropertyMetaModel)12 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)9 SeparatedItemStringBuilder (com.github.javaparser.utils.SeparatedItemStringBuilder)5 Then (org.jbehave.core.annotations.Then)5 CompilationUnit (com.github.javaparser.ast.CompilationUnit)4 Parameter (com.github.javaparser.ast.body.Parameter)4 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)3 VoidType (com.github.javaparser.ast.type.VoidType)3 AClass (scenelib.annotations.el.AClass)3 InitializerDeclaration (com.github.javaparser.ast.body.InitializerDeclaration)2 ReceiverParameter (com.github.javaparser.ast.body.ReceiverParameter)2 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)2 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)2 Statement (com.github.javaparser.ast.stmt.Statement)2 ArrayType (com.github.javaparser.ast.type.ArrayType)2 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 PrimitiveType (com.github.javaparser.ast.type.PrimitiveType)2 ReferenceType (com.github.javaparser.ast.type.ReferenceType)2 Type (com.github.javaparser.ast.type.Type)2