Skip to content

Commit

Permalink
[rubysrc2cpg] Unit Tests for Identifier Nodes (#3075)
Browse files Browse the repository at this point in the history
1. `identifier` to `method parameter in` traversal
2. `identifier` to `local` traversal
3. across the file additional unit test
  • Loading branch information
pandurangpatil authored Jul 10, 2023
1 parent 1545988 commit 3ef1614
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1698,4 +1698,37 @@ class DataFlowTests extends DataFlowCodeToCpgSuite {
}
}

"Across the file data flow test" should {
val cpg = code(
"""
|def foo(arg)
| loop do
| arg += 1
| if arg > 3
| puts arg
| return
| end
| end
|end
|""".stripMargin,
"foo.rb"
)
.moreCode(
"""
|x = 1
|foo x
|""".stripMargin,
"bar.rb"
)

"be found in" in {
val source = cpg.literal.code("1").l
val sink = cpg.call.name("puts").argument(1).l
sink.reachableByFlows(source).size shouldBe 1

val src = cpg.identifier("x").l
sink.reachableByFlows(source).size shouldBe 1
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package io.joern.rubysrc2cpg.passes.ast

import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture
import io.shiftleft.semanticcpg.language._

class IdentifierLocalTests extends RubyCode2CpgFixture {

val cpg = code("""
|def method1()
| x = 1
| x = 2
|end
|
|def method2(x)
| x = 2
|end
|
|def method3(x)
| y = 0
|
| if true
| innerx = 0
| innery = 0
|
| innerx = 1
| innery = 1
| end
|
| x = 1
| y = 1
|end
|
|""".stripMargin)

// TODO: Need to be fixed.
"be correct for local x in method1" ignore {
val List(method) = cpg.method.nameExact("method1").l
method.block.ast.isIdentifier.l.size shouldBe 2
val List(indentifierX, _) = method.block.ast.isIdentifier.l
indentifierX.name shouldBe "x"

val localX = indentifierX._localViaRefOut.get
localX.name shouldBe "x"
}

// TODO: Need to be fixed
"be correct for parameter x in method2" ignore {
val List(method) = cpg.method.nameExact("method2").l
val List(indentifierX) = method.block.ast.isIdentifier.l
indentifierX.name shouldBe "x"

indentifierX.refsTo.l.size shouldBe 1
val List(paramx) = indentifierX.refsTo.l
paramx.name shouldBe "x"

val parameterX = indentifierX._methodParameterInViaRefOut.get
parameterX.name shouldBe "x"
}

// TODO: Need to be fixed.
"Reach parameter from last identifer" ignore {
val List(method) = cpg.method.nameExact("method3").l
val List(outerIdentifierX) = method.ast.isIdentifier.lineNumber(22).l
val parameterX = outerIdentifierX._methodParameterInViaRefOut.get
parameterX.name shouldBe "x"
}

// TODO: Need to be fixed.
"inner block test" ignore {
val List(method) = cpg.method.nameExact("method3").l
method.block.astChildren.isBlock.l.size shouldBe 1
val List(nestedBlock) = method.block.astChildren.isBlock.l
nestedBlock.ast.isIdentifier.nameExact("innerx").l.size shouldBe 2
}

// TODO: Need to be fixed.
"nested block identifer to local taversal" ignore {
val List(method) = cpg.method.nameExact("method3").l
method.block.astChildren.isBlock.l.size shouldBe 1
val List(nestedBlock) = method.block.astChildren.isBlock.l
nestedBlock.ast.isIdentifier.nameExact("innerx").l.size shouldBe 2
val List(nestedIdentifierX, _) = nestedBlock.ast.isIdentifier.nameExact("innerx").l

val nestedLocalX = nestedIdentifierX._localViaRefOut.get
nestedLocalX.name shouldBe "innerx"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ class TypeDeclAstCreationPassTest extends RubyCode2CpgFixture {
myClass.fullName shouldBe "Test0.rb::program:MyClass"
}

// TODO: Need to be fixed.
"generate a basic type declaration node for an empty class with Class.new" ignore {
val cpg = code("""
|MyClass = Class.new do
|end
|""".stripMargin)
val Some(myClass) = cpg.typeDecl.nameExact("MyClass").headOption: @unchecked
myClass.name shouldBe "MyClass"
myClass.fullName shouldBe "Test0.rb::program:MyClass"
}

"generate methods under type declarations" in {
val cpg = code("""
|class Vehicle
Expand Down

0 comments on commit 3ef1614

Please sign in to comment.