Skip to content

Commit

Permalink
[ggj][infra][4/5]feat: move the expected string in unit test to golde…
Browse files Browse the repository at this point in the history
…n 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
  • Loading branch information
xiaozhenliu-gg5 authored Sep 29, 2020
1 parent 118defc commit 71ac86f
Showing 32 changed files with 3,767 additions and 4,098 deletions.
2 changes: 2 additions & 0 deletions src/test/java/com/google/api/generator/engine/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -16,10 +16,12 @@ TESTS = [
[java_test(
name = test_name,
srcs = ["{0}.java".format(test_name)],
data = ["//src/test/java/com/google/api/generator/engine/goldens:goldens_files"],
test_class = "com.google.api.generator.engine.{0}".format(test_name),
deps = [
"//src/main/java/com/google/api/generator/engine/ast",
"//src/main/java/com/google/api/generator/engine/writer",
"//src/test/java/com/google/api/generator/test/framework",
"@junit_junit//jar",
],
) for test_name in TESTS]
Original file line number Diff line number Diff line change
@@ -14,8 +14,6 @@

package com.google.api.generator.engine;

import static junit.framework.Assert.assertEquals;

import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.AnonymousClassExpr;
import com.google.api.generator.engine.ast.AssignmentExpr;
@@ -51,8 +49,11 @@
import com.google.api.generator.engine.ast.VariableExpr;
import com.google.api.generator.engine.ast.WhileStatement;
import com.google.api.generator.engine.writer.JavaWriterVisitor;
import com.google.api.generator.test.framework.Assert;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -61,6 +62,8 @@
import org.junit.Test;

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

// Private helpers.
@@ -744,156 +748,4 @@ private ClassDefinition createNestedClassNovel() {
Arrays.asList(createOverrideCreateBookMethod(novelClassRef, bookKindNovelEnumExpr)))
.build();
}

private static final String EXPECTED_CLASS_STRING =
"/*\n"
+ " * Copyright 2020 Gagpic-generator-java\n"
+ " *\n"
+ " * Licensed description and license version 2.0 (the \"License\");\n"
+ " *\n"
+ " * https://www.foo.bar/licenses/LICENSE-2.0\n"
+ " *\n"
+ " * Software distributed under the License is distributed on an \"AS IS\" BASIS.\n"
+ " * See the License for the specific language governing permissions and\n"
+ " * limitations under the License.\n"
+ " */\n"
+ "\n"
+ "package com.google.example.library.core;\n"
+ "\n"
+ "import com.google.exmaple.library.LibraryService;\n"
+ "import com.google.exmaple.library.core.LibraryServiceStub;\n"
+ "import com.google.exmaple.library.v1.BookKind;\n"
+ "import com.google.gax.grpc.Stub;\n"
+ "import java.io.FileWriter;\n"
+ "import java.io.IOException;\n"
+ "import java.util.ArrayList;\n"
+ "import java.util.HashMap;\n"
+ "import java.util.List;\n"
+ "import java.util.Stack;\n"
+ "\n"
+ "/**\n"
+ " * Service Description: This is a test comment.\n"
+ " *\n"
+ " * <pre><code>\n"
+ " * LibraryServiceStub libServiceStub = new LibraryServiceStub()\n"
+ " * </code></pre>\n"
+ " *\n"
+ " * <ol>\n"
+ " * <li>A \"flattened\" method.\n"
+ " * <li>A \"request object\" method.\n"
+ " * <li>A \"callable\" method.\n"
+ " * </ol>\n"
+ " *\n"
+ " * @deprecated This is a deprecated message.\n"
+ " */\n"
+ "@SuppressWarnings(\"all\")\n"
+ "@Deprecated\n"
+ "@Override\n"
+ "public class LibraryServiceStub extends Stub implements LibraryService {\n"
+ " private static final String serviceName = \"LibraryServiceStub\";\n"
+ " protected List<Shelf> shelfList;\n"
+ " public static HashMap<String, Shelf> shelfMap;\n"
+ "\n"
+ " public LibraryServiceStub() {\n"
+ " super();\n"
+ " this.shelfList = new ArrayList<>();\n"
+ " shelfMap = new HashMap<>();\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public String addShelf(String name, double seriesDoubleNum) {\n"
+ " int seriesNum = ((int) seriesDoubleNum);\n"
+ " if (condition) {\n"
+ " return \"Series number equals to max int value.\";\n"
+ " }\n"
+ " shelfList.add(new Shelf(name, seriesNum));\n"
+ " if (shelfMap.containsKey(name)) {\n"
+ " return \"Shelf is already existing in the map.\";\n"
+ " }\n"
+ " shelfMap.put(name, new Shelf(name, seriesNum));\n"
+ " return \"Shelf added.\";\n"
+ " }\n"
+ "\n"
+ " public void updateShelfMap(Shelf newShelf) throws Exception {\n"
+ " if (shelfMap.containsKey(newShelf.shelfName)) {\n"
+ " shelfMap.put(newShelf.shelfName, newShelf);\n"
+ " } else {\n"
+ " throw new Exception(\"Updating shelf is not existing in the map\");\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " public void printShelfListToFile(String fileName) {\n"
+ " StringBuilder sb = new StringBuilder();\n"
+ " try {\n"
+ " FileWriter fileWriter = new FileWriter(fileName);\n"
+ " for (Shelf s : shelfList) {\n"
+ " sb.append(s.shelfName).append(s.seriesNum);\n"
+ " }\n"
+ " fileName.write(sb.toString());\n"
+ " fileName.close();\n"
+ " } catch (IOException e) {\n"
+ " e.printStackTrace();\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " /**\n"
+ " * Add books to Shelf and check if there is a novel, return string message as"
+ " whether novel books\n"
+ " * are added to the shelf.\n"
+ " *\n"
+ " * @param shelf The Shelf object to which books will put.\n"
+ " * @param stack The Stack of the BookKinds.\n"
+ " */\n"
+ " public String addBooksContainsNovel(Shelf shelf, Stack<BookKind> stack) {\n"
+ " boolean containsNovel = false;\n"
+ " while (stack.isEmpty()) {\n"
+ " Book addedBook = addBookToShelf(stack.pop(), shelf);\n"
+ " if (addedBook instanceof Novel) {\n"
+ " containsNovel = true;\n"
+ " }\n"
+ " }\n"
+ " return containsNovel ? \"Added novels\" : \"No novels added\";\n"
+ " }\n"
+ "\n"
+ " // Private helper.\n"
+ " private Book addBookToShelf(BookKind bookKind, Shelf shelf) {\n"
+ " Book book =\n"
+ " new Book() {\n"
+ " @Override\n"
+ " public void createBook(int seriesNum, BookKind bookKind) {\n"
+ " this.seriesNum = seriesNum;\n"
+ " this.bookKind = bookKind;\n"
+ " }\n"
+ " };\n"
+ " return book;\n"
+ " }\n"
+ "\n"
+ " public class Shelf {\n"
+ " public String shelfName;\n"
+ " public int seriesNum;\n"
+ " public String shelfServiceName = serviceName;\n"
+ "\n"
+ " public Shelf(String shelfName, int seriesNum) {\n"
+ " this.shelfName = shelfName;\n"
+ " this.seriesNum = seriesNum;\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " // Test nested abstract class and abstract method.\n"
+ " public abstract class Book {\n"
+ " public BookKind bookKind;\n"
+ " public int seriesNum;\n"
+ "\n"
+ " public abstract void createBook(int seriesNum, BookKind bookKind);\n"
+ " }\n"
+ "\n"
+ " public class Novel extends Book {\n"
+ "\n"
+ " @Override\n"
+ " public void createBook(int seriesNum, BookKind bookKind) {\n"
+ " this.seriesNum = seriesNum;\n"
+ " this.bookKind = BookKind.NOVEL;\n"
+ " }\n"
+ " }\n"
+ "}\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,149 @@
/*
* Copyright 2020 Gagpic-generator-java
*
* Licensed description and license version 2.0 (the "License");
*
* https://www.foo.bar/licenses/LICENSE-2.0
*
* Software distributed under the License is distributed on an "AS IS" BASIS.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.example.library.core;

import com.google.exmaple.library.LibraryService;
import com.google.exmaple.library.core.LibraryServiceStub;
import com.google.exmaple.library.v1.BookKind;
import com.google.gax.grpc.Stub;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;

/**
* Service Description: This is a test comment.
*
* <pre><code>
* LibraryServiceStub libServiceStub = new LibraryServiceStub()
* </code></pre>
*
* <ol>
* <li>A "flattened" method.
* <li>A "request object" method.
* <li>A "callable" method.
* </ol>
*
* @deprecated This is a deprecated message.
*/
@SuppressWarnings("all")
@Deprecated
@Override
public class LibraryServiceStub extends Stub implements LibraryService {
private static final String serviceName = "LibraryServiceStub";
protected List<Shelf> shelfList;
public static HashMap<String, Shelf> shelfMap;

public LibraryServiceStub() {
super();
this.shelfList = new ArrayList<>();
shelfMap = new HashMap<>();
}

@Override
public String addShelf(String name, double seriesDoubleNum) {
int seriesNum = ((int) seriesDoubleNum);
if (condition) {
return "Series number equals to max int value.";
}
shelfList.add(new Shelf(name, seriesNum));
if (shelfMap.containsKey(name)) {
return "Shelf is already existing in the map.";
}
shelfMap.put(name, new Shelf(name, seriesNum));
return "Shelf added.";
}

public void updateShelfMap(Shelf newShelf) throws Exception {
if (shelfMap.containsKey(newShelf.shelfName)) {
shelfMap.put(newShelf.shelfName, newShelf);
} else {
throw new Exception("Updating shelf is not existing in the map");
}
}

public void printShelfListToFile(String fileName) {
StringBuilder sb = new StringBuilder();
try {
FileWriter fileWriter = new FileWriter(fileName);
for (Shelf s : shelfList) {
sb.append(s.shelfName).append(s.seriesNum);
}
fileName.write(sb.toString());
fileName.close();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* Add books to Shelf and check if there is a novel, return string message as whether novel books
* are added to the shelf.
*
* @param shelf The Shelf object to which books will put.
* @param stack The Stack of the BookKinds.
*/
public String addBooksContainsNovel(Shelf shelf, Stack<BookKind> stack) {
boolean containsNovel = false;
while (stack.isEmpty()) {
Book addedBook = addBookToShelf(stack.pop(), shelf);
if (addedBook instanceof Novel) {
containsNovel = true;
}
}
return containsNovel ? "Added novels" : "No novels added";
}

// Private helper.
private Book addBookToShelf(BookKind bookKind, Shelf shelf) {
Book book =
new Book() {
@Override
public void createBook(int seriesNum, BookKind bookKind) {
this.seriesNum = seriesNum;
this.bookKind = bookKind;
}
};
return book;
}

public class Shelf {
public String shelfName;
public int seriesNum;
public String shelfServiceName = serviceName;

public Shelf(String shelfName, int seriesNum) {
this.shelfName = shelfName;
this.seriesNum = seriesNum;
}
}

// Test nested abstract class and abstract method.
public abstract class Book {
public BookKind bookKind;
public int seriesNum;

public abstract void createBook(int seriesNum, BookKind bookKind);
}

public class Novel extends Book {

@Override
public void createBook(int seriesNum, BookKind bookKind) {
this.seriesNum = seriesNum;
this.bookKind = BookKind.NOVEL;
}
}
}
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ TESTS = [

filegroup(
name = "composer_files",
srcs = ["{0}.java".format(f) for f in TESTS],
srcs = glob(["*.java"]),
)

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

[java_test(
name = test_name,
srcs = ["{0}.java".format(test_name)],
srcs = [
"{0}.java".format(test_name),
"ComposerConstants.java",
],
data = [
"//src/test/java/com/google/api/generator/gapic/composer/goldens:goldens_files",
"//src/test/java/com/google/api/generator/gapic/testdata:gapic_config_files",
"//src/test/java/com/google/api/generator/gapic/testdata:service_config_files",
],
@@ -51,6 +55,7 @@ java_proto_library(
"//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",
"//src/test/java/com/google/api/generator/test/framework",
"//src/main/java/com/google/api/generator/gapic/model",
"//src/main/java/com/google/api/generator/gapic/protoparser",
"//src/test/java/com/google/api/generator/gapic/testdata:showcase_java_proto",
Loading

0 comments on commit 71ac86f

Please sign in to comment.