Skip to content

Commit

Permalink
Scalafmt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
badly-drawn-wizards committed Jan 11, 2024
1 parent 58efe62 commit 6291c99
Showing 1 changed file with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("<tmp-0>")
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("<operator>.logicalOr")).argument)(_.emit(_.whereNot(_.isCall.name("<operator>.logicalOr")))).l
orConds.map {
val orConds = List(cond)
.repeat(_.isCall.where(_.name("<operator>.logicalOr")).argument)(
_.emit(_.whereNot(_.isCall.name("<operator>.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 "<tmp-0>"
Expand All @@ -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("<operator>.logicalOr")).argument)(_.emit(_.whereNot(_.isCall.name("<operator>.logicalOr")))).l
val orConds = List(cond)
.repeat(_.isCall.where(_.name("<operator>.logicalOr")).argument)(
_.emit(_.whereNot(_.isCall.name("<operator>.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")
Expand Down

0 comments on commit 6291c99

Please sign in to comment.