|
| 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