Search in sources :

Example 66 with Statement

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

the class JavaWriterVisitorTest method writeIfStatement_simple.

@Test
public void writeIfStatement_simple() {
    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).build();
    ifStatement.accept(writerVisitor);
    assertEquals(LineFormatter.lines("if (condition) {\n", "int x = 3;\n", "int x = 3;\n", "}\n"), writerVisitor.write());
}
Also used : IfStatement(com.google.api.generator.engine.ast.IfStatement) BlockStatement(com.google.api.generator.engine.ast.BlockStatement) BreakStatement(com.google.api.generator.engine.ast.BreakStatement) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) ForStatement(com.google.api.generator.engine.ast.ForStatement) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) GeneralForStatement(com.google.api.generator.engine.ast.GeneralForStatement) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) 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) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Test(org.junit.Test)

Example 67 with Statement

use of com.google.api.generator.engine.ast.Statement 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 68 with Statement

use of com.google.api.generator.engine.ast.Statement 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)

Example 69 with Statement

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

the class SampleComposerTest method createExecutableSampleMethodArgsNoVar.

@Test
public void createExecutableSampleMethodArgsNoVar() {
    Statement sampleBody = ExprStatement.withExpr(systemOutPrint("Testing CreateExecutableSampleMethodArgsNoVar"));
    Sample sample = Sample.builder().setBody(ImmutableList.of(sampleBody)).setRegionTag(regionTag.setRpcName("createExecutableSample").setOverloadDisambiguation("MethodArgsNoVar").build()).build();
    String sampleResult = writeSample(SampleComposer.composeExecutableSample(sample, packageName));
    String expected = LineFormatter.lines("package com.google.example;\n", "\n", "// [START apiname_generated_echo_createexecutablesample_methodargsnovar_sync]\n", "public class SyncCreateExecutableSampleMethodArgsNoVar {\n", "\n", "  public static void main(String[] args) throws Exception {\n", "    syncCreateExecutableSampleMethodArgsNoVar();\n", "  }\n", "\n", "  public static void syncCreateExecutableSampleMethodArgsNoVar() 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(\"Testing CreateExecutableSampleMethodArgsNoVar\");\n", "  }\n", "}\n", "// [END apiname_generated_echo_createexecutablesample_methodargsnovar_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) Test(org.junit.Test)

Aggregations

ExprStatement (com.google.api.generator.engine.ast.ExprStatement)69 Statement (com.google.api.generator.engine.ast.Statement)69 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)65 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)58 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)53 Expr (com.google.api.generator.engine.ast.Expr)48 ArrayList (java.util.ArrayList)46 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)44 IfStatement (com.google.api.generator.engine.ast.IfStatement)43 TypeNode (com.google.api.generator.engine.ast.TypeNode)42 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)41 TryCatchStatement (com.google.api.generator.engine.ast.TryCatchStatement)36 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)32 List (java.util.List)30 ForStatement (com.google.api.generator.engine.ast.ForStatement)29 Message (com.google.api.generator.gapic.model.Message)25 Variable (com.google.api.generator.engine.ast.Variable)24 CastExpr (com.google.api.generator.engine.ast.CastExpr)23 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)23 EmptyLineStatement (com.google.api.generator.engine.ast.EmptyLineStatement)23