Skip to content

Commit

Permalink
Added test case for arithmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Nov 30, 2023
1 parent bb3c623 commit 58aee96
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ open class ValueEvaluator(
* Contains a reference to a function that gets called if the value cannot be resolved by the
* standard behaviour.
*/
val cannotEvaluate: (Node?, ValueEvaluator) -> Any? = { node: Node?, _: ValueEvaluator ->
open val cannotEvaluate: (Node?, ValueEvaluator) -> Any? = { node: Node?, _: ValueEvaluator ->
// end of the line, lets just keep the expression name
if (node != null) {
"{${node.name}}"
Expand Down Expand Up @@ -148,7 +148,7 @@ open class ValueEvaluator(
* Note: this is both used by a [BinaryOperator] with basic arithmetic operations as well as
* [AssignExpression], if [AssignExpression.isCompoundAssignment] is true.
*/
protected fun computeBinaryOpEffect(
protected open fun computeBinaryOpEffect(
lhsValue: Any?,
rhsValue: Any?,
has: HasOperatorCode?,
Expand Down
3 changes: 3 additions & 0 deletions cpg-language-python/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ publishing {
dependencies {
// jep for python support
api(libs.jep)

// to evaluate some test cases
testImplementation(project(":cpg-analysis"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package de.fraunhofer.aisec.cpg.frontends.python;

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package de.fraunhofer.aisec.cpg.frontends.python

import de.fraunhofer.aisec.cpg.BaseTest
import de.fraunhofer.aisec.cpg.TestUtils
import de.fraunhofer.aisec.cpg.analysis.ValueEvaluator
import de.fraunhofer.aisec.cpg.assertFullName
import de.fraunhofer.aisec.cpg.assertLocalName
import de.fraunhofer.aisec.cpg.graph.*
Expand Down Expand Up @@ -1082,4 +1083,42 @@ class PythonFrontendTest : BaseTest() {
barCall.arguments.first().prevDFG.firstOrNull()
)
}

@Test
fun testArithmetics() {
val topLevel = Path.of("src", "test", "resources", "python")
val tu =
TestUtils.analyzeAndGetFirstTU(
listOf(topLevel.resolve("calc.py").toFile()),
topLevel,
true
) {
it.registerLanguage<PythonLanguage>()
}
assertNotNull(tu)

val a = tu.refs["a"]
assertNotNull(a)

val result = a.evaluate(PythonValueEvaluator())
assertEquals(16.0, result)
}

class PythonValueEvaluator : ValueEvaluator() {
override fun computeBinaryOpEffect(
lhsValue: Any?,
rhsValue: Any?,
has: HasOperatorCode?,
): Any? {
return if (has?.operatorCode == "**") {
when {
lhsValue is Number && rhsValue is Number ->
Math.pow(lhsValue.toDouble(), rhsValue.toDouble())
else -> cannotEvaluate(has as Node, this)
}
} else {
super.computeBinaryOpEffect(lhsValue, rhsValue, has)
}
}
}
}
1 change: 1 addition & 0 deletions cpg-language-python/src/test/resources/python/calc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = (((3 - 1) / 2) * 4) ** 2

0 comments on commit 58aee96

Please sign in to comment.