Skip to content

Commit 9c051ba

Browse files
authored
Add a plugin test runner and a test for types-codegen simple types (#855)
* Add a plugin test runner and a test for types-codegen simple types * Address review comments * Updates after merge from upstream/main * Make spotless happy * Comment test that should be disabled by default
1 parent c610b83 commit 9c051ba

9 files changed

Lines changed: 989 additions & 1 deletion

File tree

codegen/plugins/types-codegen/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ description = "This module provides the codegen plugin for Smithy java type code
77
extra["displayName"] = "Smithy :: Java :: Codegen :: Types"
88
extra["moduleName"] = "software.amazon.smithy.java.codegen.types"
99

10+
dependencies {
11+
testImplementation(project(":codegen:test-utils"))
12+
}
13+
1014
addGenerateSrcsTask("software.amazon.smithy.java.codegen.types.TestJavaTypeCodegenRunner")
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.java.codegen.types;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertNotNull;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
11+
import static software.amazon.smithy.java.codegen.test.PluginTestRunner.addTestCasesFromUrl;
12+
import static software.amazon.smithy.java.codegen.test.PluginTestRunner.findGotContent;
13+
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.nio.charset.StandardCharsets;
17+
import java.nio.file.Files;
18+
import java.nio.file.Path;
19+
import java.util.Collection;
20+
import java.util.Set;
21+
import java.util.stream.Collectors;
22+
import org.junit.jupiter.params.ParameterizedTest;
23+
import org.junit.jupiter.params.provider.MethodSource;
24+
import software.amazon.smithy.java.codegen.test.PluginTestRunner.TestCase;
25+
26+
public class TypesCodegenPluginTest {
27+
28+
@ParameterizedTest(name = "[{index}] => {0}")
29+
@MethodSource("testCases")
30+
public void runTestCase(TestCase test) {
31+
test.builder().build();
32+
var got = test.manifests().stream().flatMap(x -> x.getFiles().stream()).collect(Collectors.toSet());
33+
for (var expected : test.expectedToContents().keySet()) {
34+
var found = findExpected(expected, got);
35+
assertNotNull(found, "Expected to find " + expected + " in the manifest");
36+
var contents = findGotContent(found, test);
37+
assertTrue(contents.isPresent());
38+
assertEquals(test.expectedToContents().get(expected), contents.get());
39+
}
40+
}
41+
42+
// Uncomment this test to render the java files when we there are changes to the codegen logic.
43+
// @ParameterizedTest(name = "[{index}] => {0}") @MethodSource("testCases")
44+
public void renderExpected(TestCase test) throws IOException {
45+
test.builder().build();
46+
var got = test.manifests().stream().flatMap(x -> x.getFiles().stream()).collect(Collectors.toSet());
47+
for (var expected : test.expectedToContents().keySet()) {
48+
var found = findExpected(expected, got);
49+
assertNotNull(found);
50+
var contents = findGotContent(found, test);
51+
var expectedFile = new File("/tmp/rendered/" + test + "/expected" + expected);
52+
expectedFile.getParentFile().mkdirs();
53+
Files.write(expectedFile.toPath(), contents.get().getBytes(StandardCharsets.UTF_8));
54+
}
55+
}
56+
57+
private Path findExpected(String expected, Set<Path> manifestFiles) {
58+
return manifestFiles.stream().filter(path -> path.toString().contains(expected)).findFirst().orElse(null);
59+
}
60+
61+
public static Collection<TestCase> testCases() {
62+
return addTestCasesFromUrl(TypesCodegenPluginTest.class.getResource("test-cases"));
63+
}
64+
}

0 commit comments

Comments
 (0)