Search in sources :

Example 56 with AssignmentExpr

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

the class JavaWriterVisitorTest method writeMethodInvocationExpr_genericWithArgs.

@Test
public void writeMethodInvocationExpr_genericWithArgs() {
    Reference mapReference = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(Integer.class))).build();
    Reference outerMapReference = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(mapReference, mapReference)).build();
    Variable variable = Variable.builder().setType(TypeNode.INT).setName("anArg").build();
    VariableExpr varExpr = VariableExpr.builder().setVariable(variable).build();
    MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("foobar").setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(Double.class), TypeNode.WILDCARD_REFERENCE, outerMapReference)).setArguments(Arrays.asList(varExpr, varExpr, varExpr)).setExprReferenceExpr(varExpr).setReturnType(TypeNode.STRING).build();
    Variable lhsVariable = Variable.builder().setType(TypeNode.STRING).setName("someStr").build();
    VariableExpr lhsVarExpr = VariableExpr.builder().setVariable(lhsVariable).setIsDecl(true).setIsFinal(true).build();
    AssignmentExpr assignExpr = AssignmentExpr.builder().setVariableExpr(lhsVarExpr).setValueExpr(methodExpr).build();
    assignExpr.accept(writerVisitor);
    assertEquals("final String someStr = anArg.<String, Double, ?, HashMap<HashMap<String, Integer>," + " HashMap<String, Integer>>>foobar(anArg, anArg, anArg)", writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Test(org.junit.Test)

Example 57 with AssignmentExpr

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

the class SampleComposerTest method createExecutableSampleMethod.

@Test
public void createExecutableSampleMethod() {
    VariableExpr variableExpr = VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.STRING).setName("content").build()).setIsDecl(true).build();
    AssignmentExpr varAssignment = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(ValueExpr.withValue(StringObjectValue.withValue("Testing CreateExecutableSampleMethod"))).build();
    Statement sampleBody = ExprStatement.withExpr(systemOutPrint(variableExpr));
    Sample sample = Sample.builder().setBody(ImmutableList.of(sampleBody)).setVariableAssignments(ImmutableList.of(varAssignment)).setRegionTag(regionTag.setRpcName("createExecutableSample").build()).build();
    String sampleResult = writeSample(SampleComposer.composeExecutableSample(sample, packageName));
    String expected = LineFormatter.lines("package com.google.example;\n", "\n", "// [START apiname_generated_echo_createexecutablesample_sync]\n", "public class SyncCreateExecutableSample {\n", "\n", "  public static void main(String[] args) throws Exception {\n", "    String content = \"Testing CreateExecutableSampleMethod\";\n", "    syncCreateExecutableSample(content);\n", "  }\n", "\n", "  public static void syncCreateExecutableSample(String content) throws Exception {\n", "    // This snippet has been automatically generated for illustrative purposes only.\n", "    // It may require modifications to work in your environment.\n", "    System.out.println(content);\n", "  }\n", "}\n", "// [END apiname_generated_echo_createexecutablesample_sync]\n");
    assertEquals(expected, sampleResult);
}
Also used : ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) Sample(com.google.api.generator.gapic.model.Sample) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Test(org.junit.Test)

Example 58 with AssignmentExpr

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

the class SampleComposerTest method createExecutableSampleMethodMultipleStatements.

@Test
public void createExecutableSampleMethodMultipleStatements() {
    VariableExpr strVariableExpr = VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.STRING).setName("content").build()).setIsDecl(true).build();
    VariableExpr intVariableExpr = VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.INT).setName("num").build()).setIsDecl(true).build();
    VariableExpr objVariableExpr = VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.OBJECT).setName("thing").build()).setIsDecl(true).build();
    AssignmentExpr strVarAssignment = AssignmentExpr.builder().setVariableExpr(strVariableExpr).setValueExpr(ValueExpr.withValue(StringObjectValue.withValue("Testing CreateExecutableSampleMethodMultipleStatements"))).build();
    AssignmentExpr intVarAssignment = AssignmentExpr.builder().setVariableExpr(intVariableExpr).setValueExpr(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("2").build())).build();
    AssignmentExpr objVarAssignment = AssignmentExpr.builder().setVariableExpr(objVariableExpr).setValueExpr(NewObjectExpr.builder().setType(TypeNode.OBJECT).build()).build();
    Statement strBodyStatement = ExprStatement.withExpr(systemOutPrint(strVariableExpr));
    Statement intBodyStatement = ExprStatement.withExpr(systemOutPrint(intVariableExpr));
    Statement objBodyStatement = ExprStatement.withExpr(systemOutPrint(MethodInvocationExpr.builder().setExprReferenceExpr(objVariableExpr.toBuilder().setIsDecl(false).build()).setMethodName("response").build()));
    Sample sample = Sample.builder().setBody(ImmutableList.of(strBodyStatement, intBodyStatement, objBodyStatement)).setVariableAssignments(ImmutableList.of(strVarAssignment, intVarAssignment, objVarAssignment)).setRegionTag(regionTag.setRpcName("createExecutableSample").setOverloadDisambiguation("MethodMultipleStatements").build()).build();
    String sampleResult = writeSample(SampleComposer.composeExecutableSample(sample, packageName));
    String expected = LineFormatter.lines("package com.google.example;\n", "\n", "// [START apiname_generated_echo_createexecutablesample_methodmultiplestatements_sync]\n", "public class SyncCreateExecutableSampleMethodMultipleStatements {\n", "\n", "  public static void main(String[] args) throws Exception {\n", "    String content = \"Testing CreateExecutableSampleMethodMultipleStatements\";\n", "    int num = 2;\n", "    Object thing = new Object();\n", "    syncCreateExecutableSampleMethodMultipleStatements(content, num, thing);\n", "  }\n", "\n", "  public static void syncCreateExecutableSampleMethodMultipleStatements(\n", "      String content, int num, Object thing) throws Exception {\n", "    // This snippet has been automatically generated for illustrative purposes only.\n", "    // It may require modifications to work in your environment.\n", "    System.out.println(content);\n", "    System.out.println(num);\n", "    System.out.println(thing.response());\n", "  }\n", "}\n", "// [END apiname_generated_echo_createexecutablesample_methodmultiplestatements_sync]\n");
    assertEquals(expected, sampleResult);
}
Also used : ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) Sample(com.google.api.generator.gapic.model.Sample) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Test(org.junit.Test)

Aggregations

AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)58 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)54 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)43 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)37 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)35 Statement (com.google.api.generator.engine.ast.Statement)33 Expr (com.google.api.generator.engine.ast.Expr)32 TypeNode (com.google.api.generator.engine.ast.TypeNode)31 Variable (com.google.api.generator.engine.ast.Variable)29 ArrayList (java.util.ArrayList)28 IfStatement (com.google.api.generator.engine.ast.IfStatement)27 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)26 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)26 ForStatement (com.google.api.generator.engine.ast.ForStatement)24 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)22 CastExpr (com.google.api.generator.engine.ast.CastExpr)21 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)21 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)20 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)20 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)20