-
Notifications
You must be signed in to change notification settings - Fork 69
[ggj][gapic][1/5] feat: add new unit test for testing file-diff infra #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| TESTS = [ | ||
| "FileDiffInfraDummyTest", | ||
| ] | ||
|
|
||
| filegroup( | ||
| name = "dummy_files", | ||
| srcs = ["{0}.java".format(f) for f in TESTS], | ||
| ) | ||
|
|
||
| [java_test( | ||
| name = test_name, | ||
| srcs = ["{0}.java".format(test_name)], | ||
| test_class = "com.google.api.generator.gapic.dummy.{0}".format(test_name), | ||
| deps = [ | ||
| "//src/main/java/com/google/api/generator/engine/ast", | ||
| "//src/main/java/com/google/api/generator/engine/writer", | ||
| "@junit_junit//jar", | ||
| ], | ||
| ) for test_name in TESTS] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // 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.dummy; | ||
|
|
||
| import static junit.framework.Assert.assertEquals; | ||
|
|
||
| import com.google.api.generator.engine.ast.BlockComment; | ||
| import com.google.api.generator.engine.ast.ClassDefinition; | ||
| import com.google.api.generator.engine.ast.CommentStatement; | ||
| import com.google.api.generator.engine.ast.LineComment; | ||
| import com.google.api.generator.engine.ast.ScopeNode; | ||
| import com.google.api.generator.engine.writer.JavaWriterVisitor; | ||
| import java.util.Arrays; | ||
| import org.junit.Test; | ||
|
|
||
| public class FileDiffInfraDummyTest { | ||
| // Add two simple tests for testing the file-diff infra. | ||
| // 1. The two unit tests create simple java classes. | ||
| // 2. Two unit tests are created because we have the case of two expected strings comparison | ||
| // in one test class e.g. ResourceNameHelperClassComposer. In that case, two golden files will be | ||
| // created. | ||
| // | ||
| // TODO(xiaozhenliu): remove this test class once the file-diff infra is in place and well-tested. | ||
| @Test | ||
| public void simpleClass() { | ||
| ClassDefinition classDef = | ||
| ClassDefinition.builder() | ||
| .setHeaderCommentStatements( | ||
| Arrays.asList( | ||
| CommentStatement.withComment( | ||
| LineComment.withComment("This is a test class for file-diff infra")))) | ||
| .setPackageString("com.google.showcase.v1beta1.stub") | ||
| .setName("EchoStubSettings") | ||
| .setScope(ScopeNode.PUBLIC) | ||
| .build(); | ||
| JavaWriterVisitor visitor = new JavaWriterVisitor(); | ||
| classDef.accept(visitor); | ||
| assertEquals(visitor.write(), EXPECTED_CLASS_STRING_SIMPLE); | ||
| } | ||
|
|
||
| @Test | ||
| public void classWithHeader() { | ||
| ClassDefinition classDef = | ||
| ClassDefinition.builder() | ||
| .setFileHeader( | ||
| Arrays.asList( | ||
| CommentStatement.withComment(BlockComment.withComment(APACHE_LICENSE_STRING)))) | ||
| .setPackageString("com.google.showcase.v1beta1.stub") | ||
| .setName("EchoStubSettings") | ||
| .setScope(ScopeNode.PUBLIC) | ||
| .build(); | ||
| JavaWriterVisitor visitor = new JavaWriterVisitor(); | ||
| classDef.accept(visitor); | ||
| assertEquals(visitor.write(), EXPECTED_CLASS_STRING_WITH_HEADER); | ||
| } | ||
|
|
||
| private static final String APACHE_LICENSE_STRING = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to make this string as one of gapic component since it will be used in every file. @miraleung @xiaozhenliu-gg5 LMK what do you think, I could help with this.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is just a temporary test, the redefinition here is fine. Other usages will leverage the constant that's defined in CommentComposer.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for explanation. As Mira mentioned above, we have the helper to add Apache License as one composer component. And this dummy test will be removed after file-diff infra is in place. |
||
| "Copyright 2020 Google LLC\n\n" | ||
| + "Licensed under the Apache License, Version 2.0 (the \"License\");\n" | ||
| + "you may not use this file except in compliance with the License.\n" | ||
| + "You may obtain a copy of the License at\n\n" | ||
| + " https://www.apache.org/licenses/LICENSE-2.0\n\n" | ||
| + "Unless required by applicable law or agreed to in writing, software\n" | ||
| + "distributed under the License is distributed on an \"AS IS\" BASIS,\n" | ||
| + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" | ||
| + "See the License for the specific language governing permissions and\n" | ||
| + "limitations under the License."; | ||
|
|
||
| private static final String EXPECTED_CLASS_STRING_SIMPLE = | ||
| "package com.google.showcase.v1beta1.stub;\n\n" | ||
| + "// This is a test class for file-diff infra\n" | ||
| + "public class EchoStubSettings {}\n"; | ||
|
|
||
| private static final String EXPECTED_CLASS_STRING_WITH_HEADER = | ||
| "/*\n" | ||
| + " * Copyright 2020 Google LLC\n" | ||
| + " *\n" | ||
| + " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" | ||
| + " * you may not use this file except in compliance with the License.\n" | ||
| + " * You may obtain a copy of the License at\n" | ||
| + " *\n" | ||
| + " * https://www.apache.org/licenses/LICENSE-2.0\n" | ||
| + " *\n" | ||
| + " * Unless required by applicable law or agreed to in writing, software\n" | ||
| + " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" | ||
| + " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" | ||
| + " * See the License for the specific language governing permissions and\n" | ||
| + " * limitations under the License.\n" | ||
| + " */\n\n" | ||
| + "package com.google.showcase.v1beta1.stub;\n\n" | ||
| + "public class EchoStubSettings {}\n"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| filegroup( | ||
| name = "goldens_files", | ||
| srcs = glob(["*.golden"]), | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * https://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.showcase.v1beta1.stub; | ||
|
|
||
| public class EchoStubSettings {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package com.google.showcase.v1beta1.stub; | ||
|
|
||
| // This is a test class for file-diff infra | ||
| public class EchoStubSettings {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the composer layer in the test class, only testing classDef node. Because it's not in gapic/composer folder any more, and the GapicClass/addApacheLicense is package visible. So Apache License is manually added here to avoid reusing the helper from composer folder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM.