Skip to content

Commit 3a5eaf3

Browse files
committed
feat(go): add method context builder test
Add a new test case for the GoMethodContextBuilder class. The test verifies that the method signature is correctly parsed and the context is built with the expected values.
1 parent 1e8b59d commit 3a5eaf3

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

goland/src/main/kotlin/cc/unitmesh/go/context/GoMethodContextBuilder.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package cc.unitmesh.go.context
22

33
import cc.unitmesh.devti.context.MethodContext
44
import cc.unitmesh.devti.context.builder.MethodContextBuilder
5+
import com.goide.psi.GoFunctionDeclaration
56
import com.goide.psi.GoFunctionOrMethodDeclaration
7+
import com.goide.psi.GoMethodDeclaration
68
import com.intellij.psi.PsiElement
79

810
class GoMethodContextBuilder : MethodContextBuilder {
@@ -16,7 +18,21 @@ class GoMethodContextBuilder : MethodContextBuilder {
1618
return null
1719
}
1820

19-
val functionSignature = psiElement.signature?.text
21+
22+
var funcName = ""
23+
val functionSignature: String = when (psiElement) {
24+
is GoMethodDeclaration -> {
25+
funcName = psiElement.receiver?.text ?: ""
26+
psiElement.signature?.text ?: ""
27+
}
28+
29+
is GoFunctionDeclaration -> {
30+
funcName = psiElement.name ?: ""
31+
psiElement.signature?.text ?: ""
32+
}
33+
34+
else -> ""
35+
}
2036
val returnType = psiElement.signature?.resultType?.text
2137
val languages = psiElement.language.displayName
2238
val returnTypeText = returnType
@@ -25,7 +41,7 @@ class GoMethodContextBuilder : MethodContextBuilder {
2541
}.orEmpty()
2642

2743
return MethodContext(
28-
psiElement, psiElement.text, psiElement.name!!, functionSignature, null, languages,
44+
psiElement, psiElement.text, funcName, functionSignature, null, languages,
2945
returnTypeText, parameterNames, includeClassContext, emptyList()
3046
)
3147

goland/src/main/kotlin/cc/unitmesh/go/context/GoVariableContextBuilder.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class GoVariableContextBuilder : VariableContextBuilder {
1616
return null
1717
}
1818

19-
2019
val name = psiElement.name
2120

2221
return VariableContext(
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cc.unitmesh.go.context;
2+
3+
import com.goide.psi.GoFunctionOrMethodDeclaration
4+
import com.goide.psi.GoTypeDeclaration
5+
import com.intellij.psi.PsiElement
6+
import com.intellij.psi.util.PsiTreeUtil
7+
import com.intellij.testFramework.fixtures.BasePlatformTestCase
8+
import org.junit.Test
9+
10+
class GoMethodContextBuilderTest : BasePlatformTestCase() {
11+
12+
fun testShouldHandleMethodSignature() {
13+
// given
14+
val file = myFixture.configureByText(
15+
"test.go", """
16+
package main
17+
18+
func f3(float64, float64, float64) {}
19+
""".trimIndent()
20+
)
21+
22+
val struct = PsiTreeUtil.getChildrenOfTypeAsList(file, GoFunctionOrMethodDeclaration::class.java).first()
23+
val context = GoMethodContextBuilder().getMethodContext(struct, false, false)
24+
25+
assertEquals(context?.name, "f3")
26+
assertEquals(context?.format(), """
27+
path: /src/test.go
28+
language: Go
29+
fun name: f3
30+
fun signature: (float64, float64, float64)
31+
""".trimIndent())
32+
}
33+
}

0 commit comments

Comments
 (0)