use of com.google.api.generator.engine.ast.LambdaExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeLambdaExpr_body.
@Test
public void writeLambdaExpr_body() {
VariableExpr argVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("arg").setType(TypeNode.INT).build()).build();
VariableExpr fooVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("foo").setType(TypeNode.INT).build()).build();
ExprStatement statement = ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(fooVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(ValueExpr.builder().setValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("1").build()).build()).build());
LambdaExpr lambdaExpr = LambdaExpr.builder().setArguments(argVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("foo"))).setBody(Arrays.asList(statement)).build();
lambdaExpr.accept(writerVisitor);
assertEquals("arg -> {\nint foo = 1;\nreturn \"foo\";\n}", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.LambdaExpr 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.LambdaExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeLambdaExpr_oneParameter.
@Test
public void writeLambdaExpr_oneParameter() {
VariableExpr argVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("arg").setType(TypeNode.INT).build()).setIsDecl(true).build();
LambdaExpr lambdaExpr = LambdaExpr.builder().setArguments(argVarExpr).setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("foo"))).build();
lambdaExpr.accept(writerVisitor);
assertEquals("arg -> \"foo\"", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.LambdaExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeLambdaExpr_severalParameters.
@Test
public void writeLambdaExpr_severalParameters() {
VariableExpr argOneVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("arg").setType(TypeNode.INT).build()).setIsDecl(true).build();
VariableExpr argTwoVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("arg2").setType(TypeNode.STRING).build()).setIsDecl(true).build();
VariableExpr argThreeVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("arg3").setType(TypeNode.BOOLEAN).build()).setIsDecl(true).build();
LambdaExpr lambdaExpr = LambdaExpr.builder().setArguments(argOneVarExpr, argTwoVarExpr, argThreeVarExpr).setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("foo"))).build();
lambdaExpr.accept(writerVisitor);
assertEquals("(int arg, String arg2, boolean arg3) -> \"foo\"", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.LambdaExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeLambdaExpr_noParameters.
@Test
public void writeLambdaExpr_noParameters() {
LambdaExpr lambdaExpr = LambdaExpr.builder().setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("foo"))).build();
lambdaExpr.accept(writerVisitor);
assertEquals("() -> \"foo\"", writerVisitor.write());
}
Aggregations