Skip to content

Commit 401f2e2

Browse files
FantoomForNeVeR
authored andcommitted
(#291) Tests: try to migrate codeinsight tests to JUnit 5
1 parent 773f898 commit 401f2e2

File tree

5 files changed

+124
-41
lines changed

5 files changed

+124
-41
lines changed

src/main/kotlin/com/intellij/plugin/powershell/lang/lsp/languagehost/LanguageServerEndpoint.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,13 @@ class LanguageServerEndpoint(
395395
delay(50)
396396
}
397397
}
398+
399+
@TestOnly
400+
suspend fun waitForEditorManagerCreated(filePath: Path) {
401+
while (!connectedEditors.containsKey(filePath.toUri()) || connectedEditors[filePath.toUri()]?.job?.isCompleted == false) {
402+
delay(50)
403+
}
404+
}
398405
}
399406

400407
private val logger = logger<LanguageServerEndpoint>()

src/test/kotlin/com/intellij/plugin/powershell/lang/PowerShellCodeInsightTest.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ package com.intellij.plugin.powershell.lang
22

33
import com.intellij.openapi.command.WriteCommandAction
44
import com.intellij.plugin.powershell.psi.PowerShellStringLiteralExpression
5+
import com.intellij.plugin.powershell.testFramework.PowerShellCodeInsightTestBase
56
import com.intellij.psi.util.PsiTreeUtil
6-
import com.intellij.testFramework.fixtures.BasePlatformTestCase
7+
import com.intellij.testFramework.junit5.TestApplication
8+
import org.junit.jupiter.api.Test
79
import java.io.File
810

9-
class PowerShellCodeInsightTest : BasePlatformTestCase() {
11+
@TestApplication
12+
class PowerShellCodeInsightTest : PowerShellCodeInsightTestBase() {
1013

1114
private val TEST_DATA_PATH = "src" + File.separatorChar + "test" + File.separatorChar + "resources" + File.separatorChar +
1215
"testData"
1316

1417
override fun getTestDataPath() = TEST_DATA_PATH
1518

19+
@Test
1620
fun testDecodeExpandableString() {
1721
//quotes
1822
checkStringDecode("This is sample host \"\"double quoted\"\" string",
@@ -44,6 +48,7 @@ class PowerShellCodeInsightTest : BasePlatformTestCase() {
4448
"$(dsdkkdkd) $(ddd) \" $(sss)sasa $(wqwq) \$eee $(")
4549
}
4650

51+
@Test
4752
fun testDecodeExpandableHereString() {
4853
//quotes
4954
decodeExpandableHereString("This is sample host \"string\" content",
@@ -83,6 +88,7 @@ class PowerShellCodeInsightTest : BasePlatformTestCase() {
8388
"text1 `` \$foo `` \"`'text2")
8489
}
8590

91+
@Test
8692
fun testDecodeVerbatimString() {
8793
//quotes
8894
decodeVerbatimString("This is sample host \"string\" content",
@@ -105,14 +111,15 @@ class PowerShellCodeInsightTest : BasePlatformTestCase() {
105111
private fun decodeVerbatimHereString(stringContent: String, expectedOutText: String) = checkStringDecode(stringContent, expectedOutText, false, true)
106112
private fun decodeExpandableHereString(stringContent: String, expectedOutText: String) = checkStringDecode(stringContent, expectedOutText, true, true)
107113

114+
@Test
108115
fun testDecodeVerbatimHereString() {
109116
decodeVerbatimHereString("This is sample host \"string\" content",
110117
"This is sample host \"string\" content")
111118
decodeVerbatimHereString("This is sample host 'string' content",
112119
"This is sample host 'string' content")
113120
}
114121

115-
122+
@Test
116123
fun testContentChangeExpandableString() {
117124
//quotes
118125
checkInjectedText("var foo = \"text\";",
@@ -147,7 +154,7 @@ class PowerShellCodeInsightTest : BasePlatformTestCase() {
147154
"`$(ddd) `\" `$(sss)sasa `$(wqwq) `\$eee `$(")
148155
}
149156

150-
157+
@Test
151158
fun testContentChangeExpandableHereString() {
152159
//quotes
153160
updateExpandableHereString("var foo = \"text\";",
@@ -163,6 +170,7 @@ class PowerShellCodeInsightTest : BasePlatformTestCase() {
163170
"text1 `\$newVar text2")
164171
}
165172

173+
@Test
166174
fun testContentChangeVerbatimString() {
167175
//quotes
168176
updateVerbatimString("var foo = \"text\";",
@@ -183,6 +191,7 @@ class PowerShellCodeInsightTest : BasePlatformTestCase() {
183191
"test `\$foo escape")
184192
}
185193

194+
@Test
186195
fun testContentChangeVerbatimHereString() {
187196
//quotes
188197
updateVerbatimHereString("var foo = \"text\";",
@@ -203,8 +212,9 @@ class PowerShellCodeInsightTest : BasePlatformTestCase() {
203212
"test escape \$foo chars")
204213
}
205214

215+
@Test
206216
fun testFolding() {
207-
myFixture.testFolding("$testDataPath/codeinsight/folding.ps1")
217+
codeInsightTestFixture.testFolding("${getTestDataPath()}/codeinsight/folding.ps1")
208218
}
209219

210220
private fun updateExpandableHereString(newInjectedText: String, expectedStringContent: String) = checkInjectedText(newInjectedText, expectedStringContent, true, true)
@@ -214,9 +224,9 @@ class PowerShellCodeInsightTest : BasePlatformTestCase() {
214224
//external editor -> PowerShell string
215225
private fun checkInjectedText(newInjectedText: String, expectedStringContent: String, isExpandable: Boolean = true, isHere: Boolean = false, defaultText: String = "var foo = 1") {
216226
val string = createStringExpression(isHere, isExpandable, defaultText)
217-
WriteCommandAction.writeCommandAction(project, myFixture.file).withName("Test checkInjectedText. New Text='$newInjectedText'").run<Throwable> {
227+
WriteCommandAction.writeCommandAction(project, codeInsightTestFixture.file).withName("Test checkInjectedText. New Text='$newInjectedText'").run<Throwable> {
218228
string.updateText(newInjectedText)
219-
val newString = PsiTreeUtil.findChildOfType(myFixture.file, PowerShellStringLiteralExpression::class.java) ?: error("file text='${myFixture.file}'")
229+
val newString = PsiTreeUtil.findChildOfType(codeInsightTestFixture.file, PowerShellStringLiteralExpression::class.java) ?: error("file text='${codeInsightTestFixture.file}'")
220230
assert(newString.getStringContent() == expectedStringContent) { "expected=\t'$expectedStringContent'\nactual=\t\t'${newString.getStringContent()}'" }
221231
}
222232
}
@@ -245,7 +255,7 @@ class PowerShellCodeInsightTest : BasePlatformTestCase() {
245255
}
246256

247257
private fun createStringExpression(nodeText: String): PowerShellStringLiteralExpression {
248-
val file = myFixture.configureByText("foo.ps1", nodeText)
258+
val file = codeInsightTestFixture.configureByText("foo.ps1", nodeText)
249259
return PsiTreeUtil.findChildOfType(file, PowerShellStringLiteralExpression::class.java) ?: error("text='$nodeText'")
250260
}
251261

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
package com.intellij.plugin.powershell.lang
22

33
import com.intellij.openapi.actionSystem.IdeActions
4-
import com.intellij.testFramework.fixtures.BasePlatformTestCase
4+
import com.intellij.plugin.powershell.testFramework.PowerShellCodeInsightTestBase
5+
import com.intellij.testFramework.junit5.TestApplication
6+
import org.junit.jupiter.api.Test
57

6-
class PowerShellCommenterTest : BasePlatformTestCase() {
8+
@TestApplication
9+
class PowerShellCommenterTest : PowerShellCodeInsightTestBase() {
710

11+
@Test
812
fun testCommentExtension() {
9-
myFixture.configureByText("file.ps1", """
13+
codeInsightTestFixture.configureByText(
14+
"file.ps1", """
1015
<#<caret>
1116
#>
12-
""".trimIndent())
13-
myFixture.performEditorAction(IdeActions.ACTION_EDITOR_ENTER)
14-
myFixture.checkResult("""
17+
""".trimIndent()
18+
)
19+
waitForEditorManagerCreated(codeInsightTestFixture.file.virtualFile.toNioPath())
20+
codeInsightTestFixture.performEditorAction(IdeActions.ACTION_EDITOR_ENTER)
21+
waitForEditorManagerCreated(codeInsightTestFixture.file.virtualFile.toNioPath())
22+
codeInsightTestFixture.checkResult(
23+
"""
1524
<#
1625
<caret>
1726
#>
18-
""".trimIndent())
27+
""".trimIndent()
28+
)
29+
waitForEditorManagerCreated(codeInsightTestFixture.file.virtualFile.toNioPath())
1930
}
2031
}
Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,24 @@
11
package com.intellij.plugin.powershell.lang
22

33
import com.intellij.codeInsight.completion.CompletionType
4-
import com.intellij.plugin.powershell.lang.lsp.LSPInitMain
5-
import com.intellij.testFramework.fixtures.BasePlatformTestCase
6-
import com.intellij.testFramework.fixtures.TempDirTestFixture
7-
import com.intellij.testFramework.fixtures.impl.TempDirTestFixtureImpl
8-
import junit.framework.TestCase
9-
import kotlinx.coroutines.runBlocking
10-
import kotlinx.coroutines.withTimeout
11-
import kotlin.time.Duration.Companion.seconds
4+
import com.intellij.plugin.powershell.testFramework.PowerShellCodeInsightTestBase
5+
import com.intellij.testFramework.junit5.TestApplication
6+
import org.junit.jupiter.api.Assertions
7+
import org.junit.jupiter.api.Test
128

13-
class PowerShellCompletionTests : BasePlatformTestCase() {
9+
@TestApplication
10+
class PowerShellCompletionTests : PowerShellCodeInsightTestBase() {
1411

1512
override fun getTestDataPath() = "src/test/resources/testData"
1613

17-
override fun createTempDirTestFixture(): TempDirTestFixture {
18-
return TempDirTestFixtureImpl()
19-
}
20-
14+
@Test
2115
fun testCompletion() {
22-
val psiFile = myFixture.configureByFile("codeinsight/completion.ps1")
23-
runBlocking {
24-
withTimeout(20.seconds) {
25-
LSPInitMain.getEditorLanguageServer(project).apply {
26-
waitForInit()
27-
waitForEditorConnect(psiFile.virtualFile.toNioPath())
28-
}
29-
}
30-
}
16+
val psiFile = codeInsightTestFixture.configureByFile("codeinsight/completion.ps1")
3117

32-
myFixture.complete(CompletionType.BASIC)
33-
val lookupElementStrings = myFixture.lookupElementStrings
34-
TestCase.assertNotNull(lookupElementStrings)
35-
assertContainsElements(lookupElementStrings!!, "Get-Alias")
18+
waitForEditorConnects(psiFile.virtualFile.toNioPath())
19+
codeInsightTestFixture.complete(CompletionType.BASIC)
20+
val lookupElementStrings = codeInsightTestFixture.lookupElementStrings
21+
Assertions.assertNotNull(lookupElementStrings)
22+
Assertions.assertTrue { lookupElementStrings!!.contains("Get-Alias") }
3623
}
3724
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.intellij.plugin.powershell.testFramework
2+
3+
import com.intellij.plugin.powershell.lang.lsp.LSPInitMain
4+
import com.intellij.testFramework.fixtures.CodeInsightTestFixture
5+
import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory
6+
import com.intellij.testFramework.fixtures.impl.TempDirTestFixtureImpl
7+
import com.intellij.testFramework.junit5.fixture.tempPathFixture
8+
import kotlinx.coroutines.runBlocking
9+
import kotlinx.coroutines.withTimeout
10+
import org.junit.jupiter.api.AfterEach
11+
import org.junit.jupiter.api.BeforeEach
12+
import org.junit.jupiter.api.TestInfo
13+
import java.nio.file.Path
14+
import kotlin.time.Duration.Companion.seconds
15+
16+
open class PowerShellCodeInsightTestBase {
17+
val tempPathFixture = tempPathFixture()
18+
lateinit var tempPath: Path
19+
lateinit var codeInsightTestFixture: CodeInsightTestFixture
20+
21+
val project get() = codeInsightTestFixture.project
22+
23+
@BeforeEach
24+
fun setupFixture(testInfo: TestInfo){
25+
tempPath = tempPathFixture.get()
26+
val factory = IdeaTestFixtureFactory.getFixtureFactory()
27+
val fixtureBuilder = factory.createLightFixtureBuilder(null, testInfo.displayName)
28+
val fixture = fixtureBuilder.getFixture()
29+
codeInsightTestFixture = IdeaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(fixture,
30+
TempDirTestFixtureImpl())
31+
codeInsightTestFixture.testDataPath = getTestDataPath()
32+
codeInsightTestFixture.setUp()
33+
}
34+
35+
@AfterEach
36+
fun tearDownEdt() {
37+
runInEdt {
38+
LSPInitMain.getInstance().dispose() // TODO: Remove this after LSPInitMain is refactored
39+
codeInsightTestFixture.tearDown()
40+
tearDownInEdt()
41+
}
42+
}
43+
44+
protected open fun tearDownInEdt() {}
45+
46+
open fun getTestDataPath(): String = tempPath.toString()
47+
48+
fun waitForEditorConnects(path: Path) {
49+
runBlocking {
50+
withTimeout(20.seconds) {
51+
LSPInitMain.getEditorLanguageServer(project).apply {
52+
waitForInit()
53+
waitForEditorConnect(path)
54+
}
55+
}
56+
}
57+
}
58+
59+
fun waitForEditorManagerCreated(path: Path) {
60+
runBlocking {
61+
withTimeout(20.seconds) {
62+
LSPInitMain.getEditorLanguageServer(project).apply {
63+
waitForEditorManagerCreated(path)
64+
}
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)