169c0141848b6eba4590855bf545092219b8a306
[idea/community.git] / python / testSrc / com / jetbrains / python / PythonParsingTest.java
1 /*
2  * Copyright 2000-2016 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.jetbrains.python;
17
18 import com.intellij.psi.PsiFile;
19 import com.intellij.psi.util.PsiTreeUtil;
20 import com.intellij.testFramework.ParsingTestCase;
21 import com.intellij.testFramework.TestDataPath;
22 import com.jetbrains.python.psi.LanguageLevel;
23 import com.jetbrains.python.psi.PyFunction;
24 import org.jetbrains.annotations.NotNull;
25
26 import java.util.Collection;
27
28 /**
29  * @author yole
30  */
31 @TestDataPath("$CONTENT_ROOT/../testData/psi/")
32 public class PythonParsingTest extends ParsingTestCase {
33   private LanguageLevel myLanguageLevel = LanguageLevel.getDefault();
34
35   public PythonParsingTest() {
36     super("psi", "py", new PythonParserDefinition());
37   }
38
39   @Override
40   protected void setUp() throws Exception {
41     super.setUp();
42     registerExtensionPoint(PythonDialectsTokenSetContributor.EP_NAME, PythonDialectsTokenSetContributor.class);
43     registerExtension(PythonDialectsTokenSetContributor.EP_NAME, new PythonTokenSetContributor());
44   }
45
46   @Override
47   protected String getTestDataPath() {
48     return PythonTestUtil.getTestDataPath();
49   }
50
51   public void testHelloWorld() {
52     doTest();
53   }
54
55   public void testIfStatement() {
56     doTest();
57   }
58
59   public void testConditionalExpression() {
60     doTest();
61   }
62
63   public void testSubscribedAssignmentLHS() {
64     doTest();
65   }
66
67   public void testConditionalParenLambda() {
68     doTest();
69   }
70
71   public void testLambdaComprehension() {
72     doTest();
73   }
74
75   public void testLambdaConditional() {
76     doTest();
77   }
78
79   public void testTryExceptFinally() {
80     doTest();
81   }
82
83   public void testTryFinally() {
84     doTest();
85   }
86
87   public void testYieldStatement() {
88     doTest();
89   }
90
91   public void testYieldInAssignment() {
92     doTest();
93   }
94
95   public void testYieldInAugAssignment() {
96     doTest();
97   }
98
99   public void testYieldInParentheses() {
100     doTest();
101   }
102
103   public void _testYieldAsArgument() {
104     // this is a strange case: PEP 342 says this syntax is valid, but
105     // Python 2.5 doesn't accept it. let's stick with Python behavior for now
106     doTest();
107   }
108
109   public void testWithStatement() {
110     doTest();
111   }
112
113   public void testWithStatement2() {
114     doTest();
115   }
116
117   public void testImportStmt() {
118     doTest();
119   }
120
121   public void testDecoratedFunction() {
122     doTest();
123   }
124
125   public void testTryExceptAs() {   // PY-293
126     doTest();
127   }
128
129   public void testWithStatement26() {
130     doTest(LanguageLevel.PYTHON26);
131   }
132
133   public void testPrintAsFunction26() {
134     doTest(LanguageLevel.PYTHON26);
135   }
136
137   public void testClassDecorators() {
138     doTest(LanguageLevel.PYTHON26);
139   }
140
141   public void testEmptySuperclassList() {  // PY-321
142     doTest();
143   }
144
145   public void testListComprehensionNestedIf() {  // PY-322
146     doTest();
147   }
148
149   public void testKeywordOnlyArgument() {   // PEP 3102
150     doTest(LanguageLevel.PYTHON30);
151   }
152
153   public void testPy3KKeywords() {
154     doTest(LanguageLevel.PYTHON30);
155   }
156
157   public void testExecPy2() {
158     doTest();
159   }
160
161   public void testExecPy3() {
162     doTest(LanguageLevel.PYTHON30);
163   }
164
165   public void testSuperclassKeywordArguments() {
166     doTest(LanguageLevel.PYTHON30);
167   }
168
169   public void testDictLiteral() {
170     doTest();
171   }
172
173   public void testSetLiteral() {
174     doTest(LanguageLevel.PYTHON30);
175   }
176
177   public void testSetComprehension() {
178     doTest(LanguageLevel.PYTHON30);
179   }
180
181   public void testDictComprehension() {
182     doTest(LanguageLevel.PYTHON30);
183   }
184
185   public void testRaiseFrom() {
186     doTest(LanguageLevel.PYTHON30);
187   }
188
189   public void testEllipsis() {
190     doTest();
191   }
192
193   public void testTupleArguments() {
194     doTest();
195   }
196
197   public void testDefaultTupleArguments() {
198     doTest();
199   }
200
201   public void testExtendedSlices() {
202     doTest();
203   }
204
205   public void testAnnotations() {
206     doTest(LanguageLevel.PYTHON30);
207   }
208
209   public void testNonlocal() {
210     doTest(LanguageLevel.PYTHON30);
211   }
212
213   public void testFloorDiv() {
214     doTest();
215   }
216
217   public void testWithStatement31() {
218     doTest(LanguageLevel.PYTHON31);
219   }
220
221   public void testLongString() {
222     doTest();
223   }
224
225   public void testTrailingSemicolon() {  // PY-363
226     doTest();
227   }
228
229   public void testStarExpression() {   // PEP-3132
230     doTest(LanguageLevel.PYTHON30);
231   }
232
233   public void testDictMissingComma() {  // PY-1025
234     doTest();
235   }
236
237   public void testInconsistentDedent() { // PY-1131
238     doTest();
239   }
240
241   public void testReturnAtEOF() {  // PY-1739
242     doTest();
243   }
244
245   public void testMissingListSeparators() {  // PY-1933
246     doTest();
247   }
248
249   public void testTrailingCommaInList() {
250     doTest();
251   }
252
253   public void testCommentBeforeMethod() { // PY-2209 & friends
254     doTest();
255   }
256
257   public void testBadDecoratorNotMethod() {
258     doTest();
259   }
260
261   public void testCommentAtEndOfMethod() { // PY-2137
262     doTest();
263   }
264
265   public void testCommentAtBeginningOfStatementList() {  // PY-2108
266     doTest();
267   }
268
269   public void testCommentBetweenClasses() {  // PY-1598
270     doTest();
271   }
272
273   public void testIncompleteDict() {
274     doTest();
275   }
276
277   public void testSliceList() {  // PY-1928
278     doTest();
279   }
280
281   public void testDictMissingValue() {  // PY-2791
282     doTest();
283   }
284
285   public void testColonBeforeEof() {  // PY-2790
286     doTest();
287   }
288
289   public void testGeneratorInArgumentList() {  // PY-3172
290     doTest();
291   }
292
293   public void testNestedGenerators() {  // PY-3030
294     doTest();
295   }
296
297   public void testMissingDefaultValue() {  // PY-3253
298     doTest();
299   }
300
301   public void testErrorInParameterList() {  // PY-3635
302     doTest();
303   }
304
305   public void testKeywordAsDefaultParameterValue() {  // PY-3713
306     doTest();
307   }
308
309   public void testTrailingCommaInArgList() {  // PY-4016
310     doTest();
311   }
312
313   public void testMissingParenInCall() {  // PY-4053
314     doTest();
315   }
316
317   public void testTupleAsDictKey() {  // PY-4144
318     doTest();
319   }
320
321   public void testIncompleteStatementList() {  // PY-3792
322     doTest();
323   }
324
325   public void testIncompleteFor() {  // PY-3792
326     doTest();
327   }
328
329   public void testCallInAssignment() {  // PY-5062
330     doTest();
331   }
332
333   public void testCommaAfterStarArg() {  // PY-4039
334     doTest();
335   }
336
337   public void testRangeAsLHS() {  // PY-6468
338     doTest();
339   }
340
341   // PY-6702
342   public void testYieldFrom() {
343     doTest(LanguageLevel.PYTHON33);
344   }
345
346   // PY-6733
347   public void testYieldFromNoExpr() {
348     doTest(LanguageLevel.PYTHON33);
349   }
350
351   // PY-6734
352   public void testRaiseFromNoExpr() {
353     doTest(LanguageLevel.PYTHON30);
354   }
355
356   // PY-6781
357   public void testComprehensionErrors() {
358     doTest();
359   }
360
361   // PY-6926
362   public void testGeneratorList() {
363     doTest();
364   }
365
366   // EA-30244
367   public void testEqYieldEq() {
368     doTest();
369   }
370
371   public void doTest() {
372     doTest(LanguageLevel.PYTHON25);
373   }
374
375   public void testCompoundStatementAfterSemicolon() {  // PY-7660
376     doTest();
377   }
378
379   // PY-8606
380   public void testEllipsisInSliceList() {
381     doTest();
382   }
383
384   // PY-8606
385   public void testEllipsisInSliceListTail() {
386     doTest();
387   }
388
389   public void testEmptySubscription() {  // PY-8652
390     doTest();
391   }
392
393   // PY-8752
394   public void testEllipsisPython3() {
395     doTest(LanguageLevel.PYTHON33);
396   }
397
398   // PY-8948
399   public void testNotClosedSlice() {
400     doTest();
401   }
402
403   // PY-11058
404   public void testResetAfterSemicolon() {
405     doTest();
406   }
407
408   public void testLoneStar() {  // PY-10177
409     doTest();
410   }
411
412   public void testCommentAfterDecorator() {  // PY-5912
413     doTest();
414   }
415
416   public void testKeywordAsNamedParameter() {  // PY-8318
417     doTest();
418   }
419
420   public void testKeywordAsClassName() {  // PY-8319
421     doTest();
422   }
423
424   public void testKeywordAsFunctionName() {  // PY-8319
425     doTest();
426   }
427
428   public void testIfInList() {  // PY-9561
429     doTest();
430   }
431
432   public void testWithMissingID() {  // PY-9853
433     doTest(LanguageLevel.PYTHON27);
434   }
435
436   public void testOverIndentedComment() {  // PY-1909
437     doTest();
438   }
439
440   public void testNotClosedBraceDict() {
441     doTest();
442   }
443
444   public void testNotClosedBraceSet() {
445     doTest(LanguageLevel.PYTHON33);
446   }
447
448   public void testEmptyBlockInFunctionBeforeFunction() {
449     doTest();
450   }
451
452   public void testBlockWithoutColon() {
453     doTest();
454   }
455
456   public void testSingleDefBeforeFunction() {
457     doTest();
458   }
459
460   public void testSingleClassBeforeFunction() {
461     doTest();
462   }
463
464   // PY-14408
465   public void testTabInsideContinuationIndent() {
466     doTest();
467   }
468
469   // PY-15390
470   public void testMatMul() {
471     doTest(LanguageLevel.PYTHON35);
472   }
473
474   // PY-15653
475   public void testMissingFunctionNameAndThenParametersList() {
476     doTest();
477   }
478
479   // PY-15653
480   public void testMissingClassNameAndThenListOfBaseClasses() {
481     doTest();
482   }
483
484   // PY-15653
485   public void testMissingClassNameAndThenColon() {
486     doTest();
487   }
488
489   public void testAsyncDef() {
490     doTest(LanguageLevel.PYTHON35);
491   }
492
493   public void testAsyncWith() {
494     doTest(LanguageLevel.PYTHON35);
495   }
496
497   public void testAsyncFor() {
498     doTest(LanguageLevel.PYTHON35);
499   }
500
501   public void testAwait() {
502     doTest(LanguageLevel.PYTHON35);
503   }
504
505   public void testDecoratedAsyncDef() {
506     doTest(LanguageLevel.PYTHON35);
507   }
508
509   // PY-19036
510   public void testAwaitInNonAsyncNestedFunction() {
511     doTest(LanguageLevel.PYTHON35);
512   }
513
514   public void testUnpackingExpressions() {
515     doTest(LanguageLevel.PYTHON35);
516   }
517
518   public void doTest(LanguageLevel languageLevel) {
519     LanguageLevel prev = myLanguageLevel;
520     myLanguageLevel = languageLevel;
521     try {
522       doTest(true);
523     }
524     finally {
525       myLanguageLevel = prev;
526     }
527     ensureEachFunctionHasStatementList(myFile, PyFunction.class);
528   }
529
530   @Override
531   protected PsiFile createFile(String name, String text) {
532     final PsiFile file = super.createFile(name, text);
533     file.getVirtualFile().putUserData(LanguageLevel.KEY, myLanguageLevel);
534     return file;
535   }
536
537   public static <T extends PyFunction> void ensureEachFunctionHasStatementList(
538     @NotNull PsiFile parentFile,
539     @NotNull Class<T> functionType) {
540     Collection<T> functions = PsiTreeUtil.findChildrenOfType(parentFile, functionType);
541     for (T functionToCheck : functions) {
542       functionToCheck.getStatementList(); //To make sure each function has statement list (does not throw exception)
543     }
544   }
545 }