Skip to content

Commit

Permalink
More testing
Browse files Browse the repository at this point in the history
  • Loading branch information
KuechA committed Oct 17, 2024
1 parent 1361c53 commit 487b38e
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ class ExpressionHandlerTest {
assertLocalName("i", singleWithIf.comprehensionExpressions[0].variable)
assertIs<Reference>(singleWithIf.comprehensionExpressions[0].iterable)
assertLocalName("x", singleWithIf.comprehensionExpressions[0].iterable)
assertNotNull(singleWithIf.comprehensionExpressions[0].predicate)
val ifPredicate = singleWithIf.comprehensionExpressions[0].predicate
assertIs<BinaryOperator>(ifPredicate)
assertEquals("==", ifPredicate.operatorCode)

val singleWithoutIfAssignment = body.statements[1] as? AssignExpression
assertNotNull(singleWithoutIfAssignment)
Expand All @@ -75,7 +77,20 @@ class ExpressionHandlerTest {
assertLocalName("x", singleWithoutIf.comprehensionExpressions[0].iterable)
assertNull(singleWithoutIf.comprehensionExpressions[0].predicate)

val doubleAssignment = body.statements[2] as? AssignExpression
val singleWithDoubleIfAssignment = body.statements[2] as? AssignExpression
assertNotNull(singleWithDoubleIfAssignment)
val singleWithDoubleIf = singleWithDoubleIfAssignment.rhs[0] as? CollectionComprehension
assertNotNull(singleWithDoubleIf)
assertIs<CallExpression>(singleWithDoubleIf.statement)
assertEquals(1, singleWithDoubleIf.comprehensionExpressions.size)
assertLocalName("i", singleWithDoubleIf.comprehensionExpressions[0].variable)
assertIs<Reference>(singleWithDoubleIf.comprehensionExpressions[0].iterable)
assertLocalName("x", singleWithDoubleIf.comprehensionExpressions[0].iterable)
val doubleIfPredicate = singleWithDoubleIf.comprehensionExpressions[0].predicate
assertIs<BinaryOperator>(doubleIfPredicate)
assertEquals("and", doubleIfPredicate.operatorCode)

val doubleAssignment = body.statements[3] as? AssignExpression
assertNotNull(doubleAssignment)
val double = doubleAssignment.rhs[0] as? CollectionComprehension
assertNotNull(double)
Expand Down Expand Up @@ -106,7 +121,9 @@ class ExpressionHandlerTest {
assertLocalName("i", singleWithIf.comprehensionExpressions[0].variable)
assertIs<Reference>(singleWithIf.comprehensionExpressions[0].iterable)
assertLocalName("x", singleWithIf.comprehensionExpressions[0].iterable)
assertNotNull(singleWithIf.comprehensionExpressions[0].predicate)
val ifPredicate = singleWithIf.comprehensionExpressions[0].predicate
assertIs<BinaryOperator>(ifPredicate)
assertEquals("==", ifPredicate.operatorCode)

val singleWithoutIfAssignment = body.statements[1] as? AssignExpression
assertNotNull(singleWithoutIfAssignment)
Expand All @@ -119,7 +136,20 @@ class ExpressionHandlerTest {
assertLocalName("x", singleWithoutIf.comprehensionExpressions[0].iterable)
assertNull(singleWithoutIf.comprehensionExpressions[0].predicate)

val doubleAssignment = body.statements[2] as? AssignExpression
val singleWithDoubleIfAssignment = body.statements[2] as? AssignExpression
assertNotNull(singleWithDoubleIfAssignment)
val singleWithDoubleIf = singleWithDoubleIfAssignment.rhs[0] as? CollectionComprehension
assertNotNull(singleWithDoubleIf)
assertIs<CallExpression>(singleWithDoubleIf.statement)
assertEquals(1, singleWithDoubleIf.comprehensionExpressions.size)
assertLocalName("i", singleWithDoubleIf.comprehensionExpressions[0].variable)
assertIs<Reference>(singleWithDoubleIf.comprehensionExpressions[0].iterable)
assertLocalName("x", singleWithDoubleIf.comprehensionExpressions[0].iterable)
val doubleIfPredicate = singleWithDoubleIf.comprehensionExpressions[0].predicate
assertIs<BinaryOperator>(doubleIfPredicate)
assertEquals("and", doubleIfPredicate.operatorCode)

val doubleAssignment = body.statements[3] as? AssignExpression
assertNotNull(doubleAssignment)
val double = doubleAssignment.rhs[0] as? CollectionComprehension
assertNotNull(double)
Expand Down Expand Up @@ -154,7 +184,9 @@ class ExpressionHandlerTest {
assertLocalName("i", singleWithIf.comprehensionExpressions[0].variable)
assertIs<Reference>(singleWithIf.comprehensionExpressions[0].iterable)
assertLocalName("x", singleWithIf.comprehensionExpressions[0].iterable)
assertNotNull(singleWithIf.comprehensionExpressions[0].predicate)
val ifPredicate = singleWithIf.comprehensionExpressions[0].predicate
assertIs<BinaryOperator>(ifPredicate)
assertEquals("==", ifPredicate.operatorCode)

val singleWithoutIfAssignment = body.statements[1] as? AssignExpression
assertNotNull(singleWithoutIfAssignment)
Expand All @@ -171,7 +203,24 @@ class ExpressionHandlerTest {
assertLocalName("x", singleWithoutIf.comprehensionExpressions[0].iterable)
assertNull(singleWithoutIf.comprehensionExpressions[0].predicate)

val doubleAssignment = body.statements[2] as? AssignExpression
val singleWithDoubleIfAssignment = body.statements[2] as? AssignExpression
assertNotNull(singleWithDoubleIfAssignment)
val singleWithDoubleIf = singleWithDoubleIfAssignment.rhs[0] as? CollectionComprehension
assertNotNull(singleWithDoubleIf)
statement = singleWithIf.statement
assertIs<KeyValueExpression>(statement)
assertIs<Reference>(statement.key)
assertLocalName("i", statement.key)
assertIs<CallExpression>(statement.value)
assertEquals(1, singleWithDoubleIf.comprehensionExpressions.size)
assertLocalName("i", singleWithDoubleIf.comprehensionExpressions[0].variable)
assertIs<Reference>(singleWithDoubleIf.comprehensionExpressions[0].iterable)
assertLocalName("x", singleWithDoubleIf.comprehensionExpressions[0].iterable)
val doubleIfPredicate = singleWithDoubleIf.comprehensionExpressions[0].predicate
assertIs<BinaryOperator>(doubleIfPredicate)
assertEquals("and", doubleIfPredicate.operatorCode)

val doubleAssignment = body.statements[3] as? AssignExpression
assertNotNull(doubleAssignment)
val double = doubleAssignment.rhs[0] as? CollectionComprehension
assertNotNull(double)
Expand All @@ -184,6 +233,44 @@ class ExpressionHandlerTest {
// TODO: Add tests on the comprehension expressions
}

@Test
fun testGeneratorExpr() {
val topLevel = Path.of("src", "test", "resources", "python")
val result =
analyze(listOf(topLevel.resolve("comprehension.py").toFile()), topLevel, true) {
it.registerLanguage<PythonLanguage>()
}
assertNotNull(result)
val listComp = result.functions["generator"]
assertNotNull(listComp)

val body = listComp.body as? Block
assertNotNull(body)
val singleWithIfAssignment = body.statements[0] as? AssignExpression
assertNotNull(singleWithIfAssignment)
val singleWithIf = singleWithIfAssignment.rhs[0] as? CollectionComprehension
assertNotNull(singleWithIf)
assertIs<BinaryOperator>(singleWithIf.statement)
assertEquals(1, singleWithIf.comprehensionExpressions.size)
assertLocalName("i", singleWithIf.comprehensionExpressions[0].variable)
assertIs<CallExpression>(singleWithIf.comprehensionExpressions[0].iterable)
assertLocalName("range", singleWithIf.comprehensionExpressions[0].iterable)
val ifPredicate = singleWithIf.comprehensionExpressions[0].predicate
assertIs<BinaryOperator>(ifPredicate)
assertEquals("==", ifPredicate.operatorCode)

val singleWithoutIfAssignment = body.statements[1] as? AssignExpression
assertNotNull(singleWithoutIfAssignment)
val singleWithoutIf = singleWithoutIfAssignment.rhs[0] as? CollectionComprehension
assertNotNull(singleWithoutIf)
assertIs<BinaryOperator>(singleWithoutIf.statement)
assertEquals(1, singleWithoutIf.comprehensionExpressions.size)
assertLocalName("i", singleWithoutIf.comprehensionExpressions[0].variable)
assertIs<CallExpression>(singleWithIf.comprehensionExpressions[0].iterable)
assertLocalName("range", singleWithIf.comprehensionExpressions[0].iterable)
assertNull(singleWithoutIf.comprehensionExpressions[0].predicate)
}

@Test
fun testBoolOps() {
val topLevel = Path.of("src", "test", "resources", "python")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ def foo(arg):
def listComp(x, y):
a = [foo(i) for i in x if i == 10]
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 ]

def setComp(x, y):
a = {foo(i) for i in x if i == 10}
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 }

def dictComp(x, y):
a = {i: foo(i) for i in x if i == 10}
b = {i: foo(i) for i in x}
d = {i: foo(i) for z in y if z in x for i in z if i == 10 }
c = {i: foo(i) for i in x if i == 10 if i < 20}
d = {i: foo(i) for z in y if z in x for i in z if i == 10 }

def generator(x, y):
a = (i**2 for i in range(10) if i == 10)
b = (i**2 for i in range(10))

0 comments on commit 487b38e

Please sign in to comment.