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