Skip to content

Commit 36870fa

Browse files
committed
add testcase for GROOVY-11202
1 parent 95283c8 commit 36870fa

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

subprojects/groovy-groovydoc/src/test/groovy/org/codehaus/groovy/tools/groovydoc/GroovyDocToolTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,40 @@ private static String switchExpressionJavaSource(String packageName, String clas
17121712
+ "}\n";
17131713
}
17141714

1715+
private static List<String> createInstanceofPatternJavaSources(Path root, int count) throws IOException {
1716+
String pkg = "org/codehaus/groovy/tools/groovydoc/testfiles/instanceofpattern";
1717+
String packageName = pkg.replace('/', '.');
1718+
Path pkgDir = root.resolve(pkg);
1719+
Files.createDirectories(pkgDir);
1720+
1721+
List<String> sources = new ArrayList<>(count);
1722+
for (int i = 0; i < count; i++) {
1723+
String className = "InstanceofPattern" + i;
1724+
Files.writeString(pkgDir.resolve(className + ".java"), instanceofPatternJavaSource(packageName, className));
1725+
sources.add(pkg + "/" + className + ".java");
1726+
}
1727+
return sources;
1728+
}
1729+
1730+
private static String instanceofPatternJavaSource(String packageName, String className) {
1731+
return "package " + packageName + ";\n"
1732+
+ "import java.math.BigDecimal;\n"
1733+
+ "/**\n"
1734+
+ " * Exercise Java 17 instanceof pattern matching (JEP 394).\n"
1735+
+ " */\n"
1736+
+ "public class " + className + " {\n"
1737+
+ " public String describe(Object value) {\n"
1738+
+ " if (value instanceof BigDecimal bd) {\n"
1739+
+ " return \"DECIMAL(\" + bd.precision() + \",\" + bd.scale() + \")\";\n"
1740+
+ " }\n"
1741+
+ " if (value instanceof String s && !s.isEmpty()) {\n"
1742+
+ " return \"VARCHAR(\" + s.length() + \")\";\n"
1743+
+ " }\n"
1744+
+ " return \"UNKNOWN\";\n"
1745+
+ " }\n"
1746+
+ "}\n";
1747+
}
1748+
17151749
// GROOVY-9057
17161750
public void testParseErrorIsTrackedInErrorCount() throws Exception {
17171751
assertEquals("Initial error count should be zero", 0, xmlToolForTests.getErrorCount());
@@ -2753,6 +2787,27 @@ public void testLanguageLevelSupportedForSwitchExpressions() throws Exception {
27532787
}
27542788
}
27552789

2790+
// GROOVY-11202
2791+
public void testLanguageLevelSupportedForInstanceofPatterns() throws Exception {
2792+
Path tmp = Files.createTempDirectory("groovydoc-instanceof-pattern-");
2793+
try {
2794+
List<String> sources = createInstanceofPatternJavaSources(tmp, 1);
2795+
GroovyDocTool tool = makeHtmltool(new ArrayList<>(), ParserConfiguration.LanguageLevel.JAVA_17.name(), new Properties(), new String[] {tmp.toString()});
2796+
tool.add(sources);
2797+
2798+
MockOutputTool output = new MockOutputTool();
2799+
tool.renderToOutput(output, MOCK_DIR);
2800+
2801+
String base = "org/codehaus/groovy/tools/groovydoc/testfiles/instanceofpattern";
2802+
String javadoc = output.getText(MOCK_DIR + "/" + base + "/InstanceofPattern0.html");
2803+
assertNotNull("Javadoc should not be null since Java 17 instanceof patterns are supported", javadoc);
2804+
assertEquals("Expected no parse errors for instanceof pattern source", 0, tool.getErrorCount());
2805+
assertTrue("Expected describe(Object) signature in:\n" + javadoc, javadoc.contains("describe"));
2806+
} finally {
2807+
deleteRecursively(tmp);
2808+
}
2809+
}
2810+
27562811
public void testGroovydocParsingUsesExplicitJavaParserLanguageLevel() throws Exception {
27572812
StaticJavaParser.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_1_4);
27582813
JavaParser javaParser = new JavaParser(

0 commit comments

Comments
 (0)