removed state from add super call quickfix, added tests

This commit is contained in:
Ekaterina Tuzova
2014-03-11 18:23:53 +04:00
parent 378b22a23d
commit 37c7fcabec
7 changed files with 87 additions and 14 deletions
@@ -70,7 +70,7 @@ public class PyMissingConstructorInspection extends PyInspection {
}
if (superClasses.length == 1 || node.isNewStyleClass())
registerProblem(initMethod.getNameIdentifier(), PyBundle.message("INSP.missing.super.constructor.message"),
new AddCallSuperQuickFix(node.getSuperClasses()[0], superClasses[0].getText()));
new AddCallSuperQuickFix());
else
registerProblem(initMethod.getNameIdentifier(), PyBundle.message("INSP.missing.super.constructor.message"));
}
@@ -38,13 +38,6 @@ import java.util.List;
* User: catherine
*/
public class AddCallSuperQuickFix implements LocalQuickFix {
private final PyClass mySuper;
private String mySuperName;
public AddCallSuperQuickFix(PyClass superClass, String superName) {
mySuper = superClass;
mySuperName = superName;
}
@NotNull
public String getName() {
@@ -60,12 +53,17 @@ public class AddCallSuperQuickFix implements LocalQuickFix {
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
PyFunction problemFunction = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PyFunction.class);
if (problemFunction == null) return;
PyFunction superInit = mySuper.findMethodByName(PyNames.INIT, true);
final StringBuilder superCall = new StringBuilder();
final PyClass klass = problemFunction.getContainingClass();
if (klass == null) return;
final PyClass[] superClasses = klass.getSuperClasses();
if (superClasses.length == 0) return;
final PyClass superClass = superClasses[0];
final PyFunction superInit = superClass.findMethodByName(PyNames.INIT, true);
if (superInit == null) return;
StringBuilder superCall = new StringBuilder();
PyClass klass = problemFunction.getContainingClass();
boolean addComma = true;
if (klass != null && klass.isNewStyleClass()) {
if (klass.isNewStyleClass()) {
addComma = false;
if (LanguageLevel.forElement(klass).isPy3K())
superCall.append("super().__init__(");
@@ -73,7 +71,7 @@ public class AddCallSuperQuickFix implements LocalQuickFix {
superCall.append("super(").append(klass.getName()).append(", self).__init__(");
}
else {
superCall.append(mySuperName);
superCall.append(superClass.getName());
superCall.append(".__init__(self");
}
StringBuilder newFunction = new StringBuilder("def __init__(self");
@@ -83,7 +81,7 @@ public class AddCallSuperQuickFix implements LocalQuickFix {
superCall.append(")");
final PyStatementList statementList = problemFunction.getStatementList();
PyExpression docstring = null;
final PyStatement[] statements = statementList == null ? new PyStatement[0] : statementList.getStatements();
final PyStatement[] statements = statementList.getStatements();
if (statements.length != 0 && statements[0] instanceof PyExpressionStatement) {
PyExpressionStatement st = (PyExpressionStatement)statements[0];
if (st.getExpression() instanceof PyStringLiteralExpression)
@@ -0,0 +1,11 @@
class A(object):
def __init__(self):
a = 1
class C(A):
def <warning descr="Call to __init__ of super class is missed">__i<caret>nit__</warning>(self):
pass
def foo(self):
pass
@@ -0,0 +1,11 @@
class A(object):
def __init__(self):
a = 1
class C(A):
def __init__(self):
super(C, self).__init__()
def foo(self):
pass
@@ -0,0 +1,10 @@
class A():
def __init__(self):
a = 1
class C(A):
def <warning descr="Call to __init__ of super class is missed">__ini<caret>t__</warning>(self):
pass
def foo(self):
pass
@@ -0,0 +1,10 @@
class A():
def __init__(self):
a = 1
class C(A):
def __init__(self):
A.__init__(self)
def foo(self):
pass
@@ -0,0 +1,33 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.quickFixes;
import com.intellij.testFramework.TestDataPath;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyQuickFixTestCase;
import com.jetbrains.python.inspections.PyMissingConstructorInspection;
@TestDataPath("$CONTENT_ROOT/../testData//quickFixes/AddCallSuperQuickFixTest/")
public class AddCallSuperQuickFixTest extends PyQuickFixTestCase {
public void testOldStyle() {
doQuickFixTest(PyMissingConstructorInspection.class, PyBundle.message("QFIX.add.super"));
}
public void testNewStyle() {
doQuickFixTest(PyMissingConstructorInspection.class, PyBundle.message("QFIX.add.super"));
}
}