Search in sources :

Example 1 with LambdaExpr

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());
}
Also used : LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Example 2 with LambdaExpr

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());
}
Also used : ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) 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) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 3 with LambdaExpr

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());
}
Also used : LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Example 4 with LambdaExpr

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());
}
Also used : LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Example 5 with LambdaExpr

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());
}
Also used : LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) Test(org.junit.Test)

Aggregations

LambdaExpr (com.google.api.generator.engine.ast.LambdaExpr)5 Test (org.junit.Test)5 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)4 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)2 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)1 ArithmeticOperationExpr (com.google.api.generator.engine.ast.ArithmeticOperationExpr)1 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)1 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)1 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)1 EmptyLineStatement (com.google.api.generator.engine.ast.EmptyLineStatement)1 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)1 Expr (com.google.api.generator.engine.ast.Expr)1 IfStatement (com.google.api.generator.engine.ast.IfStatement)1 InstanceofExpr (com.google.api.generator.engine.ast.InstanceofExpr)1 LogicalOperationExpr (com.google.api.generator.engine.ast.LogicalOperationExpr)1 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)1 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)1 Reference (com.google.api.generator.engine.ast.Reference)1 ReferenceConstructorExpr (com.google.api.generator.engine.ast.ReferenceConstructorExpr)1 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)1