@@ -5,47 +5,53 @@ import com.intellij.openapi.application.ApplicationManager
5
5
import com.intellij.openapi.command.CommandProcessor
6
6
import com.intellij.openapi.util.TextRange
7
7
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
8
10
import com.intellij.psi.PsiDocumentManager
9
11
import com.intellij.psi.PsiFileFactory
10
12
import com.intellij.psi.codeStyle.CodeStyleManager
11
13
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
12
- import com.intellij.psi.formatter.FormatterTestCase
14
+ import com.intellij.testFramework.junit5.TestApplication
13
15
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
15
20
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 () {
23
24
25
+ @Test
24
26
fun testBlockIndent () {
25
27
doTest(" indents" , " indents_res" )
26
28
}
27
29
30
+ @Test
28
31
fun testBracesShiftedIndent () {
29
32
val tempTestSettings = getCommonSettings()
30
33
tempTestSettings.BRACE_STYLE = CommonCodeStyleSettings .NEXT_LINE_SHIFTED2
31
34
tempTestSettings.METHOD_BRACE_STYLE = CommonCodeStyleSettings .NEXT_LINE_SHIFTED2
32
35
doTest(" indent_brace" , " indent_brace_res" )
33
36
}
34
37
38
+ @Test
35
39
fun testDependentBracePlacement () {
36
40
val settings = getCommonSettings()
37
41
settings.BRACE_STYLE = CommonCodeStyleSettings .NEXT_LINE_IF_WRAPPED
38
42
settings.METHOD_BRACE_STYLE = CommonCodeStyleSettings .NEXT_LINE_IF_WRAPPED
39
43
doTest(" braces1" , " braces1_if_wrapped_res" )
40
44
}
41
45
46
+ @Test
42
47
fun testPipelineWrap () {
43
48
val psSettings = getPowerShellSettings()
44
49
psSettings.PIPELINE_TAIL_WRAP = CommonCodeStyleSettings .WRAP_ALWAYS
45
50
psSettings.ALIGN_MULTILINE_PIPELINE_STATEMENT = true
46
51
doTest(" indent_brace" , " indent_brace_pipeline_wrap_res" )
47
52
}
48
53
54
+ @Test
49
55
fun testBlockParametersWrap () {
50
56
doTest(" wrap_block_parameters" , " wrap_block_parameters_default_res" )
51
57
val psSettings = getPowerShellSettings()
@@ -54,16 +60,19 @@ class PowerShellFormatterTest : FormatterTestCase() {
54
60
doTest(" wrap_block_parameters" , " wrap_block_parameters_res" )
55
61
}
56
62
63
+ @Test
57
64
fun testBlockParameterIndent () {
58
65
doTest(" block_parameter_indent" , " block_parameter_indent_res" )
59
66
}
60
67
68
+ @Test
61
69
fun testDefault1 () {
62
70
val tempTestSettings = getCommonSettings()
63
71
tempTestSettings.KEEP_LINE_BREAKS = false
64
72
doTest(" default1" , " default1_res" )
65
73
}
66
74
75
+ @Test
67
76
fun testChainedCallWrap () {
68
77
val settings = getCommonSettings()
69
78
settings.RIGHT_MARGIN = 60
@@ -74,12 +83,14 @@ class PowerShellFormatterTest : FormatterTestCase() {
74
83
doTest(" wrap_chained_call" , " wrap_chained_call_if_long_first_wrap_res" )
75
84
}
76
85
86
+ @Test
77
87
fun testNoSpacingForCompoundIdentifiers () {
78
88
val settings = getCommonSettings()
79
89
settings.SPACE_AROUND_MULTIPLICATIVE_OPERATORS = true
80
90
doTest(" compound_identifiers" , " compound_identifiers_res" )
81
91
}
82
92
93
+ @Test
83
94
fun testChainedCallWrapAlignment () {
84
95
val settings = getCommonSettings()
85
96
settings.RIGHT_MARGIN = 60
@@ -92,6 +103,7 @@ class PowerShellFormatterTest : FormatterTestCase() {
92
103
doTest(" wrap_chained_call" , " wrap_chained_call_always_res" )
93
104
}
94
105
106
+ @Test
95
107
fun testWrapBinaryExpr () {
96
108
val settings = getCommonSettings()
97
109
settings.RIGHT_MARGIN = 30
@@ -111,60 +123,61 @@ class PowerShellFormatterTest : FormatterTestCase() {
111
123
}
112
124
113
125
126
+ @Test
114
127
fun testBraces () {
115
128
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
+ " }"
139
152
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
+ " }"
168
181
169
182
doTextTest(before, after)
170
183
@@ -176,6 +189,7 @@ class PowerShellFormatterTest : FormatterTestCase() {
176
189
doTextTest(after, before, false )
177
190
}
178
191
192
+ @Test
179
193
fun testWhitespaceDependentOperatorSpacing () {
180
194
doTextTest(
181
195
"""
@@ -192,6 +206,7 @@ class PowerShellFormatterTest : FormatterTestCase() {
192
206
)
193
207
}
194
208
209
+ @Test
195
210
fun testCommandLineArgumentSpacing () {
196
211
val sample = """
197
212
git log --pretty=format:'%Cred'
@@ -200,6 +215,7 @@ class PowerShellFormatterTest : FormatterTestCase() {
200
215
doTextTest(sample, sample)
201
216
}
202
217
218
+ @Test
203
219
fun testNullCoalescingOperator () = doTextTest(" \$ foo = \$ bar ??\$ baz" , " \$ foo = \$ bar ?? \$ baz" )
204
220
205
221
private fun doTextTest (text : String , textAfter : String , reformatAsText : Boolean = true, textRange : TextRange ? = null) {
@@ -219,7 +235,7 @@ class PowerShellFormatterTest : FormatterTestCase() {
219
235
CodeStyleManager .getInstance(project).reformat(file)
220
236
}
221
237
} catch (e: IncorrectOperationException ) {
222
- TestCase . assertTrue(e.localizedMessage, false )
238
+ assertTrue(false , e.localizedMessage)
223
239
}
224
240
}
225
241
}, " " , " " )
@@ -231,7 +247,33 @@ class PowerShellFormatterTest : FormatterTestCase() {
231
247
232
248
private fun createFileFromText (text : String ) = PsiFileFactory .getInstance(project).createFileFromText(PowerShellLanguage .INSTANCE , text)
233
249
234
- private fun getPowerShellSettings (): PowerShellCodeStyleSettings = settings .getCustomSettings(PowerShellCodeStyleSettings ::class .java)
250
+ private fun getPowerShellSettings (): PowerShellCodeStyleSettings = CodeStyle .getSettings(project) .getCustomSettings(PowerShellCodeStyleSettings ::class .java)
235
251
236
252
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
+ }
237
279
}
0 commit comments