Skip to content

Commit 489ef0e

Browse files
committed
Tests: migrate PowerShellFormatterTest to JUnit 5
This should help us with update to IntelliJ 2025.2, as it has some problems running both JUnit5-based tests and old LightPlatformTestCase-based tests in the same bunch.
1 parent 37d0597 commit 489ef0e

File tree

1 file changed

+104
-62
lines changed

1 file changed

+104
-62
lines changed

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

Lines changed: 104 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,53 @@ import com.intellij.openapi.application.ApplicationManager
55
import com.intellij.openapi.command.CommandProcessor
66
import com.intellij.openapi.util.TextRange
77
import com.intellij.plugin.powershell.ide.editor.formatting.PowerShellCodeStyleSettings
8+
import com.intellij.plugin.powershell.testFramework.PowerShellCodeInsightTestBase
9+
import com.intellij.plugin.powershell.testFramework.RunInEdt
810
import com.intellij.psi.PsiDocumentManager
911
import com.intellij.psi.PsiFileFactory
1012
import com.intellij.psi.codeStyle.CodeStyleManager
1113
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
12-
import com.intellij.psi.formatter.FormatterTestCase
14+
import com.intellij.testFramework.junit5.TestApplication
1315
import com.intellij.util.IncorrectOperationException
14-
import junit.framework.TestCase
16+
import com.intellij.util.application
17+
import org.junit.jupiter.api.Assertions.assertEquals
18+
import org.junit.jupiter.api.Assertions.assertTrue
19+
import org.junit.jupiter.api.Test
1520

16-
class PowerShellFormatterTest : FormatterTestCase() {
17-
18-
private val myTestDataDir = "src/test/resources/testData/"
19-
20-
override fun getBasePath(): String = "format"
21-
override fun getFileExtension(): String = "ps1"
22-
override fun getTestDataPath(): String = myTestDataDir
21+
@TestApplication
22+
@RunInEdt
23+
class PowerShellFormatterTest : PowerShellCodeInsightTestBase() {
2324

25+
@Test
2426
fun testBlockIndent() {
2527
doTest("indents", "indents_res")
2628
}
2729

30+
@Test
2831
fun testBracesShiftedIndent() {
2932
val tempTestSettings = getCommonSettings()
3033
tempTestSettings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE_SHIFTED2
3134
tempTestSettings.METHOD_BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE_SHIFTED2
3235
doTest("indent_brace", "indent_brace_res")
3336
}
3437

38+
@Test
3539
fun testDependentBracePlacement() {
3640
val settings = getCommonSettings()
3741
settings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE_IF_WRAPPED
3842
settings.METHOD_BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE_IF_WRAPPED
3943
doTest("braces1", "braces1_if_wrapped_res")
4044
}
4145

46+
@Test
4247
fun testPipelineWrap() {
4348
val psSettings = getPowerShellSettings()
4449
psSettings.PIPELINE_TAIL_WRAP = CommonCodeStyleSettings.WRAP_ALWAYS
4550
psSettings.ALIGN_MULTILINE_PIPELINE_STATEMENT = true
4651
doTest("indent_brace", "indent_brace_pipeline_wrap_res")
4752
}
4853

54+
@Test
4955
fun testBlockParametersWrap() {
5056
doTest("wrap_block_parameters", "wrap_block_parameters_default_res")
5157
val psSettings = getPowerShellSettings()
@@ -54,16 +60,19 @@ class PowerShellFormatterTest : FormatterTestCase() {
5460
doTest("wrap_block_parameters", "wrap_block_parameters_res")
5561
}
5662

63+
@Test
5764
fun testBlockParameterIndent() {
5865
doTest("block_parameter_indent", "block_parameter_indent_res")
5966
}
6067

68+
@Test
6169
fun testDefault1() {
6270
val tempTestSettings = getCommonSettings()
6371
tempTestSettings.KEEP_LINE_BREAKS = false
6472
doTest("default1", "default1_res")
6573
}
6674

75+
@Test
6776
fun testChainedCallWrap() {
6877
val settings = getCommonSettings()
6978
settings.RIGHT_MARGIN = 60
@@ -74,12 +83,14 @@ class PowerShellFormatterTest : FormatterTestCase() {
7483
doTest("wrap_chained_call", "wrap_chained_call_if_long_first_wrap_res")
7584
}
7685

86+
@Test
7787
fun testNoSpacingForCompoundIdentifiers() {
7888
val settings = getCommonSettings()
7989
settings.SPACE_AROUND_MULTIPLICATIVE_OPERATORS = true
8090
doTest("compound_identifiers", "compound_identifiers_res")
8191
}
8292

93+
@Test
8394
fun testChainedCallWrapAlignment() {
8495
val settings = getCommonSettings()
8596
settings.RIGHT_MARGIN = 60
@@ -92,6 +103,7 @@ class PowerShellFormatterTest : FormatterTestCase() {
92103
doTest("wrap_chained_call", "wrap_chained_call_always_res")
93104
}
94105

106+
@Test
95107
fun testWrapBinaryExpr() {
96108
val settings = getCommonSettings()
97109
settings.RIGHT_MARGIN = 30
@@ -111,60 +123,61 @@ class PowerShellFormatterTest : FormatterTestCase() {
111123
}
112124

113125

126+
@Test
114127
fun testBraces() {
115128
val before = "switch -regex -casesensitive (get-childitem | sort length) {\n" +
116-
" \" ^5\" {\n" +
117-
" \"length for \$_ started with 5\"; continue\n" +
118-
" }\n" +
119-
" { \$_.length >20000 } {\n" +
120-
" \"length of \$_ is greater than 20000\"\n" +
121-
" }\n" +
122-
" default {\n" +
123-
" \"Didn't match anything else...\"\n" +
124-
" }\n" +
125-
"}\n" +
126-
"\n" +
127-
"function TrapTest {\n" +
128-
" trap {\n" +
129-
" \"Error found.\"\n" +
130-
" }\n" +
131-
" nonsenseString\n" +
132-
"}\n" +
133-
"\n" +
134-
"class A {\n" +
135-
" doSomethingMethod() {\n" +
136-
"\n" +
137-
" }\n" +
138-
"}"
129+
" \" ^5\" {\n" +
130+
" \"length for \$_ started with 5\"; continue\n" +
131+
" }\n" +
132+
" { \$_.length >20000 } {\n" +
133+
" \"length of \$_ is greater than 20000\"\n" +
134+
" }\n" +
135+
" default {\n" +
136+
" \"Didn't match anything else...\"\n" +
137+
" }\n" +
138+
"}\n" +
139+
"\n" +
140+
"function TrapTest {\n" +
141+
" trap {\n" +
142+
" \"Error found.\"\n" +
143+
" }\n" +
144+
" nonsenseString\n" +
145+
"}\n" +
146+
"\n" +
147+
"class A {\n" +
148+
" doSomethingMethod() {\n" +
149+
"\n" +
150+
" }\n" +
151+
"}"
139152
val after = "switch -regex -casesensitive (get-childitem | sort length)\n" +
140-
"{\n" +
141-
" \" ^5\" {\n" +
142-
" \"length for \$_ started with 5\"; continue\n" +
143-
" }\n" +
144-
" { \$_.length >20000 } {\n" +
145-
" \"length of \$_ is greater than 20000\"\n" +
146-
" }\n" +
147-
" default {\n" +
148-
" \"Didn't match anything else...\"\n" +
149-
" }\n" +
150-
"}\n" +
151-
"\n" +
152-
"function TrapTest\n" +
153-
"{\n" +
154-
" trap\n" +
155-
" {\n" +
156-
" \"Error found.\"\n" +
157-
" }\n" +
158-
" nonsenseString\n" +
159-
"}\n" +
160-
"\n" +
161-
"class A\n" +
162-
"{\n" +
163-
" doSomethingMethod()\n" +
164-
" {\n" +
165-
"\n" +
166-
" }\n" +
167-
"}"
153+
"{\n" +
154+
" \" ^5\" {\n" +
155+
" \"length for \$_ started with 5\"; continue\n" +
156+
" }\n" +
157+
" { \$_.length >20000 } {\n" +
158+
" \"length of \$_ is greater than 20000\"\n" +
159+
" }\n" +
160+
" default {\n" +
161+
" \"Didn't match anything else...\"\n" +
162+
" }\n" +
163+
"}\n" +
164+
"\n" +
165+
"function TrapTest\n" +
166+
"{\n" +
167+
" trap\n" +
168+
" {\n" +
169+
" \"Error found.\"\n" +
170+
" }\n" +
171+
" nonsenseString\n" +
172+
"}\n" +
173+
"\n" +
174+
"class A\n" +
175+
"{\n" +
176+
" doSomethingMethod()\n" +
177+
" {\n" +
178+
"\n" +
179+
" }\n" +
180+
"}"
168181

169182
doTextTest(before, after)
170183

@@ -176,6 +189,7 @@ class PowerShellFormatterTest : FormatterTestCase() {
176189
doTextTest(after, before, false)
177190
}
178191

192+
@Test
179193
fun testWhitespaceDependentOperatorSpacing() {
180194
doTextTest(
181195
"""
@@ -192,6 +206,7 @@ class PowerShellFormatterTest : FormatterTestCase() {
192206
)
193207
}
194208

209+
@Test
195210
fun testCommandLineArgumentSpacing() {
196211
val sample = """
197212
git log --pretty=format:'%Cred'
@@ -200,6 +215,7 @@ class PowerShellFormatterTest : FormatterTestCase() {
200215
doTextTest(sample, sample)
201216
}
202217

218+
@Test
203219
fun testNullCoalescingOperator() = doTextTest("\$foo = \$bar ??\$baz", "\$foo = \$bar ?? \$baz")
204220

205221
private fun doTextTest(text: String, textAfter: String, reformatAsText: Boolean = true, textRange: TextRange? = null) {
@@ -219,7 +235,7 @@ class PowerShellFormatterTest : FormatterTestCase() {
219235
CodeStyleManager.getInstance(project).reformat(file)
220236
}
221237
} catch (e: IncorrectOperationException) {
222-
TestCase.assertTrue(e.localizedMessage, false)
238+
assertTrue(false, e.localizedMessage)
223239
}
224240
}
225241
}, "", "")
@@ -231,7 +247,33 @@ class PowerShellFormatterTest : FormatterTestCase() {
231247

232248
private fun createFileFromText(text: String) = PsiFileFactory.getInstance(project).createFileFromText(PowerShellLanguage.INSTANCE, text)
233249

234-
private fun getPowerShellSettings(): PowerShellCodeStyleSettings = settings.getCustomSettings(PowerShellCodeStyleSettings::class.java)
250+
private fun getPowerShellSettings(): PowerShellCodeStyleSettings = CodeStyle.getSettings(project).getCustomSettings(PowerShellCodeStyleSettings::class.java)
235251

236252
private fun getCommonSettings() = CodeStyle.getLanguageSettings(createFileFromText(""), PowerShellLanguage.INSTANCE)
253+
254+
private val testDataPackage = "testData/format"
255+
private val fileExtension = "ps1"
256+
257+
private fun doTest(originalFileNameBase: String, expectedFileNameBase: String) {
258+
val originalFileName = "$originalFileNameBase.$fileExtension"
259+
val resultingFileName = "$expectedFileNameBase.$fileExtension"
260+
261+
val classLoader = this::class.java.classLoader
262+
val originalFile = classLoader.getResource("$testDataPackage/$originalFileName")!!
263+
val resultingFile = classLoader.getResource("$testDataPackage/$resultingFileName")!!
264+
265+
val originalFileText = originalFile.readText().replace("\r\n", "\n")
266+
val resultingFileText = resultingFile.readText().replace("\r\n", "\n")
267+
268+
codeInsightTestFixture.configureByText(originalFileName, originalFileText)
269+
val document = codeInsightTestFixture.editor.document
270+
assertEquals(originalFileText, document.text)
271+
272+
CommandProcessor.getInstance().executeCommand(project, {
273+
application.runWriteAction {
274+
CodeStyleManager.getInstance(project).reformat(codeInsightTestFixture.file)
275+
}
276+
}, "", null)
277+
assertEquals(resultingFileText, document.text)
278+
}
237279
}

0 commit comments

Comments
 (0)