use of com.google.api.generator.engine.ast.TryCatchStatement in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeTryCatchStatement_sampleCodeWithCatch.
@Test
public void writeTryCatchStatement_sampleCodeWithCatch() {
Reference exceptionReference = ConcreteReference.withClazz(IllegalArgumentException.class);
TypeNode type = TypeNode.withReference(exceptionReference);
VariableExpr variableExpr = VariableExpr.builder().setVariable(createVariable("e", type)).setIsDecl(true).build();
TryCatchStatement tryCatch = TryCatchStatement.builder().setIsSampleCode(true).setTryResourceExpr(createAssignmentExpr("aBool", "false", TypeNode.BOOLEAN)).setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("y", "4", TypeNode.INT)))).addCatch(variableExpr, Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("foobar", "123", TypeNode.INT)))).build();
tryCatch.accept(writerVisitor);
assertEquals(String.format("%s%s%s%s%s", "try (boolean aBool = false) {\n", "int y = 4;\n", "} catch (IllegalArgumentException e) {\n", "int foobar = 123;\n", "}\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.TryCatchStatement in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeTryCatchStatement_simpleMultiCatch.
@Test
public void writeTryCatchStatement_simpleMultiCatch() {
VariableExpr firstCatchVarExpr = VariableExpr.builder().setVariable(createVariable("e", TypeNode.withExceptionClazz(IllegalArgumentException.class))).build();
VariableExpr secondCatchVarExpr = VariableExpr.builder().setVariable(createVariable("e", TypeNode.withExceptionClazz(RuntimeException.class))).build();
TryCatchStatement tryCatch = TryCatchStatement.builder().setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))).addCatch(firstCatchVarExpr.toBuilder().setIsDecl(true).build(), Collections.emptyList()).addCatch(secondCatchVarExpr.toBuilder().setIsDecl(true).build(), Collections.emptyList()).build();
tryCatch.accept(writerVisitor);
assertEquals(LineFormatter.lines("try {\n", "int x = 3;\n", "} catch (IllegalArgumentException e) {\n", "} catch (RuntimeException e) {\n", "}\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.TryCatchStatement in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeTryCatchStatement_sampleCodeNoCatch.
@Test
public void writeTryCatchStatement_sampleCodeNoCatch() {
TryCatchStatement tryCatch = TryCatchStatement.builder().setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))).setIsSampleCode(true).build();
tryCatch.accept(writerVisitor);
assertEquals(LineFormatter.lines("try {\n", "int x = 3;\n", "}\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.TryCatchStatement in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeTryCatchStatement_simple.
@Test
public void writeTryCatchStatement_simple() {
Reference exceptionReference = ConcreteReference.withClazz(IllegalArgumentException.class);
TypeNode type = TypeNode.withReference(exceptionReference);
VariableExpr variableExpr = VariableExpr.builder().setVariable(createVariable("e", type)).setIsDecl(true).build();
TryCatchStatement tryCatch = TryCatchStatement.builder().setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))).addCatch(variableExpr, Collections.emptyList()).build();
tryCatch.accept(writerVisitor);
assertEquals(String.format("%s%s%s%s", "try {\n", "int x = 3;\n", "} catch (IllegalArgumentException e) {\n", "}\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.TryCatchStatement in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeTryCatchStatement_simpleMultiCatchOrderMatters.
@Test
public void writeTryCatchStatement_simpleMultiCatchOrderMatters() {
VariableExpr firstCatchVarExpr = VariableExpr.builder().setVariable(createVariable("e", TypeNode.withExceptionClazz(IllegalArgumentException.class))).build();
VariableExpr secondCatchVarExpr = VariableExpr.builder().setVariable(createVariable("e", TypeNode.withExceptionClazz(RuntimeException.class))).build();
TryCatchStatement tryCatch = TryCatchStatement.builder().setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))).addCatch(secondCatchVarExpr.toBuilder().setIsDecl(true).build(), Collections.emptyList()).addCatch(firstCatchVarExpr.toBuilder().setIsDecl(true).build(), Collections.emptyList()).build();
tryCatch.accept(writerVisitor);
assertEquals(LineFormatter.lines("try {\n", "int x = 3;\n", "} catch (RuntimeException e) {\n", "} catch (IllegalArgumentException e) {\n", "}\n"), writerVisitor.write());
}
Aggregations