Skip to content

Commit 7113149

Browse files
committed
fix(javascript): modify JavaScriptVersionProvider and add JavaScriptVersionProviderTest
- Modify `JavaScriptVersionProvider` to remove unnecessary code and improve readability. - Add `JavaScriptVersionProviderTest` to test the functionality of `JavaScriptVersionProvider`. - Add test cases to verify the behavior of `isApplicable` and `collect` methods.
1 parent 2bb353e commit 7113149

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/provider/JavaScriptVersionProvider.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import com.intellij.openapi.project.Project
88

99
class JavaScriptVersionProvider : ChatContextProvider {
1010
override fun isApplicable(project: Project, creationContext: ChatCreationContext): Boolean {
11-
val language = creationContext.sourceFile?.language ?: return false
1211
return LanguageApplicableUtil.isWebChatCreationContextSupported(creationContext.sourceFile)
1312
}
1413

@@ -22,7 +21,6 @@ class JavaScriptVersionProvider : ChatContextProvider {
2221
return ChatContextItem(
2322
JavaScriptContextProvider::class,
2423
"Prefer $preferType language if the used language and toolset are not defined below or in the user messages"
25-
)
26-
.let { listOf(it) }
24+
).let { listOf(it) }
2725
}
2826
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cc.unitmesh.ide.javascript.provider;
2+
3+
import cc.unitmesh.devti.gui.chat.ChatActionType
4+
import cc.unitmesh.devti.provider.context.ChatCreationContext
5+
import cc.unitmesh.devti.provider.context.ChatOrigin
6+
import com.intellij.lang.javascript.JavascriptLanguage
7+
import com.intellij.lang.javascript.dialects.TypeScriptLanguageDialect
8+
import com.intellij.lang.javascript.psi.ecmal4.JSClass
9+
import com.intellij.psi.PsiFile
10+
import com.intellij.psi.PsiFileFactory
11+
import com.intellij.testFramework.LightPlatformTestCase
12+
import kotlinx.coroutines.runBlocking
13+
14+
class JavaScriptVersionProviderTest : LightPlatformTestCase() {
15+
private val code = """
16+
export class Foo {
17+
constructor() {
18+
}
19+
}
20+
""".trimIndent()
21+
fun `testShould return true when isApplicable is called with supported creationContext`() {
22+
// given
23+
val file: PsiFile =
24+
PsiFileFactory.getInstance(project).createFileFromText(JavascriptLanguage.INSTANCE, code)
25+
val jsClazz = file.children.first() as JSClass
26+
val creationContext = ChatCreationContext(
27+
ChatOrigin.ChatAction,
28+
ChatActionType.GENERATE_TEST,
29+
sourceFile = file,
30+
element = jsClazz
31+
)
32+
33+
// when
34+
val provider = JavaScriptVersionProvider()
35+
val result = provider.isApplicable(project, creationContext)
36+
37+
// then
38+
assertTrue(result)
39+
}
40+
41+
fun testShouldReturnPreferTypeScriptWhenCallWithCollect() {
42+
// given
43+
val file: PsiFile =
44+
PsiFileFactory.getInstance(project).createFileFromText(JavascriptLanguage.INSTANCE, code)
45+
46+
val jsClazz = file.children.first() as JSClass
47+
val creationContext = ChatCreationContext(
48+
ChatOrigin.ChatAction,
49+
ChatActionType.GENERATE_TEST,
50+
sourceFile = file,
51+
element = jsClazz
52+
)
53+
54+
// when
55+
val provider = JavaScriptVersionProvider()
56+
val result = runBlocking { provider.collect(project, creationContext) }
57+
58+
// then
59+
assertEquals(
60+
"Prefer JavaScript language if the used language and toolset are not defined below or in the user messages",
61+
result.first().text
62+
)
63+
}
64+
}

javascript/src/test/kotlin/cc/unitmesh/ide/javascript/util/JSPsiUtilTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cc.unitmesh.ide.javascript.util;
22

33
import com.intellij.lang.javascript.JavascriptLanguage
4+
import com.intellij.lang.javascript.dialects.TypeScriptLanguageDialect
45
import com.intellij.lang.javascript.psi.JSFunction
56
import com.intellij.lang.javascript.psi.ecmal4.JSClass
67
import com.intellij.psi.PsiFileFactory

0 commit comments

Comments
 (0)