mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
added assert case for isinstance type flow (PY-845)
This commit is contained in:
@@ -10,15 +10,22 @@ import java.util.List;
|
||||
* @author traff
|
||||
*/
|
||||
public class InstructionBuilder {
|
||||
public static List<Instruction> buildInstructions(ControlFlowBuilder builder, List<PyConditionEvaluator.Definition> definitions) {
|
||||
public static List<Instruction> buildInstructions(ControlFlowBuilder builder, List<PyAssertionEvaluator.Assertion> assertions) {
|
||||
List<Instruction> result = CollectionFactory.arrayList();
|
||||
for (PyConditionEvaluator.Definition def: definitions) {
|
||||
for (PyAssertionEvaluator.Assertion def: assertions) {
|
||||
processDef(builder, def, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void processDef(ControlFlowBuilder builder, PyConditionEvaluator.Definition def, List<Instruction> result) {
|
||||
private static void processDef(ControlFlowBuilder builder, PyAssertionEvaluator.Assertion def, List<Instruction> result) {
|
||||
result.add(ReadWriteInstruction.writeType(builder, def.getElement(), def.getName()));
|
||||
}
|
||||
|
||||
public static void addAssertInstructions(ControlFlowBuilder builder, PyAssertionEvaluator assertionEvaluator) {
|
||||
for (Instruction instr : buildInstructions(builder, assertionEvaluator.getDefinitions())) {
|
||||
builder.addNode(instr);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -10,11 +10,11 @@ import java.util.Stack;
|
||||
/**
|
||||
* @author traff
|
||||
*/
|
||||
public class PyConditionEvaluator extends PyRecursiveElementVisitor {
|
||||
public class PyAssertionEvaluator extends PyRecursiveElementVisitor {
|
||||
|
||||
private Stack<Definition> myStack = CollectionFactory.stack();
|
||||
private Stack<Assertion> myStack = CollectionFactory.stack();
|
||||
|
||||
public PyConditionEvaluator() {
|
||||
public PyAssertionEvaluator() {
|
||||
}
|
||||
|
||||
|
||||
@@ -35,17 +35,17 @@ public class PyConditionEvaluator extends PyRecursiveElementVisitor {
|
||||
PyExpression[] args = node.getArguments();
|
||||
if (args.length == 2 && args[0] instanceof PyReferenceExpression) {
|
||||
PyReferenceExpression target = (PyReferenceExpression)args[0];
|
||||
Definition o = new Definition(target.getName(), args[1]);
|
||||
Assertion o = new Assertion(target.getName(), args[1]);
|
||||
myStack.push(o);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Definition {
|
||||
class Assertion {
|
||||
private final String name;
|
||||
private final PyElement element;
|
||||
|
||||
Definition(String name, PyElement element) {
|
||||
Assertion(String name, PyElement element) {
|
||||
this.name = name;
|
||||
this.element = element;
|
||||
}
|
||||
@@ -59,7 +59,7 @@ public class PyConditionEvaluator extends PyRecursiveElementVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
public List<Definition> getDefinitions() {
|
||||
public List<Assertion> getDefinitions() {
|
||||
return myStack;
|
||||
}
|
||||
}
|
||||
@@ -198,10 +198,10 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor {
|
||||
myBuilder.startNode(node);
|
||||
final PyIfPart ifPart = node.getIfPart();
|
||||
PyExpression condition = ifPart.getCondition();
|
||||
PyConditionEvaluator conditionEvaluator = new PyConditionEvaluator();
|
||||
PyAssertionEvaluator assertionEvaluator = new PyAssertionEvaluator();
|
||||
if (condition != null) {
|
||||
condition.accept(this);
|
||||
condition.accept(conditionEvaluator);
|
||||
condition.accept(assertionEvaluator);
|
||||
}
|
||||
// Set the head as the last instruction of condition
|
||||
Instruction head = getPrevInstruction(condition);
|
||||
@@ -209,9 +209,7 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor {
|
||||
final PyStatementList thenStatements = ifPart.getStatementList();
|
||||
if (thenStatements != null) {
|
||||
myBuilder.startConditionalNode(thenStatements, condition, true);
|
||||
for (Instruction instr : InstructionBuilder.buildInstructions(myBuilder, conditionEvaluator.getDefinitions())) {
|
||||
myBuilder.addNode(instr);
|
||||
}
|
||||
InstructionBuilder.addAssertInstructions(myBuilder, assertionEvaluator);
|
||||
thenStatements.accept(this);
|
||||
myBuilder.processPending(new ControlFlowBuilder.PendingProcessor() {
|
||||
public void process(final PsiElement pendingScope, final Instruction instruction) {
|
||||
@@ -553,4 +551,12 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor {
|
||||
result.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void visitPyAssertStatement(final PyAssertStatement node) {
|
||||
PyAssertionEvaluator evaluator = new PyAssertionEvaluator();
|
||||
node.acceptChildren(evaluator);
|
||||
InstructionBuilder.addAssertInstructions(myBuilder, evaluator);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -226,4 +226,8 @@ public class PyElementVisitor extends PsiElementVisitor {
|
||||
public void visitPyConditionalStatementPart(PyConditionalStatementPart node) {
|
||||
visitPyElement(node);
|
||||
}
|
||||
|
||||
public void visitPyAssertStatement(final PyAssertStatement node) {
|
||||
visitPyElement(node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.jetbrains.python.psi.impl;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.jetbrains.python.psi.PyAssertStatement;
|
||||
import com.jetbrains.python.psi.PyElementVisitor;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
@@ -10,4 +11,8 @@ public class PyAssertStatementImpl extends PyElementImpl implements PyAssertStat
|
||||
public PyAssertStatementImpl(ASTNode astNode) {
|
||||
super(astNode);
|
||||
}
|
||||
|
||||
protected void acceptPyVisitor(PyElementVisitor pyVisitor) {
|
||||
pyVisitor.visitPyAssertStatement(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
assert isinstance(s, basestring), isinstance(x, int)
|
||||
@@ -0,0 +1,4 @@
|
||||
0(1) element: null
|
||||
1(2) WRITETYPE ACCESS: s
|
||||
2(3) WRITETYPE ACCESS: x
|
||||
3() element: null
|
||||
@@ -30,7 +30,11 @@ public class PyControlFlowBuilderTest extends LightMarkedTestCase {
|
||||
final String fullPath = getTestDataPath() + testName + ".txt";
|
||||
check(fullPath, flow);
|
||||
}
|
||||
|
||||
|
||||
public void testAssert() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testFile() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user