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());
}
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);
}
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);
}
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);
}
Aggregations