1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.grammar.java21;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static org.junit.Assert.assertThrows;
24
25 import java.io.File;
26
27 import org.junit.jupiter.api.Test;
28
29 import com.puppycrawl.tools.checkstyle.AbstractTreeTestSupport;
30 import com.puppycrawl.tools.checkstyle.JavaParser;
31 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
32
33 public class Java21AstRegressionTest extends AbstractTreeTestSupport {
34
35 @Override
36 protected String getPackageLocation() {
37 return "com/puppycrawl/tools/checkstyle/grammar/java21";
38 }
39
40 @Test
41 public void testUnnamedVariableBasic() throws Exception {
42 verifyAst(
43 getNonCompilablePath(
44 "ExpectedUnnamedVariableBasic.txt"),
45 getNonCompilablePath(
46 "InputUnnamedVariableBasic.java"));
47 }
48
49 @Test
50 public void testUnnamedVariableSwitch() throws Exception {
51 verifyAst(
52 getNonCompilablePath(
53 "ExpectedUnnamedVariableSwitch.txt"),
54 getNonCompilablePath(
55 "InputUnnamedVariableSwitch.java"));
56 }
57
58 @Test
59 public void testTextBlockConsecutiveEscapes() throws Exception {
60 verifyAst(
61 getNonCompilablePath(
62 "ExpectedTextBlockConsecutiveEscapes.txt"),
63 getNonCompilablePath(
64 "InputTextBlockConsecutiveEscapes.java"));
65 }
66
67
68
69
70
71
72
73
74
75 @Test
76 public void testTextBlockParsingFail() throws Exception {
77 final File file =
78 new File(getNonCompilablePath("InputTextBlockParsingFail.java.fail"));
79
80 final Throwable throwable =
81 assertThrows("Exception should be thrown due to parsing failure.",
82 CheckstyleException.class,
83 () -> JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS)
84 );
85
86 final String incorrectThrowableCauseMessage =
87 "Cause of CheckstyleException should be IllegalStateException.";
88
89 assertWithMessage(incorrectThrowableCauseMessage)
90 .that(throwable.getCause())
91 .isInstanceOf(IllegalStateException.class);
92
93 final String incorrectParsingFailureMessage =
94 "Message of IllegalStateException should contain the parsing failure.";
95
96 assertWithMessage(incorrectParsingFailureMessage)
97 .that(throwable.getCause().getMessage())
98 .contains("13:14: mismatched input '}\\n"
99 + " ' expecting TEXT_BLOCK_LITERAL_END");
100
101 }
102 }