diff --git a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/querying/CaseTests.scala b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/querying/CaseTests.scala index 1e14453202c5..825f78887856 100644 --- a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/querying/CaseTests.scala +++ b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/querying/CaseTests.scala @@ -5,7 +5,7 @@ import io.shiftleft.semanticcpg.language.* import io.shiftleft.codepropertygraph.generated.nodes.* class CaseTests extends RubyCode2CpgFixture { - "`case x ... end` should be represented with if-else chain" in { + "`case x ... end` should be represented with if-else chain and multiple match expressions should be or-ed together" in { val cpg = code(""" |case 0 | when 0 @@ -17,22 +17,26 @@ class CaseTests extends RubyCode2CpgFixture { |end |""".stripMargin) - val block@List(_) = cpg.method(":program").block.astChildren.isBlock.l + val block @ List(_) = cpg.method(":program").block.astChildren.isBlock.l - val List(assign) = block.astChildren.assignment.l; + val List(assign) = block.astChildren.assignment.l; val List(lhs, rhs) = assign.argument.l List(lhs).isCall.name.l shouldBe List("") List(rhs).isLiteral.code.l shouldBe List("0") - val headIf@List(_) = block.astChildren.isControlStructure.l - val ifStmts@List(_, _, _, _) = headIf.repeat(_.astChildren.order(3).astChildren.isControlStructure)(_.emit).l; + val headIf @ List(_) = block.astChildren.isControlStructure.l + val ifStmts @ List(_, _, _, _) = headIf.repeat(_.astChildren.order(3).astChildren.isControlStructure)(_.emit).l; val conds: List[List[String]] = ifStmts.condition.map { cond => - val orConds = List(cond).repeat(_.isCall.where(_.name(".logicalOr")).argument)(_.emit(_.whereNot(_.isCall.name(".logicalOr")))).l - orConds.map { + val orConds = List(cond) + .repeat(_.isCall.where(_.name(".logicalOr")).argument)( + _.emit(_.whereNot(_.isCall.name(".logicalOr"))) + ) + .l + orConds.map { case u: Unknown => "unknown" case mExpr => - val call@List(_) = List(mExpr).isCall.l + val call @ List(_) = List(mExpr).isCall.l call.methodFullName.l shouldBe List("===") val List(lhs, rhs) = call.argument.l rhs.code shouldBe "" @@ -55,18 +59,22 @@ class CaseTests extends RubyCode2CpgFixture { |end |""".stripMargin) - val block@List(_) = cpg.method(":program").block.astChildren.isBlock.l + val block @ List(_) = cpg.method(":program").block.astChildren.isBlock.l - val headIf@List(_) = block.astChildren.isControlStructure.l - val ifStmts@List(_, _, _, _) = headIf.repeat(_.astChildren.order(3).astChildren.isControlStructure)(_.emit).l; + val headIf @ List(_) = block.astChildren.isControlStructure.l + val ifStmts @ List(_, _, _, _) = headIf.repeat(_.astChildren.order(3).astChildren.isControlStructure)(_.emit).l; val conds: List[List[String]] = ifStmts.condition.map { cond => - val orConds = List(cond).repeat(_.isCall.where(_.name(".logicalOr")).argument)(_.emit(_.whereNot(_.isCall.name(".logicalOr")))).l + val orConds = List(cond) + .repeat(_.isCall.where(_.name(".logicalOr")).argument)( + _.emit(_.whereNot(_.isCall.name(".logicalOr"))) + ) + .l orConds.map { case u: Unknown => "unknown" - case c => c.code + case c => c.code } }.l - conds shouldBe List(List("false", "true"), List("true"), List("false", "unknown"), List("unknown")) + conds shouldBe List(List("false", "true"), List("true"), List("false", "unknown"), List("unknown")) val matchResults = ifStmts.astChildren.order(2).astChildren.l matchResults.code.l shouldBe List("0", "1", "2", "3")