2 * Copyright 2000-2016 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.jetbrains.python;
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;
26 import java.util.Collection;
31 @TestDataPath("$CONTENT_ROOT/../testData/psi/")
32 public class PythonParsingTest extends ParsingTestCase {
33 private LanguageLevel myLanguageLevel = LanguageLevel.getDefault();
35 public PythonParsingTest() {
36 super("psi", "py", new PythonParserDefinition());
40 protected void setUp() throws Exception {
42 registerExtensionPoint(PythonDialectsTokenSetContributor.EP_NAME, PythonDialectsTokenSetContributor.class);
43 registerExtension(PythonDialectsTokenSetContributor.EP_NAME, new PythonTokenSetContributor());
47 protected String getTestDataPath() {
48 return PythonTestUtil.getTestDataPath();
51 public void testHelloWorld() {
55 public void testIfStatement() {
59 public void testConditionalExpression() {
63 public void testSubscribedAssignmentLHS() {
67 public void testConditionalParenLambda() {
71 public void testLambdaComprehension() {
75 public void testLambdaConditional() {
79 public void testTryExceptFinally() {
83 public void testTryFinally() {
87 public void testYieldStatement() {
91 public void testYieldInAssignment() {
95 public void testYieldInAugAssignment() {
99 public void testYieldInParentheses() {
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
109 public void testWithStatement() {
113 public void testWithStatement2() {
117 public void testImportStmt() {
121 public void testDecoratedFunction() {
125 public void testTryExceptAs() { // PY-293
129 public void testWithStatement26() {
130 doTest(LanguageLevel.PYTHON26);
133 public void testPrintAsFunction26() {
134 doTest(LanguageLevel.PYTHON26);
137 public void testClassDecorators() {
138 doTest(LanguageLevel.PYTHON26);
141 public void testEmptySuperclassList() { // PY-321
145 public void testListComprehensionNestedIf() { // PY-322
149 public void testKeywordOnlyArgument() { // PEP 3102
150 doTest(LanguageLevel.PYTHON30);
153 public void testPy3KKeywords() {
154 doTest(LanguageLevel.PYTHON30);
157 public void testExecPy2() {
161 public void testExecPy3() {
162 doTest(LanguageLevel.PYTHON30);
165 public void testSuperclassKeywordArguments() {
166 doTest(LanguageLevel.PYTHON30);
169 public void testDictLiteral() {
173 public void testSetLiteral() {
174 doTest(LanguageLevel.PYTHON30);
177 public void testSetComprehension() {
178 doTest(LanguageLevel.PYTHON30);
181 public void testDictComprehension() {
182 doTest(LanguageLevel.PYTHON30);
185 public void testRaiseFrom() {
186 doTest(LanguageLevel.PYTHON30);
189 public void testEllipsis() {
193 public void testTupleArguments() {
197 public void testDefaultTupleArguments() {
201 public void testExtendedSlices() {
205 public void testAnnotations() {
206 doTest(LanguageLevel.PYTHON30);
209 public void testNonlocal() {
210 doTest(LanguageLevel.PYTHON30);
213 public void testFloorDiv() {
217 public void testWithStatement31() {
218 doTest(LanguageLevel.PYTHON31);
221 public void testLongString() {
225 public void testTrailingSemicolon() { // PY-363
229 public void testStarExpression() { // PEP-3132
230 doTest(LanguageLevel.PYTHON30);
233 public void testDictMissingComma() { // PY-1025
237 public void testInconsistentDedent() { // PY-1131
241 public void testReturnAtEOF() { // PY-1739
245 public void testMissingListSeparators() { // PY-1933
249 public void testTrailingCommaInList() {
253 public void testCommentBeforeMethod() { // PY-2209 & friends
257 public void testBadDecoratorNotMethod() {
261 public void testCommentAtEndOfMethod() { // PY-2137
265 public void testCommentAtBeginningOfStatementList() { // PY-2108
269 public void testCommentBetweenClasses() { // PY-1598
273 public void testIncompleteDict() {
277 public void testSliceList() { // PY-1928
281 public void testDictMissingValue() { // PY-2791
285 public void testColonBeforeEof() { // PY-2790
289 public void testGeneratorInArgumentList() { // PY-3172
293 public void testNestedGenerators() { // PY-3030
297 public void testMissingDefaultValue() { // PY-3253
301 public void testErrorInParameterList() { // PY-3635
305 public void testKeywordAsDefaultParameterValue() { // PY-3713
309 public void testTrailingCommaInArgList() { // PY-4016
313 public void testMissingParenInCall() { // PY-4053
317 public void testTupleAsDictKey() { // PY-4144
321 public void testIncompleteStatementList() { // PY-3792
325 public void testIncompleteFor() { // PY-3792
329 public void testCallInAssignment() { // PY-5062
333 public void testCommaAfterStarArg() { // PY-4039
337 public void testRangeAsLHS() { // PY-6468
342 public void testYieldFrom() {
343 doTest(LanguageLevel.PYTHON33);
347 public void testYieldFromNoExpr() {
348 doTest(LanguageLevel.PYTHON33);
352 public void testRaiseFromNoExpr() {
353 doTest(LanguageLevel.PYTHON30);
357 public void testComprehensionErrors() {
362 public void testGeneratorList() {
367 public void testEqYieldEq() {
371 public void doTest() {
372 doTest(LanguageLevel.PYTHON25);
375 public void testCompoundStatementAfterSemicolon() { // PY-7660
380 public void testEllipsisInSliceList() {
385 public void testEllipsisInSliceListTail() {
389 public void testEmptySubscription() { // PY-8652
394 public void testEllipsisPython3() {
395 doTest(LanguageLevel.PYTHON33);
399 public void testNotClosedSlice() {
404 public void testResetAfterSemicolon() {
408 public void testLoneStar() { // PY-10177
412 public void testCommentAfterDecorator() { // PY-5912
416 public void testKeywordAsNamedParameter() { // PY-8318
420 public void testKeywordAsClassName() { // PY-8319
424 public void testKeywordAsFunctionName() { // PY-8319
428 public void testIfInList() { // PY-9561
432 public void testWithMissingID() { // PY-9853
433 doTest(LanguageLevel.PYTHON27);
436 public void testOverIndentedComment() { // PY-1909
440 public void testNotClosedBraceDict() {
444 public void testNotClosedBraceSet() {
445 doTest(LanguageLevel.PYTHON33);
448 public void testEmptyBlockInFunctionBeforeFunction() {
452 public void testBlockWithoutColon() {
456 public void testSingleDefBeforeFunction() {
460 public void testSingleClassBeforeFunction() {
465 public void testTabInsideContinuationIndent() {
470 public void testMatMul() {
471 doTest(LanguageLevel.PYTHON35);
475 public void testMissingFunctionNameAndThenParametersList() {
480 public void testMissingClassNameAndThenListOfBaseClasses() {
485 public void testMissingClassNameAndThenColon() {
489 public void testAsyncDef() {
490 doTest(LanguageLevel.PYTHON35);
493 public void testAsyncWith() {
494 doTest(LanguageLevel.PYTHON35);
497 public void testAsyncFor() {
498 doTest(LanguageLevel.PYTHON35);
501 public void testAwait() {
502 doTest(LanguageLevel.PYTHON35);
505 public void testDecoratedAsyncDef() {
506 doTest(LanguageLevel.PYTHON35);
510 public void testAwaitInNonAsyncNestedFunction() {
511 doTest(LanguageLevel.PYTHON35);
514 public void testUnpackingExpressions() {
515 doTest(LanguageLevel.PYTHON35);
518 public void doTest(LanguageLevel languageLevel) {
519 LanguageLevel prev = myLanguageLevel;
520 myLanguageLevel = languageLevel;
525 myLanguageLevel = prev;
527 ensureEachFunctionHasStatementList(myFile, PyFunction.class);
531 protected PsiFile createFile(String name, String text) {
532 final PsiFile file = super.createFile(name, text);
533 file.getVirtualFile().putUserData(LanguageLevel.KEY, myLanguageLevel);
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)