Search in sources :

Example 91 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeGeneralForStatement_basicIsNotDecl.

@Test
public void writeGeneralForStatement_basicIsNotDecl() {
    AssignmentExpr assignExpr = createAssignmentExpr("x", "3", TypeNode.INT);
    Statement assignExprStatement = ExprStatement.withExpr(assignExpr);
    List<Statement> body = Arrays.asList(assignExprStatement, assignExprStatement);
    VariableExpr localVarExpr = createVariableExpr("i", TypeNode.INT);
    ValueExpr initValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setValue("1").setType(TypeNode.INT).build());
    ValueExpr maxValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setValue("10").setType(TypeNode.INT).build());
    GeneralForStatement forStatement = GeneralForStatement.incrementWith(localVarExpr, initValueExpr, maxValueExpr, body);
    forStatement.accept(writerVisitor);
    assertEquals(String.format("%s%s%s%s", "for (i = 1; i < 10; i++) {\n", "int x = 3;\n", "int x = 3;\n", "}\n"), writerVisitor.write());
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) BlockStatement(com.google.api.generator.engine.ast.BlockStatement) BreakStatement(com.google.api.generator.engine.ast.BreakStatement) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) ForStatement(com.google.api.generator.engine.ast.ForStatement) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) GeneralForStatement(com.google.api.generator.engine.ast.GeneralForStatement) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) SynchronizedStatement(com.google.api.generator.engine.ast.SynchronizedStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) GeneralForStatement(com.google.api.generator.engine.ast.GeneralForStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Test(org.junit.Test)

Example 92 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeReferenceConstructorExpr_thisConstructorWithArguments.

@Test
public void writeReferenceConstructorExpr_thisConstructorWithArguments() {
    VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
    TypeNode classType = TypeNode.withReference(ref);
    VariableExpr idVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("id").setType(TypeNode.STRING).build()).build();
    VariableExpr nameVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("name").setType(TypeNode.STRING).build()).build();
    ReferenceConstructorExpr referenceConstructorExpr = ReferenceConstructorExpr.thisBuilder().setArguments(Arrays.asList(idVarExpr, nameVarExpr)).setType(classType).build();
    referenceConstructorExpr.accept(writerVisitor);
    assertThat(writerVisitor.write()).isEqualTo("this(id, name)");
}
Also used : VaporReference(com.google.api.generator.engine.ast.VaporReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) Test(org.junit.Test)

Example 93 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeTryCatchStatement_sampleCodeWithCatch.

@Test
public void writeTryCatchStatement_sampleCodeWithCatch() {
    Reference exceptionReference = ConcreteReference.withClazz(IllegalArgumentException.class);
    TypeNode type = TypeNode.withReference(exceptionReference);
    VariableExpr variableExpr = VariableExpr.builder().setVariable(createVariable("e", type)).setIsDecl(true).build();
    TryCatchStatement tryCatch = TryCatchStatement.builder().setIsSampleCode(true).setTryResourceExpr(createAssignmentExpr("aBool", "false", TypeNode.BOOLEAN)).setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("y", "4", TypeNode.INT)))).addCatch(variableExpr, Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("foobar", "123", TypeNode.INT)))).build();
    tryCatch.accept(writerVisitor);
    assertEquals(String.format("%s%s%s%s%s", "try (boolean aBool = false) {\n", "int y = 4;\n", "} catch (IllegalArgumentException e) {\n", "int foobar = 123;\n", "}\n"), writerVisitor.write());
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 94 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeUnaryOperationExpr_postfixIncrement.

@Test
public void writeUnaryOperationExpr_postfixIncrement() {
    VariableExpr variableExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.INT).setName("i").build());
    UnaryOperationExpr postIncrementOperationExpr = UnaryOperationExpr.postfixIncrementWithExpr(variableExpr);
    postIncrementOperationExpr.accept(writerVisitor);
    assertThat(writerVisitor.write()).isEqualTo("i++");
}
Also used : VariableExpr(com.google.api.generator.engine.ast.VariableExpr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) Test(org.junit.Test)

Example 95 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeSynchronizedStatement_basicVariableExpr.

@Test
public void writeSynchronizedStatement_basicVariableExpr() {
    VariableExpr strVarExpr = VariableExpr.withVariable(Variable.builder().setName("str").setType(TypeNode.STRING).build());
    SynchronizedStatement synchronizedStatement = SynchronizedStatement.builder().setLock(strVarExpr).setBody(ExprStatement.withExpr(MethodInvocationExpr.builder().setMethodName("doStuff").build())).build();
    synchronizedStatement.accept(writerVisitor);
    assertEquals(LineFormatter.lines("synchronized (str) {\n", "doStuff();\n", "}\n"), writerVisitor.write());
}
Also used : VariableExpr(com.google.api.generator.engine.ast.VariableExpr) SynchronizedStatement(com.google.api.generator.engine.ast.SynchronizedStatement) Test(org.junit.Test)

Aggregations

VariableExpr (com.google.api.generator.engine.ast.VariableExpr)217 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)137 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)132 TypeNode (com.google.api.generator.engine.ast.TypeNode)118 Expr (com.google.api.generator.engine.ast.Expr)114 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)107 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)91 Variable (com.google.api.generator.engine.ast.Variable)88 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)87 ArrayList (java.util.ArrayList)87 Test (org.junit.Test)82 Statement (com.google.api.generator.engine.ast.Statement)80 List (java.util.List)64 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)62 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)58 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)56 CastExpr (com.google.api.generator.engine.ast.CastExpr)53 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)53 Arrays (java.util.Arrays)48 IfStatement (com.google.api.generator.engine.ast.IfStatement)47