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());
}
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)");
}
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());
}
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++");
}
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());
}
Aggregations