View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.grammar.antlr4;
21  
22  import org.junit.jupiter.api.Test;
23  
24  import com.puppycrawl.tools.checkstyle.AbstractTreeTestSupport;
25  import com.puppycrawl.tools.checkstyle.JavaParser;
26  
27  public class Antlr4AstRegressionTest extends AbstractTreeTestSupport {
28  
29      @Override
30      protected String getPackageLocation() {
31          return "com/puppycrawl/tools/checkstyle/grammar/antlr4";
32      }
33  
34      @Test
35      public void testPackage() throws Exception {
36          verifyAst(getPath("ExpectedAntlr4AstRegressionPackage.txt"),
37                  getPath("InputAntlr4AstRegressionPackage.java"));
38      }
39  
40      @Test
41      public void testSimple() throws Exception {
42          verifyAst(getPath("ExpectedAntlr4AstRegressionSimple.txt"),
43                  getPath("InputAntlr4AstRegressionSimple.java"));
44      }
45  
46      @Test
47      public void testAnno() throws Exception {
48          verifyAst(getPath("ExpectedAntlr4AstRegressionSimpleWithAnno.txt"),
49                  getPath("InputAntlr4AstRegressionSimpleWithAnno.java"));
50      }
51  
52      @Test
53      public void testImports() throws Exception {
54          verifyAst(getPath("ExpectedAntlr4AstRegressionImports.txt"),
55                  getPath("InputAntlr4AstRegressionImports.java"));
56      }
57  
58      @Test
59      public void testConstructorCall() throws Exception {
60          verifyAst(getPath("ExpectedAntlr4AstRegressionConstructorCall.txt"),
61                  getPath("InputAntlr4AstRegressionConstructorCall.java"));
62      }
63  
64      @Test
65      public void testMethodCall() throws Exception {
66          verifyAst(getPath("ExpectedAntlr4AstRegressionMethodCall.txt"),
67                  getPath("InputAntlr4AstRegressionMethodCall.java"));
68      }
69  
70      @Test
71      public void testRegressionJavaClass1() throws Exception {
72          verifyAst(getPath("ExpectedRegressionJavaClass1Ast.txt"),
73                  getPath("InputRegressionJavaClass1.java"));
74      }
75  
76      @Test
77      public void testRegressionAnnotationLocation() throws Exception {
78          verifyAst(getPath("ExpectedAntlr4AstRegressionAnnotationLocation.txt"),
79                  getPath("InputAntlr4AstRegressionAnnotationLocation.java"));
80      }
81  
82      @Test
83      public void testRegressionKeywordsAndOperators() throws Exception {
84          verifyAst(getPath("ExpectedAntlr4AstRegressionKeywordsAndOperators.txt"),
85                  getPath("InputAntlr4AstRegressionKeywordsAndOperators.java"));
86      }
87  
88      @Test
89      public void testRegressionDiamondOperators() throws Exception {
90          verifyAst(getPath("ExpectedAntlr4AstRegressionKeywordsAndOperators.txt"),
91                  getPath("InputAntlr4AstRegressionKeywordsAndOperators.java"));
92      }
93  
94      @Test
95      public void testSingleLineBlocks() throws Exception {
96          verifyAst(getPath("ExpectedAntlr4AstRegressionSingleLineBlocks.txt"),
97                  getPath("InputAntlr4AstRegressionSingleLineBlocks.java"));
98      }
99  
100     @Test
101     public void testExpressionList() throws Exception {
102         verifyAst(getPath("ExpectedAntlr4AstRegressionExpressionList.txt"),
103                 getPath("InputAntlr4AstRegressionExpressionList.java"));
104     }
105 
106     @Test
107     public void testNewTypeTree() throws Exception {
108         verifyAst(getPath("ExpectedAntlr4AstRegressionNewTypeTree.txt"),
109                 getPath("InputAntlr4AstRegressionNewTypeTree.java"));
110     }
111 
112     @Test
113     public void testFallThroughDefault() throws Exception {
114         verifyAst(getPath("ExpectedAntlr4AstRegressionFallThroughDefault.txt"),
115                 getPath("InputAntlr4AstRegressionFallThroughDefault.java"));
116     }
117 
118     @Test
119     public void testPackageAnnotation() throws Exception {
120         verifyAst(getPath("ExpectedAntlr4AstRegressionPackageAnnotation.txt"),
121                 getPath("package-info.java"));
122     }
123 
124     @Test
125     public void testAnnotationOnSameLine1() throws Exception {
126         verifyAst(getPath("ExpectedAntlr4AstRegressionAnnotationOnSameLine.txt"),
127                 getPath("InputAntlr4AstRegressionAnnotationOnSameLine.java"));
128     }
129 
130     @Test
131     public void testAnnotationOnSameLine2() throws Exception {
132         verifyAst(getPath("ExpectedAntlr4AstRegressionAnnotationOnSameLine2.txt"),
133                 getPath("InputAntlr4AstRegressionAnnotationOnSameLine2.java"));
134     }
135 
136     @Test
137     public void testSuppressWarnings() throws Exception {
138         verifyAst(getPath("ExpectedAntlr4AstRegressionSuppressWarnings.txt"),
139                 getPath("InputAntlr4AstRegressionSuppressWarnings.java"));
140     }
141 
142     @Test
143     public void testBadOverride() throws Exception {
144         verifyAst(getPath("ExpectedAntlr4AstRegressionBadOverride.txt"),
145                 getPath("InputAntlr4AstRegressionBadOverride.java"));
146     }
147 
148     @Test
149     public void testTrickySwitch() throws Exception {
150         verifyAst(getPath("ExpectedAntlr4AstRegressionTrickySwitch.txt"),
151                 getPath("InputAntlr4AstRegressionTrickySwitch.java"));
152     }
153 
154     @Test
155     public void testExplicitInitialization() throws Exception {
156         verifyAst(getPath("ExpectedAntlr4AstRegressionExplicitInitialization.txt"),
157                 getPath("InputAntlr4AstRegressionExplicitInitialization.java"));
158     }
159 
160     @Test
161     public void testTypeParams() throws Exception {
162         verifyAst(getPath("ExpectedAntlr4AstRegressionTypeParams.txt"),
163                 getPath("InputAntlr4AstRegressionTypeParams.java"));
164     }
165 
166     @Test
167     public void testForLoops() throws Exception {
168         verifyAst(getPath("ExpectedAntlr4AstRegressionForLoops.txt"),
169                 getPath("InputAntlr4AstRegressionForLoops.java"));
170     }
171 
172     @Test
173     public void testIllegalCatch() throws Exception {
174         verifyAst(getPath("ExpectedAntlr4AstRegressionIllegalCatch.txt"),
175                 getPath("InputAntlr4AstRegressionIllegalCatch.java"));
176     }
177 
178     @Test
179     public void testNestedTypeParametersAndArrayDeclarators() throws Exception {
180         verifyAst(getPath("ExpectedAntlr4AstRegressionNestedTypeParametersAndArrayDeclarators.txt"),
181                 getPath("InputAntlr4AstRegressionNestedTypeParametersAndArrayDeclarators.java"));
182     }
183 
184     @Test
185     public void testNewLineAtEndOfFileCr() throws Exception {
186         verifyAst(getPath("ExpectedAntlr4AstRegressionNewLineAtEndOfFileCr.txt"),
187                 getPath("InputAntlr4AstRegressionNewLineAtEndOfFileCr.java"));
188     }
189 
190     @Test
191     public void testWeirdCtor() throws Exception {
192         verifyAst(getPath("ExpectedAntlr4AstRegressionWeirdCtor.txt"),
193                 getPath("InputAntlr4AstRegressionWeirdCtor.java"));
194     }
195 
196     @Test
197     public void testAnnotationOnQualifiedTypes() throws Exception {
198         verifyAst(getPath("ExpectedAntlr4AstRegressionAnnotationOnQualifiedTypes.txt"),
199                 getPath("InputAntlr4AstRegressionAnnotationOnQualifiedTypes.java"));
200     }
201 
202     @Test
203     public void testAnnotationOnArrays() throws Exception {
204         verifyAst(getPath("ExpectedAntlr4AstRegressionAnnotationOnArrays.txt"),
205                 getPath("InputAntlr4AstRegressionAnnotationOnArrays.java"));
206     }
207 
208     @Test
209     public void testMethodRefs() throws Exception {
210         verifyAst(getPath("ExpectedAntlr4AstRegressionMethodRefs.txt"),
211                 getPath("InputAntlr4AstRegressionMethodRefs.java"));
212     }
213 
214     @Test
215     public void testEmbeddedBlockComments() throws Exception {
216         verifyAst(getPath("ExpectedAntlr4AstRegressionEmbeddedBlockComments.txt"),
217                 getPath("InputAntlr4AstRegressionEmbeddedBlockComments.java"));
218     }
219 
220     @Test
221     public void testJava16LocalEnumAndInterface() throws Exception {
222         verifyAst(getPath("ExpectedAntlr4AstRegressionJava16LocalEnumAndInterface.txt"),
223                 getNonCompilablePath("InputAntlr4AstRegressionJava16LocalEnumAndInterface.java"));
224     }
225 
226     @Test
227     public void testTrickySwitchWithComments() throws Exception {
228         verifyAst(getPath("ExpectedAntlr4AstRegressionTrickySwitchWithComments.txt"),
229                 getPath("InputAntlr4AstRegressionTrickySwitchWithComments.java"),
230                 JavaParser.Options.WITH_COMMENTS);
231     }
232 
233     @Test
234     public void testCassandraInputWithComments() throws Exception {
235         verifyAst(getPath("ExpectedAntlr4AstRegressionCassandraInputWithComments.txt"),
236                 getPath("InputAntlr4AstRegressionCassandraInputWithComments.java"),
237                 JavaParser.Options.WITH_COMMENTS);
238     }
239 
240     @Test
241     public void testCommentsOnAnnotationsAndEnums() throws Exception {
242         verifyAst(getPath("ExpectedAntlr4AstRegressionCommentsOnAnnotationsAndEnums.txt"),
243                 getPath("InputAntlr4AstRegressionCommentsOnAnnotationsAndEnums.java"),
244                 JavaParser.Options.WITH_COMMENTS);
245     }
246 
247     @Test
248     public void testUncommon() throws Exception {
249         verifyAst(getPath("ExpectedAntlr4AstRegressionUncommon.txt"),
250                 getNonCompilablePath("InputAntlr4AstRegressionUncommon.java"),
251                 JavaParser.Options.WITH_COMMENTS);
252     }
253 
254     @Test
255     public void testUncommon2() throws Exception {
256         verifyAst(getPath("ExpectedAntlr4AstRegressionUncommon2.txt"),
257                 getNonCompilablePath("InputAntlr4AstRegressionUncommon2.java"),
258                 JavaParser.Options.WITH_COMMENTS);
259     }
260 
261     @Test
262     public void testTrickyYield() throws Exception {
263         verifyAst(getPath("ExpectedAntlr4AstRegressionTrickyYield.txt"),
264                 getPath("InputAntlr4AstRegressionTrickyYield.java"),
265                 JavaParser.Options.WITH_COMMENTS);
266     }
267 
268     @Test
269     public void testSingleCommaInArrayInit() throws Exception {
270         verifyAst(getPath("ExpectedAntlr4AstRegressionSingleCommaInArrayInit.txt"),
271                 getPath("InputAntlr4AstRegressionSingleCommaInArrayInit.java"),
272                 JavaParser.Options.WITH_COMMENTS);
273     }
274 
275     @Test
276     public void testUncommon3() throws Exception {
277         verifyAst(getPath("ExpectedAntlr4AstRegressionUncommon3.txt"),
278                 getNonCompilablePath("InputAntlr4AstRegressionUncommon3.java"),
279                 JavaParser.Options.WITH_COMMENTS);
280     }
281 
282     @Test
283     public void testUncommon4() throws Exception {
284         verifyAst(getPath("ExpectedAntlr4AstRegressionUncommon4.txt"),
285                 getPath("InputAntlr4AstRegressionUncommon4.java"),
286                 JavaParser.Options.WITH_COMMENTS);
287     }
288 
289     @Test
290     public void testQualifiedConstructorParameter() throws Exception {
291         verifyAst(getPath("ExpectedAntlr4AstRegressionQualifiedConstructorParameter.txt"),
292                 getNonCompilablePath("InputAntlr4AstRegressionQualifiedConstructorParameter.java"));
293     }
294 
295     @Test
296     public void testJava15FinalLocalRecord() throws Exception {
297         verifyAst(getPath("ExpectedAntlr4AstRegressionJava15FinalLocalRecord.txt"),
298                 getNonCompilablePath("InputAntlr4AstRegressionJava15FinalLocalRecord.java"),
299                 JavaParser.Options.WITH_COMMENTS);
300     }
301 
302     @Test
303     public void testUnusualAnnotation() throws Exception {
304         verifyAst(getPath("ExpectedAntlr4AstRegressionUnusualAnnotation.txt"),
305                 getPath("InputAntlr4AstRegressionUnusualAnnotation.java"));
306     }
307 
308     @Test
309     public void testLambda() throws Exception {
310         verifyAst(getPath("ExpectedAntlr4AstRegressionLambda.txt"),
311                 getPath("InputAntlr4AstRegressionLambda.java"));
312     }
313 
314     @Test
315     public void testExpressions() throws Exception {
316         verifyAst(getPath("ExpectedAntlr4AstRegressionExpressions.txt"),
317                 getPath("InputAntlr4AstRegressionExpressions.java"));
318     }
319 
320     @Test
321     public void testInterfaceMemberAlternativePrecedence() throws Exception {
322         verifyAst(getPath("ExpectedAntlr4AstRegressionInterfaceRecordDef.txt"),
323                 getNonCompilablePath("InputAntlr4AstRegressionInterfaceRecordDef.java"));
324     }
325 }