use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodDefinition_withCommentsAnnotationsAndThrows.
@Test
public void writeMethodDefinition_withCommentsAnnotationsAndThrows() {
LineComment lineComment = LineComment.withComment("AUTO-GENERATED DOCUMENTATION AND METHOD");
JavaDocComment javaDocComment = JavaDocComment.builder().addComment("This is an override method called `close()`").addParam("valOne", "string type").addParam("valTwo", "boolean type").addComment("The return value is int 3.").build();
ValueExpr returnExpr = ValueExpr.builder().setValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("3").build()).build();
List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(createVariable("valOne", TypeNode.STRING)).setIsDecl(true).build(), VariableExpr.builder().setVariable(createVariable("valTwo", TypeNode.BOOLEAN)).setIsDecl(true).build());
MethodDefinition methodDefinition = MethodDefinition.builder().setName("close").setIsOverride(true).setIsFinal(true).setIsStatic(true).setScope(ScopeNode.PROTECTED).setReturnType(TypeNode.INT).setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(IOException.class), TypeNode.withExceptionClazz(TimeoutException.class), TypeNode.withExceptionClazz(InterruptedException.class))).setArguments(arguments).setReturnExpr(returnExpr).setHeaderCommentStatements(Arrays.asList(CommentStatement.withComment(lineComment), CommentStatement.withComment(javaDocComment))).setAnnotations(Arrays.asList(AnnotationNode.withSuppressWarnings("all"), AnnotationNode.DEPRECATED)).setBody(Arrays.asList(createForStatement(), ExprStatement.withExpr(createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN)))).build();
methodDefinition.accept(writerVisitor);
String expected = LineFormatter.lines("// AUTO-GENERATED DOCUMENTATION AND METHOD\n", "/**\n", "* This is an override method called `close()`\n", "* The return value is int 3.\n", "* @param valOne string type\n", "* @param valTwo boolean type\n", "*/\n", "@SuppressWarnings(\"all\")\n", "@Deprecated\n", "@Override\n", "protected static final int close(String valOne, boolean valTwo) throws" + " IOException, TimeoutException, InterruptedException {\n", "for (String str : getSomeStrings()) {\n", "boolean aBool = false;\n", "}\n", "boolean foobar = false;\n", "return 3;\n", "}\n\n");
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAssignmentOperationExpr_multiplyAssignment.
@Test
public void writeAssignmentOperationExpr_multiplyAssignment() {
VariableExpr lhsExpr = createVariableExpr("h", TypeNode.INT);
ValueExpr rhsExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("1000003").build());
AssignmentOperationExpr assignmentOperationExpr = AssignmentOperationExpr.multiplyAssignmentWithExprs(lhsExpr, rhsExpr);
assignmentOperationExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("h *= 1000003");
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeLogicalOperationExpr_logicalAnd.
@Test
public void writeLogicalOperationExpr_logicalAnd() {
VariableExpr lhsExpr = VariableExpr.withVariable(createVariable("isEmpty", TypeNode.BOOLEAN));
VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
TypeNode classType = TypeNode.withReference(ref);
MethodInvocationExpr rhsExpr = MethodInvocationExpr.builder().setMethodName("isValid").setExprReferenceExpr(ValueExpr.withValue(ThisObjectValue.withType(classType))).setReturnType(TypeNode.BOOLEAN).build();
LogicalOperationExpr logicalOperationExpr = LogicalOperationExpr.logicalAndWithExprs(lhsExpr, rhsExpr);
logicalOperationExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("isEmpty && this.isValid()");
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeTernaryExpr_basic.
@Test
public void writeTernaryExpr_basic() {
Variable conditionVariable = Variable.builder().setName("condition").setType(TypeNode.BOOLEAN).build();
VariableExpr conditionExpr = VariableExpr.builder().setVariable(conditionVariable).build();
Value value1 = PrimitiveValue.builder().setType(TypeNode.INT).setValue("3").build();
Expr thenExpr = ValueExpr.builder().setValue(value1).build();
Value value2 = PrimitiveValue.builder().setType(TypeNode.INT).setValue("4").build();
Expr elseExpr = ValueExpr.builder().setValue(value2).build();
TernaryExpr ternaryExpr = TernaryExpr.builder().setConditionExpr(conditionExpr).setThenExpr(thenExpr).setElseExpr(elseExpr).build();
ternaryExpr.accept(writerVisitor);
assertEquals("condition ? 3 : 4", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAssignmentExpr_varToVar.
@Test
public void writeAssignmentExpr_varToVar() {
Variable variable = Variable.builder().setName("foobar").setType(TypeNode.INT).build();
VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).setIsStatic(true).setIsFinal(true).setScope(ScopeNode.PRIVATE).setIsDecl(true).build();
Variable anotherVariable = Variable.builder().setName("y").setType(TypeNode.INT).build();
Expr valueExpr = VariableExpr.builder().setVariable(anotherVariable).build();
AssignmentExpr assignExpr = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
assignExpr.accept(writerVisitor);
assertEquals("private static final int foobar = y", writerVisitor.write());
}
Aggregations