Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ java_library(
":samplecode_files",
],
deps = [
"//src/main/java/com/google/api/generator/engine/ast",
"//src/main/java/com/google/api/generator/engine/writer",
"@google_java_format_all_deps//jar",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.gapic.composer.samplecode;

import com.google.api.generator.engine.ast.Statement;
import com.google.api.generator.engine.writer.JavaWriterVisitor;
import java.util.Arrays;
import java.util.List;

public final class SampleCodeWriter {

public static String write(Statement ... statement) {
return write(Arrays.asList(statement));
}

public static String write(List<Statement> statements) {
JavaWriterVisitor visitor = new JavaWriterVisitor();
for (Statement statement : statements) {
statement.accept(visitor);
}
return SampleCodeJavaFormatter.format(visitor.write());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package(default_visibility = ["//visibility:public"])

TESTS = [
"SampleCodeJavaFormatterTest",
"SampleCodeWriterTest"
]

filegroup(
Expand All @@ -19,7 +20,9 @@ filegroup(
"//src/main/java/com/google/api/generator/engine/ast",
"//src/main/java/com/google/api/generator/engine/writer",
"//src/main/java/com/google/api/generator/gapic/composer/samplecode",
"//src/main/java/com/google/api/generator/gapic/model",
"@com_google_truth_truth//jar",
"@junit_junit//jar",
"@com_google_api_gax_java//gax",
],
) for test_name in TESTS]
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.gapic.composer.samplecode;

import static junit.framework.TestCase.assertEquals;

import com.google.api.gax.rpc.ClientSettings;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.PrimitiveValue;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.Statement;
import com.google.api.generator.engine.ast.TryCatchStatement;
import com.google.api.generator.engine.ast.ValueExpr;
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
import java.util.Arrays;
import org.junit.Test;

public class SampleCodeWriterTest {
@Test
public void writeSampleCode_statements() {
TypeNode settingType = TypeNode.withReference(ConcreteReference.withClazz(ClientSettings.class));
Variable aVar = Variable.builder().setName("clientSettings").setType(settingType).build();
VariableExpr aVarExpr = VariableExpr.withVariable(aVar);
MethodInvocationExpr aValueExpr = MethodInvocationExpr.builder()
.setExprReferenceExpr(MethodInvocationExpr.builder()
.setMethodName("newBuilder")
.setStaticReferenceType(settingType)
.build())
.setReturnType(settingType)
.setMethodName("build")
.build();
AssignmentExpr assignmentExpr = AssignmentExpr.builder()
.setVariableExpr(aVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(aValueExpr)
.build();
Statement sampleStatement = TryCatchStatement.builder()
.setTryResourceExpr(createAssignmentExpr("aBool", "false", TypeNode.BOOLEAN))
.setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT))))
.setIsSampleCode(true)
.build();
String result = SampleCodeWriter.write(
ExprStatement.withExpr(assignmentExpr),
sampleStatement);
String expected = "ClientSettings clientSettings = ClientSettings.newBuilder().build();\n"
+ "try (boolean aBool = false) {\n"
+ " int x = 3;\n"
+ "}";
assertEquals(expected, result);
}

private AssignmentExpr createAssignmentExpr(String varName, String varValue, TypeNode type) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Since there is only one usage of this method, let's just call the internals directly and forego this wrapper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two usage above. Line 57, and line 58. I suggest to use the wrapper make the code clean.

Variable variable = Variable.builder().setName(varName).setType(type).build();
VariableExpr variableExpr =
VariableExpr.builder()
.setVariable(variable)
.setIsDecl(true)
.build();
return AssignmentExpr.builder()
.setVariableExpr(variableExpr)
.setValueExpr(ValueExpr.withValue(PrimitiveValue.builder().setType(type).setValue(varValue).build()))
.build();
}

}