diff --git a/semanticcpg/src/test/scala/io/shiftleft/semanticcpg/language/StepsTest.scala b/semanticcpg/src/test/scala/io/shiftleft/semanticcpg/language/StepsTest.scala index af5d052f8a27..4c86a3d8d886 100644 --- a/semanticcpg/src/test/scala/io/shiftleft/semanticcpg/language/StepsTest.scala +++ b/semanticcpg/src/test/scala/io/shiftleft/semanticcpg/language/StepsTest.scala @@ -11,6 +11,7 @@ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import overflowdb.traversal.help.Table.{AvailableWidthProvider, ConstantWidth} +import java.util.Optional import scala.jdk.CollectionConverters.IteratorHasAsScala class StepsTest extends AnyWordSpec with Matchers { @@ -355,4 +356,35 @@ class StepsTest extends AnyWordSpec with Matchers { assertDoesNotCompile("cpg.id(1).outV") // `.outV` is only available on Traversal[Edge] } + "property accessors" in { + val cpg = MockCpg().withCustom { (diffGraph, _) => + diffGraph + .addNode(NewCall()) + .addNode( + NewCall() + .typeFullName("aa") // Cardinality.One + .argumentName("bb") // Cardinality.ZeroOrOne + .dynamicTypeHintFullName(Seq("cc", "dd")) // Cardinality.List + ) + }.cpg + + val (Seq(emptyCall), Seq(callWithProperties)) = cpg.call.l.partition(_.argumentName.isEmpty) + + emptyCall.propertyOption(Properties.TYPE_FULL_NAME) shouldBe Optional.of("") + emptyCall.propertyOption(Properties.TYPE_FULL_NAME.name) shouldBe Optional.of("") + emptyCall.propertyOption(Properties.ARGUMENT_NAME) shouldBe Optional.empty + emptyCall.propertyOption(Properties.ARGUMENT_NAME.name) shouldBe Optional.empty + // these ones are rather a historic accident it'd be better and more consistent to return `None` here - + // we'll defer that change until after the flatgraph port though and just document it for now + emptyCall.propertyOption(Properties.DYNAMIC_TYPE_HINT_FULL_NAME) shouldBe Optional.of(Seq.empty) + emptyCall.propertyOption(Properties.DYNAMIC_TYPE_HINT_FULL_NAME.name) shouldBe Optional.of(Seq.empty) + + callWithProperties.propertyOption(Properties.TYPE_FULL_NAME) shouldBe Optional.of("aa") + callWithProperties.propertyOption(Properties.TYPE_FULL_NAME.name) shouldBe Optional.of("aa") + callWithProperties.propertyOption(Properties.ARGUMENT_NAME) shouldBe Optional.of("bb") + callWithProperties.propertyOption(Properties.ARGUMENT_NAME.name) shouldBe Optional.of("bb") + callWithProperties.propertyOption(Properties.DYNAMIC_TYPE_HINT_FULL_NAME) shouldBe Optional.of(Seq("cc", "dd")) + callWithProperties.propertyOption(Properties.DYNAMIC_TYPE_HINT_FULL_NAME.name) shouldBe Optional.of(Seq("cc", "dd")) + } + }