From f41d765891b6f04579766d3ac7776aad183aa38b Mon Sep 17 00:00:00 2001 From: Alexander Kuechler Date: Thu, 24 Oct 2024 16:07:44 +0200 Subject: [PATCH] More tests --- .../fraunhofer/aisec/cpg/graph/FluentTest.kt | 173 ++++++++++++++++-- 1 file changed, 153 insertions(+), 20 deletions(-) diff --git a/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/graph/FluentTest.kt b/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/graph/FluentTest.kt index b67bc4d9cc..d9a23a1cf1 100644 --- a/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/graph/FluentTest.kt +++ b/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/graph/FluentTest.kt @@ -25,7 +25,9 @@ */ package de.fraunhofer.aisec.cpg.graph +import de.fraunhofer.aisec.cpg.frontends.TestLanguage import de.fraunhofer.aisec.cpg.frontends.TestLanguageFrontend +import de.fraunhofer.aisec.cpg.frontends.testFrontend import de.fraunhofer.aisec.cpg.graph.builder.* import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration import de.fraunhofer.aisec.cpg.graph.scopes.BlockScope @@ -36,8 +38,10 @@ import de.fraunhofer.aisec.cpg.graph.statements.IfStatement import de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement import de.fraunhofer.aisec.cpg.graph.statements.expressions.* import de.fraunhofer.aisec.cpg.graph.statements.expressions.CollectionComprehension +import de.fraunhofer.aisec.cpg.passes.ControlDependenceGraphPass import de.fraunhofer.aisec.cpg.passes.EvaluationOrderGraphPass import de.fraunhofer.aisec.cpg.passes.ImportResolver +import de.fraunhofer.aisec.cpg.passes.ProgramDependenceGraphPass import de.fraunhofer.aisec.cpg.passes.SymbolResolver import de.fraunhofer.aisec.cpg.test.* import kotlin.test.* @@ -184,35 +188,42 @@ class FluentTest { @Test fun testCollectionComprehensions() { val result = - TestLanguageFrontend().build { - translationResult { - translationUnit("File") { - function("main", t("list")) { - param("argc", t("int")) - body { - declare { - variable("some") { - listComp { - ref("i") - compExpr { + testFrontend { + it.registerLanguage(TestLanguage(".")) + it.defaultPasses() + it.registerPass() + it.registerPass() + } + .build { + translationResult { + translationUnit("File") { + function("main", t("list")) { + param("argc", t("int")) + body { + declare { + variable("some") { + listComp { ref("i") - ref("someIterable") - ref("i") gt - literal( - 5, - t("int") - ) // TODO: This line doesn't work as expected + compExpr { + ref("i") + ref("someIterable") + ref("i") gt + literal( + 5, + t("int") + ) // TODO: This line doesn't work as + // expected + } } } } - } - returnStmt { ref("some") } + returnStmt { ref("some") } + } } } } } - } val listComp = result.variables["some"]?.initializer assertIs(listComp) @@ -231,4 +242,126 @@ class FluentTest { assertLocalName("someIterable", compExpr.iterable) // assertEquals(1, compExpr.predicates.size) } + + @Test + fun testCollectionComprehensionsWithDeclaration() { + val result = + testFrontend { + it.registerLanguage(TestLanguage(".")) + it.defaultPasses() + it.registerPass() + it.registerPass() + } + .build { + translationResult { + translationUnit("File") { + function("main", t("list")) { + param("argc", t("int")) + body { + declare { + variable("some") { + listComp { + ref("i") + compExpr { + this.variable = declare { variable("i") } + ref("someIterable") + ref("i") gt + literal( + 5, + t("int") + ) // TODO: This line doesn't work as + // expected + } + } + } + } + + returnStmt { ref("some") } + } + } + } + } + } + + val listComp = result.variables["some"]?.initializer + assertIs(listComp) + print(listComp.toString()) // This is only here to get a better test coverage + print( + listComp.comprehensionExpressions.firstOrNull()?.toString() + ) // This is only here to get a better test coverage + assertIs(listComp.statement) + assertLocalName("i", listComp.statement) + assertEquals(1, listComp.comprehensionExpressions.size) + val compExpr = listComp.comprehensionExpressions.single() + assertIs(compExpr) + val variableDecl = compExpr.variable + assertIs(variableDecl) + assertLocalName("i", variableDecl.singleDeclaration) + assertIs(compExpr.iterable) + assertLocalName("someIterable", compExpr.iterable) + // assertEquals(1, compExpr.predicates.size) + } + + @Test + fun testCollectionComprehensionsWithTwoDeclarations() { + val result = + testFrontend { + it.registerLanguage(TestLanguage(".")) + it.defaultPasses() + it.registerPass() + it.registerPass() + } + .build { + translationResult { + translationUnit("File") { + function("main", t("list")) { + param("argc", t("int")) + body { + declare { + variable("some") { + listComp { + ref("i") + compExpr { + this.variable = declare { + variable("i") + variable("y") + } + ref("someIterable") + ref("i") gt + literal( + 5, + t("int") + ) // TODO: This line doesn't work as + // expected + } + } + } + } + + returnStmt { ref("some") } + } + } + } + } + } + + val listComp = result.variables["some"]?.initializer + assertIs(listComp) + print(listComp.toString()) // This is only here to get a better test coverage + print( + listComp.comprehensionExpressions.firstOrNull()?.toString() + ) // This is only here to get a better test coverage + assertIs(listComp.statement) + assertLocalName("i", listComp.statement) + assertEquals(1, listComp.comprehensionExpressions.size) + val compExpr = listComp.comprehensionExpressions.single() + assertIs(compExpr) + val variableDecl = compExpr.variable + assertIs(variableDecl) + assertLocalName("i", variableDecl.declarations[0]) + assertLocalName("y", variableDecl.declarations[1]) + assertIs(compExpr.iterable) + assertLocalName("someIterable", compExpr.iterable) + // assertEquals(1, compExpr.predicates.size) + } }