Search in sources :

Example 6 with TryCatchStatement

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());
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 7 with TryCatchStatement

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());
}
Also used : TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Example 8 with TryCatchStatement

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());
}
Also used : TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) Test(org.junit.Test)

Example 9 with TryCatchStatement

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());
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 10 with TryCatchStatement

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());
}
Also used : TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Aggregations

TryCatchStatement (com.google.api.generator.engine.ast.TryCatchStatement)11 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)9 Test (org.junit.Test)7 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)5 TypeNode (com.google.api.generator.engine.ast.TypeNode)5 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)4 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)4 Reference (com.google.api.generator.engine.ast.Reference)4 VaporReference (com.google.api.generator.engine.ast.VaporReference)4 Variable (com.google.api.generator.engine.ast.Variable)3 Expr (com.google.api.generator.engine.ast.Expr)2 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)2 Statement (com.google.api.generator.engine.ast.Statement)2 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)2 Message (com.google.api.generator.gapic.model.Message)2 AbstractMessage (com.google.protobuf.AbstractMessage)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2