Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lshala committed Nov 27, 2024
1 parent cdab949 commit f1c2cc5
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1377,93 +1377,6 @@ class PythonFrontendTest : BaseTest() {
assertEquals(2L, fStmtRhsThird.value)
}

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

// Test for g = f'Number: {42:.2f}'
val gStmt = namespace.statements[6]
assertIs<AssignExpression>(gStmt)
val gStmtRhs = gStmt.rhs.singleOrNull()
assertIs<BinaryOperator>(gStmtRhs)
val gFormatCall = gStmtRhs.rhs
assertIs<CallExpression>(gFormatCall)
assertEquals("format", gFormatCall.name.localName)
val gArguments = gFormatCall.arguments
assertEquals(2, gArguments.size)
assertIs<Literal<*>>(gArguments[0])
assertEquals(42.toLong(), gArguments[0].value.value)
assertIs<Literal<*>>(gArguments[1])
assertEquals(".2f", gArguments[1].value.value)

// Test for h = f'Hexadecimal: {255:#x}'
val hStmt = namespace.statements[7]
assertIs<AssignExpression>(hStmt)
val hStmtRhs = hStmt.rhs.singleOrNull()
assertIs<BinaryOperator>(hStmtRhs)
val hFormatCall = hStmtRhs.rhs
assertIs<CallExpression>(hFormatCall)
assertEquals("format", hFormatCall.name.localName)
val hArguments = hFormatCall.arguments
assertEquals(2, hArguments.size)
assertIs<Literal<*>>(hArguments[0])
assertEquals(255L.toLong(), hArguments[0].value.value)
assertIs<Literal<*>>(hArguments[1])
assertEquals("#x", hArguments[1].value.value)

// Test for i = f'String with conversion: {c!r}'
val iStmt = namespace.statements[8]
assertIs<AssignExpression>(iStmt)
val iStmtRhs = iStmt.rhs.singleOrNull()
assertIs<BinaryOperator>(iStmtRhs)
val iConversionCall = iStmtRhs.rhs
assertIs<CallExpression>(iConversionCall)
assertEquals("repr", iConversionCall.name.localName)
val iArg = iConversionCall.arguments.singleOrNull()
assertNotNull(iArg)
assertEquals("c", iArg.name.localName)

// Test for j = f'ASCII representation: {d!a}'
val jStmt = namespace.statements[9]
assertIs<AssignExpression>(jStmt)
val jStmtRhs = jStmt.rhs.singleOrNull()
assertIs<BinaryOperator>(jStmtRhs)
val jConversionCall = jStmtRhs.rhs
assertIs<CallExpression>(jConversionCall)
assertEquals("ascii", jConversionCall.name.localName)
val jArg = jConversionCall.arguments.singleOrNull()
assertNotNull(jArg)
assertEquals("d", jArg.name.localName)

// Test for k = f'Combined: {b!s:10}'
val kStmt = namespace.statements[10]
assertIs<AssignExpression>(kStmt)
val kStmtRhs = kStmt.rhs.singleOrNull()
assertIs<BinaryOperator>(kStmtRhs)
val kFormatCall = kStmtRhs.rhs
assertIs<CallExpression>(kFormatCall)
assertEquals("format", kFormatCall.name.localName)
val kArguments = kFormatCall.arguments
assertEquals(2, kArguments.size)
val kConversionCall = kArguments[0]
assertIs<CallExpression>(kConversionCall)
assertEquals("str", kConversionCall.name.localName)
assertEquals("b", kConversionCall.arguments.singleOrNull()?.name?.localName)
assertIs<Literal<*>>(kArguments[1])
assertEquals("10", kArguments[1].value.value)
}

@Test
fun testSimpleImport() {
val topLevel = Path.of("src", "test", "resources", "python")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,124 @@
*/
package de.fraunhofer.aisec.cpg.frontends.python.expressionHandler

class FormattedValueHandlerTest {}
import de.fraunhofer.aisec.cpg.frontends.python.PythonLanguage
import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration
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.CallExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Literal
import de.fraunhofer.aisec.cpg.graph.variables
import de.fraunhofer.aisec.cpg.test.analyzeAndGetFirstTU
import de.fraunhofer.aisec.cpg.test.assertLiteralValue
import de.fraunhofer.aisec.cpg.test.assertLocalName
import java.nio.file.Path
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertNotNull
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class FormattedValueHandlerTest {

private lateinit var topLevel: Path
private lateinit var result: TranslationUnitDeclaration

@BeforeAll
fun setup() {
topLevel = Path.of("src", "test", "resources", "python")
analyzeFile()
}

fun analyzeFile() {
result =
analyzeAndGetFirstTU(
listOf(topLevel.resolve("formatted_values.py").toFile()),
topLevel,
true
) {
it.registerLanguage<PythonLanguage>()
}
assertNotNull(result)
}

@Test
fun testFormattedValues() {
// Test for a = f'Number: {42:.2f}'
val aAssExpression =
result.variables.find { variable -> variable.name.localName == "a" }?.astParent
assertIs<AssignExpression>(aAssExpression)
val aExprRhs = aAssExpression.rhs.singleOrNull()
assertIs<BinaryOperator>(aExprRhs)
val aFormatCall = aExprRhs.rhs
assertIs<CallExpression>(aFormatCall)
assertLocalName("format", aFormatCall)
val aArguments = aFormatCall.arguments
assertEquals(2, aArguments.size)
assertIs<Literal<*>>(aArguments[0])
assertLiteralValue(42.toLong(), aArguments[0])
assertIs<Literal<*>>(aArguments[1])
assertLiteralValue(".2f", aArguments[1])

// Test for b = f'Hexadecimal: {255:#x}'
val bAssExpression =
result.variables.find { variable -> variable.name.localName == "b" }?.astParent
assertIs<AssignExpression>(bAssExpression)
val bExprRhs = bAssExpression.rhs.singleOrNull()
assertIs<BinaryOperator>(bExprRhs)
val bFormatCall = bExprRhs.rhs
assertIs<CallExpression>(bFormatCall)
assertLocalName("format", bFormatCall)
val bArguments = bFormatCall.arguments
assertEquals(2, bArguments.size)
assertIs<Literal<*>>(bArguments[0])
assertLiteralValue(255L.toLong(), bArguments[0])
assertIs<Literal<*>>(bArguments[1])
assertLiteralValue("#x", bArguments[1])

// Test for c = f'String with conversion: {"Hello, world!"!r}'
val cAssExpression =
result.variables.find { variable -> variable.name.localName == "c" }?.astParent
assertIs<AssignExpression>(cAssExpression)
val cExprRhs = cAssExpression.rhs.singleOrNull()
assertIs<BinaryOperator>(cExprRhs)
val cConversionCall = cExprRhs.rhs
assertIs<CallExpression>(cConversionCall)
assertLocalName("repr", cConversionCall)
val cArguments = cConversionCall.arguments.singleOrNull()
assertNotNull(cArguments)
assertLocalName("c", cArguments)

// Test for d = f'ASCII representation: {"50$"!a}'
val dAssExpression =
result.variables.find { variable -> variable.name.localName == "d" }?.astParent
assertIs<AssignExpression>(dAssExpression)
val dExprRhs = dAssExpression.rhs.singleOrNull()
assertIs<BinaryOperator>(dExprRhs)
val dConversionCall = dExprRhs.rhs
assertIs<CallExpression>(dConversionCall)
assertLocalName("ascii", dConversionCall)
val dArguments = dConversionCall.arguments.singleOrNull()
assertNotNull(dArguments)
assertLocalName("d", dArguments)

// Test for e = f'Combined: {42!s:10}'
val eAssExpression =
result.variables.find { variable -> variable.name.localName == "e" }?.astParent
assertIs<AssignExpression>(eAssExpression)
val eExprRhs = eAssExpression.rhs.singleOrNull()
assertIs<BinaryOperator>(eExprRhs)
val eFormatCall = eExprRhs.rhs
assertIs<CallExpression>(eFormatCall)
assertLocalName("format", eFormatCall)
val eArguments = eFormatCall.arguments
assertEquals(2, eArguments.size)
val kConversionCall = eArguments[0]
assertIs<CallExpression>(kConversionCall)
assertLocalName("str", kConversionCall)
assertLocalName("b", kConversionCall.arguments.singleOrNull())
assertIs<Literal<*>>(eArguments[1])
assertLiteralValue("10", eArguments[1])
}
}
5 changes: 0 additions & 5 deletions cpg-language-python/src/test/resources/python/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,3 @@
e = f'Values of a: {a} and b: {b!s}'
f = a[1:3:2]

g = f'Number: {42:.2f}'
h = f'Hexadecimal: {255:#x}'
i = f'String with conversion: {c!r}'
j = f'ASCII representation: {d!a}'
k = f'Combined: {b!s:10}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a = f'Number: {42:.2f}'
b = f'Hexadecimal: {255:#x}'
c = f'String with conversion: {"Hello, world!"!r}'
d = f'ASCII representation: {"50$"!a}'
e = f'Combined: {42!s:10}'

0 comments on commit f1c2cc5

Please sign in to comment.