Skip to content

Commit 71ac86f

Browse files
[ggj][infra][4/5]feat: move the expected string in unit test to golden files (#279)
* add golden folder * format * move all expected strings to golden files * format * update goldens * update ast integration test * keep service client test * constant helper * format * format * feedback
1 parent 118defc commit 71ac86f

32 files changed

Lines changed: 3767 additions & 4098 deletions

src/test/java/com/google/api/generator/engine/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ TESTS = [
1616
[java_test(
1717
name = test_name,
1818
srcs = ["{0}.java".format(test_name)],
19+
data = ["//src/test/java/com/google/api/generator/engine/goldens:goldens_files"],
1920
test_class = "com.google.api.generator.engine.{0}".format(test_name),
2021
deps = [
2122
"//src/main/java/com/google/api/generator/engine/ast",
2223
"//src/main/java/com/google/api/generator/engine/writer",
24+
"//src/test/java/com/google/api/generator/test/framework",
2325
"@junit_junit//jar",
2426
],
2527
) for test_name in TESTS]

src/test/java/com/google/api/generator/engine/JavaCodeGeneratorTest.java

Lines changed: 7 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
package com.google.api.generator.engine;
1616

17-
import static junit.framework.Assert.assertEquals;
18-
1917
import com.google.api.generator.engine.ast.AnnotationNode;
2018
import com.google.api.generator.engine.ast.AnonymousClassExpr;
2119
import com.google.api.generator.engine.ast.AssignmentExpr;
@@ -51,8 +49,11 @@
5149
import com.google.api.generator.engine.ast.VariableExpr;
5250
import com.google.api.generator.engine.ast.WhileStatement;
5351
import com.google.api.generator.engine.writer.JavaWriterVisitor;
52+
import com.google.api.generator.test.framework.Assert;
5453
import java.io.FileWriter;
5554
import java.io.IOException;
55+
import java.nio.file.Path;
56+
import java.nio.file.Paths;
5657
import java.util.ArrayList;
5758
import java.util.Arrays;
5859
import java.util.HashMap;
@@ -61,6 +62,8 @@
6162
import org.junit.Test;
6263

6364
public class JavaCodeGeneratorTest {
65+
private static final String GOLDENFILES_DIRECTORY =
66+
"src/test/java/com/google/api/generator/engine/goldens/";
6467
// Create shared VaporReferences.
6568
private static final VaporReference stubRef = createVaporReference("com.google.gax.grpc", "Stub");
6669
private static final VaporReference bookKindRef =
@@ -169,7 +172,8 @@ public void validJavaClass() {
169172
.build();
170173
JavaWriterVisitor javaWriterVisitor = new JavaWriterVisitor();
171174
libraryServiceStubClass.accept(javaWriterVisitor);
172-
assertEquals(javaWriterVisitor.write(), EXPECTED_CLASS_STRING);
175+
Path goldenFilePath = Paths.get(GOLDENFILES_DIRECTORY, "JavaCodeGeneratorTest.golden");
176+
Assert.assertCodeEquals(goldenFilePath, javaWriterVisitor.write());
173177
}
174178

175179
// Private helpers.
@@ -744,156 +748,4 @@ private ClassDefinition createNestedClassNovel() {
744748
Arrays.asList(createOverrideCreateBookMethod(novelClassRef, bookKindNovelEnumExpr)))
745749
.build();
746750
}
747-
748-
private static final String EXPECTED_CLASS_STRING =
749-
"/*\n"
750-
+ " * Copyright 2020 Gagpic-generator-java\n"
751-
+ " *\n"
752-
+ " * Licensed description and license version 2.0 (the \"License\");\n"
753-
+ " *\n"
754-
+ " * https://www.foo.bar/licenses/LICENSE-2.0\n"
755-
+ " *\n"
756-
+ " * Software distributed under the License is distributed on an \"AS IS\" BASIS.\n"
757-
+ " * See the License for the specific language governing permissions and\n"
758-
+ " * limitations under the License.\n"
759-
+ " */\n"
760-
+ "\n"
761-
+ "package com.google.example.library.core;\n"
762-
+ "\n"
763-
+ "import com.google.exmaple.library.LibraryService;\n"
764-
+ "import com.google.exmaple.library.core.LibraryServiceStub;\n"
765-
+ "import com.google.exmaple.library.v1.BookKind;\n"
766-
+ "import com.google.gax.grpc.Stub;\n"
767-
+ "import java.io.FileWriter;\n"
768-
+ "import java.io.IOException;\n"
769-
+ "import java.util.ArrayList;\n"
770-
+ "import java.util.HashMap;\n"
771-
+ "import java.util.List;\n"
772-
+ "import java.util.Stack;\n"
773-
+ "\n"
774-
+ "/**\n"
775-
+ " * Service Description: This is a test comment.\n"
776-
+ " *\n"
777-
+ " * <pre><code>\n"
778-
+ " * LibraryServiceStub libServiceStub = new LibraryServiceStub()\n"
779-
+ " * </code></pre>\n"
780-
+ " *\n"
781-
+ " * <ol>\n"
782-
+ " * <li>A \"flattened\" method.\n"
783-
+ " * <li>A \"request object\" method.\n"
784-
+ " * <li>A \"callable\" method.\n"
785-
+ " * </ol>\n"
786-
+ " *\n"
787-
+ " * @deprecated This is a deprecated message.\n"
788-
+ " */\n"
789-
+ "@SuppressWarnings(\"all\")\n"
790-
+ "@Deprecated\n"
791-
+ "@Override\n"
792-
+ "public class LibraryServiceStub extends Stub implements LibraryService {\n"
793-
+ " private static final String serviceName = \"LibraryServiceStub\";\n"
794-
+ " protected List<Shelf> shelfList;\n"
795-
+ " public static HashMap<String, Shelf> shelfMap;\n"
796-
+ "\n"
797-
+ " public LibraryServiceStub() {\n"
798-
+ " super();\n"
799-
+ " this.shelfList = new ArrayList<>();\n"
800-
+ " shelfMap = new HashMap<>();\n"
801-
+ " }\n"
802-
+ "\n"
803-
+ " @Override\n"
804-
+ " public String addShelf(String name, double seriesDoubleNum) {\n"
805-
+ " int seriesNum = ((int) seriesDoubleNum);\n"
806-
+ " if (condition) {\n"
807-
+ " return \"Series number equals to max int value.\";\n"
808-
+ " }\n"
809-
+ " shelfList.add(new Shelf(name, seriesNum));\n"
810-
+ " if (shelfMap.containsKey(name)) {\n"
811-
+ " return \"Shelf is already existing in the map.\";\n"
812-
+ " }\n"
813-
+ " shelfMap.put(name, new Shelf(name, seriesNum));\n"
814-
+ " return \"Shelf added.\";\n"
815-
+ " }\n"
816-
+ "\n"
817-
+ " public void updateShelfMap(Shelf newShelf) throws Exception {\n"
818-
+ " if (shelfMap.containsKey(newShelf.shelfName)) {\n"
819-
+ " shelfMap.put(newShelf.shelfName, newShelf);\n"
820-
+ " } else {\n"
821-
+ " throw new Exception(\"Updating shelf is not existing in the map\");\n"
822-
+ " }\n"
823-
+ " }\n"
824-
+ "\n"
825-
+ " public void printShelfListToFile(String fileName) {\n"
826-
+ " StringBuilder sb = new StringBuilder();\n"
827-
+ " try {\n"
828-
+ " FileWriter fileWriter = new FileWriter(fileName);\n"
829-
+ " for (Shelf s : shelfList) {\n"
830-
+ " sb.append(s.shelfName).append(s.seriesNum);\n"
831-
+ " }\n"
832-
+ " fileName.write(sb.toString());\n"
833-
+ " fileName.close();\n"
834-
+ " } catch (IOException e) {\n"
835-
+ " e.printStackTrace();\n"
836-
+ " }\n"
837-
+ " }\n"
838-
+ "\n"
839-
+ " /**\n"
840-
+ " * Add books to Shelf and check if there is a novel, return string message as"
841-
+ " whether novel books\n"
842-
+ " * are added to the shelf.\n"
843-
+ " *\n"
844-
+ " * @param shelf The Shelf object to which books will put.\n"
845-
+ " * @param stack The Stack of the BookKinds.\n"
846-
+ " */\n"
847-
+ " public String addBooksContainsNovel(Shelf shelf, Stack<BookKind> stack) {\n"
848-
+ " boolean containsNovel = false;\n"
849-
+ " while (stack.isEmpty()) {\n"
850-
+ " Book addedBook = addBookToShelf(stack.pop(), shelf);\n"
851-
+ " if (addedBook instanceof Novel) {\n"
852-
+ " containsNovel = true;\n"
853-
+ " }\n"
854-
+ " }\n"
855-
+ " return containsNovel ? \"Added novels\" : \"No novels added\";\n"
856-
+ " }\n"
857-
+ "\n"
858-
+ " // Private helper.\n"
859-
+ " private Book addBookToShelf(BookKind bookKind, Shelf shelf) {\n"
860-
+ " Book book =\n"
861-
+ " new Book() {\n"
862-
+ " @Override\n"
863-
+ " public void createBook(int seriesNum, BookKind bookKind) {\n"
864-
+ " this.seriesNum = seriesNum;\n"
865-
+ " this.bookKind = bookKind;\n"
866-
+ " }\n"
867-
+ " };\n"
868-
+ " return book;\n"
869-
+ " }\n"
870-
+ "\n"
871-
+ " public class Shelf {\n"
872-
+ " public String shelfName;\n"
873-
+ " public int seriesNum;\n"
874-
+ " public String shelfServiceName = serviceName;\n"
875-
+ "\n"
876-
+ " public Shelf(String shelfName, int seriesNum) {\n"
877-
+ " this.shelfName = shelfName;\n"
878-
+ " this.seriesNum = seriesNum;\n"
879-
+ " }\n"
880-
+ " }\n"
881-
+ "\n"
882-
+ " // Test nested abstract class and abstract method.\n"
883-
+ " public abstract class Book {\n"
884-
+ " public BookKind bookKind;\n"
885-
+ " public int seriesNum;\n"
886-
+ "\n"
887-
+ " public abstract void createBook(int seriesNum, BookKind bookKind);\n"
888-
+ " }\n"
889-
+ "\n"
890-
+ " public class Novel extends Book {\n"
891-
+ "\n"
892-
+ " @Override\n"
893-
+ " public void createBook(int seriesNum, BookKind bookKind) {\n"
894-
+ " this.seriesNum = seriesNum;\n"
895-
+ " this.bookKind = BookKind.NOVEL;\n"
896-
+ " }\n"
897-
+ " }\n"
898-
+ "}\n";
899751
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
filegroup(
4+
name = "goldens_files",
5+
srcs = glob(["*.golden"]),
6+
)
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright 2020 Gagpic-generator-java
3+
*
4+
* Licensed description and license version 2.0 (the "License");
5+
*
6+
* https://www.foo.bar/licenses/LICENSE-2.0
7+
*
8+
* Software distributed under the License is distributed on an "AS IS" BASIS.
9+
* See the License for the specific language governing permissions and
10+
* limitations under the License.
11+
*/
12+
13+
package com.google.example.library.core;
14+
15+
import com.google.exmaple.library.LibraryService;
16+
import com.google.exmaple.library.core.LibraryServiceStub;
17+
import com.google.exmaple.library.v1.BookKind;
18+
import com.google.gax.grpc.Stub;
19+
import java.io.FileWriter;
20+
import java.io.IOException;
21+
import java.util.ArrayList;
22+
import java.util.HashMap;
23+
import java.util.List;
24+
import java.util.Stack;
25+
26+
/**
27+
* Service Description: This is a test comment.
28+
*
29+
* <pre><code>
30+
* LibraryServiceStub libServiceStub = new LibraryServiceStub()
31+
* </code></pre>
32+
*
33+
* <ol>
34+
* <li>A "flattened" method.
35+
* <li>A "request object" method.
36+
* <li>A "callable" method.
37+
* </ol>
38+
*
39+
* @deprecated This is a deprecated message.
40+
*/
41+
@SuppressWarnings("all")
42+
@Deprecated
43+
@Override
44+
public class LibraryServiceStub extends Stub implements LibraryService {
45+
private static final String serviceName = "LibraryServiceStub";
46+
protected List<Shelf> shelfList;
47+
public static HashMap<String, Shelf> shelfMap;
48+
49+
public LibraryServiceStub() {
50+
super();
51+
this.shelfList = new ArrayList<>();
52+
shelfMap = new HashMap<>();
53+
}
54+
55+
@Override
56+
public String addShelf(String name, double seriesDoubleNum) {
57+
int seriesNum = ((int) seriesDoubleNum);
58+
if (condition) {
59+
return "Series number equals to max int value.";
60+
}
61+
shelfList.add(new Shelf(name, seriesNum));
62+
if (shelfMap.containsKey(name)) {
63+
return "Shelf is already existing in the map.";
64+
}
65+
shelfMap.put(name, new Shelf(name, seriesNum));
66+
return "Shelf added.";
67+
}
68+
69+
public void updateShelfMap(Shelf newShelf) throws Exception {
70+
if (shelfMap.containsKey(newShelf.shelfName)) {
71+
shelfMap.put(newShelf.shelfName, newShelf);
72+
} else {
73+
throw new Exception("Updating shelf is not existing in the map");
74+
}
75+
}
76+
77+
public void printShelfListToFile(String fileName) {
78+
StringBuilder sb = new StringBuilder();
79+
try {
80+
FileWriter fileWriter = new FileWriter(fileName);
81+
for (Shelf s : shelfList) {
82+
sb.append(s.shelfName).append(s.seriesNum);
83+
}
84+
fileName.write(sb.toString());
85+
fileName.close();
86+
} catch (IOException e) {
87+
e.printStackTrace();
88+
}
89+
}
90+
91+
/**
92+
* Add books to Shelf and check if there is a novel, return string message as whether novel books
93+
* are added to the shelf.
94+
*
95+
* @param shelf The Shelf object to which books will put.
96+
* @param stack The Stack of the BookKinds.
97+
*/
98+
public String addBooksContainsNovel(Shelf shelf, Stack<BookKind> stack) {
99+
boolean containsNovel = false;
100+
while (stack.isEmpty()) {
101+
Book addedBook = addBookToShelf(stack.pop(), shelf);
102+
if (addedBook instanceof Novel) {
103+
containsNovel = true;
104+
}
105+
}
106+
return containsNovel ? "Added novels" : "No novels added";
107+
}
108+
109+
// Private helper.
110+
private Book addBookToShelf(BookKind bookKind, Shelf shelf) {
111+
Book book =
112+
new Book() {
113+
@Override
114+
public void createBook(int seriesNum, BookKind bookKind) {
115+
this.seriesNum = seriesNum;
116+
this.bookKind = bookKind;
117+
}
118+
};
119+
return book;
120+
}
121+
122+
public class Shelf {
123+
public String shelfName;
124+
public int seriesNum;
125+
public String shelfServiceName = serviceName;
126+
127+
public Shelf(String shelfName, int seriesNum) {
128+
this.shelfName = shelfName;
129+
this.seriesNum = seriesNum;
130+
}
131+
}
132+
133+
// Test nested abstract class and abstract method.
134+
public abstract class Book {
135+
public BookKind bookKind;
136+
public int seriesNum;
137+
138+
public abstract void createBook(int seriesNum, BookKind bookKind);
139+
}
140+
141+
public class Novel extends Book {
142+
143+
@Override
144+
public void createBook(int seriesNum, BookKind bookKind) {
145+
this.seriesNum = seriesNum;
146+
this.bookKind = BookKind.NOVEL;
147+
}
148+
}
149+
}

src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ TESTS = [
2020

2121
filegroup(
2222
name = "composer_files",
23-
srcs = ["{0}.java".format(f) for f in TESTS],
23+
srcs = glob(["*.java"]),
2424
)
2525

2626
java_proto_library(
@@ -39,8 +39,12 @@ java_proto_library(
3939

4040
[java_test(
4141
name = test_name,
42-
srcs = ["{0}.java".format(test_name)],
42+
srcs = [
43+
"{0}.java".format(test_name),
44+
"ComposerConstants.java",
45+
],
4346
data = [
47+
"//src/test/java/com/google/api/generator/gapic/composer/goldens:goldens_files",
4448
"//src/test/java/com/google/api/generator/gapic/testdata:gapic_config_files",
4549
"//src/test/java/com/google/api/generator/gapic/testdata:service_config_files",
4650
],
@@ -51,6 +55,7 @@ java_proto_library(
5155
"//src/main/java/com/google/api/generator/engine/ast",
5256
"//src/main/java/com/google/api/generator/engine/writer",
5357
"//src/main/java/com/google/api/generator/gapic/composer",
58+
"//src/test/java/com/google/api/generator/test/framework",
5459
"//src/main/java/com/google/api/generator/gapic/model",
5560
"//src/main/java/com/google/api/generator/gapic/protoparser",
5661
"//src/test/java/com/google/api/generator/gapic/testdata:showcase_java_proto",

0 commit comments

Comments
 (0)