Skip to content

Commit

Permalink
Extend test to check fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
KuechA committed Jan 31, 2025
1 parent 209c218 commit 1ff35b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package de.fraunhofer.aisec.cpg.frontends.python

import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration
import de.fraunhofer.aisec.cpg.graph.statements.expressions.AssignExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Block
Expand All @@ -36,6 +37,8 @@ import de.fraunhofer.aisec.cpg.graph.statements.expressions.Reference
import de.fraunhofer.aisec.cpg.test.analyze
import de.fraunhofer.aisec.cpg.test.assertLiteralValue
import de.fraunhofer.aisec.cpg.test.assertLocalName
import de.fraunhofer.aisec.cpg.test.assertNotRefersTo
import de.fraunhofer.aisec.cpg.test.assertRefersTo
import java.nio.file.Path
import kotlin.test.*

Expand All @@ -57,14 +60,30 @@ class ExpressionHandlerTest {
assertIs<AssignExpression>(singleWithIfAssignment)
val singleWithIf = singleWithIfAssignment.rhs[0]
assertIs<CollectionComprehension>(singleWithIf)
assertIs<CallExpression>(singleWithIf.statement)
val fooCall = singleWithIf.statement
assertIs<CallExpression>(fooCall)
val usageI = fooCall.arguments[0]
assertIs<Reference>(usageI)
assertEquals(1, singleWithIf.comprehensionExpressions.size)
assertLocalName("i", singleWithIf.comprehensionExpressions[0].variable)
val variableI = singleWithIf.comprehensionExpressions[0].variable
assertIs<Reference>(variableI)
assertLocalName("i", variableI)
val declI = variableI.refersTo
assertIs<VariableDeclaration>(declI)
assertEquals(singleWithIf, declI.scope?.astNode)
assertIs<Reference>(singleWithIf.comprehensionExpressions[0].iterable)
assertLocalName("x", singleWithIf.comprehensionExpressions[0].iterable)
val ifPredicate = singleWithIf.comprehensionExpressions[0].predicate
assertIs<BinaryOperator>(ifPredicate)
assertEquals("==", ifPredicate.operatorCode)
assertRefersTo(usageI, declI)

val fooIOutside = body.statements[4]
assertIs<CallExpression>(fooIOutside)
val outsideI = fooIOutside.arguments[0]
assertIs<Reference>(outsideI)
assertLocalName("i", outsideI)
assertNotRefersTo(outsideI, declI)

val singleWithoutIfAssignment = body.statements[1]
assertIs<AssignExpression>(singleWithoutIfAssignment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def listComp(x, y):
b = [foo(i) for i in x]
c = {foo(i) for i in x if i == 10 if i < 20}
d = [foo(i) for z in y if z in x for i in z if i == 10 ]
foo(i)

def setComp(x, y):
a = {foo(i) for i in x if i == 10}
Expand Down

0 comments on commit 1ff35b8

Please sign in to comment.