From 586467afefb2930df0bff1418966eb1699727588 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 12 Nov 2024 09:39:57 +0800 Subject: [PATCH] test(goast): add test case for resolving struct field types This commit adds a new test case to the GoFullIdentListenerTest file. The test checks the correct resolution of struct field types and function calls within a given Go code snippet. It ensures that the analysis correctly identifies the function name, number of parameters, and the node name of the function call. --- .../ast/goast/GoFullIdentListenerTest.kt | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoFullIdentListenerTest.kt b/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoFullIdentListenerTest.kt index 540bcc6d..5168d3fc 100644 --- a/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoFullIdentListenerTest.kt +++ b/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoFullIdentListenerTest.kt @@ -501,4 +501,43 @@ func (d *Dao) UpdateCapsule() (affect int64, err error) { println(firstParameter) assertEquals(firstParameter.TypeValue, "string") } + + @Test + fun shouldResolveStructFieldType() { + @Language("Go") + val code = """ +package user + +import ( + "context" + rcv1 "go-common/app/service/live/rc/api/liverpc/v1" +) + +type Dao struct { + walletUrl string + rcClient rcv1.AchvRPCClient +} + +func (d *Dao) GetLiveAchieve(ctx context.Context, uid int64) (achieve int64, err error) { + resp, err := d.rcClient.Userstatus(ctx, &rcv1.AchvUserstatusReq{}) + if err != nil || resp == nil || resp.Data == nil { + log.Error("[dao.user|GetLiveAchieve] get rc achieve error(%v), uid(%d), resp(%v)", err, uid, resp) + return + } + achieve = resp.Data.Point + return +} +""" + + val codeFile = GoAnalyser().analysis(code, "") + val functionCalls = codeFile.DataStructures[0].Functions[0].FunctionCalls + + assertEquals(functionCalls.size, 2) + + val getExecFunc = functionCalls[0] + assertEquals(getExecFunc.FunctionName, "Userstatus") + assertEquals(getExecFunc.Parameters.size, 2) + + assertEquals(getExecFunc.NodeName, "Dao.rcClient") + } }