Skip to content

Commit

Permalink
[GoSrc2cpg] - ValueSpec and FuncDecl reference-id fix (#3679)
Browse files Browse the repository at this point in the history
Handled the ValueSpec and FuncDecl reference-id issue fixed
Ticket: #3652
  • Loading branch information
ankit-privado authored Sep 20, 2023
1 parent 4ef5cdc commit 1b9d738
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ trait CacheBuilder { this: AstCreator =>
)
) {
processFuncDecl(obj)
createParserNodeInfo(obj)
} else if (
json.obj
.contains(ParserKeys.NodeType) && obj(ParserKeys.NodeType).str == "ast.ValueSpec" && !json.obj.contains(
ParserKeys.NodeReferenceId
)
) {
createParserNodeInfo(obj)
}
obj.value.values.foreach(subJson => findAndProcess(subJson))
case arr: Arr =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,87 @@ class MethodTests extends GoCodeToCpgSuite {
}

}

"when constant is used in initializing struct" should {
val cpg = code("""
|package main
|
|var person = Person()
|
|type Name struct {
| name string
|}
|
|const (
| personName string = "peter"
|)
|
|func Person() Name {
| return Name{
| name: personName,
| }
|}
|""".stripMargin)

"test basic ast structure for Person" in {
val List(method) = cpg.method.name("Person").l
method.signature shouldBe "main.Person()main.Name"

val List(typeDeclNode) = cpg.typeDecl.name("Name").l
typeDeclNode.fullName shouldBe "main.Name"
typeDeclNode.member.size shouldBe 1
val List(name) = typeDeclNode.member.l
name.code shouldBe "name"
name.typeFullName shouldBe "string"
}

"test basic ast structure for identifiers" in {

val List(person, personName, _) = cpg.identifier.l
person.name shouldBe "person"
personName.name shouldBe "personName"
personName.typeFullName shouldBe "string"
}
}

"when function is passed as argument" should {
val cpg = code("""
|package main
|
|func help() {
| Person(Hello, "hi")
|}
|
|func Hello(value string) {}
|
|func Person(hello func(string), value string) {
| hello(value)
|}
|""".stripMargin)

// TODO: Handle function as argument

"test basic ast structure for help" in {
val List(methodNode) = cpg.method.name("help").l
methodNode.signature shouldBe "main.help()"

val List(personCall) = cpg.call.name("Person").l
personCall.signature shouldBe "main.Person(ANY, string)"

val List(arg1, arg2) = personCall.argument.l
arg1.code shouldBe "Hello"
arg2.code shouldBe "\"hi\""
}

"test basic ast structure for Person" in {
val List(methodNode) = cpg.method.name("Person").l
methodNode.signature shouldBe "main.Person(ANY, string)"

val List(callNode) = methodNode.call.l
callNode.code shouldBe "hello(value)"
callNode.signature shouldBe "main.hello()"
}
}
// TODO: add unit test for "sem chan int"
// resultErrChan := make(chan error)
// sem := make(chan int, concurrency)
Expand Down

0 comments on commit 1b9d738

Please sign in to comment.