Skip to content

Commit 8843527

Browse files
Add regression tests for type-pattern switches (#345)
The `PartialSourcePosition only contains a CompilationUnit` crash came from Spoon 10.4.2 at the old compliance level; it no longer reproduces since Spoon 11.5.0 and compliance level detection (#352, #368). Pattern switches need Java 21, above the level of liquidjava-example (20, where they fail to compile and produce a position-less warning), so the tests verify a temporary project that declares release 21: a pattern switch expression and statement (with a guard and the pattern variable used in the case body) verify cleanly, and a violation inside a pattern case body is still reported. Fixes #345 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a0b466a commit 8843527

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package liquidjava.api.tests;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.io.TempDir;
12+
13+
import liquidjava.api.CommandLineLauncher;
14+
import liquidjava.diagnostics.Diagnostics;
15+
import liquidjava.diagnostics.errors.LJError;
16+
17+
/**
18+
* Type-pattern switches need Java 21, above the level of liquidjava-example, so they are verified here in a project
19+
* that declares release 21 (#345).
20+
*/
21+
class TestPatternSwitch {
22+
23+
@TempDir
24+
Path project;
25+
26+
private Diagnostics verify(String className, String source) throws IOException {
27+
Files.writeString(project.resolve("pom.xml"),
28+
"<project><modelVersion>4.0.0</modelVersion><properties><maven.compiler.release>21"
29+
+ "</maven.compiler.release></properties></project>");
30+
Path src = Files.createDirectories(project.resolve("src"));
31+
Files.writeString(src.resolve(className + ".java"), source);
32+
CommandLineLauncher.launch(src.toString());
33+
return Diagnostics.getInstance();
34+
}
35+
36+
@Test
37+
void verifiesTypePatternSwitch() throws IOException {
38+
Diagnostics diagnostics = verify("Kinds", """
39+
public class Kinds {
40+
static String kind(Object o) {
41+
return switch (o) {
42+
case String s -> "string " + s;
43+
case Integer i when i > 0 -> "positive";
44+
default -> "other";
45+
};
46+
}
47+
48+
static int size(Object o) {
49+
switch (o) {
50+
case String s -> { return s.length(); }
51+
default -> { return 0; }
52+
}
53+
}
54+
}
55+
""");
56+
assertTrue(diagnostics.getErrors().isEmpty(), diagnostics.getErrorOutput());
57+
assertTrue(diagnostics.getWarnings().isEmpty(), diagnostics.getWarningOutput());
58+
}
59+
60+
@Test
61+
void checksRefinementsInsidePatternCase() throws IOException {
62+
Diagnostics diagnostics = verify("Cases", """
63+
import liquidjava.specification.Refinement;
64+
65+
public class Cases {
66+
static int positive(@Refinement("_ > 0") int x) {
67+
return x;
68+
}
69+
70+
static int kind(Object o) {
71+
return switch (o) {
72+
case String s -> positive(-1);
73+
default -> 0;
74+
};
75+
}
76+
}
77+
""");
78+
assertEquals(1, diagnostics.getErrors().size(), diagnostics.getErrorOutput());
79+
LJError error = diagnostics.getErrors().iterator().next();
80+
assertEquals("Refinement Error", error.getTitle());
81+
assertEquals(10, error.getPosition().getLine());
82+
}
83+
}

0 commit comments

Comments
 (0)