-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ggj][gapic][1/5] feat: add new unit test for testing file-diff infra (…
…#280) * add new dummy unit test * format * move dummy test to separate folder
- Loading branch information
1 parent
76d61a0
commit 52c3eb7
Showing
6 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/test/java/com/google/api/generator/gapic/dummy/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
104 changes: 104 additions & 0 deletions
104
src/test/java/com/google/api/generator/gapic/dummy/FileDiffInfraDummyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = | ||
"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"; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/test/java/com/google/api/generator/gapic/dummy/goldens/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]), | ||
) |
19 changes: 19 additions & 0 deletions
19
...com/google/api/generator/gapic/dummy/goldens/FileDiffInfraDummyTestClassWithHeader.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
4 changes: 4 additions & 0 deletions
4
...ava/com/google/api/generator/gapic/dummy/goldens/FileDiffInfraDummyTestSimpleClass.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |