use of com.google.api.generator.engine.ast.LineComment in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeLineCommentStatement_basic.
@Test
public void writeLineCommentStatement_basic() {
String content = "this is a test comment";
LineComment lineComment = LineComment.builder().setComment(content).build();
CommentStatement commentStatement = CommentStatement.withComment(lineComment);
String expected = "// this is a test comment\n";
commentStatement.accept(writerVisitor);
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.engine.ast.LineComment 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.LineComment in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeLineComment_specialChar.
@Test
public void writeLineComment_specialChar() {
String content = "usage: gradle run -PmainClass=com.google.example.examples.library.v1.Hopper" + " [--args='[--shelf \"Novel\\\"`\b\t\n\r" + "\"]']";
LineComment lineComment = LineComment.withComment(content);
String expected = LineFormatter.lines("// usage: gradle run -PmainClass=com.google.example.examples.library.v1.Hopper" + " [--args='[--shelf\n", "// \"Novel\\\\\"`\\b\\t\n", "// \\r", "\"]']\n");
lineComment.accept(writerVisitor);
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.engine.ast.LineComment in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeClassDefinition_commentsStatementsAndMethods.
@Test
public void writeClassDefinition_commentsStatementsAndMethods() {
LineComment lineComment = LineComment.withComment("AUTO-GENERATED DOCUMENTATION AND CLASS");
JavaDocComment javaDocComment = JavaDocComment.builder().addComment("Class to configure an instance of {@link LibraryServiceStub}.").addParagraph("The default instance has everything set to sensible defaults:").addUnorderedList(Arrays.asList("The default service address (library-example.googleapis.com) and default port" + " (1234) are used.", "Credentials are acquired automatically through Application Default" + " Credentials.", "Retries are configured for idempotent methods but not for non-idempotent" + " methods.")).build();
List<Reference> subGenerics = Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(MethodDefinition.class));
Reference mapEntryReference = ConcreteReference.builder().setClazz(Map.Entry.class).setGenerics(subGenerics).build();
List<Reference> generics = Arrays.asList(ConcreteReference.withClazz(ClassDefinition.class), mapEntryReference);
Reference mapReference = ConcreteReference.builder().setClazz(Map.class).setGenerics(generics).build();
List<Statement> statements = Arrays.asList(ExprStatement.withExpr(VariableExpr.builder().setVariable(createVariable("x", TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class)))).setIsDecl(true).setScope(ScopeNode.PRIVATE).build()), ExprStatement.withExpr(VariableExpr.builder().setVariable(createVariable("y", TypeNode.withReference(mapReference))).setIsDecl(true).setScope(ScopeNode.PROTECTED).build()));
MethodDefinition methodOne = MethodDefinition.builder().setName("open").setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.BOOLEAN).setReturnExpr(ValueExpr.builder().setValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build()).build()).build();
MethodDefinition methodTwo = MethodDefinition.builder().setName("close").setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN)))).build();
List<MethodDefinition> methods = Arrays.asList(methodOne, methodTwo);
ClassDefinition nestedClassDef = ClassDefinition.builder().setName("IAmANestedClass").setIsNested(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setMethods(Arrays.asList(methodOne)).build();
ClassDefinition classDef = ClassDefinition.builder().setPackageString("com.google.example.library.v1.stub").setHeaderCommentStatements(Arrays.asList(CommentStatement.withComment(lineComment), CommentStatement.withComment(javaDocComment))).setName("LibraryServiceStub").setScope(ScopeNode.PUBLIC).setStatements(statements).setMethods(methods).setNestedClasses(Arrays.asList(nestedClassDef)).build();
classDef.accept(writerVisitor);
String expected = LineFormatter.lines("package com.google.example.library.v1.stub;\n", "\n", "import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import com.google.api.generator.engine.ast.ClassDefinition;\n", "import com.google.api.generator.engine.ast.MethodDefinition;\n", "import java.util.Map;\n", "\n", "// AUTO-GENERATED DOCUMENTATION AND CLASS\n", "/**\n", " * Class to configure an instance of {{@literal @}link LibraryServiceStub}.\n", " *\n", " * <p>The default instance has everything set to sensible defaults:\n", " *\n", " * <ul>\n", " * <li>The default service address (library-example.googleapis.com) and default" + " port (1234) are\n", " * used.\n", " * <li>Credentials are acquired automatically through Application Default" + " Credentials.\n", " * <li>Retries are configured for idempotent methods but not for non-idempotent" + " methods.\n", " * </ul>\n", " */\n", "public class LibraryServiceStub {\n", " private AssignmentExpr x;\n", " protected Map<ClassDefinition, Map.Entry<String, MethodDefinition>> y;\n\n", " public boolean open() {\n", " return true;\n", " }\n\n", " public void close() {\n", " boolean foobar = false;\n", " }\n", "\n", " private static class IAmANestedClass {\n\n", " public boolean open() {\n", " return true;\n", " }\n", " }\n", "}\n");
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.engine.ast.LineComment in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeLineComment_longLine.
@Test
public void writeLineComment_longLine() {
String content = "this is a long test comment with so many words, hello world, hello again, hello for 3" + " times, blah, blah!";
LineComment lineComment = LineComment.builder().setComment(content).build();
String expected = LineFormatter.lines("// this is a long test comment with so many words, hello world, hello again, hello" + " for 3 times,\n", "// blah, blah!\n");
lineComment.accept(writerVisitor);
assertEquals(expected, writerVisitor.write());
}
Aggregations