Skip to content

Commit 914b9f2

Browse files
committed
Add formatter test case
1 parent a7002e8 commit 914b9f2

File tree

17 files changed

+699
-444
lines changed

17 files changed

+699
-444
lines changed

.editorconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
[*.{kt,kts}]
1+
[*.{kt,kts}]
2+
3+
[src/test/testData/sql/**.sql]
4+
trim_trailing_whitespace = false

src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlPostProcessor.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.domaframework.doma.intellij.formatter
217

318
import com.intellij.openapi.application.ApplicationManager
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.formatter
17+
18+
import com.intellij.openapi.command.WriteCommandAction
19+
import com.intellij.psi.codeStyle.CodeStyleManager
20+
import com.intellij.testFramework.fixtures.BasePlatformTestCase
21+
import com.intellij.util.ThrowableRunnable
22+
import org.domaframework.doma.intellij.state.DomaToolsFunctionEnableSettings
23+
24+
class SqlFormatterTest : BasePlatformTestCase() {
25+
override fun getBasePath(): String? = "src/test/testData/sql/formatter"
26+
27+
override fun getTestDataPath(): String? = "src/test/testData/sql/formatter"
28+
29+
override fun setUp() {
30+
super.setUp()
31+
val settings = DomaToolsFunctionEnableSettings.getInstance()
32+
settings.state.isEnableSqlFormat = true
33+
}
34+
35+
override fun tearDown() {
36+
try {
37+
val settings = DomaToolsFunctionEnableSettings.getInstance()
38+
settings.state.isEnableSqlFormat = false
39+
} finally {
40+
super.tearDown()
41+
}
42+
}
43+
44+
fun testSelectFormatter() {
45+
formatSqlFime("Select.sql", "FormattedSelect.sql")
46+
}
47+
48+
fun testCreateTableFormatter() {
49+
formatSqlFime("CreateTable.sql", "FormattedCreateTable.sql")
50+
}
51+
52+
fun testCreateViewFormatter() {
53+
formatSqlFime("CreateView.sql", "FormattedCreateView.sql")
54+
}
55+
56+
fun testInsertFormatter() {
57+
formatSqlFime("Insert.sql", "FormattedInsert.sql")
58+
}
59+
60+
fun testDeleteFormatter() {
61+
formatSqlFime("Delete.sql", "FormattedDelete.sql")
62+
}
63+
64+
private fun formatSqlFime(
65+
beforeFile: String,
66+
afterFile: String,
67+
) {
68+
myFixture.configureByFiles(beforeFile)
69+
val currentFile = myFixture.file
70+
WriteCommandAction
71+
.writeCommandAction(project)
72+
.run<RuntimeException?>(
73+
ThrowableRunnable {
74+
CodeStyleManager.getInstance(project).reformatText(
75+
currentFile,
76+
arrayListOf(currentFile.textRange),
77+
)
78+
},
79+
)
80+
myFixture.checkResultByFile(afterFile)
81+
}
82+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE departments
2+
(
3+
id INT PRIMARY KEY
4+
, name VARCHAR(100)
5+
, loc INT NOT NULL
6+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE VIEW EmployeeView AS
2+
SELECT employeeid
3+
, firstname
4+
, lastname
5+
, hiredate
6+
FROM employee
7+
WHERE salary > 50000
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
delete from x where id in (select id from x2 where id > /* id */ 101 and div = 't')
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE departments
2+
(
3+
id INT PRIMARY KEY
4+
, name VARCHAR(100)
5+
, loc INT NOT NULL
6+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE VIEW EmployeeView
2+
AS
3+
SELECT employeeid
4+
, firstname
5+
, lastname
6+
, hiredate
7+
FROM employee
8+
WHERE salary > 50000
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DELETE FROM x
2+
WHERE id IN ( SELECT id
3+
FROM x2
4+
WHERE id > /* id */101
5+
AND div = 't' )
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
INSERT INTO employee
2+
(id
3+
, name)
4+
VALUES ( /* employees.id */0
5+
, /* employees.name */'name')

0 commit comments

Comments
 (0)