use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposer method createRpcExceptionTestMethod.
@Override
protected MethodDefinition createRpcExceptionTestMethod(Method method, Service service, List<MethodArgument> methodSignature, int variantIndex, Map<String, VariableExpr> classMemberVarExprs, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
VariableExpr exceptionVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_GRPC_TYPESTORE.get("StatusRuntimeException")).setName("exception").build());
// First two assignment lines.
Expr exceptionAssignExpr = AssignmentExpr.builder().setVariableExpr(exceptionVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(FIXED_GRPC_TYPESTORE.get("StatusRuntimeException")).setArguments(EnumRefExpr.builder().setType(GRPC_STATUS_TYPE).setName("INVALID_ARGUMENT").build()).build()).build();
Expr addExceptionExpr = MethodInvocationExpr.builder().setExprReferenceExpr(classMemberVarExprs.get(getMockServiceVarName(service))).setMethodName("addException").setArguments(exceptionVarExpr).build();
// Try-catch block. Build the method call.
String exceptionTestMethodName = String.format("%sExceptionTest%s", JavaStyle.toLowerCamelCase(method.name()), variantIndex > 0 ? variantIndex + 1 : "");
boolean isStreaming = !method.stream().equals(Method.Stream.NONE);
List<Statement> methodBody = new ArrayList<>();
methodBody.add(ExprStatement.withExpr(exceptionAssignExpr));
methodBody.add(ExprStatement.withExpr(addExceptionExpr));
if (isStreaming) {
methodBody.addAll(createStreamingRpcExceptionTestStatements(method, classMemberVarExprs, resourceNames, messageTypes));
} else {
methodBody.addAll(createRpcExceptionTestStatements(method, methodSignature, classMemberVarExprs, resourceNames, messageTypes));
}
return MethodDefinition.builder().setAnnotations(Arrays.asList(TEST_ANNOTATION)).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName(exceptionTestMethodName).setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(Exception.class))).setBody(methodBody).build();
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeSuperObjectValue_accessFieldAndInvokeMethod.
@Test
public void writeSuperObjectValue_accessFieldAndInvokeMethod() {
VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
TypeNode classType = TypeNode.withReference(ref);
SuperObjectValue superObjectValue = SuperObjectValue.withType(classType);
ValueExpr superValueExpr = ValueExpr.withValue(superObjectValue);
Variable subVariable = Variable.builder().setName("name").setType(TypeNode.STRING).build();
VariableExpr superVariableExpr = VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(superValueExpr).build();
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("getName").setExprReferenceExpr(ValueExpr.withValue(superObjectValue)).setReturnType(TypeNode.STRING).build();
AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(superVariableExpr).setValueExpr(methodExpr).build();
assignmentExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("super.name = super.getName()");
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodInvocationExpr_chained.
@Test
public void writeMethodInvocationExpr_chained() {
Variable variable = Variable.builder().setType(TypeNode.INT).setName("libraryClient").build();
VariableExpr varExpr = VariableExpr.builder().setVariable(variable).build();
MethodInvocationExpr firstMethodExpr = MethodInvocationExpr.builder().setMethodName("streamBooksCallable").setExprReferenceExpr(varExpr).build();
MethodInvocationExpr secondMethodExpr = MethodInvocationExpr.builder().setMethodName("doAnotherThing").setExprReferenceExpr(firstMethodExpr).build();
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("call").setExprReferenceExpr(secondMethodExpr).build();
methodExpr.accept(writerVisitor);
assertEquals("libraryClient.streamBooksCallable().doAnotherThing().call()", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeCastExpr_nested.
@Test
public void writeCastExpr_nested() {
Variable variable = Variable.builder().setType(TypeNode.STRING).setName("str").build();
VariableExpr varExpr = VariableExpr.builder().setVariable(variable).build();
CastExpr castExpr = CastExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(Object.class))).setExpr(varExpr).build();
castExpr = CastExpr.builder().setType(TypeNode.STRING).setExpr(castExpr).build();
castExpr.accept(writerVisitor);
assertEquals("((String) ((Object) str))", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeForStatement.
@Test
public void writeForStatement() {
AssignmentExpr assignExpr = createAssignmentExpr("x", "3", TypeNode.INT);
Statement assignExprStatement = ExprStatement.withExpr(assignExpr);
List<Statement> body = Arrays.asList(assignExprStatement, assignExprStatement);
VariableExpr varDeclExpr = createVariableDeclExpr("str", TypeNode.STRING);
Expr collectionExpr = MethodInvocationExpr.builder().setMethodName("getSomeStrings").build();
ForStatement forStatement = ForStatement.builder().setLocalVariableExpr(varDeclExpr).setCollectionExpr(collectionExpr).setBody(body).build();
forStatement.accept(writerVisitor);
assertEquals(String.format("%s%s%s%s", "for (String str : getSomeStrings()) {\n", "int x = 3;\n", "int x = 3;\n", "}\n"), writerVisitor.write());
}
Aggregations