use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeLambdaExprImports.
@Test
public void writeLambdaExprImports() {
// Similar to method defnitions.
Reference mapRef = ConcreteReference.withClazz(Map.class);
List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(createVariable("x", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("K", TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class)))).build(), VariableExpr.builder().setVariable(createVariable("y", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("T", "V")).build());
Statement bodyStatement = ExprStatement.withExpr(MethodInvocationExpr.builder().setMethodName("doStuff").setReturnType(TypeNode.withReference(ConcreteReference.withClazz(Arrays.class))).build());
TypeNode returnType = TypeNode.withReference(ConcreteReference.withClazz(Expr.class));
LambdaExpr lambdaExpr = LambdaExpr.builder().setArguments(arguments).setBody(Arrays.asList(bodyStatement)).setReturnExpr(MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(returnType).build()).build();
lambdaExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import com.google.api.generator.engine.ast.Expr;\n", "import java.util.Arrays;\n", "import java.util.Map;\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeIfStatement_withElse.
@Test
public void writeIfStatement_withElse() {
AssignmentExpr assignExpr = createAssignmentExpr("x", "3", TypeNode.INT);
Statement assignExprStatement = ExprStatement.withExpr(assignExpr);
List<Statement> ifBody = Arrays.asList(assignExprStatement, assignExprStatement);
VariableExpr condExpr = createVariableExpr("condition", TypeNode.BOOLEAN);
IfStatement ifStatement = IfStatement.builder().setConditionExpr(condExpr).setBody(ifBody).setElseBody(ifBody).build();
ifStatement.accept(writerVisitor);
assertEquals(String.format("%s%s%s" + "%s%s%s%s", "if (condition) {\n", "int x = 3;\n", "int x = 3;\n", "} else {\n", "int x = 3;\n", "int x = 3;\n", "}\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeIfStatement_nested.
@Test
public void writeIfStatement_nested() {
List<Statement> ifBody = Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)), ExprStatement.withExpr(createAssignmentExpr("fooBar", "true", TypeNode.BOOLEAN)));
VariableExpr condExprOne = createVariableExpr("condition", TypeNode.BOOLEAN);
VariableExpr condExprTwo = createVariableExpr("fooBarCheck", TypeNode.BOOLEAN);
VariableExpr condExprThree = createVariableExpr("anotherCondition", TypeNode.BOOLEAN);
VariableExpr condExprFour = createVariableExpr("lookAtMe", TypeNode.BOOLEAN);
IfStatement nestedTwoIfStatement = IfStatement.builder().setConditionExpr(condExprThree).setBody(ifBody).setElseBody(ifBody).build();
IfStatement nestedOneIfStatement = IfStatement.builder().setConditionExpr(condExprTwo).setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("anInt", "10", TypeNode.INT)), nestedTwoIfStatement)).build();
IfStatement nestedZeroIfStatement = IfStatement.builder().setConditionExpr(condExprOne).setBody(Arrays.asList(nestedOneIfStatement)).addElseIf(condExprFour, ifBody).build();
IfStatement ifStatement = IfStatement.builder().setConditionExpr(condExprOne).setBody(Arrays.asList(nestedZeroIfStatement)).build();
ifStatement.accept(writerVisitor);
String expected = LineFormatter.lines("if (condition) {\n", "if (condition) {\n", "if (fooBarCheck) {\n", "int anInt = 10;\n", "if (anotherCondition) {\n", "int x = 3;\n", "boolean fooBar = true;\n", "} else {\n", "int x = 3;\n", "boolean fooBar = true;\n", "}\n", "}\n", "} else if (lookAtMe) {\n", "int x = 3;\n", "boolean fooBar = true;\n", "}\n", "}\n");
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeGeneralForStatement_basicIsDecl.
@Test
public void writeGeneralForStatement_basicIsDecl() {
AssignmentExpr assignExpr = createAssignmentExpr("x", "3", TypeNode.INT);
Statement assignExprStatement = ExprStatement.withExpr(assignExpr);
List<Statement> body = Arrays.asList(assignExprStatement, assignExprStatement);
VariableExpr localVarExpr = createVariableDeclExpr("i", TypeNode.INT);
ValueExpr initValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setValue("0").setType(TypeNode.INT).build());
Expr maxSizeExpr = MethodInvocationExpr.builder().setMethodName("maxSize").setReturnType(TypeNode.INT).build();
GeneralForStatement forStatement = GeneralForStatement.incrementWith(localVarExpr, initValueExpr, maxSizeExpr, body);
forStatement.accept(writerVisitor);
assertEquals(String.format("%s%s%s%s", "for (int i = 0; i < maxSize(); i++) {\n", "int x = 3;\n", "int x = 3;\n", "}\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeIfStatement_elseIfs.
@Test
public void writeIfStatement_elseIfs() {
List<Statement> ifBody = Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)), ExprStatement.withExpr(createAssignmentExpr("fooBar", "true", TypeNode.BOOLEAN)));
VariableExpr condExprOne = createVariableExpr("condition", TypeNode.BOOLEAN);
VariableExpr condExprTwo = createVariableExpr("fooBarCheck", TypeNode.BOOLEAN);
VariableExpr condExprThree = createVariableExpr("anotherCondition", TypeNode.BOOLEAN);
VariableExpr condExprFour = createVariableExpr("lookAtMe", TypeNode.BOOLEAN);
IfStatement ifStatement = IfStatement.builder().setConditionExpr(condExprOne).setBody(ifBody).addElseIf(condExprTwo, ifBody).addElseIf(condExprThree, ifBody).addElseIf(condExprFour, ifBody).build();
ifStatement.accept(writerVisitor);
String expected = LineFormatter.lines("if (condition) {\n", "int x = 3;\n", "boolean fooBar = true;\n", "} else if (fooBarCheck) {\n", "int x = 3;\n", "boolean fooBar = true;\n", "} else if (anotherCondition) {\n", "int x = 3;\n", "boolean fooBar = true;\n", "} else if (lookAtMe) {\n", "int x = 3;\n", "boolean fooBar = true;\n", "}\n");
assertEquals(expected, writerVisitor.write());
}
Aggregations